-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_exe.py
More file actions
123 lines (102 loc) · 3.8 KB
/
Copy pathbuild_exe.py
File metadata and controls
123 lines (102 loc) · 3.8 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
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from printer_tool.packaging import (
APP_NAME,
MAINTENANCE_VISIBLE_APP_NAME,
build_pyinstaller_command,
map_path_to_alias_root,
path_needs_ascii_alias,
)
ASCII_ALIAS_ENV = "PRINTER_TOOL_ASCII_ALIAS_ACTIVE"
@dataclass(frozen=True)
class BuildVariant:
app_name: str
show_maintenance_ui: bool
def _pick_drive_letter() -> str:
for letter in "PQRSTUVWXYZ":
if not Path(f"{letter}:\\").exists():
return f"{letter}:"
raise RuntimeError("未找到可用的临时盘符,无法创建 ASCII 打包路径。")
@contextmanager
def temporary_ascii_alias(project_root: Path):
drive = _pick_drive_letter()
subprocess.run(["subst", drive, str(project_root)], check=True)
try:
yield Path(f"{drive}\\")
finally:
subprocess.run(["subst", drive, "/d"], check=False)
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Build the PrinterTool executable.")
parser.add_argument(
"--show-maintenance-ui",
action="store_true",
help="Build a variant that shows the built-in maintenance trace window.",
)
parser.add_argument(
"--both",
dest="build_both",
action="store_true",
help="Build both the default and visible-maintenance variants.",
)
return parser.parse_args(argv)
def build_variants(*, show_maintenance_ui: bool, build_both: bool) -> list[BuildVariant]:
if build_both:
return [
BuildVariant(app_name=APP_NAME, show_maintenance_ui=False),
BuildVariant(
app_name=MAINTENANCE_VISIBLE_APP_NAME,
show_maintenance_ui=True,
),
]
return [
BuildVariant(
app_name=MAINTENANCE_VISIBLE_APP_NAME if show_maintenance_ui else APP_NAME,
show_maintenance_ui=show_maintenance_ui,
)
]
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
forwarded_args = list(argv if argv is not None else sys.argv[1:])
project_root = Path(__file__).resolve().parent
current_python = Path(sys.executable).resolve()
real_project_root = project_root.resolve()
if (
os.getenv(ASCII_ALIAS_ENV) != "1"
and path_needs_ascii_alias(current_python)
and current_python.is_relative_to(real_project_root)
):
with temporary_ascii_alias(real_project_root) as alias_root:
alias_python = map_path_to_alias_root(current_python, real_project_root, alias_root)
alias_script = alias_root / "build_exe.py"
env = os.environ.copy()
env[ASCII_ALIAS_ENV] = "1"
print(f"检测到非 ASCII Python 路径,使用临时盘符 {alias_root.drive} 重新执行打包...")
completed = subprocess.run(
[str(alias_python), str(alias_script), *forwarded_args],
cwd=alias_root,
env=env,
check=False,
)
return completed.returncode
for variant in build_variants(
show_maintenance_ui=args.show_maintenance_ui,
build_both=args.build_both,
):
command = build_pyinstaller_command(
project_root,
show_maintenance_ui=variant.show_maintenance_ui,
)
print(f"开始打包 {variant.app_name}.exe ...")
completed = subprocess.run(command, cwd=project_root, check=False)
if completed.returncode != 0:
return completed.returncode
print(f"打包完成: {project_root / 'dist' / f'{variant.app_name}.exe'}")
return 0
if __name__ == "__main__":
raise SystemExit(main())