-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
397 lines (330 loc) · 11 KB
/
build.py
File metadata and controls
397 lines (330 loc) · 11 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AssignSticker 打包脚本
默认使用 Nuitka(standalone)打包,支持 Windows/Linux:
python build.py
python build.py --backend nuitka --onefile
python build.py --backend pyinstaller
"""
import argparse
import os
import shutil
import subprocess
import sys
from datetime import datetime
from pathlib import Path
if sys.platform == "win32":
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
APP_NAME = "AssignSticker"
MAIN_SCRIPT = "main.py"
EXCLUDE_DIRS = {
"__pycache__",
"build",
"dist",
"release",
"logs",
"data",
".git",
".github",
".vscode",
".idea",
".ruff_cache",
".venv",
"venv",
"env",
".codex",
"scripts",
}
EXCLUDE_FILES = {
"build.py",
".gitignore",
".python-version",
}
EXCLUDE_SUFFIXES = {
".py",
".pyc",
".pyo",
".spec",
".md",
".toml",
".lock",
}
def get_platform_key() -> str:
if sys.platform == "win32":
return "windows"
if sys.platform.startswith("linux"):
return "linux"
if sys.platform == "darwin":
return "macos"
return sys.platform
def get_arch_label() -> str:
machine = os.uname().machine.lower() if hasattr(os, "uname") else ""
if not machine and sys.platform == "win32":
machine = os.environ.get("PROCESSOR_ARCHITECTURE", "").lower()
aliases = {
"amd64": "x64",
"x86_64": "x64",
"x64": "x64",
"arm64": "arm64",
"aarch64": "arm64",
}
return aliases.get(machine, machine or "unknown")
def get_project_root() -> Path:
return Path(__file__).parent.absolute()
def clean_build_dirs() -> None:
print("清理构建目录...")
project_root = get_project_root()
for dir_name in ["build", "dist", "__pycache__"]:
dir_path = project_root / dir_name
if dir_path.exists():
shutil.rmtree(dir_path)
print(f" 已删除: {dir_name}")
def collect_data_entries_nuitka() -> list[str]:
project_root = get_project_root()
args: list[str] = []
print("\n扫描并收集资源(Nuitka)...")
for item in sorted(project_root.iterdir(), key=lambda p: p.name.lower()):
name = item.name
if name in EXCLUDE_FILES or name in EXCLUDE_DIRS:
print(f" 排除: {name}")
continue
if name.startswith("."):
print(f" 排除隐藏项: {name}")
continue
if item.is_dir():
args.append(f"--include-data-dir={name}={name}")
print(f" 包含目录: {name}")
continue
if item.suffix.lower() in EXCLUDE_SUFFIXES:
print(f" 排除文件: {name}")
continue
args.append(f"--include-data-file={name}={name}")
print(f" 包含文件: {name}")
return args
def collect_data_entries_pyinstaller() -> list[str]:
project_root = get_project_root()
args: list[str] = []
separator = ";" if sys.platform == "win32" else ":"
print("\n扫描并收集资源(PyInstaller)...")
for item in sorted(project_root.iterdir(), key=lambda p: p.name.lower()):
name = item.name
if name in EXCLUDE_FILES or name in EXCLUDE_DIRS:
print(f" 排除: {name}")
continue
if name.startswith("."):
print(f" 排除隐藏项: {name}")
continue
if item.is_dir():
args.append(f"--add-data={name}{separator}{name}")
print(f" 包含目录: {name}")
continue
if item.suffix.lower() in EXCLUDE_SUFFIXES:
print(f" 排除文件: {name}")
continue
args.append(f"--add-data={name}{separator}.")
print(f" 包含文件: {name}")
return args
def ensure_backend_installed(backend: str) -> bool:
try:
if backend == "nuitka":
import nuitka # noqa: F401
print(f"Nuitka 已安装")
else:
import PyInstaller # noqa: F401
print(f"PyInstaller 已安装")
return True
except ImportError:
if backend == "nuitka":
print("错误: 未安装 Nuitka,请先执行: uv pip install nuitka")
else:
print("错误: 未安装 PyInstaller,请先执行: uv pip install pyinstaller")
return False
def run_nuitka(onefile: bool) -> Path:
data_args = collect_data_entries_nuitka()
mode_arg = "--onefile" if onefile else "--standalone"
platform_key = get_platform_key()
cmd = [
sys.executable,
"-m",
"nuitka",
mode_arg,
f"--output-filename={APP_NAME}",
"--enable-plugin=tk-inter",
"--assume-yes-for-downloads",
"--output-dir=dist",
]
if platform_key == "windows":
cmd.extend(
[
"--windows-console-mode=disable",
"--windows-icon-from-ico=icon.ico",
"--include-module=webview.platforms.winforms",
"--include-module=webview.platforms.edgechromium",
"--include-module=webview.platforms.mshtml",
"--include-module=pystray._win32",
]
)
elif platform_key == "linux":
cmd.extend(
[
"--include-module=webview.platforms.gtk",
"--include-module=pystray._appindicator",
"--include-module=pystray._gtk",
]
)
else:
print(f"提示: 当前平台 {platform_key} 未设置专用 Nuitka 模块参数,使用通用配置")
cmd.extend(data_args)
cmd.append(MAIN_SCRIPT)
print("\n执行命令:")
print(" ".join(cmd))
subprocess.run(cmd, check=True, capture_output=False)
dist_root = get_project_root() / "dist"
if onefile:
onefile_candidates = [
dist_root / f"{APP_NAME}.exe",
dist_root / APP_NAME,
dist_root / f"{APP_NAME}.bin",
]
for candidate in onefile_candidates:
if candidate.exists():
return candidate
recent_files = sorted(
[p for p in dist_root.iterdir() if p.is_file()],
key=lambda p: p.stat().st_mtime,
reverse=True,
)
if recent_files:
return recent_files[0]
raise FileNotFoundError("Nuitka 未生成 onefile 产物")
dist_dirs = sorted(
dist_root.glob("*.dist"), key=lambda p: p.stat().st_mtime, reverse=True
)
for folder in dist_dirs:
if any(
(folder / name).exists()
for name in (f"{APP_NAME}.exe", APP_NAME, f"{APP_NAME}.bin")
):
return folder
if dist_dirs:
return dist_dirs[0]
raise FileNotFoundError("Nuitka 未生成 *.dist 目录")
def run_pyinstaller() -> Path:
data_args = collect_data_entries_pyinstaller()
cmd = [
sys.executable,
"-m",
"PyInstaller",
"--onefile",
"--windowed",
f"--name={APP_NAME}",
"--icon=icon.ico",
"--clean",
"--noconfirm",
]
cmd.extend(data_args)
cmd.append(MAIN_SCRIPT)
print("\n执行命令:")
print(" ".join(cmd))
subprocess.run(cmd, check=True, capture_output=False)
dist_root = get_project_root() / "dist"
candidates = [
dist_root / f"{APP_NAME}.exe",
dist_root / APP_NAME,
]
for candidate in candidates:
if candidate.exists():
return candidate
raise FileNotFoundError("PyInstaller 未生成预期可执行文件")
def create_release_package(build_output: Path) -> Path:
print("\n创建发布包...")
project_root = get_project_root()
platform_key = get_platform_key()
arch = get_arch_label()
release_dir = project_root / "release" / f"{APP_NAME}-{platform_key}"
if release_dir.exists():
shutil.rmtree(release_dir)
release_dir.mkdir(parents=True, exist_ok=True)
if build_output.is_dir():
copied_count = 0
for item in build_output.iterdir():
target = release_dir / item.name
if item.is_dir():
shutil.copytree(item, target)
else:
shutil.copy2(item, target)
copied_count += 1
print(f" 已平铺复制目录内容: {build_output.name} ({copied_count} 项)")
elif build_output.exists():
shutil.copy2(build_output, release_dir / build_output.name)
print(f" 复制文件: {build_output.name}")
else:
raise FileNotFoundError(f"找不到构建产物: {build_output}")
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
zip_name = f"{APP_NAME}-{platform_key}-{arch}-{timestamp}"
zip_path = project_root / "release" / zip_name
shutil.make_archive(str(zip_path), "zip", root_dir=str(release_dir))
print(f" 创建压缩包: {zip_name}.zip")
return zip_path.with_suffix(".zip")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="AssignSticker 打包工具")
parser.add_argument(
"--backend",
choices=["nuitka", "pyinstaller"],
default="nuitka",
help="打包后端,默认 nuitka",
)
parser.add_argument(
"--onefile",
action="store_true",
help="仅对 nuitka 生效,启用 onefile 模式(默认 standalone)",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
platform_key = get_platform_key()
print("AssignSticker 打包工具")
print(f"Python: {sys.version}")
print(f"平台: {sys.platform}")
print(f"后端: {args.backend}")
if args.backend == "nuitka":
print(f"Nuitka 模式: {'onefile' if args.onefile else 'standalone'}")
print()
if platform_key not in {"windows", "linux"}:
print(f"警告: 当前平台 {platform_key} 未正式支持,可能打包失败")
response = input("是否继续? (y/N): ")
if response.lower() != "y":
print("已取消")
return
if not ensure_backend_installed(args.backend):
sys.exit(1)
project_root = get_project_root()
os.chdir(project_root)
clean_build_dirs()
try:
print("\n" + "=" * 60)
print(f"开始构建 {APP_NAME} {platform_key} 可执行文件")
print("=" * 60)
if args.backend == "nuitka":
build_output = run_nuitka(onefile=args.onefile)
else:
build_output = run_pyinstaller()
zip_file = create_release_package(build_output)
print("\n" + "=" * 60)
print("打包完成!")
print("=" * 60)
print(f"\n构建产物: {build_output}")
print(f"发布包: {zip_file}")
except subprocess.CalledProcessError as e:
print("\n打包失败,请检查错误信息")
print(f"错误代码: {e.returncode}")
sys.exit(1)
except Exception as e:
print("\n打包失败")
print(f"错误: {e}")
sys.exit(1)
if __name__ == "__main__":
main()