-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_with_pyinstaller.py
More file actions
126 lines (108 loc) · 2.87 KB
/
build_with_pyinstaller.py
File metadata and controls
126 lines (108 loc) · 2.87 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
HitoTop 打包脚本 - 使用 PyInstaller 打包
"""
import os
import sys
import subprocess
import shutil
import plistlib
# 创建 .spec 文件内容
SPEC_CONTENT = '''
# -*- mode: python ; coding: utf-8 -*-
a = Analysis(
['hitotop.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=['rumps', 'objc', 'Foundation', 'AppKit', 'PyObjCTools', 'certifi', 'urllib3'],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='HitoTop',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
disable_windowed_traceback=False,
argv_emulation=True,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='HitoTop',
)
app = BUNDLE(
coll,
name='HitoTop.app',
icon=None,
bundle_identifier='com.yourname.hitotop',
info_plist={
'LSUIElement': True,
'CFBundleName': 'HitoTop',
'CFBundleDisplayName': 'HitoTop',
'CFBundleIdentifier': 'com.yourname.hitotop',
'CFBundleVersion': '0.1.0',
'CFBundleShortVersionString': '0.1.0',
'NSHighResolutionCapable': True,
},
)
'''
def clean_previous_build():
"""清理之前的构建文件"""
print("清理之前的构建文件...")
dirs_to_remove = ['build', 'dist']
for dir_name in dirs_to_remove:
if os.path.exists(dir_name):
shutil.rmtree(dir_name)
if os.path.exists('hitotop.spec'):
os.remove('hitotop.spec')
def create_spec_file():
"""创建 PyInstaller 规格文件"""
print("创建 .spec 文件...")
with open('hitotop.spec', 'w') as f:
f.write(SPEC_CONTENT)
def build_app():
"""运行 PyInstaller 构建应用"""
print("正在构建应用程序...")
cmd = ['pyinstaller', 'hitotop.spec', '--clean']
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print(stdout.decode('utf-8'))
if process.returncode != 0:
print("构建失败!错误信息:")
print(stderr.decode('utf-8'))
return False
return True
def main():
"""主函数"""
print("===== 开始打包 HitoTop 应用 =====")
clean_previous_build()
create_spec_file()
if build_app():
print("\n===== 打包成功! =====")
print("应用程序位置: dist/HitoTop.app")
print("你可以将它拖到应用程序文件夹中,或直接运行它")
else:
print("\n===== 打包失败! =====")
print("请检查错误信息并解决问题")
if __name__ == "__main__":
main()