Download code/backup.py from d9993r/CRICKET: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/datasets/d9993r/CRICKET/resolve/main/code/backup.py
- Command line
-
hf download hf://datasets/d9993r/CRICKET/code/backup.py
-
curl -L -o backup.py https://huggingface.co/datasets/d9993r/CRICKET/resolve/main/code/backup.py
3.93 kB
| #!/usr/bin/env python3 | |
| """Second copy with checksum verification (standard section 11.2: two independent | |
| copies, checksums verified after transfer; never delete before verification). | |
| python code/backup.py --to /media/usb/cricket_backup # copy + verify | |
| python code/backup.py --to /media/usb/cricket_backup --verify # verify only | |
| python code/backup.py --s3 http://127.0.0.1:9000 --bucket cricket-data-foundry # MinIO / S3 (pip install boto3) | |
| Copies what cannot be recomputed: source videos, reviewer edits, registries, | |
| releases and the code/config that built them. Caches and clips are left out | |
| because run_all.py can rebuild them.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import os | |
| import shutil | |
| import sys | |
| from datetime import datetime | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from common import ROOT, rel # noqa: E402 | |
| INCLUDE = ["datasets", "code", "results/annotations", "results/registry", "results/releases", | |
| "results/segments", "results/qa"] | |
| SKIP_SUFFIX = {".pkl", ".pyc"} | |
| def sha(p): | |
| h = hashlib.sha256() | |
| with open(p, "rb") as f: | |
| for b in iter(lambda: f.read(1 << 20), b""): | |
| h.update(b) | |
| return h.hexdigest() | |
| def files(): | |
| for top in INCLUDE: | |
| base = ROOT / top | |
| if base.is_file(): | |
| yield base | |
| for p in sorted(base.rglob("*")) if base.exists() else []: | |
| if p.is_file() and p.suffix not in SKIP_SUFFIX and "__pycache__" not in p.parts: | |
| yield p | |
| def to_folder(dest, verify_only=False): | |
| dest = Path(dest) | |
| man_path = dest / "MANIFEST.json" | |
| old = json.load(open(man_path))["files"] if man_path.exists() else {} | |
| man, copied, bad = {}, 0, [] | |
| for p in files(): | |
| r = rel(p) | |
| h = sha(p) | |
| man[r] = {"sha256": h, "bytes": p.stat().st_size} | |
| target = dest / r | |
| if verify_only: | |
| if not target.exists() or sha(target) != h: | |
| bad.append(r) | |
| continue | |
| if old.get(r, {}).get("sha256") != h or not target.exists(): | |
| target.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copy2(p, target) | |
| copied += 1 | |
| if sha(target) != h: | |
| bad.append(r) | |
| if not verify_only: | |
| json.dump({"created_at": datetime.now().isoformat(timespec="seconds"), "source_root": str(ROOT), | |
| "files": man}, open(man_path, "w"), indent=1) | |
| print(f"{len(man)} files checked, {copied} copied, {len(bad)} mismatches -> {dest}") | |
| for b in bad[:20]: | |
| print(" MISMATCH", b) | |
| return not bad | |
| def to_s3(endpoint, bucket, prefix): | |
| try: | |
| import boto3 | |
| except ImportError: | |
| raise SystemExit("pip install boto3 (and set AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY for MinIO)") | |
| s3 = boto3.client("s3", endpoint_url=endpoint) | |
| try: | |
| s3.head_bucket(Bucket=bucket) | |
| except Exception: | |
| s3.create_bucket(Bucket=bucket) | |
| n = 0 | |
| for p in files(): | |
| key = f"{prefix}/{rel(p)}" if prefix else rel(p) | |
| h = sha(p) | |
| try: | |
| if s3.head_object(Bucket=bucket, Key=key).get("Metadata", {}).get("sha256") == h: | |
| continue | |
| except Exception: | |
| pass | |
| s3.upload_file(str(p), bucket, key, ExtraArgs={"Metadata": {"sha256": h}}) | |
| n += 1 | |
| print(f"uploaded {n} changed files to s3://{bucket}/{prefix}") | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--to") | |
| ap.add_argument("--verify", action="store_true") | |
| ap.add_argument("--s3") | |
| ap.add_argument("--bucket", default="cricket-data-foundry") | |
| ap.add_argument("--prefix", default="") | |
| a = ap.parse_args() | |
| if a.to: | |
| ok = to_folder(a.to, a.verify) | |
| sys.exit(0 if ok else 1) | |
| if a.s3: | |
| to_s3(a.s3, a.bucket, a.prefix) | |
| return | |
| ap.print_help() | |
| if __name__ == "__main__": | |
| main() | |