#!/usr/bin/env python3 import sys import time import shutil import zipfile from pathlib import Path def main(): if len(sys.argv) != 4: print("Usage: update_helper.py ") sys.exit(1) zip_path = Path(sys.argv[1]) app_dir = Path(sys.argv[2]) temp_dir = Path(sys.argv[3]) try: # 解压更新包到临时目录 with zipfile.ZipFile(zip_path, 'r') as zip_ref: zip_ref.extractall(temp_dir) # 备份当前应用 backup_dir = app_dir.parent / f"{app_dir.name}_backup" if backup_dir.exists(): shutil.rmtree(backup_dir) shutil.copytree(app_dir, backup_dir) # 复制更新文件 (跳过conf目录) for item in temp_dir.iterdir(): if item.name == "conf": continue dest = app_dir / item.name if item.is_dir(): if dest.exists(): shutil.rmtree(dest) shutil.copytree(item, dest) else: if dest.exists(): dest.unlink() shutil.copy2(item, dest) # 设置执行权限 for bin_file in app_dir.rglob("v2ray*"): bin_file.chmod(0o755) # 清理 shutil.rmtree(temp_dir, ignore_errors=True) # 重启应用 main_executable = app_dir / "main.py" os.execv(sys.executable, [sys.executable, str(main_executable)]) except Exception as e: print(f"Update failed: {str(e)}") # 恢复备份 if backup_dir.exists(): shutil.rmtree(app_dir) shutil.copytree(backup_dir, app_dir) sys.exit(1) if __name__ == "__main__": main()