Files
douyi_guaji_main/tools/cleanup/sanitize_cursor_paths.py
T
CodexandClaude Opus 4.5 a19fa35a0f chore: 整理 tools/ 47 个脚本按功能分组
- diagnostics/ 11 个:环境/配置/依赖诊断
- logs/ 7 个:日志分析与查看
- coords/ 6 个:坐标拾取与区域选择
- cleanup/ 3 个:代码清理工具
- adb/ 2 个:ADB / 输入法相关
- windows/ 4 个:Windows 系统修复(备份)
- dev/ 9 个:开发辅助 / 运行时探测

并更新 tools/README.md 描述新结构。
删除 3 个明显的临时调试脚本 (_dump_lines.py / tmp_*.py)。

tools/ 不被业务代码 import,分组仅为人工浏览方便。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-20 23:25:28 +08:00

83 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
Sanitize logs and text files by replacing absolute .cursor worktree paths
with a safer relative marker. Example absolute paths are shown using
forward-slashes to avoid source-level escape issues, e.g.:
C:/Users/<user>/.cursor/worktrees/...
Creates a backup copy with suffix .bak for each file modified.
Usage:
python tools/sanitize_cursor_paths.py path/to/logs [--dry-run]
This is safe to run on a logs directory. It will only update files that contain
matches and will save a backup as <file>.bak before writing changes.
"""
from __future__ import annotations
import argparse
import re
from pathlib import Path
# Regex to match Windows absolute .cursor worktree paths
# Use raw string to avoid Unicode escape issues
_CURSOR_RE = re.compile(r"C:\\Users\\[^\\\s]+\\.cursor\\worktrees\\")
def sanitize_file(p: Path, dry_run: bool = False) -> int:
"""Sanitize a single file by replacing .cursor worktree paths.
Args:
p: Path to the file to sanitize
dry_run: If True, only check for matches without modifying
Returns:
1 if file was modified or would be modified, 0 otherwise
"""
text = p.read_text(encoding="utf-8", errors="replace")
if not _CURSOR_RE.search(text):
return 0
new_text = _CURSOR_RE.sub(".cursor/worktrees/", text)
if dry_run:
return 1
bak = p.with_suffix(p.suffix + ".bak")
bak.write_text(text, encoding="utf-8")
p.write_text(new_text, encoding="utf-8")
return 1
def main() -> None:
"""Main entry point for the sanitize script."""
ap = argparse.ArgumentParser(description="Sanitize .cursor absolute paths in files")
ap.add_argument("path", type=Path, help="File or directory to scan")
ap.add_argument(
"--dry-run", action="store_true", help="Only report matches, do not modify"
)
args = ap.parse_args()
target = args.path
files = []
if target.is_dir():
files = list(target.rglob("*.log")) + list(target.rglob("*.txt"))
elif target.is_file():
files = [target]
else:
print(f"No such file or directory: {target}")
raise SystemExit(2)
modified = 0
for f in files:
try:
res = sanitize_file(f, dry_run=args.dry_run)
if res:
modified += 1
print(("DRY:" if args.dry_run else "MODIFIED:"), f)
except Exception as e:
print("ERROR processing", f, e)
print("Done. Files matched/modified:", modified)
if __name__ == "__main__":
main()