Skip to content

Reverse-engineer any video, shot by shot: filmed, AI-generated or coded, and how to remake it. Breakdowns in the open VIDEO.md format. 逆向一切视频,逐个镜头看是拍的、AI 生成的还是代码做的,再告诉你怎么复刻;拆解结果按 VIDEO.md 格式输出。

clean-paths: detect rooted paths instead of matching $HOME text

xdAsaah53f78ff5a7db
diff --git a/rea_video/clean_paths.py b/rea_video/clean_paths.py
index 8c2deaf..88126e4 100644
--- a/rea_video/clean_paths.py
+++ b/rea_video/clean_paths.py
@@ -7,6 +7,7 @@ from __future__ import annotations
import json
import os
+import re
import shlex
from pathlib import Path
from typing import Any
@@ -14,6 +15,49 @@ from typing import Any
from .util import BREAKDOWN, _portable_breakdown, _sources_file, _source_key, dumps_rows
TEXT = {".json", ".md", ".html", ".txt", ".vtt", ".yaml", ".yml", ".csv", ".ass", ".srt", ".jsonl"}
+JSON_SUFFIXES = {".json", ".jsonl"}
+
+_URL = re.compile(r"[A-Za-z][A-Za-z0-9+.\-]*://[^\s\"'<>]*")
+# A path is local when it is rooted: a drive letter, a UNC share, or a POSIX root. A URL is not.
+_ROOTED = (
+ re.compile(r"(?<![\w\\])(?:[A-Za-z]:[\\/])[^\s\"'<>|]*"),
+ re.compile(r"\\\\[^\s\"'<>|\\]+\\[^\s\"'<>|]*"),
+ re.compile(r"(?<![\w:/])/(?:Users|home|Volumes|private|tmp|opt|srv|root|var)/[^\s\"'<>|]*"),
+)
+
+
+def local_paths(text: str) -> list[str]:
+ """Every rooted local path in this text. URLs are stripped first, so `https://host/a/b` is not one."""
+ text = _URL.sub(" ", text)
+ return [m.group(0) for rx in _ROOTED for m in rx.finditer(text)]
+
+
+def has_local_path(value: Any) -> bool:
+ """True when any string in the value (decoded, not JSON-escaped) is or contains a rooted path.
+
+ Decoding first is the point: on Windows the text holds `C:\\\\Users\\\\name`, so comparing against
+ `str(Path.home())` finds nothing even though the path is right there."""
+ if isinstance(value, str):
+ return bool(local_paths(value))
+ if isinstance(value, dict):
+ return any(has_local_path(k) or has_local_path(v) for k, v in value.items())
+ if isinstance(value, (list, tuple)):
+ return any(has_local_path(v) for v in value)
+ return False
+
+
+def file_has_local_path(f: Path) -> bool:
+ """A package file still carrying a rooted path, judged on its decoded content where it has structure."""
+ try:
+ text = f.read_text(encoding="utf-8")
+ except (OSError, UnicodeDecodeError):
+ return False
+ if f.suffix.lower() in JSON_SUFFIXES:
+ try:
+ return has_local_path(json.loads(text))
+ except ValueError:
+ pass
+ return bool(local_paths(text))
def _command(cmd: str) -> str:
@@ -76,7 +120,7 @@ def _remembered(src: dict) -> bool:
return False
-def clean_package(root: Path, home: str, apply: bool = False) -> dict:
+def clean_package(root: Path, apply: bool = False) -> dict:
root = Path(root)
p = root / BREAKDOWN
old = p.read_text(encoding="utf-8")
@@ -104,35 +148,32 @@ def clean_package(root: Path, home: str, apply: bool = False) -> dict:
if not f.is_file():
continue
text = f.read_text(encoding="utf-8")
- if home not in text:
+ data = json.loads(text)
+ if not has_local_path(data):
continue
res["cleaned"].append(rel)
if apply:
- jobs.append((f, text, fix(json.loads(text)), rows))
+ jobs.append((f, text, fix(data), rows))
for f, text, data, rows in jobs:
_replace(f, _dump(data, text, rows))
for f in sorted(root.rglob("*")):
if f.is_file() and f.suffix.lower() in TEXT:
- rel = str(f.relative_to(root))
- try:
- has = home in f.read_text(encoding="utf-8")
- except (OSError, UnicodeDecodeError):
- continue
- if has and (apply or rel not in res["cleaned"]):
+ rel = f.relative_to(root).as_posix()
+ if file_has_local_path(f) and (apply or rel not in res["cleaned"]):
res["kept"].append(rel)
return res
def clean_paths(start: Path, apply: bool = False, home: str | None = None) -> list[dict]:
- home = home or str(Path.home())
- return [_guarded(f.parent, home, apply) for f in sorted(Path(start).expanduser().resolve().rglob(BREAKDOWN))
+ """`home` is kept for callers that still pass it; rooted paths are recognised without it now."""
+ return [_guarded(f.parent, apply) for f in sorted(Path(start).expanduser().resolve().rglob(BREAKDOWN))
if (f.parent / "VIDEO.md").is_file()]
-def _guarded(root: Path, home: str, apply: bool) -> dict:
+def _guarded(root: Path, apply: bool) -> dict:
"""One unreadable or half-written package is reported like the other per-file failures, so the rest of the
directory still gets cleaned instead of the whole run dying on it."""
try:
- return clean_package(root, home, apply)
+ return clean_package(root, apply)
except (OSError, ValueError, TypeError) as exc:
return {"package": str(root), "cleaned": [], "kept": [], "error": f"could not clean this package: {exc}"}