"""Stage code + processed datasets + campaign results, then push to Hugging Face. Excludes weights/checkpoints, raw trips, run logs, and local envs. """ from __future__ import annotations import os import shutil import sys import time from pathlib import Path os.environ.pop("HF_ENDPOINT", None) os.environ["HF_ENDPOINT"] = "https://huggingface.co" os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0" from huggingface_hub import HfApi ROOT = Path(__file__).resolve().parents[1] STAGE = Path(os.environ.get("TRAFFIC_BAN_HF_STAGE", r"D:\traffic-ban-hf-upload")) REPO_ID = os.environ.get("TRAFFIC_BAN_HF_REPO", "tjtrans/Traffic-Demand-Bench") SKIP_DIRS = { ".cache", ".cursor", ".git", "__pycache__", "_raw", "_tile_cache", "_tmp", "logs", "py312", "venv311", "venv312", "weights", } SKIP_SUFFIXES = { ".bin", ".ckpt", ".err", ".log", ".pid", ".pt", ".pth", ".pyc", ".safetensors", } def _skip_dir(name: str) -> bool: return name in SKIP_DIRS or name.endswith(".egg-info") def _skip_file(path: Path) -> bool: if path.suffix.lower() in SKIP_SUFFIXES: return True if path.name in {".DS_Store", "Thumbs.db"}: return True return False def stage(reuse: bool = False) -> list[Path]: if reuse and STAGE.exists() and any(STAGE.iterdir()): copied = [p for p in STAGE.rglob("*") if p.is_file()] print("reuse existing stage", flush=True) return copied if STAGE.exists(): shutil.rmtree(STAGE) copied: list[Path] = [] for dirpath, dirnames, filenames in os.walk(ROOT, followlinks=True): src_dir = Path(dirpath) rel_dir = src_dir.relative_to(ROOT) dirnames[:] = [d for d in dirnames if not _skip_dir(d)] if any(part in SKIP_DIRS for part in rel_dir.parts): dirnames[:] = [] continue dest_dir = STAGE / rel_dir dest_dir.mkdir(parents=True, exist_ok=True) for name in filenames: src = src_dir / name if _skip_file(src) or not src.is_file(): continue dest = dest_dir / name shutil.copy2(src, dest) copied.append(dest) return copied def main() -> int: print("staging from", ROOT, "->", STAGE, flush=True) copied = stage() nbytes = sum(p.stat().st_size for p in copied) print(f"staged {len(copied)} files, {nbytes / (1024 ** 3):.2f} GB", flush=True) api = HfApi() api.create_repo(REPO_ID, repo_type="dataset", exist_ok=True, private=False) url = f"https://huggingface.co/datasets/{REPO_ID}" print("uploading to", url, flush=True) last = None for attempt in range(1, 6): try: api.upload_large_folder( folder_path=str(STAGE), repo_id=REPO_ID, repo_type="dataset", print_report=True, ) print("done", url, flush=True) return 0 except Exception as exc: last = exc print(f"upload attempt {attempt} failed: {exc}", flush=True) time.sleep(min(30 * attempt, 120)) raise last if __name__ == "__main__": sys.exit(main())