"""Write a SHA-256 manifest of the Volve data this project reads, and check it against the committed manifest. python scripts/00_data_manifest.py # create/update data_manifest/volve_dia.json python scripts/00_data_manifest.py --check # check only; exit 1 on a missing file or a hash mismatch Only the subtrees in SUBTREES are hashed (~20 GB, mostly WITSML); the rest of data/Volve (well logs, seismic) is not read by this project. """ from __future__ import annotations import argparse import hashlib import json import sys from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timezone from pathlib import Path from volve_dia.paths import REPO_ROOT, volve_root MANIFEST = REPO_ROOT / "data_manifest" / "volve_dia.json" DESCRIPTION = "Volve data village (Equinor and the Volve licence partners), CC BY-NC-SA 4.0" SUBTREES = ["Well_technical_data", "Reports", "WITSML Realtime drilling data"] def sha256(path: Path, chunk: int = 1 << 22) -> str: h = hashlib.sha256() with path.open("rb") as f: while block := f.read(chunk): h.update(block) return h.hexdigest() def scan(root: Path) -> dict[str, dict]: files = sorted(p for sub in SUBTREES for p in (root / sub).rglob("*") if p.is_file()) with ThreadPoolExecutor(max_workers=8) as ex: digests = list(ex.map(sha256, files)) return { p.relative_to(root).as_posix(): {"size_bytes": p.stat().st_size, "sha256": d} for p, d in zip(files, digests) } def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--check", action="store_true") args = ap.parse_args() root = volve_root() missing_dirs = [s for s in SUBTREES if not (root / s).is_dir()] if missing_dirs: print(f"missing under {root}: {missing_dirs}") return 1 current = scan(root) if args.check: ref = json.loads(MANIFEST.read_text(encoding="utf-8"))["files"] missing = sorted(set(ref) - set(current)) changed = sorted(k for k in set(ref) & set(current) if ref[k]["sha256"] != current[k]["sha256"]) extra = sorted(set(current) - set(ref)) for name, items in (("missing", missing), ("hash mismatch", changed), ("extra", extra)): for k in items: print(f"{name}: {k}") print(f"{len(current)} files, {len(missing)} missing, {len(changed)} hash mismatch, {len(extra)} extra") return 1 if (missing or changed) else 0 out = { "dataset": DESCRIPTION, "root": root.relative_to(REPO_ROOT).as_posix() if root.is_relative_to(REPO_ROOT) else str(root), "subtrees": SUBTREES, "created_utc": datetime.now(timezone.utc).isoformat(timespec="seconds"), "n_files": len(current), "total_bytes": sum(v["size_bytes"] for v in current.values()), "files": current, } MANIFEST.parent.mkdir(parents=True, exist_ok=True) MANIFEST.write_text(json.dumps(out, indent=1, ensure_ascii=False) + "\n", encoding="utf-8") print(f"{out['n_files']} files, {out['total_bytes'] / 1e9:.2f} GB -> {MANIFEST.relative_to(REPO_ROOT)}") return 0 if __name__ == "__main__": sys.exit(main())