#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 简化构建脚本 在当前平台构建可执行文件 """ import os import sys import subprocess import platform import shutil from pathlib import Path def get_platform_info(): """获取平台信息""" system = platform.system() machine = platform.machine().lower() if system == 'Windows': if machine in ['amd64', 'x86_64']: return 'windows_x64' elif machine in ['i386', 'i686', 'x86']: return 'windows_x86' else: return f'windows_{machine}' elif system == 'Darwin': if machine in ['arm64', 'aarch64']: return 'macos_arm64' else: return 'macos_x64' elif system == 'Linux': return f'linux_{machine}' else: return f'{system.lower()}_{machine}' def install_dependencies(): """安装依赖""" print("安装依赖包...") packages = ['pandas', 'openpyxl', 'pyinstaller'] for package in packages: try: __import__(package) print(f"✅ {package} 已安装") except ImportError: print(f"📦 安装 {package}...") try: subprocess.check_call([sys.executable, '-m', 'pip', 'install', package]) print(f"✅ {package} 安装成功") except subprocess.CalledProcessError: print(f"❌ {package} 安装失败") return False return True def build_executable(): """构建可执行文件""" print("\n开始构建可执行文件...") # 获取平台信息 platform_name = get_platform_info() exe_name = f"座位分配系统_{platform_name}" print(f"目标平台: {platform_name}") print(f"输出文件: {exe_name}") # 清理之前的构建 if os.path.exists('dist'): shutil.rmtree('dist') if os.path.exists('build'): shutil.rmtree('build') # 构建命令 cmd = [ sys.executable, '-m', 'PyInstaller', '--onefile', '--console', '--clean', '--name', exe_name, 'seat_allocation_system.py' ] try: print(f"执行命令: {' '.join(cmd)}") result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode == 0: print("✅ 构建成功!") # 检查生成的文件 if platform.system() == 'Windows': exe_path = Path('dist') / f'{exe_name}.exe' else: exe_path = Path('dist') / exe_name if exe_path.exists(): file_size = exe_path.stat().st_size / (1024 * 1024) # MB print(f"生成文件: {exe_path}") print(f"文件大小: {file_size:.1f} MB") return True, exe_path else: print("❌ 未找到生成的文件") return False, None else: print("❌ 构建失败!") print("错误输出:") print(result.stderr) return False, None except Exception as e: print(f"❌ 构建过程中出现错误: {e}") return False, None def create_distribution(): """创建分发包""" print("\n创建分发包...") platform_name = get_platform_info() package_name = f"座位分配系统_{platform_name}_分发包" package_dir = Path(package_name) # 创建分发目录 if package_dir.exists(): shutil.rmtree(package_dir) package_dir.mkdir() # 复制可执行文件 dist_dir = Path('dist') if dist_dir.exists(): for file in dist_dir.iterdir(): if file.is_file(): shutil.copy2(file, package_dir) print(f"复制文件: {file.name}") # 复制示例文件 if Path('人员信息.xlsx').exists(): shutil.copy2('人员信息.xlsx', package_dir / '人员信息_示例.xlsx') print("复制示例文件: 人员信息_示例.xlsx") if Path('座位信息.xlsx').exists(): shutil.copy2('座位信息.xlsx', package_dir / '座位信息_示例.xlsx') print("复制示例文件: 座位信息_示例.xlsx") # 创建使用说明 readme_content = f"""座位分配系统 使用说明 平台: {platform_name} 构建时间: {platform.platform()} 使用方法: 1. 准备Excel文件 - 人员信息.xlsx: 包含姓名、证件信息、备注等 - 座位信息.xlsx: 包含区域、楼层、排号、座位号等 2. 运行程序 - 将可执行文件放在Excel文件同一目录 - 双击运行可执行文件 - 等待处理完成 3. 查看结果 - 座位信息_最终分配.xlsx: 分配结果 - 最终座位分配日志.xlsx: 详细记录 - seat_allocation_log.txt: 运行日志 功能特点: - 支持1-10人连坐需求 - 自动处理不连续座位 - 完整的数据校验 - 详细的分配日志 注意事项: - 确保Excel文件格式正确 - 备注数字表示连坐人数 - 运行时会在同目录生成结果文件 """ with open(package_dir / '使用说明.txt', 'w', encoding='utf-8') as f: f.write(readme_content) print(f"✅ 分发包已创建: {package_dir}") return package_dir def main(): """主函数""" print("座位分配系统 - 简化构建工具") print("=" * 50) # 显示系统信息 print(f"系统: {platform.system()} {platform.release()}") print(f"架构: {platform.machine()}") print(f"Python: {sys.version}") # 检查主程序文件 if not os.path.exists('seat_allocation_system.py'): print("❌ 未找到 seat_allocation_system.py 文件") return # 安装依赖 if not install_dependencies(): print("❌ 依赖安装失败") return # 构建可执行文件 success, exe_path = build_executable() if not success: print("❌ 构建失败") return # 创建分发包 package_dir = create_distribution() print("\n🎉 构建完成!") print(f"✅ 可执行文件: {exe_path}") print(f"✅ 分发包: {package_dir}") # 平台特定说明 if platform.system() == 'Windows': print("\n📝 Windows使用说明:") print("- 可以直接在Windows系统上运行") print("- 确保安装了Visual C++ Redistributable") elif platform.system() == 'Darwin': print("\n📝 macOS使用说明:") print("- 只能在macOS系统上运行") print("- 如需Windows版本,请在Windows系统上构建") print("- 或使用GitHub Actions自动构建") else: print(f"\n📝 {platform.system()}使用说明:") print("- 只能在相同系统上运行") print("- 如需其他平台版本,请在对应系统上构建") if __name__ == "__main__": main()