77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
import os
|
||
import sys
|
||
import glob
|
||
import platform
|
||
import subprocess
|
||
from pathlib import Path
|
||
from zipfile import ZIP_DEFLATED, ZipFile
|
||
|
||
from main import APP_NAME, APP_VERSION
|
||
|
||
COMPILER = "mingw64"
|
||
OUTPUT_PATH = Path('output')
|
||
RESOURCES_PATH = Path("resources")
|
||
RELEASE_PATH = OUTPUT_PATH / APP_NAME
|
||
|
||
BUILD_PATH = OUTPUT_PATH / f'{platform.system().lower()}-{platform.machine().lower()}'
|
||
|
||
|
||
def build_main():
|
||
nuitka_cmd = [
|
||
sys.executable,
|
||
'-m',
|
||
'nuitka',
|
||
'--standalone',
|
||
'--show-memory',
|
||
'--show-progress',
|
||
'--plugin-enable=pyside6',
|
||
'--include-module=qt_material',
|
||
# 把插件 SDK 的 runtime_hook 编进 exe,启动时把 plugins/*/ 加进 sys.path。
|
||
# Nuitka 无 --runtime-hook 选项,用 --include-module + main.py 顶部 import 替代。
|
||
# 宿主不需要预知插件依赖哪些 stdlib——纯 Python 依赖编进 mil.pyd,
|
||
# C 扩展由插件自带,runtime_hook 把 plugins/*/ 加进 sys.path 后能加载。
|
||
'--include-module=runtime_hook',
|
||
f"--include-data-dir={RESOURCES_PATH}=resources",
|
||
]
|
||
|
||
icon_path = str(RESOURCES_PATH / "main.ico")
|
||
if os.path.exists(icon_path):
|
||
nuitka_cmd.extend([
|
||
f'--windows-icon-from-ico={icon_path}',
|
||
])
|
||
nuitka_cmd.append(f'--output-dir={BUILD_PATH}')
|
||
if platform.system() == 'Windows':
|
||
nuitka_cmd.append('--windows-console-mode=disable')
|
||
nuitka_cmd.append(f'--{COMPILER}')
|
||
|
||
nuitka_cmd.append('./main.py')
|
||
|
||
result = subprocess.run(nuitka_cmd,shell=True)
|
||
if result.returncode != 0:
|
||
raise RuntimeError('Nuitka building failed.')
|
||
|
||
print('Building done.')
|
||
|
||
|
||
def create_zip():
|
||
file_list = glob.glob(f'{BUILD_PATH / APP_NAME / "dist"}', recursive=True)
|
||
file_list.sort()
|
||
|
||
if not os.path.exists(RELEASE_PATH):
|
||
os.mkdir(RELEASE_PATH)
|
||
|
||
portable_file = RELEASE_PATH / f'{APP_NAME}-{platform.system()}-{platform.machine()}-{APP_VERSION}.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'{APP_NAME}/{"/".join(file.parts[3:])}'
|
||
print(name_in_zip)
|
||
zf.write(file, name_in_zip)
|
||
|
||
print('Creating portable package done.')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
build_main() |