#!/usr/bin/env python3 """Validate CARLA-MWRS tar.zst member names and counts without extraction.""" from __future__ import annotations import argparse import hashlib import json import subprocess import sys from pathlib import Path MODALITIES = { "image_2": (".png", 2400, 1200), "gt_image_2": (".png", 2400, 1200), "depth_u16": (".png", 2400, 1200), "depth_meters": (".npy", 2400, 1200), "normal": (".npy", 2400, 1200), "calib": (".txt", 2400, 1200), } def members(path: Path) -> list[str]: # GNU tar + zstd is also the extraction command documented in README. proc = subprocess.run(["tar", "--zstd", "-tf", str(path)], text=True, capture_output=True) if proc.returncode: raise RuntimeError(f"{path}: tar listing failed: {proc.stderr[-1000:]}") return [line.rstrip("/") for line in proc.stdout.splitlines() if line and not line.endswith("/")] def sha256(path: Path) -> str: h = hashlib.sha256() with path.open("rb") as f: for block in iter(lambda: f.read(8 * 1024 * 1024), b""): h.update(block) return h.hexdigest() def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--release-root", type=Path, required=True) ap.add_argument("--report", type=Path) args = ap.parse_args() errors: list[str] = [] result: dict = {"status": "PASS", "archives": {}} for split_idx, split in enumerate(("training", "validation")): expected_count_idx = 0 if split == "training" else 1 for modality, (suffix, train_count, val_count) in MODALITIES.items(): path = args.release_root / split / f"{modality}.tar.zst" key = f"{split}/{modality}.tar.zst" if not path.is_file(): errors.append(f"missing archive {key}") continue try: all_members = members(path) except Exception as exc: errors.append(str(exc)) continue data_members = [m for m in all_members if m.startswith(modality + "/") and not m.endswith("/")] bad = [m for m in data_members if not m.lower().endswith(suffix)] if bad: errors.append(f"{key}: wrong suffix in {bad[:3]}") expected = train_count if expected_count_idx == 0 else val_count if len(data_members) != expected: errors.append(f"{key}: expected {expected} data members, got {len(data_members)}") outside = [m for m in all_members if not (m == modality or m.startswith(modality + "/"))] if outside: errors.append(f"{key}: members outside {modality}/: {outside[:3]}") result["archives"][key] = {"bytes": path.stat().st_size, "members": len(data_members), "sha256": sha256(path)} result["errors"] = errors result["status"] = "PASS" if not errors else "FAIL" text = json.dumps(result, indent=2) + "\n" if args.report: args.report.write_text(text, encoding="utf-8") print(text, end="") return 0 if not errors else 1 if __name__ == "__main__": sys.exit(main())