| |
| """Build deterministic per-split/per-modality tar.zst archives. |
| |
| The source tree is never modified. A completed archive is atomically renamed |
| from a `.partial` path, so an interrupted run can be resumed safely. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import hashlib |
| import json |
| import os |
| import subprocess |
| import sys |
| from pathlib import Path |
|
|
|
|
| MODALITIES = ("image_2", "gt_image_2", "depth_u16", "depth_meters", "normal", "calib") |
| SPLITS = ("training", "validation") |
|
|
|
|
| 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 build_one(source_split: Path, modality: str, output: Path, level: int, threads: int) -> None: |
| output.parent.mkdir(parents=True, exist_ok=True) |
| partial = output.with_suffix(output.suffix + ".partial") |
| if output.exists() and output.stat().st_size > 0: |
| print(f"[skip] {output} ({output.stat().st_size / 2**30:.2f} GiB)", flush=True) |
| return |
| if partial.exists(): |
| partial.unlink() |
| tar_cmd = [ |
| "tar", "--sort=name", "--mtime=UTC 1970-01-01", "--owner=0", |
| "--group=0", "--numeric-owner", "--format=gnu", "-C", |
| str(source_split), "-cf", "-", modality, |
| ] |
| zstd_cmd = ["zstd", f"-{level}", f"-T{threads}", "-q", "-o", str(partial)] |
| print(f"[build] {output}", flush=True) |
| tar_proc = subprocess.Popen(tar_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| assert tar_proc.stdout is not None |
| zstd_proc = subprocess.Popen(zstd_cmd, stdin=tar_proc.stdout, stderr=subprocess.PIPE) |
| tar_proc.stdout.close() |
| zerr = zstd_proc.stderr.read().decode("utf-8", "replace") if zstd_proc.stderr else "" |
| zrc = zstd_proc.wait() |
| terr = tar_proc.stderr.read().decode("utf-8", "replace") if tar_proc.stderr else "" |
| trc = tar_proc.wait() |
| if zrc != 0 or trc != 0: |
| partial.unlink(missing_ok=True) |
| raise RuntimeError(f"archive failed for {modality}: tar={trc} zstd={zrc}\n{terr}\n{zerr}") |
| os.replace(partial, output) |
| print(f"[done] {output.stat().st_size / 2**30:.2f} GiB sha256={sha256(output)}", flush=True) |
|
|
|
|
| def main() -> int: |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--source-root", type=Path, required=True) |
| ap.add_argument("--release-root", type=Path, required=True) |
| ap.add_argument("--level", type=int, default=6) |
| ap.add_argument("--threads", type=int, default=0, help="zstd threads; 0 means all CPUs") |
| args = ap.parse_args() |
| for split in SPLITS: |
| source_split = args.source_root / split |
| for modality in MODALITIES: |
| build_one(source_split, modality, args.release_root / split / f"{modality}.tar.zst", args.level, args.threads) |
| records = [] |
| for split in SPLITS: |
| for modality in MODALITIES: |
| p = args.release_root / split / f"{modality}.tar.zst" |
| if not p.exists(): |
| raise FileNotFoundError(p) |
| records.append({"split": split, "modality": modality, "path": str(p.relative_to(args.release_root)), "bytes": p.stat().st_size, "sha256": sha256(p)}) |
| (args.release_root / "archive_manifest.json").write_text(json.dumps({"archives": records}, indent=2) + "\n", encoding="utf-8") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|