177 lines
4.5 KiB
Python
177 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
使用cx_Freeze分别构建主程序和验证程序
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
def clean_build():
|
|
"""清理之前的构建文件"""
|
|
dirs_to_clean = ["build", "dist"]
|
|
for dir_name in dirs_to_clean:
|
|
if os.path.exists(dir_name):
|
|
print(f"清理目录: {dir_name}")
|
|
shutil.rmtree(dir_name)
|
|
|
|
|
|
def build_main():
|
|
"""构建主程序"""
|
|
print("构建主程序...")
|
|
|
|
# 创建临时的setup文件
|
|
setup_content = '''
|
|
from cx_Freeze import setup, Executable
|
|
|
|
build_exe_options = {
|
|
"packages": ["tkinter", "mysql.connector", "cryptography", "pyperclip", "json"],
|
|
"build_exe": "dist/main",
|
|
"excludes": ["unittest", "test"]
|
|
}
|
|
|
|
executables = [Executable("main.py", base="Win32GUI")]
|
|
|
|
setup(
|
|
name="MainApp",
|
|
version="1.0",
|
|
options={"build_exe": build_exe_options},
|
|
executables=executables
|
|
)
|
|
'''
|
|
|
|
with open("setup_main.py", "w", encoding="utf-8") as f:
|
|
f.write(setup_content)
|
|
|
|
try:
|
|
result = subprocess.run([sys.executable, "setup_main.py", "build"],
|
|
capture_output=True, text=True)
|
|
if result.returncode == 0:
|
|
print("主程序构建成功")
|
|
return True
|
|
else:
|
|
print(f"主程序构建失败: {result.stderr}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"主程序构建异常: {e}")
|
|
return False
|
|
finally:
|
|
# 清理临时文件
|
|
if os.path.exists("setup_main.py"):
|
|
os.remove("setup_main.py")
|
|
|
|
|
|
def build_validator():
|
|
"""构建验证程序"""
|
|
print("构建验证程序...")
|
|
|
|
# 创建临时的setup文件
|
|
setup_content = '''
|
|
from cx_Freeze import setup, Executable
|
|
|
|
build_exe_options = {
|
|
"packages": ["mysql.connector", "cryptography", "ctypes", "tempfile", "subprocess"],
|
|
"build_exe": "dist/validator",
|
|
"excludes": ["unittest", "test", "tkinter"]
|
|
}
|
|
|
|
executables = [Executable("validator.py", base="Console")]
|
|
|
|
setup(
|
|
name="Validator",
|
|
version="1.0",
|
|
options={"build_exe": build_exe_options},
|
|
executables=executables
|
|
)
|
|
'''
|
|
|
|
with open("setup_validator.py", "w", encoding="utf-8") as f:
|
|
f.write(setup_content)
|
|
|
|
try:
|
|
result = subprocess.run([sys.executable, "setup_validator.py", "build"],
|
|
capture_output=True, text=True)
|
|
if result.returncode == 0:
|
|
print("验证程序构建成功")
|
|
return True
|
|
else:
|
|
print(f"验证程序构建失败: {result.stderr}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"验证程序构建异常: {e}")
|
|
return False
|
|
finally:
|
|
# 清理临时文件
|
|
if os.path.exists("setup_validator.py"):
|
|
os.remove("setup_validator.py")
|
|
|
|
|
|
def organize_files():
|
|
"""整理构建后的文件"""
|
|
print("整理文件...")
|
|
|
|
# 创建最终的dist目录
|
|
final_dist = Path("dist/final")
|
|
final_dist.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 复制主程序文件
|
|
main_dir = Path("dist/main")
|
|
if main_dir.exists():
|
|
for file in main_dir.iterdir():
|
|
if file.is_file():
|
|
shutil.copy2(file, final_dist / file.name)
|
|
|
|
# 复制验证程序文件
|
|
validator_dir = Path("dist/validator")
|
|
if validator_dir.exists():
|
|
for file in validator_dir.iterdir():
|
|
if file.is_file():
|
|
# 避免重复复制相同的DLL文件
|
|
dest_file = final_dist / file.name
|
|
if not dest_file.exists() or file.name.endswith('.exe'):
|
|
shutil.copy2(file, dest_file)
|
|
|
|
# 重命名主程序
|
|
main_exe = final_dist / "main.exe"
|
|
if main_exe.exists():
|
|
main_exe.rename(final_dist / "EXE加密工具.exe")
|
|
|
|
print(f"所有文件已整理到: {final_dist.absolute()}")
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
print("开始构建EXE加密工具...")
|
|
|
|
# 清理之前的构建
|
|
clean_build()
|
|
|
|
# 分别构建两个程序
|
|
success = True
|
|
|
|
if not build_main():
|
|
success = False
|
|
|
|
if not build_validator():
|
|
success = False
|
|
|
|
if success:
|
|
organize_files()
|
|
print("构建完成!")
|
|
print("可执行文件位于 dist/final/ 目录")
|
|
else:
|
|
print("构建失败!")
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if main():
|
|
print("构建成功!")
|
|
else:
|
|
print("构建失败!")
|
|
sys.exit(1) |