forked from 66maer/jx3_piano
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_exe.py
More file actions
141 lines (116 loc) · 4.26 KB
/
Copy pathbuild_exe.py
File metadata and controls
141 lines (116 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""
自动化打包脚本
使用PyInstaller将Python脚本打包成单一的可执行文件
"""
import os
import sys
import shutil
import subprocess
import time
from pathlib import Path
def main():
"""主打包函数"""
# 获取当前脚本所在目录
current_dir = Path(__file__).parent.absolute()
print("=" * 60)
print("剑网三自动演奏工具 - 自动化打包脚本")
print("=" * 60)
# 检查必要文件是否存在
required_files = ["gui.py", "build_music.py", "pydd.py", "DD.dll", "icon.ico"]
missing_files = []
for file in required_files:
if not (current_dir / file).exists():
missing_files.append(file)
if missing_files:
print(f"错误:缺少必要文件: {', '.join(missing_files)}")
return False
print("✓ 所有必要文件检查完成")
# 清理之前的构建文件
build_dirs = ["build", "dist", "__pycache__"]
for dir_name in build_dirs:
dir_path = current_dir / dir_name
if dir_path.exists():
print(f"清理目录: {dir_path}")
try:
shutil.rmtree(dir_path)
except PermissionError as e:
print(f"警告:无法删除 {dir_path},可能文件正在使用中")
print(f"错误详情: {e}")
# 尝试重命名旧目录
backup_path = current_dir / f"{dir_name}_backup_{int(time.time())}"
try:
dir_path.rename(backup_path)
print(f"已将旧目录重命名为: {backup_path}")
except Exception as e2:
print(f"重命名也失败了: {e2}")
print("请手动关闭可能正在运行的程序后重试")
# 删除旧的spec文件
spec_file = current_dir / "gui.spec"
if spec_file.exists():
spec_file.unlink()
print("✓ 清理完成")
# 获取Python环境路径
python_exe = sys.executable
venv_dir = Path(python_exe).parent
pyinstaller_exe = venv_dir / "pyinstaller.exe"
# 如果找不到pyinstaller.exe,尝试Scripts目录
if not pyinstaller_exe.exists():
pyinstaller_exe = venv_dir / "Scripts" / "pyinstaller.exe"
if not pyinstaller_exe.exists():
print("错误:找不到pyinstaller.exe")
return False
# 构建PyInstaller命令
cmd = [
str(pyinstaller_exe),
"--onefile", # 打包成单一exe文件
"--windowed", # GUI模式,不显示控制台
"--noconfirm", # 不询问确认
f"--icon={current_dir / 'icon.ico'}", # 设置图标
f"--add-data={current_dir / 'DD.dll'};.", # 添加DLL文件
"--uac-admin", # 请求管理员权限
"--clean", # 清理临时文件
"gui.py", # 主脚本
]
print("开始打包...")
print(f"执行命令: {' '.join(cmd)}")
print("-" * 40)
try:
# 执行打包命令
result = subprocess.run(
cmd, cwd=current_dir, capture_output=False, text=True, check=True
)
print("-" * 40)
print("✓ 打包完成!")
# 检查输出文件
exe_file = current_dir / "dist" / "gui.exe"
if exe_file.exists():
file_size = exe_file.stat().st_size / (1024 * 1024) # MB
print(f"输出文件: {exe_file}")
print(f"文件大小: {file_size:.2f} MB")
# 创建发布目录
release_dir = current_dir / "release"
release_dir.mkdir(exist_ok=True)
# 复制exe到发布目录
release_exe = release_dir / "剑网三自动演奏工具.exe"
shutil.copy2(exe_file, release_exe)
print(f"发布文件: {release_exe}")
print("\n" + "=" * 60)
print("打包成功完成!")
print("=" * 60)
return True
else:
print("错误:未找到输出的exe文件")
return False
except subprocess.CalledProcessError as e:
print(f"打包失败: {e}")
return False
except Exception as e:
print(f"发生错误: {e}")
return False
if __name__ == "__main__":
if main():
input("按任意键退出...")
else:
input("按任意键退出...")
sys.exit(1)