# /// script # requires-python = ">=3.11" # dependencies = [ # "httpx>=0.27,<1", # "polars>=1.24,<2", # "pyarrow>=18,<21", # "deltalake>=0.22,<2", # "huggingface-hub>=1.19,<2", # "pytest>=8,<9", # "pytest-asyncio>=0.24,<1", # "ruff>=0.11,<1", # "pyyaml>=6,<7", # ] # /// """Hugging Face Jobs entry point for the ZipLime corporate-actions dataset. restore repository -> lint -> tests -> build -> verify -> publish The build reads two sibling datasets from the Hub. That is the point of this one: it is glue, and what it says has to be derivable from what the others published, not from a working copy that only exists on somebody's laptop. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path DEFAULT_REPO = "ZipLime/corporate-actions" DEFAULT_WORKSPACE = "/tmp/corporate-actions" def log(message: str) -> None: print(f"[corporate-actions] {message}", flush=True) def restore_repository(repo_id: str, token: str | None, workspace: Path) -> Path: from huggingface_hub import snapshot_download from huggingface_hub.errors import RepositoryNotFoundError workspace.mkdir(parents=True, exist_ok=True) try: snapshot_download( repo_id=repo_id, repo_type="dataset", token=token, local_dir=str(workspace), # Only the recipe and tests are needed: every table is rebuilt from # the sources each run, so restoring the data would download tens of # megabytes to overwrite them. allow_patterns=["recipe/**", "tests/**", "jobs/**", "*.md", "*.json", "*.toml"], ) log(f"restored {repo_id} into {workspace}") except RepositoryNotFoundError: log(f"{repo_id} does not exist yet; treating this as the first publication") if not (workspace / "recipe" / "cli.py").is_file(): raise SystemExit(f"{workspace} has no recipe; publish it before scheduling a Job") return workspace def run(command: list[str], *, cwd: Path, env: dict[str, str]) -> int: log(f"running: {' '.join(command)}") return subprocess.run(command, cwd=cwd, env=env, check=False).returncode def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--mode", choices=["update", "build"], default="update") parser.add_argument("--workspace", default=os.environ.get("JOB_WORKSPACE", DEFAULT_WORKSPACE)) parser.add_argument("--skip-gates", action="store_true") args = parser.parse_args(argv) token = os.environ.get("HF_TOKEN") if not token: raise SystemExit("the HF_TOKEN secret is required") repo_id = os.environ.get("HF_DATASET_REPO", DEFAULT_REPO) workspace = restore_repository(repo_id, token, Path(args.workspace)) env = {**os.environ, "PYTHONPATH": str(workspace), "DATA_DIR": "data"} if not args.skip_gates: for name, command in (("lint", ["ruff", "check", "."]), ("tests", ["pytest", "-q"])): code = run(command, cwd=workspace, env=env) if code != 0: log(f"{name} failed; nothing was fetched and nothing was published") return code code = run([sys.executable, "-m", "recipe.cli", "build"], cwd=workspace, env=env) if code != 0: log("build or verification failed; nothing published") return code if args.mode == "build": return 0 return run( [sys.executable, "-m", "recipe.cli", "publish", "--repo", repo_id], cwd=workspace, env=env, ) if __name__ == "__main__": raise SystemExit(main())