"""Download the original files of every test set (sources.json) into raw/ and verify their SHA-256. python fetch_sources.py # everything (about 830 MB, most of it khammoon's training text) python fetch_sources.py --no-khmdd # without khammoon's training text: every set except khmdd Files already present with the right SHA-256 are not downloaded again. Standard library only. `fetch(raw, sets)` downloads only the files of the given test sets (benchmark.py uses it on its first run). """ import argparse import hashlib import json import sys import urllib.request from pathlib import Path HERE = Path(__file__).resolve().parent def sha256(path: Path) -> str: h = hashlib.sha256() with path.open("rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def fetch(raw: Path, sets: set[str] | None = None, skip_khmdd: bool = False, quiet: bool = False) -> list[str]: """Download the original files (of the given test sets, or all) into raw/; return the files whose SHA-256 differs.""" files = json.loads((HERE / "sources.json").read_text())["files"] bad = [] for rel, info in files.items(): if sets is not None and not set(info["sets"]) & sets: continue if skip_khmdd and info["sets"] == ["khmdd"]: continue dest = raw / rel if dest.exists() and sha256(dest) == info["sha256"]: if not quiet: print(f"ok {rel}") continue dest.parent.mkdir(parents=True, exist_ok=True) print(f"fetch {rel} ({info['licence']})", flush=True) req = urllib.request.Request(info["url"], headers={"User-Agent": "thai-ws-bench"}) with urllib.request.urlopen(req) as r, dest.open("wb") as f: while chunk := r.read(1 << 20): f.write(chunk) if sha256(dest) != info["sha256"]: bad.append(rel) print(f"WRONG SHA-256: {rel} (the upstream file changed?)") return bad def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--raw", type=Path, default=HERE / "raw") ap.add_argument("--no-khmdd", action="store_true", help="skip khammoon's training text (only needed for khmdd)") args = ap.parse_args() bad = fetch(args.raw, skip_khmdd=args.no_khmdd) if bad: sys.exit(f"{len(bad)} file(s) differ from the versions the benchmark was built from: {bad}") print("all sources present and verified") if __name__ == "__main__": main()