Difficult-Rocket/nuitka_build.py

78 lines
2.7 KiB
Python
Raw Normal View History

2023-06-09 15:10:15 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
import os
2023-06-09 16:57:26 +08:00
import sys
import time
2023-06-10 15:10:44 +08:00
import shutil
import zipfile
2023-06-09 15:10:15 +08:00
import subprocess
2023-06-10 15:10:44 +08:00
from pathlib import Path
2023-06-09 15:10:15 +08:00
from libs.utils import nuitka
if __name__ == '__main__':
2023-06-10 14:45:51 +08:00
compiler = nuitka.CompilerHelper()
2023-06-09 15:10:15 +08:00
2023-06-09 16:57:26 +08:00
# 修改 python 执行文件 为 运行时的 python
compiler.python_cmd = sys.executable
# 检测 --output xx 参数
if '--output' in sys.argv:
# 输入的是输出目录
compiler.output_path = sys.argv[sys.argv.index('--output') + 1]
sys.argv.remove('--output')
sys.argv.remove(compiler.output_path)
2023-06-09 17:04:17 +08:00
# 检测 --github 参数
2023-06-10 15:10:44 +08:00
is_github = False
2023-06-09 17:04:17 +08:00
if '--github' in sys.argv:
2023-06-10 15:10:44 +08:00
is_github = True
2023-06-09 17:04:17 +08:00
compiler.use_ccache = False
2023-06-10 15:10:44 +08:00
compiler.show_progress = False
compiler.output_path = Path('./build/github')
2023-06-09 17:04:17 +08:00
2023-06-09 16:57:26 +08:00
print(compiler.output_path)
2023-06-09 15:40:30 +08:00
print(compiler)
print(compiler.gen_subprocess_cmd())
2023-06-09 15:10:15 +08:00
2023-06-09 16:57:26 +08:00
# 确认是否需要编译
# 如果包含 -y 参数 则直接编译
2023-06-10 15:10:44 +08:00
if ('-y' or '-n') not in sys.argv or not is_github:
2023-06-09 16:57:26 +08:00
while (do_compile := input('Do you want to compile this file? (y/n) ')) not in ['y', 'n']:
pass
elif '-y' in sys.argv:
do_compile = 'y'
2023-06-10 15:10:44 +08:00
elif is_github:
do_compile = 'y'
2023-06-09 16:57:26 +08:00
else:
do_compile = 'n'
if do_compile == 'y':
# 编译
start_time = time.time_ns()
subprocess.run(compiler.gen_subprocess_cmd())
print('Compile Done!')
2023-06-09 17:05:14 +08:00
print(f'Compile Time: {time.time_ns() - start_time} ns ({(time.time_ns() - start_time) / 1000_000_000} s)')
2023-06-10 15:10:44 +08:00
if is_github:
# 去除无用字体文件
shutil.rmtree(compiler.output_path / 'fonts' / 'Fira_Code', ignore_errors=True)
shutil.rmtree(compiler.output_path / 'fonts' / 'scientifica', ignore_errors=True)
shutil.rmtree(compiler.output_path / 'fonts' / 'HarmonyOS_Sans' / 'HarmonyOS_Sans_Condensed', ignore_errors=True)
shutil.rmtree(compiler.output_path / 'fonts' / 'HarmonyOS_Sans' / 'HarmonyOS_Sans', ignore_errors=True)
os.remove(compiler.output_path / 'fonts' / 'Monocraft.otf')
os.remove(compiler.output_path / 'fonts' / 'SmileySans-Oblique.ttf')
# 压缩
with zipfile.ZipFile(Path('./build/Difficult_Rocket.zip'), 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as dist_zip:
for path, sub_paths, sub_files in os.walk(compiler.output_path / 'DR.dist'):
print(f'writing {path}')
for file in sub_files:
file_path = os.path.join(path, file)
dist_zip.write(file_path)