|
|
#!/usr/bin/env python3
|
|
|
"""
|
|
|
MagicWord v0.3 打包脚本
|
|
|
版本号:0.3
|
|
|
输出目录:dist/
|
|
|
图标:resources/icons/app_icon_256X256.png
|
|
|
"""
|
|
|
|
|
|
import os
|
|
|
import sys
|
|
|
import subprocess
|
|
|
import shutil
|
|
|
from pathlib import Path
|
|
|
|
|
|
def clean_dist():
|
|
|
"""清理dist目录"""
|
|
|
dist_dir = Path("dist")
|
|
|
if dist_dir.exists():
|
|
|
shutil.rmtree(dist_dir)
|
|
|
dist_dir.mkdir(exist_ok=True)
|
|
|
|
|
|
def build_executable():
|
|
|
"""使用PyInstaller构建可执行文件"""
|
|
|
print("开始构建 MagicWord v0.3...")
|
|
|
|
|
|
# 清理之前的构建
|
|
|
clean_dist()
|
|
|
|
|
|
# PyInstaller 参数
|
|
|
pyinstaller_args = [
|
|
|
"-m", "PyInstaller",
|
|
|
"--onefile", # 单文件模式
|
|
|
"--windowed", # 窗口模式(无控制台)
|
|
|
f"--name=MagicWord_v0.3", # 可执行文件名
|
|
|
f"--icon=resources/icons/app_icon_256X256.png", # 图标文件
|
|
|
"--add-data=resources:resources", # 添加资源文件
|
|
|
"--add-data=src:src", # 添加源代码
|
|
|
"--clean", # 清理临时文件
|
|
|
"--noconfirm", # 不确认覆盖
|
|
|
"src/main.py" # 主程序入口
|
|
|
]
|
|
|
|
|
|
# 执行打包命令
|
|
|
cmd = [sys.executable] + pyinstaller_args
|
|
|
print(f"执行命令: {' '.join(cmd)}")
|
|
|
|
|
|
try:
|
|
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
|
if result.returncode != 0:
|
|
|
print(f"打包失败: {result.stderr}")
|
|
|
return False
|
|
|
else:
|
|
|
print("打包成功!")
|
|
|
return True
|
|
|
except Exception as e:
|
|
|
print(f"执行打包时出错: {e}")
|
|
|
return False
|
|
|
|
|
|
def check_output():
|
|
|
"""检查输出文件"""
|
|
|
dist_dir = Path("dist")
|
|
|
exe_files = list(dist_dir.glob("MagicWord_v0.3*"))
|
|
|
|
|
|
if exe_files:
|
|
|
print(f"生成的文件:")
|
|
|
for exe in exe_files:
|
|
|
size = exe.stat().st_size / (1024 * 1024) # MB
|
|
|
print(f" {exe.name} - {size:.1f} MB")
|
|
|
return True
|
|
|
else:
|
|
|
print("未找到生成的可执行文件")
|
|
|
return False
|
|
|
|
|
|
def main():
|
|
|
"""主函数"""
|
|
|
print("=" * 50)
|
|
|
print("MagicWord v0.3 打包工具")
|
|
|
print("=" * 50)
|
|
|
|
|
|
# 检查Python环境
|
|
|
print(f"Python版本: {sys.version}")
|
|
|
print(f"Python路径: {sys.executable}")
|
|
|
|
|
|
# 检查文件是否存在
|
|
|
required_files = [
|
|
|
"src/main.py",
|
|
|
"resources/icons/app_icon_256X256.png"
|
|
|
]
|
|
|
|
|
|
missing_files = []
|
|
|
for file in required_files:
|
|
|
if not Path(file).exists():
|
|
|
missing_files.append(file)
|
|
|
|
|
|
if missing_files:
|
|
|
print(f"缺少必需文件: {missing_files}")
|
|
|
return
|
|
|
|
|
|
# 构建可执行文件
|
|
|
if build_executable():
|
|
|
# 检查输出
|
|
|
if check_output():
|
|
|
print("\n✅ 打包完成!可执行文件位于 dist/ 目录")
|
|
|
else:
|
|
|
print("\n❌ 打包可能存在问题")
|
|
|
else:
|
|
|
print("\n❌ 打包失败")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main() |