File size: 6,002 Bytes
0b2a584
a54e579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2a584
a54e579
 
 
 
 
 
 
4fb6456
 
a54e579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b2a584
a54e579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c73cfd7
 
 
 
 
a54e579
c73cfd7
 
 
a54e579
c73cfd7
 
 
 
 
 
a54e579
c73cfd7
 
 
 
 
 
 
a54e579
 
 
 
 
 
 
 
 
 
 
 
 
2de4806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a54e579
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

"""Every path a published doc tells you to run must itself be published.

The READMEs' "Reproducing" sections named nine `scripts/*.py`. None was on the
Hub, and when checked they carried absolute paths to one laptop. A reproduction
section nobody but the author can run is worse than absent: it reads as a
promise.

This walks every published .md, extracts the repository paths it mentions, and
checks each exists on the Hub. It also greps the published scripts for absolute
paths under a user's home or a specific mount, which is the other half of the
same failure — published but unrunnable.

    python scripts/test_doc_references.py
"""
from __future__ import annotations


import re
import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from react_toolbox.staging import staging_dir

RESULTS: list[tuple[bool, str, str]] = []
REPO = "yxma/React"
PATH_RE = re.compile(
    r"(?<![\w/])((?:scripts|toolbox|examples|data|test_sets|preprocess|docs)"
    r"/[\w./+-]+\.(?:py|json|md|jpg|jsonl))")
ABS_RE = re.compile(r"[\"'](/(?:home|media|Users)/[^\"']+)[\"']")


def check(ok: bool, name: str, evidence: str) -> None:
    RESULTS.append((bool(ok), name, evidence))


def main() -> int:
    from huggingface_hub import HfApi, hf_hub_download
    api = HfApi()
    files = set(api.list_repo_files(REPO, repo_type="dataset"))
    d = str(staging_dir())

    docs = sorted(f for f in files if f.endswith(".md"))
    missing, seen = [], 0
    for doc in docs:
        text = Path(hf_hub_download(REPO, doc, repo_type="dataset",
                                    local_dir=d)).read_text(errors="ignore")
        for ref in sorted(set(PATH_RE.findall(text))):
            seen += 1
            if ref not in files:
                missing.append(f"{doc} -> {ref}")
    check(not missing, "every path a published doc names is published",
          f"{len(docs)} docs, {seen} path references, all present"
          + (f"; MISSING {missing[:4]}" if missing else ""))

    # ...and the published scripts must not point at one person's disk
    # react_paths.py is the ONE place a default may live, and only as the
    # fallback of an environment lookup. Exempting it wholesale would let a
    # literal creep back in beside the documented ones, so the exemption is
    # narrowed: every absolute path in that file must sit on a line that reads
    # the environment.
    scripts = sorted(f for f in files if f.startswith("scripts/") and f.endswith(".py"))
    hard, loose = [], []
    for s_ in scripts:
        text = Path(hf_hub_download(REPO, s_, repo_type="dataset",
                                    local_dir=d)).read_text(errors="ignore")
        if s_ == "scripts/react_paths.py":
            for line in text.splitlines():
                if ABS_RE.search(line) and "os.environ" not in line \
                        and not line.lstrip().startswith(("#", "_DEF_")):
                    loose.append(line.strip()[:70])
            continue
        for m in ABS_RE.findall(text):
            hard.append(f"{s_}: {m}")
    check(not hard and not loose,
          "only react_paths.py holds a default path, behind the environment",
          f"{len(scripts)} scripts: {len(scripts)-1} with no absolute path, and "
          f"react_paths.py's defaults all sit behind os.environ.get"
          + (f"; hard-coded {hard[:3]}" if hard else "")
          + (f"; loose in react_paths {loose[:2]}" if loose else ""))

    # ...and the path module they share is published, since they import it
    imports_paths = []
    for s in scripts:
        text = Path(hf_hub_download(REPO, s, repo_type="dataset",
                                    local_dir=d)).read_text(errors="ignore")
        if "from react_paths import" in text:
            imports_paths.append(s)
    check("scripts/react_paths.py" in files and imports_paths,
          "the shared path module ships with the scripts that import it",
          f"{len(imports_paths)} scripts import react_paths; "
          f"published: {'scripts/react_paths.py' in files}")

    # 4 — the published copy must BE the repo copy. `scripts/` and `USAGE.md`
    #     were uploaded once, by hand, and nothing has kept them current
    #     since: exactly the failure the toolbox upload block in
    #     build_release_publish.py describes, one directory over. Drift here is
    #     invisible -- the files exist, the doc-reference check above is happy,
    #     and a reader downloads instructions for a dataset that has moved.
    from huggingface_hub import HfApi, hf_hub_download
    import hashlib
    api = HfApi()
    files = api.list_repo_files(REPO, repo_type="dataset")
    root = Path(__file__).resolve().parents[1]
    pairs = [(f, root / f) for f in files if f.startswith("scripts/")]
    pairs += [(f, src) for f, src in (
        ("USAGE.md", root / "docs/USAGE.md"),
        ("README.md", root / "docs/superpowers/specs/README_v2_release.md"))
        if f in files]
    drift, absent = [], []
    for remote, local in pairs:
        if not local.exists():
            absent.append(remote)
            continue
        got = hf_hub_download(REPO, remote, repo_type="dataset")
        if Path(got).read_bytes() != local.read_bytes():
            drift.append(remote)
    check(not drift and not absent,
          "every published script and doc is the repo's current copy",
          f"{len(pairs)} files checked, all identical" if not (drift or absent)
          else f"{len(drift)} stale on the Hub: {drift[:6]}"
               + (f"; {len(absent)} published with no source: {absent[:3]}"
                  if absent else ""))

    w = max(len(x) for _, x, _ in RESULTS)
    print()
    for ok, name, ev in RESULTS:
        print(f"  [{'ok' if ok else 'FAIL'}] {name:<{w}}  {ev}")
    n = sum(not x for x, _, _ in RESULTS)
    print(f"\ndoc references: {len(RESULTS)} checks, {n} failing")
    return 1 if n else 0


if __name__ == "__main__":
    raise SystemExit(main())