# /// script # requires-python = '>=3.12' # /// '''Stage (and optionally push) a report snapshot to a private HF Space. SUPERSEDED: since the single-document merge (report v061), build_landing.py assembles AND stages the whole Space into personalgui-accel-report/ on every build. This script predates the merged layout — its report-ref resolution assumes the old reports/-relative links and would fail on merged pages. Kept only for pushing pre-merge snapshots; do not use it on report v061 or later. The report and its detail/ pages reference video and frame assets with relative paths ('../experiments/...', '../../media/...'). Copying only the referenced files into a staging directory that mirrors the repo's own layout means those relative paths resolve unchanged on HF Spaces too -- no HTML rewriting needed. uv run deploy_hf_space.py # stage only, latest report uv run deploy_hf_space.py --report reports/report_v059_2026-07-27T18-56.html uv run deploy_hf_space.py --push --owner phil-hf-user Pushing requires git-lfs installed and an HF token either in $HF_TOKEN or already saved by `hf auth login` -- the script checks and tells you what's missing rather than half-pushing. ''' import re import shutil from argparse import ArgumentParser from pathlib import Path _ROOT = Path(__file__).parent _REPORTS = _ROOT / 'reports' _DETAIL = _REPORTS / 'detail' _REF = re.compile(r'(?:src|href)="([^"]+)"') _LFS_PATTERNS = ('*.mp4', '*.jpg', '*.jpeg', '*.png') def _latest_report() -> Path: reports = sorted(_REPORTS.glob('report_v*.html'), key=lambda p: p.stat().st_mtime) return reports[-1] def _refs(html: Path) -> list[str]: text = html.read_text() return [r for r in _REF.findall(text) if not r.startswith(('http', '#', 'data:'))] def _resolve(base_dir: Path, ref: str) -> tuple[Path, Path]: '''(source path, path relative to repo root) for a ref found in base_dir's HTML.''' src = (base_dir / ref).resolve() return src, src.relative_to(_ROOT) def _collect(report: Path) -> dict[Path, Path]: '''Map of repo-relative dest path -> absolute source path, for report + its closure.''' manifest: dict[Path, Path] = {} src, rel = _resolve(_REPORTS, report.name) manifest[rel] = src for ref in _refs(report): src, rel = _resolve(_REPORTS, ref) if not src.is_file(): raise FileNotFoundError(f'{report.name} references missing file: {ref}') manifest[rel] = src if src.parent == _DETAIL: for detail_ref in _refs(src): dsrc, drel = _resolve(_DETAIL, detail_ref) if not dsrc.is_file(): raise FileNotFoundError(f'{src.name} references missing file: {detail_ref}') manifest[drel] = dsrc return manifest def _stage(report: Path, stage_dir: Path) -> dict[Path, Path]: manifest = _collect(report) if stage_dir.exists(): shutil.rmtree(stage_dir) stage_dir.mkdir(parents=True) for rel, src in manifest.items(): dest = stage_dir / rel dest.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(src, dest) report_rel = next(rel for rel, src in manifest.items() if src == report.resolve()) (stage_dir / 'index.html').write_text( f'' f'{report.name}\n' ) (stage_dir / 'README.md').write_text( '---\n' 'title: personalgui-accel-report\n' 'emoji: \U0001F4CA\n' 'colorFrom: blue\n' 'colorTo: purple\n' 'sdk: static\n' 'pinned: false\n' '---\n' ) (stage_dir / '.gitattributes').write_text( '\n'.join(f'{pat} filter=lfs diff=lfs merge=lfs -text' for pat in _LFS_PATTERNS) + '\n' ) return manifest def _push(stage_dir: Path, owner: str, space: str, private: bool) -> int: if shutil.which('git-lfs') is None: print('git-lfs is not installed. Install it first, e.g.: sudo apt-get install git-lfs') return 1 token_cache = Path.home() / '.cache' / 'huggingface' / 'token' if not token_cache.exists(): print('No saved HF token found. Run `hf auth login` (or `huggingface-cli login`) first, ' 'then re-run this script with --push.') return 1 url = f'https://huggingface.co/spaces/{owner}/{space}' print(f'Pushing {stage_dir} to {url} (private={private}) ...') print('This creates/updates a real Space on huggingface.co -- run it yourself once the ' 'Space exists (Settings page lets you create it as private), or create it first via ' 'the HF web UI, then:') print(f' cd {stage_dir}') print(' git init && git lfs install') print(f' git remote add origin {url}') print(' git add . && git commit -m "deploy report"') print(' git push -f origin main') return 0 def main() -> int: ap = ArgumentParser(description=__doc__) ap.add_argument('--report', type=Path, default=None, help='defaults to the newest reports/report_v*.html') ap.add_argument('--stage-dir', type=Path, default=_ROOT / '.hf_space_stage') ap.add_argument('--owner', default=None, help='HF username/org, required with --push') ap.add_argument('--space', default='personalgui-accel-report') ap.add_argument('--private', action='store_true', default=True) ap.add_argument('--push', action='store_true', help='print/run the git-lfs push steps after staging') args = ap.parse_args() report = args.report or _latest_report() manifest = _stage(report, args.stage_dir) total = sum(src.stat().st_size for src in manifest.values()) print(f'staged {len(manifest)} files ({total / 1_048_576:.1f} MB) -> {args.stage_dir}') print(f'landing page: {args.stage_dir / "index.html"}') if args.push: if not args.owner: print('--push requires --owner ') return 1 return _push(args.stage_dir, args.owner, args.space, args.private) return 0 if __name__ == '__main__': raise SystemExit(main())