Skip to content

Commit

Permalink
refactor(reset_machine): 优化代码结构并提高可读性
Browse files Browse the repository at this point in the history
- 调整了代码缩进,使逻辑层次更清晰
- 移除了多余的 else 语句,简化了代码结构
- 统一了备份文件存在性和权限检查的逻辑
- 优化了配置恢复过程的代码顺序
- 调整了 0.45.0 版本以上恢复逻辑,提高了代码的可维护性
  • Loading branch information
wqjuser committed Feb 7, 2025
1 parent f98b7b9 commit e7a4eaa
Showing 1 changed file with 35 additions and 37 deletions.
72 changes: 35 additions & 37 deletions reset_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,43 +155,41 @@ def restore_machine_ids(self):
sys.exit(1)

is_045_version = patch_cursor_get_machine_id.version_check(version, min_version="0.45.0")
if not is_045_version:
backup_path = f"{self.db_path}.backup"

# 检查备份文件是否存在
if not os.path.exists(backup_path):
print(f"{Fore.RED}{EMOJI['ERROR']} 备份文件不存在: {backup_path}{Style.RESET_ALL}")
return False

# 检查备份文件权限
if not os.access(backup_path, os.R_OK):
print(f"{Fore.RED}{EMOJI['ERROR']} 无法读取备份文件,请检查文件权限!{Style.RESET_ALL}")
return False

# 读取备份配置
print(f"{Fore.CYAN}{EMOJI['FILE']} 读取备份配置...{Style.RESET_ALL}")
with open(backup_path, "r", encoding="utf-8") as f:
backup_config = json.load(f)

# 检查原始文件权限
if not os.access(self.db_path, os.W_OK):
print(f"{Fore.RED}{EMOJI['ERROR']} 无法写入配置文件,请检查文件权限!{Style.RESET_ALL}")
return False

# 恢复配置
print(f"{Fore.CYAN}{EMOJI['RESET']} 正在恢复配置...{Style.RESET_ALL}")
with open(self.db_path, "w", encoding="utf-8") as f:
json.dump(backup_config, f, indent=4)

print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 机器标识已恢复!{Style.RESET_ALL}")
print(f"\n{Fore.CYAN}已恢复的机器标识:{Style.RESET_ALL}")
for key in ['telemetry.devDeviceId', 'telemetry.macMachineId',
'telemetry.machineId', 'telemetry.sqmId']:
if key in backup_config:
print(f"{EMOJI['INFO']} {key}: {Fore.GREEN}{backup_config[key]}{Style.RESET_ALL}")

return True
else:
backup_path = f"{self.db_path}.backup"

# 检查备份文件是否存在
if not os.path.exists(backup_path):
print(f"{Fore.RED}{EMOJI['ERROR']} 备份文件不存在: {backup_path}{Style.RESET_ALL}")
return False

# 检查备份文件权限
if not os.access(backup_path, os.R_OK):
print(f"{Fore.RED}{EMOJI['ERROR']} 无法读取备份文件,请检查文件权限!{Style.RESET_ALL}")
return False

# 读取备份配置
print(f"{Fore.CYAN}{EMOJI['FILE']} 读取备份配置...{Style.RESET_ALL}")
with open(backup_path, "r", encoding="utf-8") as f:
backup_config = json.load(f)

# 检查原始文件权限
if not os.access(self.db_path, os.W_OK):
print(f"{Fore.RED}{EMOJI['ERROR']} 无法写入配置文件,请检查文件权限!{Style.RESET_ALL}")
return False

# 恢复配置
print(f"{Fore.CYAN}{EMOJI['RESET']} 正在恢复配置...{Style.RESET_ALL}")
with open(self.db_path, "w", encoding="utf-8") as f:
json.dump(backup_config, f, indent=4)

print(f"{Fore.GREEN}{EMOJI['SUCCESS']} 机器标识已恢复!{Style.RESET_ALL}")
print(f"\n{Fore.CYAN}已恢复的机器标识:{Style.RESET_ALL}")
for key in ['telemetry.devDeviceId', 'telemetry.macMachineId',
'telemetry.machineId', 'telemetry.sqmId']:
if key in backup_config:
print(f"{EMOJI['INFO']} {key}: {Fore.GREEN}{backup_config[key]}{Style.RESET_ALL}")

if is_045_version:
patch_cursor_get_machine_id.main(restore_mode=True)

return True
Expand Down

0 comments on commit e7a4eaa

Please sign in to comment.