"""Upload the source to the dataset repo's code/ folder (the daily HF Job runs from there). python scripts/push_code.py [--check | --force] Uploads every git-tracked file except eval/ (the hand-labelled holdout): the pipeline, the page's source in web/ and the Space card, so a new module can't be forgotten. Files in code/ that git does not track are removed. code/ is shared: other people push to it directly (see AGENTS.md). Before uploading, this compares code/ now with code/ at the last sync from here; if someone else changed a file there that this checkout does not have, it stops instead of overwriting or deleting their work. Bring their changes in (copy the files, commit) and run it again. --check only runs that comparison; --force overwrites anyway. Ask the person you work for before running this: what is in code/ goes live at the next run. """ import hashlib import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) from huggingface_hub import CommitOperationAdd, CommitOperationDelete # noqa: E402 from hfeco.hub import api # noqa: E402 from hfeco.store import DATASET_REPO # noqa: E402 SYNC = "code: sync from git" def git(*a): return subprocess.run(["git", *a], cwd=ROOT, capture_output=True, text=True, check=True).stdout def remote_code(rev): """{path under code/: content id} at a revision: the git blob id, or the LFS sha256.""" out = {} for f in api.list_repo_tree(DATASET_REPO, repo_type="dataset", path_in_repo="code", recursive=True, revision=rev): if hasattr(f, "blob_id"): out[f.path[len("code/"):]] = f.lfs.sha256 if f.lfs else f.blob_id return out def local_id(path, like): """This checkout's file in the same form as `like` (an LFS sha256 or a git blob id).""" p = ROOT / path if not p.is_file(): return None data = p.read_bytes() if like and len(like) == 64: return hashlib.sha256(data).hexdigest() return hashlib.sha1(b"blob %d\0" % len(data) + data).hexdigest() files = [f for f in git("ls-files").splitlines() if not f.startswith("eval/")] commits = api.list_repo_commits(DATASET_REPO, repo_type="dataset") last = next((c for c in commits if c.title.startswith(SYNC)), None) now = remote_code("main") if last and "--force" not in sys.argv: base = remote_code(last.commit_id) theirs = [p for p in sorted(set(base) | set(now)) if base.get(p) != now.get(p) and now.get(p) != local_id(p, now.get(p) or base.get(p))] if theirs: who = sorted({a for c in commits if c.created_at > last.created_at for a in c.authors}) print(f"code/ has changes this checkout does not have (since {last.commit_id[:10]}, by {', '.join(who)}):") for p in theirs: print(" ", "added" if p not in base else "deleted" if p not in now else "changed", p) print("Bring them in and commit first, or pass --force to overwrite them.") sys.exit(1) if "--check" in sys.argv: print(f"code/ has nothing this checkout lacks (last sync {last.commit_id[:10] if last else 'none'})") sys.exit(0) head = git("log", "-1", "--format=%h %s").strip() ops = [CommitOperationAdd(path_in_repo=f"code/{f}", path_or_fileobj=str(ROOT / f)) for f in files] ops += [CommitOperationDelete(path_in_repo=f"code/{f}") for f in sorted(set(now) - set(files))] api.create_commit(DATASET_REPO, ops, repo_type="dataset", commit_message=f"{SYNC} {head}"[:200]) print(f"code/: {len(files)} files uploaded, {len(set(now) - set(files))} removed")