#!/usr/bin/env python3 """Put the training shards that a dataset folder carries as they are into the build output (2026-11-19). A folder whose set.json names this loader (Dress-ED, editing/dressed/) carries the shards the training read, in shards/, and lists the size, md5 and CRC-32C of each in MANIFEST.json. This script checks every shard asked for against MANIFEST.json (size and md5) and puts it into --out: a hard link where the file system allows one, otherwise a copy. Without --out it only checks. tools/openembed_data/install_training.py checks the bytes and CRC-32C again when it installs them. python tools/openembed_data/copy_shards.py --set-dir editing/dressed [--out shards/editing/dressed] [--shards NAME,...] [--problems FILE] It writes one JSON line to --problems for each shard that is missing or differs, and exits with status 1 if there is any. """ from __future__ import annotations import argparse import base64 import hashlib import json import os import shutil import sys def md5_b64(path): h = hashlib.md5() with open(path, "rb") as fh: for block in iter(lambda: fh.read(1 << 22), b""): h.update(block) return base64.b64encode(h.digest()).decode() def main(argv=None): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--set-dir", required=True, help="the dataset folder (holds shards/ and MANIFEST.json)") ap.add_argument("--out", default=None, help="where the shards go; without it the shards are only checked") ap.add_argument("--shards", default="", help="only these shards, comma separated (with or without .tfrecord)") ap.add_argument("--problems", default=None, help="JSON lines, one per shard that is missing or differs") a = ap.parse_args(argv) listed = json.load(open(os.path.join(a.set_dir, "MANIFEST.json"), encoding="utf-8"))["shards"] names = sorted(listed) if a.shards: want = [s.strip() for s in a.shards.split(",") if s.strip()] want = [s if s.endswith(".tfrecord") else s + ".tfrecord" for s in want] unknown = [s for s in want if s not in listed] if unknown: raise SystemExit(f"not in MANIFEST.json: {unknown}") names = want if a.out: os.makedirs(a.out, exist_ok=True) problems = [] for i, name in enumerate(names, 1): src, want = os.path.join(a.set_dir, "shards", name), listed[name] if not os.path.exists(src): problems.append({"shard": name, "reason": "missing", "expected_size": want["size"]}) elif os.path.getsize(src) != want["size"]: problems.append({"shard": name, "reason": "size", "size": os.path.getsize(src), "expected_size": want["size"]}) else: got = md5_b64(src) if got != want["md5"]: problems.append({"shard": name, "reason": "md5", "md5": got, "expected_md5": want["md5"]}) elif a.out: dst = os.path.join(a.out, name) if os.path.lexists(dst): os.remove(dst) try: os.link(src, dst) except OSError: shutil.copyfile(src, dst) if i % 25 == 0 or i == len(names): print(f"{i}/{len(names)} shards checked", flush=True) if a.problems: with open(a.problems, "w", encoding="utf-8") as fh: for p in problems: fh.write(json.dumps(p) + "\n") print(f"{len(names) - len(problems)} of {len(names)} shards as listed in MANIFEST.json" + (f", in {a.out}" if a.out else " (checked only)"), flush=True) return 1 if problems else 0 if __name__ == "__main__": sys.exit(main())