oss-ecosystem-data / code /scripts /deploy_space.py
saladday1's picture
code: sync from git 9bc5001 daily job also builds and deploys the page when web/ changed (version.json on the Space; Node 22.22.2); push_code stops instead of overwriting others' changes in code/ (--c
0ef6066 verified
Raw History Blame Contribute Delete
3.85 kB
"""Build the page (web/) and upload it to the Space, leaving the daily data alone.
python scripts/deploy_space.py build and upload
python scripts/deploy_space.py --ci npm ci first (a clean copy, as in the daily job)
python scripts/deploy_space.py --changed exit 0 when web/ differs from what the Space serves, else 1
The page is static: it reads data/site.json and data/<category>.json, which the daily job
replaces. The daily job also rebuilds the page when web/ changed (scripts/job.sh), so a change
pushed to the dataset's code/ goes live at the next 00:30 UTC run. Every upload carries
version.json with a hash of web/'s source; that is how --changed tells. Data files the page no
longer reads (DATA_FILES lists the ones it does) are removed from the Space.
"""
import hashlib
import json
import re
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from huggingface_hub import CommitOperationAdd, CommitOperationDelete, hf_hub_download # noqa: E402
from hfeco.hub import api # noqa: E402
SPACE = "MiniMaxAI/ecosystem"
KEEP = ("data/", "README.md", ".gitattributes")
DATA_FILES = re.compile(r"data/(site|video|language|image|music|audio)\.json")
WEB = ROOT / "web"
# What is not source: build output, installed packages, local data and logs (web/.gitignore).
NOT_SOURCE = re.compile(r"(^|/)(node_modules|dist|dist-ssr)/|^public/data/|\.(local|log|tsbuildinfo)$|(^|/)\.DS_Store$")
def source_files():
"""web/'s source, relative to web/: what git tracks in a checkout (this repo or a clone of the
dataset), else everything but build output and local data (the job's copy of code/)."""
r = subprocess.run(["git", "ls-files", "-z"], cwd=WEB, capture_output=True, text=True)
if r.returncode == 0 and r.stdout:
return sorted(f for f in r.stdout.split("\0") if f and (WEB / f).is_file())
return sorted(rel for p in WEB.rglob("*") if p.is_file()
and not NOT_SOURCE.search(rel := p.relative_to(WEB).as_posix()))
def source_hash():
"""One hash over web/'s source files, the same in a git checkout and in the job's copy."""
h = hashlib.sha256()
for rel in source_files():
h.update(rel.encode() + b"\0" + hashlib.sha256((WEB / rel).read_bytes()).digest())
return h.hexdigest()[:16]
def live_hash():
try:
return json.loads(Path(hf_hub_download(SPACE, "version.json", repo_type="space")).read_text()).get("source")
except Exception: # first deploy with version.json, or the Space unreachable: build
return None
src = source_hash()
if "--changed" in sys.argv:
live = live_hash()
print(f"web/ source {src}, live {live or 'unknown'}: {'changed' if src != live else 'unchanged'}")
sys.exit(0 if src != live else 1)
if "--ci" in sys.argv:
subprocess.run(["npm", "ci", "--no-audit", "--no-fund"], cwd=WEB, check=True)
subprocess.run(["npm", "run", "build"], cwd=WEB, check=True)
dist = WEB / "dist"
(dist / "version.json").write_text(json.dumps({"source": src, "built": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}))
files = [p for p in dist.rglob("*") if p.is_file() and not p.relative_to(dist).as_posix().startswith("data/")]
local = {p.relative_to(dist).as_posix() for p in files}
remote = set(api.list_repo_files(SPACE, repo_type="space"))
ops = [CommitOperationAdd(path_in_repo=p.relative_to(dist).as_posix(), path_or_fileobj=str(p)) for p in files]
ops += [CommitOperationDelete(path_in_repo=f) for f in sorted(remote - local)
if not f.startswith(KEEP) or (f.startswith("data/") and not DATA_FILES.fullmatch(f))]
api.create_commit(SPACE, ops, repo_type="space", commit_message=f"dashboard page {src}")
print(f"uploaded {len(local)} files, removed {len(ops) - len(local)}; web/ source {src}")