-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_app.py
More file actions
104 lines (85 loc) · 2.86 KB
/
Copy pathbuild_app.py
File metadata and controls
104 lines (85 loc) · 2.86 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
import shutil
import subprocess
import sys
import zipfile
from pathlib import Path
APP_NAME = "VideoToScreensaver"
def run(command: list[str], cwd: Path) -> None:
result = subprocess.run(command, cwd=cwd, text=True)
if result.returncode != 0:
raise SystemExit(result.returncode)
def recreate_dir(path: Path) -> None:
if path.exists():
shutil.rmtree(path)
path.mkdir(parents=True, exist_ok=True)
def write_release_zip(release_exe: Path, release_dir: Path) -> Path:
zip_path = release_dir / f"{APP_NAME}.zip"
if zip_path.exists():
zip_path.unlink()
with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as package:
package.write(release_exe, arcname=release_exe.name)
return zip_path
def main() -> None:
root = Path(__file__).resolve().parent
source = root / "src" / "app.py"
build_root = root / "build_artifacts"
dist_dir = build_root / "dist"
work_dir = build_root / "work"
spec_dir = build_root / "spec"
release_dir = root / "release"
icon_path = root / "assets" / "vts_icon.ico"
if not source.is_file():
raise SystemExit(f"Missing source file: {source}")
if not icon_path.is_file():
raise SystemExit(f"Missing icon file: {icon_path}")
recreate_dir(build_root)
dist_dir.mkdir(parents=True, exist_ok=True)
work_dir.mkdir(parents=True, exist_ok=True)
spec_dir.mkdir(parents=True, exist_ok=True)
release_dir.mkdir(parents=True, exist_ok=True)
command = [
sys.executable,
"-m",
"PyInstaller",
"--onefile",
"--windowed",
"--name",
APP_NAME,
"--icon",
str(icon_path),
"--add-data",
f"{icon_path};assets",
"--distpath",
str(dist_dir),
"--workpath",
str(work_dir),
"--specpath",
str(spec_dir),
str(source),
]
run(command, cwd=root)
built_exe = dist_dir / f"{APP_NAME}.exe"
if not built_exe.is_file():
raise SystemExit(f"Expected output not found: {built_exe}")
release_exe = release_dir / f"{APP_NAME}.exe"
try:
if release_exe.exists():
release_exe.unlink()
shutil.copy2(built_exe, release_exe)
except PermissionError:
raise SystemExit(
f"Cannot overwrite {release_exe} because it is in use. "
"Close VideoToScreensaver.exe and run build_app.py again."
)
release_zip = write_release_zip(release_exe, release_dir)
legacy_onedir = release_dir / APP_NAME
if legacy_onedir.exists():
try:
shutil.rmtree(legacy_onedir)
except OSError:
pass
print(f"Build complete: {release_exe}")
print(f"Update package: {release_zip}")
print("Run this executable to choose a video and install it as screensaver.")
if __name__ == "__main__":
main()