"""Download and verify public RefSeg-CA artifacts using Python's standard library.""" from pathlib import Path import argparse, gzip, hashlib, json, shutil, tarfile, urllib.request ROOT = Path(__file__).resolve().parents[1] REPO = 'Ethosoft/RefSeg-CA' def fetch(base, name, cache, manifest): out = cache / name; out.parent.mkdir(parents=True, exist_ok=True) expected = manifest['files'][name]['sha256'] if not out.exists() or hashlib.sha256(out.read_bytes()).hexdigest() != expected: tmp = out.with_suffix(out.suffix + '.part') with urllib.request.urlopen(base + '/' + name, timeout=120) as r, tmp.open('wb') as f: shutil.copyfileobj(r, f) assert hashlib.sha256(tmp.read_bytes()).hexdigest() == expected, name + ': checksum mismatch' tmp.replace(out) return out def extract(path, dest): with tarfile.open(path) as archive: for m in archive.getmembers(): target = (dest / m.name).resolve() if not m.isfile() or not target.is_relative_to(dest.resolve()): raise ValueError('Unsafe archive member: ' + m.name) target.parent.mkdir(parents=True, exist_ok=True) with archive.extractfile(m) as source, target.open('wb') as out: shutil.copyfileobj(source, out) def main(): ap = argparse.ArgumentParser(); ap.add_argument('--revision', default='r3-data-v1') ap.add_argument('--synthetic', action='store_true'); a = ap.parse_args() base = f'https://huggingface.co/datasets/{REPO}/resolve/{a.revision}' with urllib.request.urlopen(base + '/DATA_MANIFEST.json', timeout=60) as r: manifest = json.load(r) cache = ROOT / 'build/downloads'; cache.mkdir(parents=True, exist_ok=True) extract(fetch(base, 'results/primary-and-review-metrics.tar.gz', cache, manifest), ROOT) for name in ['rich_synthetic', 'rich_scenes']: p = fetch(base, 'manifests/' + name + '.jsonl.gz', cache, manifest) out = ROOT / 'data' / (name + '.jsonl'); out.parent.mkdir(parents=True, exist_ok=True) with gzip.open(p, 'rb') as r, out.open('wb') as f: shutil.copyfileobj(r, f) if a.synthetic: for seed in manifest['seeds']: extract(fetch(base, f'archives/rich-seed-{seed}.tar.gz', cache, manifest), ROOT) print(json.dumps({'status': 'DOWNLOADED_AND_VERIFIED', 'revision': a.revision, 'synthetic_images': a.synthetic})) if __name__ == '__main__': main()