"""v2 step 7: check the published branch against the staged folder (read-only on the Hub). * main is still at the pinned commit (untouched) * the branch holds exactly the staged files: no missing, no extra * every file's size matches, and its sha256 matches (LFS files: Hub-recorded sha256 vs local; small files: downloaded bytes vs local bytes) * a random sample of data files is downloaded fresh from the branch and compared byte for byte usage: v2_remote_check.py [n_sample] [workers] """ import hashlib, os, random, sys, filecmp from multiprocessing import Pool from huggingface_hub import HfApi, hf_hub_download repo, main_sha, branch, staged, dl = sys.argv[1:6] ns = int(sys.argv[6]) if len(sys.argv) > 6 else 8; nw = int(sys.argv[7]) if len(sys.argv) > 7 else 24 api = HfApi(); T = "dataset" def sha(p): h = hashlib.sha256() with open(p, "rb") as f: for b in iter(lambda: f.read(1 << 24), b""): h.update(b) return p, h.hexdigest() if __name__ == "__main__": refs = {b.name: b.target_commit for b in api.list_repo_refs(repo, repo_type=T).branches} print(f"main at {refs['main']}; unchanged: {refs['main'] == main_sha}; {branch} at {refs.get(branch)}") ok = refs["main"] == main_sha info = api.dataset_info(repo, revision=refs[branch], files_metadata=True) remote = {s.rfilename: s for s in info.siblings} local = set() for d, _, fs in os.walk(staged): if ".cache" in os.path.relpath(d, staged).split(os.sep): continue for f in fs: r = os.path.relpath(os.path.join(d, f), staged) if r != ".manifest.tsv": local.add(r) missing, extra = sorted(local - set(remote)), sorted(set(remote) - local) print(f"staged {len(local)} files, branch {len(remote)} files; missing on branch {missing[:5]} ({len(missing)}); extra on branch {extra[:5]} ({len(extra)})") ok &= not missing and not extra with Pool(nw) as pool: loc = dict(pool.map(sha, [os.path.join(staged, f) for f in sorted(local & set(remote))])) bad = [] for f in sorted(local & set(remote)): s = remote[f]; lp = os.path.join(staged, f) if s.size != os.path.getsize(lp): bad.append((f, "size")); continue if s.lfs: if s.lfs.sha256 != loc[lp]: bad.append((f, "sha256")) else: p = hf_hub_download(repo, f, repo_type=T, revision=refs[branch], local_dir=dl) if not filecmp.cmp(p, lp, shallow=False): bad.append((f, "bytes")) print(f"size/sha256 mismatches: {len(bad)} {bad[:5]}"); ok &= not bad data = sorted(f for f in local if f.endswith(".aselmdb")); random.seed(0) sample = sorted(set(random.sample(data, min(ns, len(data))) + [f for f in data if f.startswith("mp20bat/") or f.startswith("oc22/")])) nb = 0 for f in sample: p = hf_hub_download(repo, f, repo_type=T, revision=refs[branch], local_dir=dl) same = filecmp.cmp(p, os.path.join(staged, f), shallow=False); nb += not same print(f" downloaded {f}: identical to staged {same}", flush=True) ok &= nb == 0 print("REMOTE CHECK PASSED" if ok else "*** REMOTE CHECK FAILED ***")