#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ BootRepairTool 打包脚本 使用 Python 3.9 + PyInstaller 打包 """ import sys import os import subprocess import shutil # 需要的 Python 版本 REQUIRED_PYTHON_VERSION = (3, 9) def check_python_version(): """检查 Python 版本是否为 3.9""" current = sys.version_info[:2] if current != REQUIRED_PYTHON_VERSION: print(f"错误: 需要使用 Python {REQUIRED_PYTHON_VERSION[0]}.{REQUIRED_PYTHON_VERSION[1]}") print(f"当前版本: Python {current[0]}.{current[1]}") print("\n请使用以下命令运行:") print(f" python3.9 {sys.argv[0]}") return False print(f"✓ Python 版本检查通过: {sys.version.split()[0]}") return True def install_dependencies(): """安装打包所需的依赖""" print("\n[1/4] 安装依赖...") deps = ["pyinstaller"] for dep in deps: print(f" 安装 {dep}...") try: subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", dep]) print(f" ✓ {dep} 安装成功") except subprocess.CalledProcessError as e: print(f" ✗ {dep} 安装失败: {e}") return False return True def clean_build(): """清理之前的构建文件""" print("\n[2/4] 清理构建文件...") dirs_to_remove = ["build", "dist"] files_to_remove = ["*.pyc", "*.spec"] for d in dirs_to_remove: if os.path.exists(d): shutil.rmtree(d) print(f" 删除 {d}/") # 清理 __pycache__ for root, dirs, files in os.walk("."): if "__pycache__" in dirs: pycache_path = os.path.join(root, "__pycache__") shutil.rmtree(pycache_path) print(f" 删除 {pycache_path}/") print(" ✓ 清理完成") def build_executable(): """使用 PyInstaller 打包""" print("\n[3/4] 开始打包...") # 打包命令 cmd = [ sys.executable, "-m", "PyInstaller", "--name=LinuxGrubRepair", "--onefile", "--console", "--clean", "--noconfirm", "frontend.py" ] print(f" 执行: {' '.join(cmd)}") try: subprocess.check_call(cmd) print(" ✓ 打包成功") return True except subprocess.CalledProcessError as e: print(f" ✗ 打包失败: {e}") return False def show_result(): """显示打包结果""" print("\n[4/4] 打包完成!") dist_path = os.path.join(os.getcwd(), "dist") exe_name = "LinuxGrubRepair" exe_path = os.path.join(dist_path, exe_name) if os.path.exists(exe_path): size = os.path.getsize(exe_path) size_mb = size / (1024 * 1024) print(f"\n 可执行文件: {exe_path}") print(f" 文件大小: {size_mb:.2f} MB") print("\n 使用方式:") print(f" sudo ./{exe_name}") else: print(" 未找到生成的可执行文件") def main(): """主函数""" print("=" * 50) print("BootRepairTool 打包脚本") print("=" * 50) # 检查 Python 版本 if not check_python_version(): sys.exit(1) # 安装依赖 if not install_dependencies(): print("依赖安装失败") sys.exit(1) # 清理旧构建 clean_build() # 打包 if not build_executable(): print("打包失败") sys.exit(1) # 显示结果 show_result() if __name__ == "__main__": main()