fix:修复一些BUG
This commit is contained in:
109
script/setup.py
Normal file
109
script/setup.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# setup.py
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
|
||||
def copy_resources():
|
||||
"""
|
||||
将资源文件从项目根目录复制到 dist 文件夹中,
|
||||
以便用户可以直接在 exe 旁边修改这些文件。
|
||||
"""
|
||||
# 1. 定义路径
|
||||
# setup.py 所在的目录 (script/)
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
# 项目根目录 (script/ 的上一级)
|
||||
project_root = os.path.dirname(current_dir)
|
||||
# 输出目录 (script/dist)
|
||||
dist_dir = os.path.join(current_dir, "dist")
|
||||
|
||||
print(f"\n--- 正在复制外部资源到 {dist_dir} ---")
|
||||
|
||||
if not os.path.exists(dist_dir):
|
||||
print("错误: dist 文件夹不存在,请先运行打包。")
|
||||
return
|
||||
|
||||
# 2. 定义要复制的资源清单
|
||||
# 格式: (源路径相对root, 目标文件夹相对dist)
|
||||
# 如果目标是根目录,用 "" 表示
|
||||
resources_to_copy = [
|
||||
("config.toml", ""), # 复制 config.toml 到 dist/
|
||||
("fonts", "fonts"), # 复制 fonts 文件夹 到 dist/fonts
|
||||
("data", "data"), # 复制 data 文件夹 到 dist/data
|
||||
("templates", "templates"), # 复制 templates 文件夹 到 dist/templates
|
||||
("public", "public"), # 复制 public 文件夹 到 dist/public
|
||||
]
|
||||
|
||||
for src_name, dest_name in resources_to_copy:
|
||||
src_path = os.path.join(project_root, src_name)
|
||||
dest_path = os.path.join(dist_dir, dest_name)
|
||||
|
||||
try:
|
||||
if os.path.isfile(src_path):
|
||||
# --- 复制文件 ---
|
||||
shutil.copy2(src_path, dest_path) # copy2 保留文件元数据
|
||||
print(f"✅ 已复制文件: {src_name}")
|
||||
|
||||
elif os.path.isdir(src_path):
|
||||
# --- 复制文件夹 ---
|
||||
# dirs_exist_ok=True 允许覆盖已存在的目录 (Python 3.8+)
|
||||
shutil.copytree(src_path, dest_path, dirs_exist_ok=True)
|
||||
print(f"✅ 已复制目录: {src_name}")
|
||||
|
||||
else:
|
||||
print(f"⚠️ 警告: 源文件不存在,跳过: {src_path}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 复制失败 {src_name}: {e}")
|
||||
|
||||
|
||||
def build_exe():
|
||||
"""使用 PyInstaller 打包 main_app.py"""
|
||||
|
||||
# --- 内部资源 (打入包内的资源) ---
|
||||
# 即使我们在外部复制了一份,为了保证 exe 独立运行(万一外部文件被删),
|
||||
# 建议依然保留这些作为“默认出厂设置”打入包内。
|
||||
resource_paths = [
|
||||
"--add-data=../config.toml:.",
|
||||
"--add-data=../fonts:fonts",
|
||||
"--add-data=../data:data",
|
||||
"--add-data=../templates:templates",
|
||||
"--add-data=../public:public",
|
||||
]
|
||||
|
||||
try:
|
||||
command = [
|
||||
sys.executable, "-m", "PyInstaller",
|
||||
"--onefile",
|
||||
"--windowed", # 调试阶段建议先注释掉,确认无误后再开启
|
||||
"--name=尚城幼儿园幼儿学期发展报告",
|
||||
"--icon=../public/icon.ico",
|
||||
"../main.pyw"
|
||||
]
|
||||
|
||||
# 添加资源参数
|
||||
command.extend(resource_paths)
|
||||
|
||||
print("--- 开始打包 (PyInstaller) ---")
|
||||
# 运行 PyInstaller
|
||||
subprocess.run(command, check=True, cwd=os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
print("\n--- PyInstaller 打包完成!---")
|
||||
|
||||
# === 执行资源复制 ===
|
||||
copy_resources()
|
||||
|
||||
print(f"\n🎉 全部完成!请查看 'dist' 文件夹。")
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"\n--- 打包失败 ---")
|
||||
print(f"命令执行出错: {e}")
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"\n--- 打包失败 ---")
|
||||
print("错误:找不到 PyInstaller。")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
build_exe()
|
||||
Reference in New Issue
Block a user