#!/usr/bin/env python # -*- coding: utf-8 -*- """ 测试构建脚本 用于验证打包环境和基本功能 """ import os import sys import subprocess from pathlib import Path def test_python_environment(): """测试Python环境""" print("=== 测试Python环境 ===") print(f"Python版本: {sys.version}") print(f"Python路径: {sys.executable}") # 测试关键模块 modules = ['tkinter', 'subprocess', 'pathlib', 'configparser'] for module in modules: try: __import__(module) print(f"✓ {module} 可用") except ImportError: print(f"✗ {module} 不可用") return True def test_project_structure(): """测试项目结构""" print("\n=== 测试项目结构 ===") project_root = Path(__file__).parent.parent.parent required_files = [ 'app.py', 'requirements.txt', 'config/settings.py', 'config/tf_api_config.py', 'packaging/main_with_config.py', 'packaging/config_tool/config_manager.py' ] all_found = True for file_path in required_files: full_path = project_root / file_path if full_path.exists(): print(f"✓ {file_path}") else: print(f"✗ {file_path} 未找到") all_found = False return all_found def test_config_tool(): """测试配置工具(不启动GUI)""" print("\n=== 测试配置工具 ===") try: project_root = Path(__file__).parent.parent.parent config_tool_path = project_root / "packaging" / "config_tool" / "startup_loader.py" # 测试配置加载器 result = subprocess.run([ sys.executable, str(config_tool_path) ], capture_output=True, text=True, cwd=str(project_root)) if result.returncode == 0: print("✓ 配置加载器测试通过") print(f"输出: {result.stdout.strip()}") return True else: print("✗ 配置加载器测试失败") print(f"错误: {result.stderr}") return False except Exception as e: print(f"✗ 配置工具测试异常: {e}") return False def test_pyinstaller(): """测试PyInstaller是否可用""" print("\n=== 测试PyInstaller ===") try: result = subprocess.run([ sys.executable, "-m", "pip", "show", "pyinstaller" ], capture_output=True, text=True) if result.returncode == 0: print("✓ PyInstaller 已安装") # 尝试运行 pyinstaller --version version_result = subprocess.run([ "pyinstaller", "--version" ], capture_output=True, text=True) if version_result.returncode == 0: print(f"✓ PyInstaller版本: {version_result.stdout.strip()}") return True else: print("✗ PyInstaller 无法运行") return False else: print("✗ PyInstaller 未安装") print("尝试安装...") install_result = subprocess.run([ sys.executable, "-m", "pip", "install", "pyinstaller" ], capture_output=True, text=True) if install_result.returncode == 0: print("✓ PyInstaller 安装成功") return True else: print("✗ PyInstaller 安装失败") print(f"错误: {install_result.stderr}") return False except Exception as e: print(f"✗ PyInstaller测试异常: {e}") return False def test_main_imports(): """测试主程序导入""" print("\n=== 测试主程序导入 ===") try: project_root = Path(__file__).parent.parent.parent # 临时添加项目路径 sys.path.insert(0, str(project_root)) # 测试主要模块导入 test_modules = [ 'config.settings', 'config.tf_api_config', 'utils.logger' ] for module in test_modules: try: __import__(module) print(f"✓ {module}") except ImportError as e: print(f"✗ {module}: {e}") return False print("✓ 主程序模块导入测试通过") return True except Exception as e: print(f"✗ 主程序导入测试异常: {e}") return False finally: # 清理路径 if str(project_root) in sys.path: sys.path.remove(str(project_root)) def main(): """主函数""" print("VWED任务系统打包环境测试") print("=" * 50) tests = [ ("Python环境", test_python_environment), ("项目结构", test_project_structure), ("配置工具", test_config_tool), ("PyInstaller", test_pyinstaller), ("主程序导入", test_main_imports) ] results = [] for test_name, test_func in tests: try: result = test_func() results.append((test_name, result)) except Exception as e: print(f"测试 {test_name} 发生异常: {e}") results.append((test_name, False)) # 总结 print("\n" + "=" * 50) print("测试结果总结:") all_passed = True for test_name, result in results: status = "✓ 通过" if result else "✗ 失败" print(f"{test_name}: {status}") if not result: all_passed = False if all_passed: print("\n 所有测试通过!可以进行打包。") return 0 else: print("\n 部分测试失败,请检查并解决问题后重试。") return 1 if __name__ == "__main__": exit(main())