Skip to content

Commit f166581

Browse files
更新打包脚本:自动扫描并打包项目根目录下所有内容
1 parent 7e1b606 commit f166581

File tree

1 file changed

+54
-70
lines changed

1 file changed

+54
-70
lines changed

build.py

Lines changed: 54 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
AssignSticker 打包脚本
44
使用 PyInstaller 打包成 Windows x64 可执行文件
5+
打包项目根目录下的所有内容(排除特定文件和目录)
56
"""
67

78
import os
@@ -29,47 +30,61 @@ def clean_build_dirs():
2930

3031

3132
def get_all_data_files():
32-
"""获取所有需要打包的数据文件和目录"""
33+
"""获取所有需要打包的数据文件和目录(自动扫描根目录)"""
3334
project_root = get_project_root()
3435

35-
# 定义需要包含的目录
36-
include_dirs = [
37-
'icons',
38-
'htmls',
39-
'saying',
40-
'desktop_widgets',
41-
]
42-
43-
# 定义需要包含的单个文件
44-
include_files = [
45-
'font.ttf',
46-
'icon.ico',
47-
'introduce',
48-
'banner.png',
49-
'LICENSE',
50-
'README.md',
51-
]
36+
# 定义需要排除的文件和目录
37+
exclude_items = {
38+
# Python 相关
39+
'build.py', # 本打包脚本
40+
'build', # 构建目录
41+
'dist', # 输出目录
42+
'__pycache__', # Python 缓存
43+
'*.pyc', # 编译后的 Python 文件
44+
'*.pyo', # 优化的 Python 文件
45+
'.git', # Git 目录
46+
'.gitignore', # Git 忽略文件
47+
'.github', # GitHub 工作流目录
48+
'release', # 发布目录
49+
'logs', # 日志目录
50+
'data', # 运行时数据目录
51+
# IDE 相关
52+
'.vscode', # VS Code 配置
53+
'.idea', # PyCharm 配置
54+
'*.spec', # PyInstaller spec 文件
55+
}
5256

5357
datas = []
5458

55-
# 添加目录
56-
for dir_name in include_dirs:
57-
dir_path = project_root / dir_name
58-
if dir_path.exists():
59-
# PyInstaller --add-data 格式: "源路径;目标路径"
60-
datas.append(f"--add-data={dir_name};{dir_name}")
61-
print(f" 包含目录: {dir_name}")
62-
else:
63-
print(f" 警告: 目录不存在 {dir_name}")
64-
65-
# 添加单个文件
66-
for file_name in include_files:
67-
file_path = project_root / file_name
68-
if file_path.exists():
69-
datas.append(f"--add-data={file_name};.")
70-
print(f" 包含文件: {file_name}")
71-
else:
72-
print(f" 警告: 文件不存在 {file_name}")
59+
print("\n扫描项目根目录...")
60+
61+
# 遍历项目根目录下的所有项目
62+
for item in project_root.iterdir():
63+
item_name = item.name
64+
65+
# 检查是否在排除列表中
66+
if item_name in exclude_items:
67+
print(f" 排除: {item_name}")
68+
continue
69+
70+
# 跳过隐藏文件(以.开头)
71+
if item_name.startswith('.') and item_name != '.':
72+
print(f" 排除隐藏项: {item_name}")
73+
continue
74+
75+
# 跳过 Python 缓存文件
76+
if item_name.endswith(('.pyc', '.pyo', '.spec')):
77+
print(f" 排除缓存: {item_name}")
78+
continue
79+
80+
if item.is_dir():
81+
# 目录: --add-data=目录名;目录名
82+
datas.append(f"--add-data={item_name};{item_name}")
83+
print(f" 包含目录: {item_name}")
84+
elif item.is_file():
85+
# 文件: --add-data=文件名;.
86+
datas.append(f"--add-data={item_name};.")
87+
print(f" 包含文件: {item_name}")
7388

7489
return datas
7590

@@ -127,7 +142,7 @@ def build_exe():
127142

128143

129144
def create_distribution():
130-
"""创建发布包"""
145+
"""创建发布包(复制dist目录下的exe文件)"""
131146
print("\n创建发布包...")
132147

133148
project_root = get_project_root()
@@ -150,46 +165,14 @@ def create_distribution():
150165
print(f" 错误: 找不到 {exe_source}")
151166
return False
152167

153-
# 复制其他文件和目录
154-
copy_items = [
155-
'icons',
156-
'htmls',
157-
'saying',
158-
'desktop_widgets',
159-
'font.ttf',
160-
'icon.ico',
161-
'introduce',
162-
'banner.png',
163-
'LICENSE',
164-
'README.md',
165-
]
166-
167-
for item in copy_items:
168-
source = project_root / item
169-
target = release_dir / item
170-
171-
if not source.exists():
172-
print(f" 跳过: {item} (不存在)")
173-
continue
174-
175-
try:
176-
if source.is_dir():
177-
shutil.copytree(source, target, ignore=shutil.ignore_patterns('__pycache__', '*.pyc'))
178-
print(f" 复制目录: {item}")
179-
else:
180-
shutil.copy2(source, target)
181-
print(f" 复制文件: {item}")
182-
except Exception as e:
183-
print(f" 错误复制 {item}: {e}")
184-
185168
# 创建 ZIP 压缩包
186169
print("\n创建 ZIP 压缩包...")
187170
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
188171
zip_name = f"AssignSticker-windows-x64-{timestamp}"
189172
zip_path = project_root / 'release' / zip_name
190173

191174
try:
192-
shutil.make_archive(str(zip_path), 'zip', str(release_dir))
175+
shutil.make_archive(str(zip_path), 'zip', str(dist_dir))
193176
print(f" 创建: {zip_name}.zip")
194177
except Exception as e:
195178
print(f" 错误创建 ZIP: {e}")
@@ -233,6 +216,7 @@ def main():
233216
print("="*60)
234217
print(f"\n可执行文件: dist/AssignSticker.exe")
235218
print(f"发布包: release/AssignSticker-windows-x64-*.zip")
219+
print("\n注意: 所有资源文件已通过 --add-data 打包到exe内部")
236220
else:
237221
print("\n打包失败,请检查错误信息")
238222
sys.exit(1)

0 commit comments

Comments
 (0)