"""Verify the pinned training data in data/raw/, downloading whatever is missing. The three small tables are committed with the repository, so the forecasts reproduce offline. `daily.parquet` (49 MB, used only by the audit) is downloaded on demand. Every file is checked against the sha256 of the pinned Hub revision. Usage: python code/fetch_data.py [--no-daily] """ import hashlib import sys import urllib.request from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from protostar.common import DATA_RAW, FILES, HF_DATASET, HF_REVISION # noqa: E402 URL = "https://huggingface.co/datasets/{repo}/resolve/{rev}/{path}" 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 main() -> int: skip = {"daily.parquet"} if "--no-daily" in sys.argv else set() DATA_RAW.mkdir(parents=True, exist_ok=True) for name, (hub_path, digest) in FILES.items(): target = DATA_RAW / name if name in skip and not target.exists(): print(f"skipped {name}") continue if target.exists() and sha256(target) != digest: # e.g. a git-lfs pointer file from a clone made without git-lfs: fetch the real file print(f"mismatch {name}: not the pinned file (git-lfs pointer?), downloading it again") target.unlink() if not target.exists(): url = URL.format(repo=HF_DATASET, rev=HF_REVISION, path=hub_path) print(f"download {name} <- {url}") try: tmp = target.with_suffix(".part") urllib.request.urlretrieve(url, tmp) tmp.replace(target) except OSError as e: # no network: the audit skips its daily cross-check if name == "daily.parquet": print(f"WARNING could not download {name} ({e}); the audit will skip the daily checks") continue raise if sha256(target) != digest: print(f"ERROR {name}: sha256 does not match revision {HF_REVISION}") return 1 print(f"verified {name}") print(f"training data = {HF_DATASET} @ {HF_REVISION} (nothing after 2018-12-31)") return 0 if __name__ == "__main__": sys.exit(main())