2025-09-17 17:48:35 +08:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
|
|
|
|
|
生产环境打包脚本 - 自动禁用日志功能
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
import shutil
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
def disable_logging():
|
|
|
|
|
|
"""自动禁用所有日志功能"""
|
|
|
|
|
|
print("🔧 正在禁用日志功能...")
|
|
|
|
|
|
|
|
|
|
|
|
# 备份原文件
|
|
|
|
|
|
files_to_backup = ['main.py', 'exe_file_logger.py']
|
|
|
|
|
|
for file_name in files_to_backup:
|
|
|
|
|
|
if os.path.exists(file_name):
|
|
|
|
|
|
shutil.copy(file_name, f'{file_name}.backup')
|
|
|
|
|
|
print(f"✅ 已备份 {file_name}")
|
|
|
|
|
|
|
|
|
|
|
|
# 1. 修改 main.py - 注释掉日志初始化
|
|
|
|
|
|
if os.path.exists('main.py'):
|
|
|
|
|
|
with open('main.py', 'r', encoding='utf-8') as f:
|
|
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
# 注释掉日志相关的导入和初始化
|
|
|
|
|
|
content = content.replace(
|
|
|
|
|
|
'from exe_file_logger import setup_file_logging, log_to_file',
|
|
|
|
|
|
'# from exe_file_logger import setup_file_logging, log_to_file # 生产环境禁用'
|
|
|
|
|
|
).replace(
|
|
|
|
|
|
'setup_file_logging()',
|
|
|
|
|
|
'# setup_file_logging() # 生产环境禁用'
|
|
|
|
|
|
).replace(
|
|
|
|
|
|
'print("文件日志系统已在main.py中初始化")',
|
|
|
|
|
|
'# print("文件日志系统已在main.py中初始化") # 生产环境禁用'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
with open('main.py', 'w', encoding='utf-8') as f:
|
|
|
|
|
|
f.write(content)
|
|
|
|
|
|
print("✅ 已禁用 main.py 中的日志初始化")
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 修改 exe_file_logger.py - 让所有函数变成空操作
|
|
|
|
|
|
if os.path.exists('exe_file_logger.py'):
|
|
|
|
|
|
with open('exe_file_logger.py', 'r', encoding='utf-8') as f:
|
|
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
# 将 setup_file_logging 函数替换为空函数
|
|
|
|
|
|
content = content.replace(
|
|
|
|
|
|
'def setup_file_logging():',
|
|
|
|
|
|
'def setup_file_logging():\n """生产环境 - 禁用文件日志"""\n return None\n\ndef setup_file_logging_original():'
|
|
|
|
|
|
).replace(
|
|
|
|
|
|
'def log_to_file(message):',
|
|
|
|
|
|
'def log_to_file(message):\n """生产环境 - 禁用文件日志"""\n pass\n\ndef log_to_file_original(message):'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
with open('exe_file_logger.py', 'w', encoding='utf-8') as f:
|
|
|
|
|
|
f.write(content)
|
|
|
|
|
|
print("✅ 已禁用 exe_file_logger.py 中的日志功能")
|
|
|
|
|
|
|
|
|
|
|
|
print("✅ 所有日志功能已禁用")
|
|
|
|
|
|
|
|
|
|
|
|
def restore_logging():
|
|
|
|
|
|
"""恢复日志功能"""
|
|
|
|
|
|
files_to_restore = ['main.py', 'exe_file_logger.py']
|
|
|
|
|
|
for file_name in files_to_restore:
|
|
|
|
|
|
backup_file = f'{file_name}.backup'
|
|
|
|
|
|
if os.path.exists(backup_file):
|
|
|
|
|
|
shutil.copy(backup_file, file_name)
|
|
|
|
|
|
os.remove(backup_file)
|
|
|
|
|
|
print(f"✅ 已恢复 {file_name}")
|
|
|
|
|
|
print("✅ 所有文件已恢复到开发环境配置")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
"""主函数"""
|
|
|
|
|
|
print("🔥 生产环境打包工具(无日志版本)")
|
|
|
|
|
|
print("=" * 60)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 1. 禁用日志功能
|
|
|
|
|
|
disable_logging()
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 执行打包
|
|
|
|
|
|
print("\n🚀 开始打包...")
|
|
|
|
|
|
result = subprocess.run(['python', 'quick_build.py'], capture_output=False)
|
|
|
|
|
|
|
|
|
|
|
|
if result.returncode == 0:
|
2025-10-10 16:16:36 +08:00
|
|
|
|
# 验证最终输出(quick_build.py 已统一输出到 MultiPlatformGUI)
|
2025-09-28 17:00:02 +08:00
|
|
|
|
if os.path.exists('dist/MultiPlatformGUI'):
|
|
|
|
|
|
print("\n🎉 生产环境打包成功!")
|
|
|
|
|
|
print("📁 输出目录: dist/MultiPlatformGUI/")
|
|
|
|
|
|
print("🔇 已禁用所有日志功能,客户端不会产生日志文件")
|
|
|
|
|
|
|
|
|
|
|
|
# 验证关键文件
|
|
|
|
|
|
exe_path = 'dist/MultiPlatformGUI/main.exe'
|
|
|
|
|
|
if os.path.exists(exe_path):
|
|
|
|
|
|
size = os.path.getsize(exe_path) / 1024 / 1024 # MB
|
|
|
|
|
|
print(f"✅ 主程序: main.exe ({size:.1f} MB)")
|
|
|
|
|
|
else:
|
|
|
|
|
|
print("⚠️ 主程序文件未找到")
|
|
|
|
|
|
else:
|
2025-10-10 16:16:36 +08:00
|
|
|
|
print("\n❌ 输出目录验证失败: 未找到 dist/MultiPlatformGUI")
|
2025-09-17 17:48:35 +08:00
|
|
|
|
else:
|
|
|
|
|
|
print("\n❌ 打包失败")
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"❌ 打包过程出错: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# 3. 恢复日志功能(用于开发)
|
|
|
|
|
|
print("\n🔄 恢复开发环境配置...")
|
|
|
|
|
|
restore_logging()
|
|
|
|
|
|
|
|
|
|
|
|
print("\n" + "=" * 60)
|
|
|
|
|
|
input("按Enter键退出...")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
main()
|