-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_installer.py
More file actions
357 lines (274 loc) · 10.1 KB
/
build_installer.py
File metadata and controls
357 lines (274 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from __future__ import annotations
import argparse
import os
import shutil
import subprocess
import sys
import textwrap
import tomllib
from pathlib import Path
APP_NAME = "HoloDoppler"
APP_EXE_NAME = f"{APP_NAME}.exe"
APP_PUBLISHER = "HoloDoppler"
PROJECT_ROOT = Path(__file__).resolve().parent
SRC_DIR = PROJECT_ROOT / "src"
PYPROJECT_FILE = PROJECT_ROOT / "pyproject.toml"
VERSION_FILE = PROJECT_ROOT / "version_holodoppler.txt"
DIST_DIR = PROJECT_ROOT / "dist"
BUILD_DIR = PROJECT_ROOT / "build"
PYINSTALLER_WORK_DIR = BUILD_DIR / APP_NAME
PAYLOAD_DIR = BUILD_DIR / "installer_payload"
GENERATED_ENTRYPOINT = BUILD_DIR / "_pyinstaller_holodoppler_entry.py"
GENERATED_ISS_FILE = BUILD_DIR / f"{APP_NAME}.iss"
INSTALLER_OUTPUT_DIR = DIST_DIR
DIST_EXE = DIST_DIR / APP_EXE_NAME
INNO_SETUP_CANDIDATES = (
Path.home() / "AppData" / "Local" / "Programs" / "Inno Setup 6" / "ISCC.exe",
Path(r"C:\Program Files (x86)\Inno Setup 6\ISCC.exe"),
Path(r"C:\Program Files\Inno Setup 6\ISCC.exe"),
)
PAYLOAD_EXTRA_FILES = (
PROJECT_ROOT / "LICENSE",
PROJECT_ROOT / "README.md",
PROJECT_ROOT / "pyproject.toml",
VERSION_FILE,
)
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Build the HoloDoppler Windows installer with PyInstaller and Inno Setup."
)
parser.add_argument(
"--skip-pyinstaller",
action="store_true",
help="Reuse dist/HoloDoppler.exe instead of rebuilding it with PyInstaller.",
)
parser.add_argument(
"--skip-inno",
action="store_true",
help="Build and stage the PyInstaller payload without compiling an installer.",
)
parser.add_argument(
"--iscc",
type=Path,
help="Optional full path to ISCC.exe.",
)
parser.add_argument(
"--console",
action="store_true",
help="Build the executable with a visible console window.",
)
return parser.parse_args()
def _ensure_supported_python() -> None:
required = (3, 13)
if sys.version_info < required:
version = ".".join(str(part) for part in sys.version_info[:3])
raise SystemExit(
f"build_installer.py must run with Python {required[0]}.{required[1]} or newer. "
f"Current interpreter: {sys.executable} ({version})."
)
def _read_version() -> str:
if PYPROJECT_FILE.exists():
data = tomllib.loads(PYPROJECT_FILE.read_text(encoding="utf-8"))
print(data)
version = data.get("project", {}).get("version")
if isinstance(version, str) and version.strip():
return version.strip()
if VERSION_FILE.exists():
version = VERSION_FILE.read_text(encoding="utf-8").strip()
if version:
return version
raise RuntimeError(f"Could not read version from {PYPROJECT_FILE} or {VERSION_FILE}")
def _find_iscc(explicit_path: Path | None) -> Path:
candidates: list[Path] = []
if explicit_path is not None:
candidates.append(explicit_path.expanduser())
env_override = os.environ.get("INNO_SETUP_COMPILER")
if env_override:
candidates.append(Path(env_override).expanduser())
for command_name in ("iscc.exe", "iscc"):
resolved = shutil.which(command_name)
if resolved:
candidates.append(Path(resolved))
candidates.extend(INNO_SETUP_CANDIDATES)
for candidate in candidates:
if candidate.exists():
return candidate
searched = "\n".join(str(path) for path in candidates if path)
raise FileNotFoundError(
"Could not find ISCC.exe. Set INNO_SETUP_COMPILER, pass --iscc, "
"or add Inno Setup 6 to PATH.\n"
f"Searched:\n{searched}"
)
def _run_command(command: list[str | Path]) -> None:
cmd = [str(part) for part in command]
print(f"> {' '.join(cmd)}")
result = subprocess.run(
cmd,
cwd=PROJECT_ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
print(result.stdout)
if result.returncode != 0:
raise SystemExit(f"Command failed with exit code {result.returncode}")
def _remove_path(path: Path) -> None:
if path.is_dir():
shutil.rmtree(path)
elif path.exists():
path.unlink()
def _clean_pyinstaller_outputs() -> None:
_remove_path(DIST_EXE)
_remove_path(PYINSTALLER_WORK_DIR)
_remove_path(PROJECT_ROOT / f"{APP_NAME}.spec")
_remove_path(GENERATED_ENTRYPOINT)
def _write_pyinstaller_entrypoint() -> Path:
BUILD_DIR.mkdir(parents=True, exist_ok=True)
GENERATED_ENTRYPOINT.write_text(
textwrap.dedent(
"""
from __future__ import annotations
import sys
from holodoppler.cli import main as cli_main
from holodoppler.ui import UI
def main() -> int:
if len(sys.argv) == 1:
UI().mainloop()
return 0
return cli_main()
if __name__ == "__main__":
raise SystemExit(main())
"""
).lstrip(),
encoding="utf-8",
)
return GENERATED_ENTRYPOINT
def _run_pyinstaller(console: bool) -> None:
if not SRC_DIR.exists():
raise SystemExit(f"Package source directory not found: {SRC_DIR}")
_clean_pyinstaller_outputs()
entrypoint = _write_pyinstaller_entrypoint()
command: list[str | Path] = [
sys.executable,
"-m",
"PyInstaller",
"--noconfirm",
"--clean",
"--onefile",
"--name",
APP_NAME,
"--workpath",
PYINSTALLER_WORK_DIR,
"--distpath",
DIST_DIR,
"--paths",
SRC_DIR,
"--collect-submodules",
"holodoppler",
"--collect-submodules",
"cupy",
"--collect-submodules",
"cupyx",
"--collect-submodules",
"scipy",
"--collect-submodules",
"h5py",
"--collect-submodules",
"tkinterdnd2",
"--collect-data",
"tkinterdnd2",
]
if console:
command.append("--console")
else:
command.append("--windowed")
command.append(entrypoint)
_run_command(command)
def _prepare_payload() -> None:
if not DIST_EXE.is_file():
raise FileNotFoundError(
"PyInstaller output not found. Expected "
f"{DIST_EXE}. Run without --skip-pyinstaller first."
)
if PAYLOAD_DIR.exists():
shutil.rmtree(PAYLOAD_DIR)
PAYLOAD_DIR.mkdir(parents=True, exist_ok=True)
shutil.copy2(DIST_EXE, PAYLOAD_DIR / APP_EXE_NAME)
for extra_file in PAYLOAD_EXTRA_FILES:
if extra_file.exists():
shutil.copy2(extra_file, PAYLOAD_DIR / extra_file.name)
def _iss_string(value: str | Path) -> str:
return str(value).replace('"', '""')
def _version_info_version(app_version: str) -> str:
numeric_parts: list[str] = []
for part in app_version.replace("-", ".").replace("+", ".").split("."):
if not part.isdigit():
break
numeric_parts.append(part)
if not numeric_parts:
numeric_parts.append("0")
return ".".join((numeric_parts + ["0", "0", "0", "0"])[:4])
def _write_inno_script(app_version: str) -> Path:
BUILD_DIR.mkdir(parents=True, exist_ok=True)
INSTALLER_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
script = f"""
#define AppName "{_iss_string(APP_NAME)}"
#define AppExeName "{_iss_string(APP_EXE_NAME)}"
#define AppVersion "{_iss_string(app_version)}"
#define VersionInfoVersion "{_iss_string(_version_info_version(app_version))}"
#define AppPublisher "{_iss_string(APP_PUBLISHER)}"
#define PayloadDir "{_iss_string(PAYLOAD_DIR)}"
#define OutputDir "{_iss_string(INSTALLER_OUTPUT_DIR)}"
[Setup]
AppId={{{APP_NAME}-7C3E24DA-5E1F-4E4E-91F1-91D62A0F1B18}}
AppName={{#AppName}}
AppVersion={{#AppVersion}}
AppVerName={{#AppName}} {{#AppVersion}}
AppPublisher={{#AppPublisher}}
DefaultDirName={{localappdata}}\\Programs\\{{#AppName}}
DefaultGroupName={{#AppName}}
DisableProgramGroupPage=yes
OutputDir={{#OutputDir}}
OutputBaseFilename={{#AppName}}-setup-{{#AppVersion}}
Compression=lzma2
SolidCompression=yes
WizardStyle=modern
PrivilegesRequired=lowest
UninstallDisplayIcon={{app}}\\{{#AppExeName}}
VersionInfoCompany={{#AppPublisher}}
VersionInfoDescription={{#AppName}} installer
VersionInfoVersion={{#VersionInfoVersion}}
[Tasks]
Name: "desktopicon"; Description: "{{cm:CreateDesktopIcon}}"; GroupDescription: "{{cm:AdditionalIcons}}"; Flags: unchecked
[Dirs]
Name: "{{userappdata}}\\{{#AppName}}"; Flags: uninsneveruninstall
Name: "{{userappdata}}\\{{#AppName}}\\logs"; Flags: uninsneveruninstall
[Files]
Source: "{{#PayloadDir}}\\*"; DestDir: "{{app}}"; Flags: ignoreversion recursesubdirs createallsubdirs
[Icons]
Name: "{{autoprograms}}\\{{#AppName}}"; Filename: "{{app}}\\{{#AppExeName}}"
Name: "{{autodesktop}}\\{{#AppName}}"; Filename: "{{app}}\\{{#AppExeName}}"; Tasks: desktopicon
[Run]
Filename: "{{app}}\\{{#AppExeName}}"; Description: "Launch {{#AppName}}"; Flags: nowait postinstall skipifsilent
"""
GENERATED_ISS_FILE.write_text(textwrap.dedent(script).lstrip(), encoding="utf-8")
return GENERATED_ISS_FILE
def _run_inno_setup(iscc_path: Path, app_version: str) -> None:
iss_file = _write_inno_script(app_version)
_run_command([iscc_path, iss_file])
def main() -> None:
args = _parse_args()
_ensure_supported_python()
app_version = _read_version()
iscc_path = None if args.skip_inno else _find_iscc(args.iscc)
if not args.skip_pyinstaller:
_run_pyinstaller(console=args.console)
_prepare_payload()
if args.skip_inno:
print(f"Installer payload staged at {PAYLOAD_DIR}")
return
_run_inno_setup(iscc_path, app_version)
installer_name = INSTALLER_OUTPUT_DIR / f"{APP_NAME}-setup-{app_version}.exe"
print(f"Installer created at {installer_name}")
if __name__ == "__main__":
main()