-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathbuild_windows.py
More file actions
162 lines (146 loc) · 4.31 KB
/
build_windows.py
File metadata and controls
162 lines (146 loc) · 4.31 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
#!/usr/bin/env python3
"""
Windows exe 打包脚本
使用方法(在 Windows 上运行):
1. pip install pyinstaller
2. python build_windows.py
3. exe 自动复制到 wechat-cli-dist/wechat-cli.exe
"""
import os
import sys
import shutil
import subprocess
# 确保在项目根目录运行
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
print("=" * 60)
print(" wechat-cli Windows exe 打包")
print("=" * 60)
# 1. 清理旧文件
print("\n[1/4] 清理旧构建文件...")
for d in ["build", "dist", "__pycache__"]:
if os.path.exists(d):
shutil.rmtree(d)
print(f" 删除: {d}")
# 2. 确保 PyInstaller 已安装
print("\n[2/4] 检查 PyInstaller...")
try:
import PyInstaller
print(" PyInstaller 已安装")
except ImportError:
print(" 安装 PyInstaller...")
subprocess.run([sys.executable, "-m", "pip", "install", "pyinstaller"], check=True)
# 3. 创建 spec 文件(更好的控制)
print("\n[3/4] 创建 spec 文件...")
spec_content = '''# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['entry.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[
'wechat_cli',
'wechat_cli.main',
'wechat_cli.core',
'wechat_cli.core.config',
'wechat_cli.core.context',
'wechat_cli.core.crypto',
'wechat_cli.core.messages',
'wechat_cli.core.contacts',
'wechat_cli.core.db_cache',
'wechat_cli.core.key_utils',
'wechat_cli.keys',
'wechat_cli.keys.scanner_windows',
'wechat_cli.keys.common',
'wechat_cli.commands',
'wechat_cli.commands.init',
'wechat_cli.commands.sessions',
'wechat_cli.commands.history',
'wechat_cli.commands.search',
'wechat_cli.commands.contacts',
'wechat_cli.commands.new_messages',
'wechat_cli.commands.members',
'wechat_cli.commands.export',
'wechat_cli.commands.stats',
'wechat_cli.commands.unread',
'wechat_cli.commands.favorites',
'wechat_cli.output',
'Crypto',
'Crypto.Cipher',
'Crypto.Cipher.AES',
'zstandard',
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[
'wechat_cli.keys.scanner_macos',
'wechat_cli.keys.scanner_linux',
'PyObjCTools',
'objc',
],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='wechat-cli',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
'''
with open("wechat-cli.spec", "w", encoding="utf-8") as f:
f.write(spec_content)
print(" 创建: wechat-cli.spec")
# 4. 运行 PyInstaller
print("\n[4/4] 运行 PyInstaller...")
result = subprocess.run(
[sys.executable, "-m", "PyInstaller", "--clean", "wechat-cli.spec"],
check=False
)
if result.returncode == 0:
exe_path = os.path.join("dist", "wechat-cli.exe")
if os.path.exists(exe_path):
size_mb = os.path.getsize(exe_path) / 1024 / 1024
# 复制到分发目录
dist_dir = "wechat-cli-dist"
if os.path.exists(dist_dir):
dest_path = os.path.join(dist_dir, "wechat-cli.exe")
shutil.copy2(exe_path, dest_path)
print(f"\n 已复制到: {dest_path}")
print("\n" + "=" * 60)
print(" 打包成功!")
print("=" * 60)
print(f"\n 构建: {os.path.abspath(exe_path)}")
print(f" 分发: {os.path.abspath(os.path.join(dist_dir, 'wechat-cli.exe'))}")
print(f" 大小: {size_mb:.1f} MB")
print("\n使用方法:")
print(" cd wechat-cli-dist")
print(" wechat-cli.exe init # 初始化")
print(" wechat-cli.exe sessions # 查看会话")
print(" wechat-cli.exe --daily # 每日导出")
else:
print("\n[!] 未找到输出文件")
sys.exit(1)
else:
print("\n[!] PyInstaller 失败")
sys.exit(1)