scripts: paths from the environment, not one disk
Browse files
scripts/test_doc_references.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Every path a published doc tells you to run must itself be published.
|
| 2 |
+
|
| 3 |
+
The READMEs' "Reproducing" sections named nine `scripts/*.py`. None was on the
|
| 4 |
+
Hub, and when checked they carried absolute paths to one laptop. A reproduction
|
| 5 |
+
section nobody but the author can run is worse than absent: it reads as a
|
| 6 |
+
promise.
|
| 7 |
+
|
| 8 |
+
This walks every published .md, extracts the repository paths it mentions, and
|
| 9 |
+
checks each exists on the Hub. It also greps the published scripts for absolute
|
| 10 |
+
paths under a user's home or a specific mount, which is the other half of the
|
| 11 |
+
same failure — published but unrunnable.
|
| 12 |
+
|
| 13 |
+
python scripts/test_doc_references.py
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import re
|
| 18 |
+
import sys
|
| 19 |
+
import tempfile
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
|
| 22 |
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
| 23 |
+
|
| 24 |
+
RESULTS: list[tuple[bool, str, str]] = []
|
| 25 |
+
REPO = "yxma/React"
|
| 26 |
+
PATH_RE = re.compile(
|
| 27 |
+
r"(?<![\w/])((?:scripts|toolbox|examples|data|test_sets|preprocess|docs)"
|
| 28 |
+
r"/[\w./+-]+\.(?:py|json|md|jpg|jsonl))")
|
| 29 |
+
ABS_RE = re.compile(r"[\"'](/(?:home|media|Users)/[^\"']+)[\"']")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def check(ok: bool, name: str, evidence: str) -> None:
|
| 33 |
+
RESULTS.append((bool(ok), name, evidence))
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def main() -> int:
|
| 37 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 38 |
+
api = HfApi()
|
| 39 |
+
files = set(api.list_repo_files(REPO, repo_type="dataset"))
|
| 40 |
+
d = tempfile.mkdtemp()
|
| 41 |
+
|
| 42 |
+
docs = sorted(f for f in files if f.endswith(".md"))
|
| 43 |
+
missing, seen = [], 0
|
| 44 |
+
for doc in docs:
|
| 45 |
+
text = Path(hf_hub_download(REPO, doc, repo_type="dataset",
|
| 46 |
+
local_dir=d)).read_text(errors="ignore")
|
| 47 |
+
for ref in sorted(set(PATH_RE.findall(text))):
|
| 48 |
+
seen += 1
|
| 49 |
+
if ref not in files:
|
| 50 |
+
missing.append(f"{doc} -> {ref}")
|
| 51 |
+
check(not missing, "every path a published doc names is published",
|
| 52 |
+
f"{len(docs)} docs, {seen} path references, all present"
|
| 53 |
+
+ (f"; MISSING {missing[:4]}" if missing else ""))
|
| 54 |
+
|
| 55 |
+
# ...and the published scripts must not point at one person's disk
|
| 56 |
+
scripts = sorted(f for f in files if f.startswith("scripts/") and f.endswith(".py"))
|
| 57 |
+
hard = []
|
| 58 |
+
for s in scripts:
|
| 59 |
+
text = Path(hf_hub_download(REPO, s, repo_type="dataset",
|
| 60 |
+
local_dir=d)).read_text(errors="ignore")
|
| 61 |
+
for m in ABS_RE.findall(text):
|
| 62 |
+
hard.append(f"{s}: {m}")
|
| 63 |
+
check(not hard, "no published script hard-codes an absolute local path",
|
| 64 |
+
f"{len(scripts)} scripts checked, none point at a specific mount"
|
| 65 |
+
+ (f"; {hard[:3]}" if hard else ""))
|
| 66 |
+
|
| 67 |
+
# ...and the path module they share is published, since they import it
|
| 68 |
+
imports_paths = []
|
| 69 |
+
for s in scripts:
|
| 70 |
+
text = Path(hf_hub_download(REPO, s, repo_type="dataset",
|
| 71 |
+
local_dir=d)).read_text(errors="ignore")
|
| 72 |
+
if "from react_paths import" in text:
|
| 73 |
+
imports_paths.append(s)
|
| 74 |
+
check("scripts/react_paths.py" in files and imports_paths,
|
| 75 |
+
"the shared path module ships with the scripts that import it",
|
| 76 |
+
f"{len(imports_paths)} scripts import react_paths; "
|
| 77 |
+
f"published: {'scripts/react_paths.py' in files}")
|
| 78 |
+
|
| 79 |
+
w = max(len(x) for _, x, _ in RESULTS)
|
| 80 |
+
print()
|
| 81 |
+
for ok, name, ev in RESULTS:
|
| 82 |
+
print(f" [{'ok' if ok else 'FAIL'}] {name:<{w}} {ev}")
|
| 83 |
+
n = sum(not x for x, _, _ in RESULTS)
|
| 84 |
+
print(f"\ndoc references: {len(RESULTS)} checks, {n} failing")
|
| 85 |
+
return 1 if n else 0
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
raise SystemExit(main())
|