-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
223 lines (207 loc) · 7.09 KB
/
build.py
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File : build.py
@Time : 2024/07/05 11:19:32
@Author : KmBase
@Version : 1.0
@License : (C)Copyright 2022, KmBase
@Desc : 使用前需要先安装InnoSetup,应用更新时请不要修改app_id
"""
import glob
import platform
import subprocess
import sys
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
from perfect_build import Config
import uuid
iss_compiler = "D:\\Program Files (x86)\\Inno Setup 6\\Compil32.exe"
# subprocess.call(['pip', 'install', '-U', 'nuitka'])
# subprocess.call(['pip', 'install', '-r', 'requirements.txt'])
# subprocess.call(['pip', 'freeze', '>', 'equirements.txt'])
def generate_new_id(mode):
if mode:
print(str(uuid.uuid4()).upper())
else:
return "EF37701A-BF20-4C1C-8459-34041F620CFE"
class PerfectBuild:
# 系统信息
system = platform.system()
arch = platform.architecture()[0][:2]
# 配置信息
app_ver = Config.app_ver
app_name = Config.app_name
app_publisher = Config.app_publisher
app_url = Config.app_url
app_icon = Config.app_icon
app_exec = Config.app_exec
app_dir = Config.app_dir
def __init__(self, app_id, mode="--p"):
"""
初始化变量
"""
self.app_id = app_id
self.mode = mode.replace("--", "")
self.dist = f"{self.app_exec}.dist"
if self.mode == "p":
self.dist = f"dist//{self.app_exec}"
self.build_dir = Path.joinpath(self.app_dir, "build")
if not self.build_dir.exists():
self.build_dir.mkdir()
self.release_dir = Path.joinpath(
self.app_dir, "release", f"{self.app_ver}{self.mode}"
)
if not self.release_dir.exists():
if not self.release_dir.parent.exists():
self.release_dir.parent.mkdir()
self.release_dir.mkdir()
def nbuild(self):
"""
官方文档 : https://nuitka.net/
使用Nuitka构建example:
nuitka_cmd = [
"python",
"-m",
"nuitka",
"--show-progress",
"--show-memory",
"--standalone",
"--include-data-dir=output=output",
"--include-data-dir=icon=icon",
"--plugin-enable=pyside6",
f"--output-dir={output_dir}",
f"--include-data-files=example.db=example.db",
]
"""
output_dir = Path.joinpath(self.app_dir, "build", f"{self.system}-{self.arch}")
cmd_args = [
"python",
"-m",
"nuitka",
"--show-progress",
"--show-memory",
"--standalone",
"--include-data-dir=icon=icon",
"--plugin-enable=pyside6",
f"--output-dir={output_dir}",
]
if platform.system() == "Windows":
cmd_args.extend((f"--windows-icon-from-ico={self.app_icon}",))
# '--windows-console-mode=disable',
cmd_args.append(f"{self.app_dir}/{self.app_exec}.py")
print(cmd_args)
process = subprocess.run(cmd_args, shell=True)
if process.returncode != 0:
raise ChildProcessError("Nuitka building failed.")
print("Nuitka Building done.")
def pbuild(self):
"""
官方文档 : https://pyinstaller.org/
使用Pyinstaller构建example:
"""
output_dir = Path.joinpath(self.app_dir, "build", f"{self.system}-{self.arch}")
build_dir = Path.joinpath(output_dir, "build")
dist_dir = Path.joinpath(output_dir, "dist")
cmd_args = [
"pyinstaller",
"--onedir",
"--add-data=icon:icon",
f"--distpath={dist_dir}",
f"--workpath={build_dir}",
"--contents-directory=.",
]
if platform.system() == "Windows":
cmd_args.extend((f"-i{self.app_icon}",))
# '-w',
cmd_args.append(f"{self.app_dir}/{self.app_exec}.py")
print(cmd_args)
process = subprocess.run(cmd_args, shell=True)
if process.returncode != 0:
raise ChildProcessError("Pyinstaller building failed.")
print("Pyinstaller Building done.")
def create_setup(self):
iss_work = self.update_iss()
if Path(iss_compiler).exists:
print("Creating Windows Installer...", end="")
compiler_cmd = [str(iss_compiler), "/cc", str(iss_work)]
process = subprocess.run(compiler_cmd)
if process.returncode != 0:
raise ChildProcessError("Creating Windows installer failed.")
print("done")
def update_iss(self):
settings = {
"AppId": self.app_id,
"AppName": self.app_name,
"AppVersion": self.app_ver,
"AppMode": self.mode,
"System": self.system,
"Arch": self.arch,
"AppPublisher": self.app_publisher,
"AppURL": self.app_url,
"AppIcon": self.app_icon,
"AppExeName": self.app_exec + ".exe",
"ProjectDir": str(self.app_dir),
"BuildDir": str(self.build_dir),
"ReleaseDir": str(self.release_dir),
"Dist": str(self.dist),
"ARCH_MODE": (
"ArchitecturesInstallIn64BitMode=x64" if self.arch == "64" else ""
),
}
iss_template = f"nuitka-setup-template.iss"
iss_work = Path.joinpath(
self.build_dir, f"{self.app_name}-{self.arch}-{self.mode}.iss"
)
with open(iss_template) as template:
iss_script = template.read()
for key in settings:
iss_script = iss_script.replace(f"%%{key}%%", settings.get(key))
with open(iss_work, "w") as iss:
iss.write(iss_script)
return iss_work
def create_portable(self):
file_list = glob.glob(
f"{self.build_dir}/{self.system}-{self.arch}/{self.dist}/**",
recursive=True,
)
file_list.sort()
portable_file = (
self.release_dir
/ f"{self.app_exec}-{self.app_ver}{self.mode}-Portable-{self.system}-{self.arch}.zip"
)
print("Creating portable package...")
with ZipFile(portable_file, "w", compression=ZIP_DEFLATED) as zf:
for file in file_list:
file = Path(file)
name_in_zip = f'{self.app_exec}/{"/".join(file.parts[6:])}'
print(name_in_zip)
if file.is_file():
zf.write(file, name_in_zip)
print("Creating portable package done.")
def main(args):
"""
:param args:
--n:Nuitka building
--p:Pyinstaller building
--g:Generate APPID
:return:
"""
if len(sys.argv) < 2:
mode = "--p"
else:
mode = args[1]
if mode == "--g":
generate_new_id(True)
return
app_id = generate_new_id(False)
pb = PerfectBuild(app_id, mode)
if mode == "--n":
pb.nbuild()
else:
pb.pbuild()
pb.create_portable()
if pb.system == "Windows":
pb.create_setup()
if __name__ == "__main__":
main(sys.argv)