55import platform
66import argparse
77from pathlib import Path
8+ from zip import zip_files_and_folders
89
910def main ():
1011 print ("=" * 50 )
@@ -52,6 +53,7 @@ def main():
5253 dist_path = base_dir / task ['distpath' ]
5354 requirements = task .get ('install-requirements' , [])
5455 use_upx = task .get ('upx' , False )
56+ onefile = task .get ('onefile' , 0 )
5557 icon = task .get ('icon' )
5658 windowed = task .get ('windowed' , False )
5759 name = task .get ('name' )
@@ -87,8 +89,7 @@ def main():
8789 '--specpath' , str (base_dir / 'build' ),
8890 '--workpath' , str (base_dir / 'build' / 'temp' ),
8991 '--noconfirm' ,
90- '--clean' ,
91- '--onefile'
92+ '--clean'
9293 ]
9394
9495 # 添加窗口模式选项
@@ -112,18 +113,49 @@ def main():
112113 print (f"警告: UPX目录不存在 { upx_dir } " )
113114 else :
114115 print ("不使用UPX压缩" )
116+
117+ # 添加单文件打包选项
118+ if onefile == 0 :
119+ pass
120+ elif onefile == 1 :
121+ cmd .append ('--onefile' )
122+ elif onefile == 2 :
123+ cmd .append ('--onefile' )
124+ bcmd = cmd
125+
115126 # 添加主Python文件
116127 cmd .append (str (python_file ))
117128
118129 # 打印并执行命令
119130 print ("执行命令:" , ' ' .join (cmd ))
120- result = subprocess .run (cmd )
131+ result1 = subprocess .run (cmd )
132+
133+ if onefile == 2 :
134+ cmd .append (str (python_file ))
135+ result2 = subprocess .run (bcmd )
121136
122- if result .returncode == 0 :
123- print (f"打包成功: { dist_path / output_name } " )
137+ if result1 .returncode == 0 :
138+ print (f"(onefile) 打包成功: { dist_path / output_name } " )
124139 success_count += 1
125140 else :
126- print (f"打包失败,退出码: { result .returncode } " )
141+ print (f"(onefile)打包失败,退出码: { result1 .returncode } " )
142+
143+ if result2 .returncode == 0 :
144+ print (f"(onedir)打包成功: { dist_path / output_name } " )
145+ zip_files_and_folders (None , dist_path / output_name , str (dist_path / output_name )+ '.zip' )
146+ success_count += 1
147+ else :
148+ print (f"(onefdir)打包失败,退出码: { result2 .returncode } " )
149+
150+ if onefile == 0 :
151+ if result1 .returncode == 0 :
152+ success_count += 1
153+ elif onefile == 1 :
154+ if result2 .returncode == 0 :
155+ success_count += 1
156+ elif onefile == 2 :
157+ if result2 .returncode == 0 and result1 .returncode == 0 :
158+ success_count += 1
127159 except Exception as e :
128160 print (f"任务[{ i } /{ len (config )} { task ['name' ]} ]失败: { e } " )
129161 task_error_list .append (task ['name' ])
@@ -135,7 +167,7 @@ def main():
135167 else :
136168 print ("打包失败,没有成功打包任何任务" )
137169 print (f"失败的任务: { ', ' .join (task_error_list )} " )
138- sys .exit (0 )
170+ sys .exit (1 )
139171
140172if __name__ == '__main__' :
141173 main ()
0 commit comments