#!/usr/bin/env python3 """Parse a DCPM metadata.bin index. Stage 1: read only, write nothing. Record layout, decoded from the bytes: int64 name_len | name | int64 ndim | int64 shape[ndim] int32 dtype | int32 ? | int64 ? | int32 ? | int64 file_len | file int64 offset | ... trailing fields We locate records by scanning for the length-prefixed names rather than assuming a fixed stride, so a variable-length shape list cannot desync us. """ import struct, sys, re, json def parse(path): b = open(path, "rb").read() assert b[:4] == b"DCPM", f"not DCPM: {b[:4]!r}" recs = [] for m in re.finditer(rb"param_(\d{6})", b): i = m.start() nlen = struct.unpack_from("4} {'shape':<20} {'dtype':>5} {'file':<16} {'offset':>14}") for r in recs[:14]: print(f" {r['idx']:>4} {str(r['shape']):<20} {r['dtype']:>5} {r['file']:<16} {r['offset']:>14,}") print(" ...") for r in recs[-6:]: print(f" {r['idx']:>4} {str(r['shape']):<20} {r['dtype']:>5} {r['file']:<16} {r['offset']:>14,}") tot = sum(int(__import__("math").prod(r["shape"])) for r in recs) print(f"\n total elements across all params: {tot:,}") json.dump(recs, open("/tmp/dcpm_index.json", "w")) print(" wrote /tmp/dcpm_index.json")