224 lines
5.4 KiB
Python
224 lines
5.4 KiB
Python
|
#!/usr/bin/env python3
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
"""
|
|||
|
最小功能测试框架打包脚本
|
|||
|
用于将Flask + SocketIO应用打包成exe文件
|
|||
|
"""
|
|||
|
|
|||
|
import os
|
|||
|
import sys
|
|||
|
import shutil
|
|||
|
import subprocess
|
|||
|
from pathlib import Path
|
|||
|
|
|||
|
def check_dependencies():
|
|||
|
"""检查必需的依赖"""
|
|||
|
print("检查依赖模块...")
|
|||
|
required_modules = [
|
|||
|
'flask',
|
|||
|
'flask_socketio',
|
|||
|
'socketio',
|
|||
|
'engineio'
|
|||
|
]
|
|||
|
|
|||
|
missing_modules = []
|
|||
|
for module in required_modules:
|
|||
|
try:
|
|||
|
__import__(module)
|
|||
|
print(f"✓ {module}")
|
|||
|
except ImportError:
|
|||
|
print(f"✗ {module} (缺失)")
|
|||
|
missing_modules.append(module)
|
|||
|
|
|||
|
if missing_modules:
|
|||
|
print(f"\n缺失模块: {', '.join(missing_modules)}")
|
|||
|
print("请运行: pip install -r requirements_minimal.txt")
|
|||
|
return False
|
|||
|
|
|||
|
print("✓ 所有依赖模块检查通过")
|
|||
|
return True
|
|||
|
|
|||
|
def create_spec_file():
|
|||
|
"""创建PyInstaller spec文件"""
|
|||
|
spec_content = '''
|
|||
|
# -*- mode: python ; coding: utf-8 -*-
|
|||
|
|
|||
|
a = Analysis(
|
|||
|
['minimal_test_app.py'],
|
|||
|
pathex=[],
|
|||
|
binaries=[],
|
|||
|
datas=[],
|
|||
|
hiddenimports=[
|
|||
|
'flask',
|
|||
|
'flask_socketio',
|
|||
|
'socketio',
|
|||
|
'engineio',
|
|||
|
'engineio.async_drivers.threading',
|
|||
|
'socketio.namespace',
|
|||
|
'dns',
|
|||
|
'dns.resolver',
|
|||
|
'dns.asyncresolver'
|
|||
|
],
|
|||
|
hookspath=[],
|
|||
|
hooksconfig={},
|
|||
|
runtime_hooks=[],
|
|||
|
excludes=[
|
|||
|
'eventlet',
|
|||
|
'gevent',
|
|||
|
'gevent_uwsgi'
|
|||
|
],
|
|||
|
noarchive=False,
|
|||
|
)
|
|||
|
|
|||
|
pyz = PYZ(a.pure)
|
|||
|
|
|||
|
exe = EXE(
|
|||
|
pyz,
|
|||
|
a.scripts,
|
|||
|
a.binaries,
|
|||
|
a.datas,
|
|||
|
[],
|
|||
|
name='MinimalTestApp',
|
|||
|
debug=False,
|
|||
|
bootloader_ignore_signals=False,
|
|||
|
strip=False,
|
|||
|
upx=True,
|
|||
|
upx_exclude=[],
|
|||
|
runtime_tmpdir=None,
|
|||
|
console=True,
|
|||
|
disable_windowed_traceback=False,
|
|||
|
argv_emulation=False,
|
|||
|
target_arch=None,
|
|||
|
codesign_identity=None,
|
|||
|
entitlements_file=None,
|
|||
|
icon=None
|
|||
|
)
|
|||
|
'''
|
|||
|
|
|||
|
with open('minimal_test.spec', 'w', encoding='utf-8') as f:
|
|||
|
f.write(spec_content)
|
|||
|
|
|||
|
print("✓ 已创建 minimal_test.spec 文件")
|
|||
|
|
|||
|
def clean_build_dirs():
|
|||
|
"""清理构建目录"""
|
|||
|
dirs_to_clean = ['build', 'dist', '__pycache__']
|
|||
|
|
|||
|
for dir_name in dirs_to_clean:
|
|||
|
if os.path.exists(dir_name):
|
|||
|
try:
|
|||
|
shutil.rmtree(dir_name)
|
|||
|
print(f"✓ 清理目录: {dir_name}")
|
|||
|
except Exception as e:
|
|||
|
print(f"警告: 无法清理目录 {dir_name}: {e}")
|
|||
|
|
|||
|
def build_exe():
|
|||
|
"""构建exe文件"""
|
|||
|
print("\n开始构建exe文件...")
|
|||
|
|
|||
|
try:
|
|||
|
# 使用PyInstaller构建
|
|||
|
cmd = [sys.executable, '-m', 'PyInstaller', 'minimal_test.spec', '--clean']
|
|||
|
print(f"执行命令: {' '.join(cmd)}")
|
|||
|
|
|||
|
result = subprocess.run(cmd, capture_output=True, text=True)
|
|||
|
|
|||
|
if result.returncode == 0:
|
|||
|
print("✓ 构建成功!")
|
|||
|
return True
|
|||
|
else:
|
|||
|
print(f"✗ 构建失败")
|
|||
|
print(f"错误输出: {result.stderr}")
|
|||
|
return False
|
|||
|
|
|||
|
except Exception as e:
|
|||
|
print(f"✗ 构建过程出错: {e}")
|
|||
|
return False
|
|||
|
|
|||
|
def create_test_script():
|
|||
|
"""创建测试脚本"""
|
|||
|
test_script = '''
|
|||
|
@echo off
|
|||
|
echo 启动最小功能测试应用...
|
|||
|
echo.
|
|||
|
echo 测试说明:
|
|||
|
echo 1. 应用启动后,在浏览器中访问 http://localhost:5000
|
|||
|
echo 2. 测试HTTP API和WebSocket功能
|
|||
|
echo 3. 按Ctrl+C停止应用
|
|||
|
echo.
|
|||
|
"MinimalTestApp.exe"
|
|||
|
pause
|
|||
|
'''
|
|||
|
|
|||
|
with open('dist/start_test.bat', 'w', encoding='utf-8') as f:
|
|||
|
f.write(test_script)
|
|||
|
|
|||
|
print("✓ 创建测试脚本: dist/start_test.bat")
|
|||
|
|
|||
|
def main():
|
|||
|
"""主函数"""
|
|||
|
print("="*60)
|
|||
|
print("最小功能测试框架 - 打包工具")
|
|||
|
print("="*60)
|
|||
|
print()
|
|||
|
|
|||
|
# 检查当前目录
|
|||
|
if not os.path.exists('minimal_test_app.py'):
|
|||
|
print("✗ 错误: 找不到 minimal_test_app.py 文件")
|
|||
|
print("请确保在正确的目录下运行此脚本")
|
|||
|
input("按回车键退出...")
|
|||
|
return
|
|||
|
|
|||
|
# 检查依赖
|
|||
|
if not check_dependencies():
|
|||
|
input("按回车键退出...")
|
|||
|
return
|
|||
|
|
|||
|
print()
|
|||
|
|
|||
|
# 清理构建目录
|
|||
|
print("清理构建目录...")
|
|||
|
clean_build_dirs()
|
|||
|
print()
|
|||
|
|
|||
|
# 创建spec文件
|
|||
|
print("创建PyInstaller配置...")
|
|||
|
create_spec_file()
|
|||
|
print()
|
|||
|
|
|||
|
# 构建exe
|
|||
|
if build_exe():
|
|||
|
print()
|
|||
|
print("后处理...")
|
|||
|
|
|||
|
# 检查生成的exe文件
|
|||
|
exe_path = 'dist/MinimalTestApp.exe'
|
|||
|
if os.path.exists(exe_path):
|
|||
|
print(f"✓ exe文件位置: {exe_path}")
|
|||
|
|
|||
|
# 创建测试脚本
|
|||
|
create_test_script()
|
|||
|
|
|||
|
print()
|
|||
|
print("🎉 打包完成!")
|
|||
|
print()
|
|||
|
print("测试方式:")
|
|||
|
print("1. 直接运行: dist/MinimalTestApp.exe")
|
|||
|
print("2. 使用脚本: dist/start_test.bat")
|
|||
|
print()
|
|||
|
print("测试步骤:")
|
|||
|
print("1. 启动应用")
|
|||
|
print("2. 浏览器访问 http://localhost:5000")
|
|||
|
print("3. 测试HTTP API和WebSocket功能")
|
|||
|
print("4. 确认所有功能正常工作")
|
|||
|
|
|||
|
else:
|
|||
|
print("✗ 错误: 未找到生成的exe文件")
|
|||
|
else:
|
|||
|
print("\n✗ 打包失败")
|
|||
|
|
|||
|
print()
|
|||
|
input("按回车键退出...")
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
main()
|