#!/usr/bin/env python3 """Read one cell's episode parquet: list it, unpack an episode, verify the hashes. python3 scripts/read_episodes.py .parquet # what is in here python3 scripts/read_episodes.py .parquet --extract --out DIR python3 scripts/read_episodes.py .parquet --verify # recompute every sha256 Needs pyarrow; nothing else beyond the standard library. Layout: one row per EPISODE. The artefacts live in the `files` column as ``list>`` -- `name` is the path the file had inside the episode directory, so --extract restores the original tree. `sha256` is taken over the PUBLISHED bytes (after absolute paths and box names were scrubbed out of the text payloads), so --verify passes on this file as it stands; it does not check against anything outside the repository. """ import argparse, collections, hashlib, os, sys import pyarrow.parquet as pq def rows_of(path, columns=None): for batch in pq.ParquetFile(path).iter_batches(batch_size=1, columns=columns): for row in batch.to_pylist(): yield row def listing(path): meta = pq.ParquetFile(path).metadata light = ["entry_id", "task_name", "source_model", "condition", "cell_id", "is_successful", "score", "file_count", "payload_size_bytes"] rows = list(rows_of(path, columns=light)) print("%s %.1f MB · %d row(s) · %d row group(s)" % (os.path.basename(path), os.path.getsize(path) / 1e6, meta.num_rows, meta.num_row_groups)) if rows: print("cell: %s" % rows[0]["cell_id"]) print() print("%-58s %-9s %6s %9s" % ("entry_id", "verdict", "files", "payload")) for r in rows: print("%-58s %-9s %6d %8.1fM" % (r["entry_id"], "-" if r["is_successful"] is None else ("success" if r["is_successful"] >= 1 else "fail"), r["file_count"], r["payload_size_bytes"] / 1e6)) kinds = collections.Counter() for r in rows_of(path, columns=["files"]): for f in r["files"]: kinds[f["kind"]] += 1 print("\nfile kinds: %s" % ", ".join("%s %d" % (k, n) for k, n in kinds.most_common())) def extract(path, entry_id, out_dir): for r in rows_of(path): if r["entry_id"] != entry_id: continue for f in r["files"]: dst = os.path.join(out_dir, entry_id, f["name"]) os.makedirs(os.path.dirname(dst), exist_ok=True) with open(dst, "wb") as fh: fh.write(f["bytes"]) print("%d file(s) -> %s" % (len(r["files"]), os.path.join(out_dir, entry_id))) return 0 print("no episode with entry_id %r (use the listing to see them)" % entry_id, file=sys.stderr) return 1 def verify(path): # A checker that examines NOTHING must not report success: an empty or # truncated file would otherwise print "0/0 file(s) match" and exit 0. n = bad = 0 for r in rows_of(path, columns=["entry_id", "files"]): for f in r["files"]: n += 1 if hashlib.sha256(f["bytes"]).hexdigest() != f["sha256"]: bad += 1 print("MISMATCH %s/%s" % (r["entry_id"], f["name"])) if n == 0: print("no payload files found in %s -- nothing was verified" % path, file=sys.stderr) return 2 print("%d/%d file(s) match their stored sha256" % (n - bad, n)) return 1 if bad else 0 def main(): ap = argparse.ArgumentParser() ap.add_argument("parquet") ap.add_argument("--extract", metavar="ENTRY_ID") ap.add_argument("--out", default=".") ap.add_argument("--verify", action="store_true") a = ap.parse_args() if a.extract: return extract(a.parquet, a.extract, a.out) if a.verify: return verify(a.parquet) listing(a.parquet) return 0 sys.exit(main())