Files
myrzx_bot/ranch_assistant/account_storage.py
T

44 lines
2.0 KiB
Python

"""Delete one local account workspace without touching sibling accounts."""
from pathlib import Path
import re
import shutil
DEFAULT_ACCOUNT_ENTRIES = ("credentials.json", "credentials.tmp", "sessions", "roles",
"settings.json", "settings.tmp", "purchase_budget.json", "purchase_budget.tmp",
"daily_claims.json", "daily_claims.tmp", "sweep_runs.json", "sweep_runs.tmp",
"maps.json", "maps.tmp", "network_debug.jsonl", "network_debug.previous.jsonl",
"sweep_rewards.sqlite3", "sweep_rewards.sqlite3-journal", "sweep_rewards.sqlite3-wal", "sweep_rewards.sqlite3-shm",
"cultivation_choices.json", "cultivation_choices.tmp")
def profile_directory(root, identifier):
root = Path(root).resolve()
if not isinstance(identifier, str) or not re.fullmatch(r"default|[a-f0-9]{32}", identifier):
raise ValueError("无效的账号工作区编号")
folder = root if identifier == "default" else root / "profiles" / identifier
if folder.resolve() != folder:
raise ValueError("账号工作区包含链接路径,已停止删除")
return folder
def remove_account_files(root, identifier):
"""Caller holds workspace.lock; the legacy default shares the registry root."""
folder = profile_directory(root, identifier)
if not folder.exists():
return
if identifier == "default":
targets = [folder / name for name in DEFAULT_ACCOUNT_ENTRIES if (folder / name).exists() or (folder / name).is_symlink()]
else:
targets = [path for path in folder.iterdir() if path.name != "workspace.lock"]
# Validate the whole deletion set before removing anything. In particular,
# never recursively delete the default root, which contains other accounts.
for path in targets:
if path.resolve().parent != folder or path.is_symlink() or getattr(path, "is_junction", lambda: False)():
raise ValueError("账号记录包含链接路径,已停止删除")
for path in targets:
if path.is_dir():
shutil.rmtree(path)
else:
path.unlink()