#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 服务器端快速修复脚本 修复PyInstaller打包时的database目录缺失问题 """ import os from pathlib import Path def fix_build_spec(): """修复build.spec文件,处理可选数据文件""" spec_file = Path('build.spec') if not spec_file.exists(): print("错误: build.spec 文件不存在") return False # 读取原文件内容 with open(spec_file, 'r', encoding='utf-8') as f: content = f.read() # 查找并替换datas部分 old_datas = '''# 需要包含的数据文件 datas = [ (str(ROOT_DIR / 'app'), 'app'), # 包含整个app目录 ('.env.example', '.'), # 包含环境配置示例文件 ('database', 'database'), # 包含数据库相关文件 ]''' new_datas = '''# 需要包含的数据文件 datas = [ (str(ROOT_DIR / 'app'), 'app'), # 包含整个app目录 ('.env.example', '.'), # 包含环境配置示例文件 ] # 可选数据文件(如果存在才包含) optional_files = [ ('database', 'database'), # 包含数据库相关文件 ] # 添加可选数据文件 for src, dst in optional_files: src_path = ROOT_DIR / src if src_path.exists(): datas.append((src, dst)) print(f"包含可选数据文件: {src}") else: print(f"跳过可选数据文件: {src} (不存在)")''' if old_datas in content: content = content.replace(old_datas, new_datas) # 写回文件 with open(spec_file, 'w', encoding='utf-8') as f: f.write(content) print("✓ 已修复 build.spec 文件") return True else: print("build.spec 文件已经包含修复或格式不匹配") return False def create_database_dir(): """创建database目录(如果不存在)""" db_dir = Path('database') if not db_dir.exists(): db_dir.mkdir(exist_ok=True) # 创建一个空的初始化文件 init_file = db_dir / 'init' / '.gitkeep' init_file.parent.mkdir(exist_ok=True) init_file.touch() # 创建一个说明文件 readme_file = db_dir / 'README.md' with open(readme_file, 'w', encoding='utf-8') as f: f.write("""# Database目录 此目录包含数据库相关的初始化脚本和配置文件。 ## 文件说明 - `init/`: 数据库初始化脚本目录 - `create_files_table.py`: 创建文件表的脚本 ## 注意 如果此目录为空,不会影响应用的正常运行。应用会自动创建所需的数据库表。 """) print("✓ 已创建 database 目录") return True else: print("✓ database 目录已存在") return True def main(): """主函数""" print("=== 服务器端快速修复脚本 ===") # 修复build.spec文件 print("1. 修复 build.spec 文件...") fix_build_spec() # 创建database目录 print("\n2. 检查 database 目录...") create_database_dir() print("\n=== 修复完成 ===") print("现在可以重新运行打包:") print("python build_linux.py") # 验证修复结果 print("\n=== 验证修复结果 ===") if Path('build.spec').exists(): print("✓ build.spec 文件存在") else: print("✗ build.spec 文件不存在") if Path('database').exists(): print("✓ database 目录存在") files = list(Path('database').rglob('*')) print(f" 包含 {len(files)} 个文件/目录") else: print("✗ database 目录不存在") if __name__ == '__main__': main()