Skip to content

Commit 4ba9b05

Browse files
committed
添加 zip.py 文件,包含压缩文件和文件夹的功能;更新 build.py,支持单文件打包选项;修改 config.yml,新增 onefile 配置
1 parent 1876811 commit 4ba9b05

3 files changed

Lines changed: 101 additions & 7 deletions

File tree

package/build.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import argparse
77
from pathlib import Path
8+
from zip import zip_files_and_folders
89

910
def 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

140172
if __name__ == '__main__':
141173
main()

package/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'ebooklib'
88
]
99
upx: true
10+
onefile: 2 # 0:文件夹 1:单文件 2:两者
1011
icon:
1112
windowed: False
1213
distpath: 'dist'
@@ -21,6 +22,7 @@
2122
'pywin32'
2223
]
2324
upx: true
25+
onefile: 1
2426
icon:
2527
windowed: False
2628
distpath: 'dist'
@@ -36,6 +38,7 @@
3638
'mutagen'
3739
]
3840
upx: true
41+
onefile: 1
3942
icon:
4043
windowed: False
4144
distpath: 'dist'
@@ -48,6 +51,7 @@
4851
'pyftpdlib'
4952
]
5053
upx: true
54+
onefile: 1
5155
icon:
5256
windowed: False
5357
distpath: 'dist'
@@ -92,6 +96,7 @@
9296
'pyftpdlib'
9397
]
9498
upx: true
99+
onefile: 1 # 0:文件夹 1:单文件 2:两者
95100
icon:
96101
windowed: False
97102
distpath: 'dist'

package/zip.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
import zipfile
3+
4+
def add_to_zip(zipf, path, arcname=None):
5+
"""递归地将文件或文件夹添加到 ZIP 文件中"""
6+
if os.path.isfile(path):
7+
zipf.write(path, arcname or path)
8+
elif os.path.isdir(path):
9+
for root, dirs, files in os.walk(path):
10+
for file in files:
11+
file_path = os.path.join(root, file)
12+
# 计算在 ZIP 文件中的相对路径
13+
archive_path = os.path.relpath(file_path, os.path.dirname(path))
14+
zipf.write(file_path, archive_path)
15+
16+
def zip_files_and_folders(file_path, folder_path, output_zip):
17+
"""将文件和文件夹压缩到一个 ZIP 文件中"""
18+
try:
19+
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
20+
if not file_path and not folder_path:
21+
print("错误: 至少需要提供一个文件或文件夹路径")
22+
os.remove(output_zip)
23+
return
24+
if isinstance(file_path, str):
25+
file_path = [file_path]
26+
if isinstance(folder_path, str):
27+
folder_path = [folder_path]
28+
# 添加单个文件
29+
if file_path:
30+
if isinstance(file_path, list):
31+
for file in file_path:
32+
if os.path.exists(file) and os.path.isfile(file):
33+
add_to_zip(zipf, file)
34+
print(f"add: {file}")
35+
else:
36+
print(f"not found file: {file}")
37+
38+
# 添加文件夹
39+
if folder_path:
40+
if isinstance(folder_path, list):
41+
for folder in folder_path:
42+
if os.path.exists(folder) and os.path.isdir(folder):
43+
add_to_zip(zipf, folder)
44+
print(f"add: {folder}")
45+
else:
46+
print(f"not found folder: {folder}")
47+
print(f"压缩完成! ZIP 文件已保存为: {output_zip}")
48+
return
49+
except Exception as e:
50+
print(f"压缩过程中发生错误: {e}")
51+
return
52+
if __name__ == "__main__":
53+
# usage
54+
print('usage:')
55+
print("="*42)
56+
print("from zip import zip_files_and_folders")
57+
print("zip_files_and_folders('file_path', 'folder_path', 'output_zip')")

0 commit comments

Comments
 (0)