#!/usr/bin/env python3 """Upload selected workspace artifacts to a Hugging Face dataset repository. The script is intentionally conservative: dry-run mode never imports ``huggingface_hub``, empty targets are skipped, and result targets are resolved from ``/root`` while source/code targets are resolved from ``/workspace``. """ from __future__ import annotations import argparse import getpass import os from pathlib import Path WORKSPACE_ROOT = Path(os.environ.get("VSI_WORKSPACE_ROOT", "/workspace")) RESULTS_HOST_ROOT = Path(os.environ.get("VSI_RESULTS_HOST_ROOT", "/root")) DEFAULT_REPO = os.environ.get("VSI_BACKUP_REPO", "AntonioJun/workspace") TARGETS = { "code": [ "README.md", "setup.sh", "backup.py", "analysis", "encoder", "harness", "inference", "symbolic", "tests", "selective_frame_counts.csv", ], "reports": ["reports"], "spatial-codes": ["data/spatial codes", "bundles/spatial-codes.tar.gz"], "caches": ["data/caches"], "A": ["results/A"], "B": ["results/B"], "C": ["results/C"], "F": ["results/F"], } def _resolve_targets(target): if target == "all": return list(TARGETS) requested = [part.strip() for part in str(target).split(",") if part.strip()] unknown = [name for name in requested if name not in TARGETS] if unknown: raise ValueError(f"unknown target(s): {', '.join(unknown)}") return requested def _local_path(relative): relative = str(relative).lstrip("/") root = RESULTS_HOST_ROOT if relative.startswith("results/") else WORKSPACE_ROOT return root / relative def _has_files(path): path = Path(path) if path.is_file(): return True if path.is_dir(): return any(child.is_file() for child in path.rglob("*")) return False def _target_root(relatives): roots = { "results" if str(rel).lstrip("/").startswith("results/") else "workspace" for rel in relatives } if len(roots) > 1: raise ValueError( f"target mixes incompatible workspace/results roots: {relatives}" ) return RESULTS_HOST_ROOT if "results" in roots else WORKSPACE_ROOT def _path_in_repo(relative): return str(relative).lstrip("/") def backup(repo_id=DEFAULT_REPO, target="all", dry_run=False, token=None): selected = _resolve_targets(target) uploaded = [] for name in selected: relatives = TARGETS[name] _target_root(relatives) existing = [(relative, _local_path(relative)) for relative in relatives] existing = [(relative, path) for relative, path in existing if _has_files(path)] if not existing: print(f"[{name}] skipped: no files found") continue uploaded.append(name) if dry_run: for relative, path in existing: print( f"[{name}] would upload {path} -> {repo_id}/{_path_in_repo(relative)}" ) continue from huggingface_hub import HfApi api = HfApi(token=token) for relative, path in existing: repo_path = _path_in_repo(relative) if path.is_dir(): api.upload_folder( folder_path=str(path), repo_id=repo_id, repo_type="dataset", path_in_repo=repo_path, ) else: api.upload_file( path_or_fileobj=str(path), repo_id=repo_id, repo_type="dataset", path_in_repo=repo_path, ) print(f"[{name}] uploaded {path} -> {repo_id}/{repo_path}") return uploaded def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "target", nargs="?", default="all", help="target name, comma list, or all" ) parser.add_argument("--repo", default=DEFAULT_REPO) parser.add_argument( "--token", default=os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN"), ) parser.add_argument("--dry-run", action="store_true") args = parser.parse_args(argv) token = args.token if not args.dry_run and not token: token = getpass.getpass("HF token: ") backup(args.repo, args.target, dry_run=args.dry_run, token=token) if __name__ == "__main__": main()