Repository navigation
Expand file tree
/
Copy pathbuild_nuitka.py
More file actions
326 lines (293 loc) · 13.8 KB
/
Copy pathbuild_nuitka.py
File metadata and controls
326 lines (293 loc) · 13.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PhoneMic 统一构建脚本
用法:
python build.py --mode standalone # 编译 + 打包 (默认)
python build.py --mode standalone --action compile # 只编译
python build.py --mode standalone --action package # 只打包 (需已有编译结果)
python build.py --mode onefile --action compile # 只编译单文件
python build.py --mode onefile # 编译单文件并复制到 dist
"""
import subprocess
import sys
import argparse
import shutil
from datetime import datetime
from pathlib import Path
import json
import tomllib
# ---------- 项目配置 ----------
PROJECT_ROOT = Path(__file__).parent
PYPROJECT_PATH = PROJECT_ROOT / "pyproject.toml"
DIST_DIR = PROJECT_ROOT / "dist"
BUILD_DIR = PROJECT_ROOT / "build" / "phonemic_nuitka"
# Nuitka 配置
ICON_PATH = PROJECT_ROOT / "phonemic" / "resources" / "favicon.ico"
MAIN_SCRIPT = PROJECT_ROOT / "phonemic" / "PhoneMic.py"
# NSIS 配置
NSIS_SCRIPT = PROJECT_ROOT / "makesetup.nsi"
# ---------- 第三方二进制依赖 ----------
VENDOR_CACHE = PROJECT_ROOT / "build" / "vendor"
VENDORS = [
{
"name": "cloudflared",
"url": "https://github.com/cloudflare/cloudflared/releases/download/2026.10.7/cloudflared-windows-amd64.exe",
"sha256": "2493dbc168923bb3e59e5e7716f71deced92992eea9acd6e9d74f84ac7751343",
"dest": "bin/cloudflared.exe",
},
]
# ---------- 第三方二进制下载 ----------
def prepare_vendors() -> None:
"""下载并校验第三方二进制到缓存目录,然后拷贝到 standalone dist。"""
import hashlib
import urllib.request
VENDOR_CACHE.mkdir(parents=True, exist_ok=True)
standalone_dist = BUILD_DIR / "PhoneMic.dist"
for v in VENDORS:
filename = Path(v["dest"]).name
cache_path = VENDOR_CACHE / filename
if cache_path.exists():
sha = hashlib.sha256(cache_path.read_bytes()).hexdigest()
if sha == v["sha256"]:
print(f" ✓ {v['name']}: 缓存有效,跳过下载")
else:
print(f" ↓ {v['name']}: SHA256 不匹配,重新下载...")
urllib.request.urlretrieve(v["url"], cache_path)
else:
print(f" ↓ {v['name']}: 下载中...")
urllib.request.urlretrieve(v["url"], cache_path)
sha = hashlib.sha256(cache_path.read_bytes()).hexdigest()
if sha != v["sha256"]:
print(f" ✗ {v['name']}: SHA256 校验失败\n 期望: {v['sha256']}\n 实际: {sha}", file=sys.stderr)
sys.exit(1)
print(f" ✓ {v['name']}: SHA256 校验通过")
dest_path = standalone_dist / v["dest"]
dest_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(cache_path, dest_path)
print(f" → {v['name']}: 已拷贝到 {dest_path.relative_to(PROJECT_ROOT)}")
# ---------- 辅助函数 ----------
def read_version_from_pyproject():
"""从 pyproject.toml 读取版本号(PEP 621)"""
with open(PYPROJECT_PATH, "rb") as f:
data = tomllib.load(f)
try:
version = data["project"]["version"]
print(f"从 pyproject.toml 读取版本: {version}")
return version
except KeyError:
print("错误: pyproject.toml 中缺少 [project] 下的 version 字段", file=sys.stderr)
sys.exit(1)
def get_git_commit_short():
"""获取当前 Git 短哈希"""
try:
result = subprocess.run(
["git", "rev-parse", "--short", "HEAD"],
capture_output=True, text=True, check=True, encoding="utf-8"
)
return result.stdout.strip()
except (subprocess.CalledProcessError, FileNotFoundError):
print("警告: 无法获取 Git commit,使用 'nogit'", file=sys.stderr)
return "nogit"
def get_current_date():
"""返回当前日期字符串 YYYYMMDD"""
return datetime.now().strftime("%Y%m%d")
def ensure_dir(path: Path):
path.mkdir(parents=True, exist_ok=True)
def clean_build_output():
"""清理旧的 Nuitka 构建产物(可选)"""
if BUILD_DIR.exists():
print(f"清理旧构建目录: {BUILD_DIR}")
shutil.rmtree(BUILD_DIR)
def run_nuitka(onefile: bool, version: str, commit: str):
"""
调用 Nuitka 编译
:param onefile: True -> 单文件模式,False -> standalone 目录模式
:param version: 版本号,用于嵌入可执行文件
:param commit: git commit,用于 build_info.json
"""
# 生成 build_info.json 到 BUILD_DIR
build_info = {
"version": version,
"commit": commit,
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
build_info_path = BUILD_DIR / "build_info.json"
BUILD_DIR.mkdir(parents=True, exist_ok=True)
with open(build_info_path, "w", encoding="utf-8") as f:
json.dump(build_info, f, indent=2)
cmd = [
sys.executable, "-m", "nuitka",
"--onefile" if onefile else "--standalone",
"--windows-console-mode=disable",
f"--windows-icon-from-ico={ICON_PATH}",
f"--output-dir={BUILD_DIR}",
"--lto=yes",
"--enable-plugin=pyside6",
"--include-data-dir=./phonemic/resources=phonemic/resources",
# 调试资源(test.html / debug_log.js)随产物一起发布:发布版也能带着
# --debug(或 PHONEMIC_DEBUG=1)临时打开手机端日志浮层与 /test 页面。
# 默认运行不会下发它们 —— 门禁在运行期(phonemic/utils/debug.py 的
# is_debug_mode(),_serve_test / _serve_debug_log 各自兜底),不再靠
# 「文件不在产物里」。别在这里重新加 --noinclude-data-files 排除这两者,
# 否则打包版开 --debug 只会拿到 404。
f"--include-data-files={build_info_path}=build_info.json",
"--include-data-files=./LICENSE=LICENSE",
"--include-data-files=./LICENSE.LGPL.txt=LICENSE.LGPL.txt",
"--include-data-files=./NOTICE.txt=NOTICE.txt",
"--include-data-files=./NOTICE_en.txt=NOTICE_en.txt",
"--include-data-files=./README.md=README.md",
"--include-data-files=./USER_GUIDE.md=USER_GUIDE.md",
# 使用帮助窗口按界面语言挑说明书(phonemic/gui/help_window.py 的
# guide_path),缺哪份就回退到中文原版——不会崩,但会变成「界面英文、
# 说明书中文」,所以四份都打进去。
"--include-data-files=./USER_GUIDE_en_US.md=USER_GUIDE_en_US.md",
"--include-data-files=./USER_GUIDE_zh_TW.md=USER_GUIDE_zh_TW.md",
"--include-data-files=./USER_GUIDE_zh_HK.md=USER_GUIDE_zh_HK.md",
# uvicorn 的 ws 协议类是字符串动态导入(uvicorn.config.load → import_from_string),
# Nuitka 静态分析看不到;Nuitka 自带的 uvicorn 包规则只硬编码了 auto /
# websockets_impl / wsproto_impl 三个(standard.nuitka-package.config.yml),
# 不含新版才有的 websockets_sansio_impl。api.py 传的是 ws="websockets-sansio",
# 不显式包含的话打包版启动即 ImportError(--windows-console-mode=disable 下
# 无声无息),GUI 只能报「服务器启动超时,请检查端口是否可用」——端口是背锅的。
"--include-module=uvicorn.protocols.websockets.websockets_sansio_impl",
"--noinclude-default-mode=error",
"--nofollow-import-to=tkinter,unittest,pydoc,test,distutils,setuptools,pdb",
"--nofollow-import-to=PIL",
# 已移除的旧依赖(防御性排除,防止残留引用)
"--nofollow-import-to=fastapi,pydantic,tremolo,aiohttp,multidict,yarl,frozenlist,aiosignal",
"--nofollow-import-to=jinja2,requests",
# aiohttp 可选加速包(未安装,防御性排除)
"--nofollow-import-to=aiodns,brotli,cchardet",
# 注意: ssl/hashlib/http 不可排除,Starlette/Uvicorn 依赖它们
"--nofollow-import-to=PySide6.QtWebEngineWidgets",
"--nofollow-import-to=PySide6.QtNetworkAuth",
"--nofollow-import-to=PySide6.QtQml",
"--nofollow-import-to=PySide6.QtQuick",
"--nofollow-import-to=PySide6.QtTest",
"--nofollow-import-to=PySide6.QtXml",
"--jobs=4",
"--experimental=debug-report-traceback",
"--product-name=PhoneMic",
'--company-name="PhoneMic Team"',
f"--file-version={version}",
f"--product-version={version}",
'--file-description="PhoneMic - 手机语音输入电脑端"',
'--copyright="Copyright (c) 2026 PhoneMic Team. Licensed under Apache 2.0."',
str(MAIN_SCRIPT)
]
print("Running Nuitka command:")
print(" ".join(cmd))
subprocess.check_call(cmd)
def check_makensis():
"""检查 makensis 是否在 PATH 中,若不存在则退出"""
if shutil.which("makensis") is None:
print("错误: 找不到 makensis,请安装 NSIS 并将其添加到 PATH 环境变量中", file=sys.stderr)
sys.exit(1)
def copy_to_dist_onefile(version: str, date_str: str, commit: str):
"""将生成的 single exe 复制到 dist/ 目录并重命名"""
src_exe = BUILD_DIR / "PhoneMic.exe"
if not src_exe.exists():
print(f"错误: 找不到生成的 exe 文件 {src_exe}", file=sys.stderr)
sys.exit(1)
target_name = f"PhoneMic_{version}_{date_str}_{commit}.exe"
target_path = DIST_DIR / target_name
shutil.copy2(src_exe, target_path)
print(f"✅ OneFile 已复制到: {target_path}")
def compile_standalone(version: str, commit: str):
"""只编译 standalone 模式,不打包"""
run_nuitka(onefile=False, version=version, commit=commit)
standalone_dist = BUILD_DIR / "PhoneMic.dist"
if not standalone_dist.exists():
print(f"错误: Nuitka standalone 输出目录不存在: {standalone_dist}", file=sys.stderr)
sys.exit(1)
print(f"✅ Standalone 编译完成,输出目录: {standalone_dist}")
def package_standalone(version: str, date_str: str, commit: str, vendor: bool = False):
"""只打包 NSIS 安装包(假设 standalone 目录已存在)"""
check_makensis()
standalone_dist = BUILD_DIR / "PhoneMic.dist"
if not standalone_dist.exists():
print(f"错误: standalone 输出目录不存在,请先运行编译: {standalone_dist}", file=sys.stderr)
sys.exit(1)
if not NSIS_SCRIPT.exists():
print(f"错误: 找不到 NSIS 脚本 {NSIS_SCRIPT}", file=sys.stderr)
sys.exit(1)
suffix_parts = []
if vendor:
suffix_parts.append("bundled")
suffix_parts.extend([date_str, version, commit])
build_suffix = "_".join(suffix_parts)
cmd = [
"makensis",
f"/DVERSION={version}",
f"/DBUILD_DATE={date_str}",
f"/DBUILD_COMMIT={commit}",
f"/DBUILD_SUFFIX={build_suffix}",
str(NSIS_SCRIPT)
]
print("Running makensis:")
print(" ".join(cmd))
subprocess.check_call(cmd)
print(f"✅ 安装包已生成到 {DIST_DIR}/PhoneMic_Setup_{build_suffix}.exe")
def build_standalone_installer(version: str, date_str: str, commit: str, action: str, vendor: bool = False):
"""
根据 action 执行 standalone 构建流程
"""
if action == "package":
print("⚠️ package 模式将跳过编译,仅打包现有 standalone 目录")
if action in ("all", "compile"):
compile_standalone(version, commit)
if vendor:
print("📦 准备第三方二进制依赖...")
prepare_vendors()
if action in ("all", "package"):
package_standalone(version, date_str, commit, vendor)
def build_onefile(version: str, date_str: str, commit: str, action: str):
"""
根据 action 执行 onefile 构建流程
"""
if action == "package":
print("错误: onefile 模式不支持 package 动作(无 NSIS 打包)", file=sys.stderr)
sys.exit(1)
# compile 或 all 都执行编译
run_nuitka(onefile=True, version=version, commit=commit)
# 复制到 dist
copy_to_dist_onefile(version, date_str, commit)
print("✅ OneFile 构建完成")
# ---------- 主入口 ----------
def main():
parser = argparse.ArgumentParser(description="PhoneMic 构建脚本")
parser.add_argument(
"--mode", choices=["standalone", "onefile"], default="standalone",
help="构建模式: standalone (默认:生成安装包) 或 onefile (单文件)"
)
parser.add_argument(
"--action", choices=["all", "compile", "package"], default="all",
help="执行动作: all (默认,编译+打包), compile (只编译), package (只打包,仅 standalone 有效)"
)
parser.add_argument(
"--vendor", action="store_true",
help="编译后将第三方二进制(如 cloudflared)拷贝到 standalone dist 的 bin/ 目录"
)
args = parser.parse_args()
# 前置检查
if not ICON_PATH.exists():
print(f"错误: 图标文件不存在: {ICON_PATH}", file=sys.stderr)
sys.exit(1)
if not MAIN_SCRIPT.exists():
print(f"错误: 主程序入口不存在: {MAIN_SCRIPT}", file=sys.stderr)
sys.exit(1)
# 读取版本号
version = read_version_from_pyproject()
date_str = get_current_date()
commit = get_git_commit_short()
# 确保输出目录存在
ensure_dir(DIST_DIR)
if args.mode == "onefile":
print("🔧 构建模式: OneFile 单文件")
build_onefile(version, date_str, commit, args.action)
else: # standalone
print("🔧 构建模式: Standalone + NSIS 安装包")
build_standalone_installer(version, date_str, commit, args.action, args.vendor)
if __name__ == "__main__":
main()