1+ import os
2+ import sys
3+ import yaml
4+ import subprocess
5+ import platform
6+ import argparse
7+ from pathlib import Path
8+ # from zip import zip_files_and_folders
9+
10+ def main ():
11+ print ("=" * 50 )
12+ print ("=" * 50 )
13+ # 修复编码问题 - 设置UTF-8输出
14+ sys .stdout .reconfigure (encoding = 'utf-8' ) # Python 3.7+
15+ sys .stderr .reconfigure (encoding = 'utf-8' )
16+
17+ # 检查Python版本
18+ if sys .version_info < (3 , 7 ):
19+ print ("错误: 请使用Python 3.7或更高版本" )
20+ return
21+
22+ Machine = platform .system ()
23+ arrch = platform .machine ()
24+
25+ print (f"当前操作系统: { Machine } { arrch } " )
26+ print (f"平台详情: { platform .platform ()} " )
27+ print (f"Python版本: { sys .version } " )
28+
29+ # 解析命令行参数
30+ parser = argparse .ArgumentParser (description = 'PyInstaller打包脚本' )
31+ parser .add_argument ('config' , help = '配置文件路径' )
32+ args = parser .parse_args ()
33+
34+ # 读取配置文件
35+ with open (args .config , 'r' , encoding = 'utf-8' ) as f :
36+ config = yaml .safe_load (f )
37+
38+ # 基础路径设置
39+ base_dir = Path (__file__ ).parent .parent # 项目根目录
40+
41+ success_count = 0
42+ task_error_list = []
43+ # 遍历所有打包任务
44+ for i , task in enumerate (config , start = 1 ):
45+ try :
46+ print (f"\n { '=' * 40 } " )
47+ print (f"开始打包任务: [{ i } /{ len (config )} ] { task ['name' ]} " )
48+ print (f"{ '=' * 40 } " )
49+
50+ # 解析任务参数
51+ python_file = base_dir / task ['python-file' ]
52+ dist_path = base_dir / task ['distpath' ]
53+ requirements = task .get ('install-requirements' , [])
54+ use_upx = task .get ('upx' , False )
55+ # onefile = task.get('onefile', 0)
56+ icon = task .get ('icon' )
57+ windows_disable_console = task .get ('windows-disable-console' , False )
58+ name = task .get ('name' )
59+ version = task .get ('version' )
60+ output_name_template = task .get ('output-name-template' , '{{name}}_{{version}}_{{arch}}_{{os}}' )
61+ if arrch == "AMD64" :
62+ arrch = "x64"
63+ output_name = output_name_template .replace ('{{name}}' , name ).replace ('{{version}}' , version ).replace ('{{arch}}' , arrch ).replace ('{{os}}' , Machine )
64+
65+ # 检查操作系统和架构
66+ # if Machine not in task.get('os', []):
67+ # print(f"警告: 任务 [{i}/{len(config)} {task['name']}] 不支持当前操作系统 {Machine}")
68+ # continue
69+ # if arrch not in task.get('arch', []):
70+ # print(f"警告: 任务 [{i}/{len(config)} {task['name']}] 不支持当前架构 {arrch}")
71+ # continue
72+
73+ # 检查Python文件是否存在
74+ if not python_file .exists ():
75+ print (f"错误: Python文件不存在 { python_file } " )
76+ continue
77+
78+ # 安装依赖
79+ if requirements :
80+ print (f"安装依赖: { ', ' .join (requirements )} " )
81+ subprocess .run ([sys .executable , '-m' , 'pip' , 'install' ] + requirements , check = True )
82+
83+ # 构建PyInstaller命令
84+ cmd = [
85+ sys .executable , '-m' , 'nuitka' ,
86+ f'--output-filename={ output_name } .exe' ,
87+ f'--output-dir={ dist_path } ' , # 输出目录
88+ '--onefile' , # 单文件
89+ '--standalone' ,
90+ '--mingw64'
91+ ]
92+
93+ # 添加窗口模式选项
94+ if windows_disable_console :
95+ cmd .append ('--windows-disable-console' )
96+
97+ # 添加图标选项
98+ if icon :
99+ icon_path = base_dir / icon
100+ if icon_path .exists ():
101+ cmd .extend (['--windows-icon-from-ico=icon.ico' , str (icon_path )])
102+ else :
103+ print (f"警告: 图标文件不存在 { icon_path } " )
104+
105+
106+ # 添加主Python文件
107+ cmd .append (str (python_file ))
108+
109+ # 打印并执行命令
110+ print ("执行命令:" , ' ' .join (cmd ))
111+ result = subprocess .run (cmd )
112+
113+ if result .returncode == 0 :
114+ print (f"(onefile)打包成功: { dist_path / output_name } " )
115+ success_count += 1
116+ else :
117+ print (f"(onefile)打包失败,退出码: { result .returncode } " )
118+ except Exception as e :
119+ print (f"任务[{ i } /{ len (config )} { task ['name' ]} ]失败: { e } " )
120+ task_error_list .append (task ['name' ])
121+ continue
122+ if success_count != 0 :
123+ print (f"打包完成,成功打包 [{ success_count } /{ len (config )} ] 个任务" )
124+ if task_error_list != []:
125+ print (f"打包失败的任务: { ', ' .join (task_error_list )} " )
126+ else :
127+ print ("打包失败,没有成功打包任何任务" )
128+ print (f"失败的任务: { ', ' .join (task_error_list )} " )
129+ sys .exit (0 )
130+
131+ if __name__ == '__main__' :
132+ main ()
0 commit comments