Update the security master
Browse files- recipe/hf_jobs.py +10 -10
- tests/test_docs_and_jobs.py +68 -0
recipe/hf_jobs.py
CHANGED
|
@@ -14,7 +14,7 @@ from __future__ import annotations
|
|
| 14 |
from dataclasses import dataclass
|
| 15 |
from typing import Any
|
| 16 |
|
| 17 |
-
from .config import DEFAULT_HF_REPO
|
| 18 |
|
| 19 |
# The work is downloading the sibling datasets and four hundred settlement
|
| 20 |
# archives, then a few minutes of grouping. Memory is modest; bandwidth is not.
|
|
@@ -109,16 +109,16 @@ def backfill_spec(
|
|
| 109 |
)
|
| 110 |
|
| 111 |
|
| 112 |
-
def job_secrets(*, hf_token: str
|
| 113 |
-
"""
|
| 114 |
-
|
| 115 |
-
if not hf_token.strip():
|
| 116 |
-
raise ValueError("HF_TOKEN is required to create or run a Hugging Face Job")
|
| 117 |
-
return {
|
| 118 |
-
"HF_TOKEN": hf_token.strip(),
|
| 119 |
-
"SEC_USER_AGENT": validate_sec_user_agent(sec_user_agent),
|
| 120 |
-
}
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
def _api(token: str | None) -> Any:
|
| 124 |
try:
|
|
|
|
| 14 |
from dataclasses import dataclass
|
| 15 |
from typing import Any
|
| 16 |
|
| 17 |
+
from .config import DEFAULT_HF_REPO
|
| 18 |
|
| 19 |
# The work is downloading the sibling datasets and four hundred settlement
|
| 20 |
# archives, then a few minutes of grouping. Memory is modest; bandwidth is not.
|
|
|
|
| 109 |
)
|
| 110 |
|
| 111 |
|
| 112 |
+
def job_secrets(*, hf_token: str) -> dict[str, str]:
|
| 113 |
+
"""Credentials the Job needs, and only those.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
No SEC User-Agent here: this dataset fetches nothing from SEC. It is
|
| 116 |
+
derived entirely from a sibling dataset on the Hub, so the only secret it
|
| 117 |
+
can use is the token that reads and writes it.
|
| 118 |
+
"""
|
| 119 |
+
if not hf_token:
|
| 120 |
+
raise ValueError("an HF token is required to run or schedule a Job")
|
| 121 |
+
return {"HF_TOKEN": hf_token}
|
| 122 |
|
| 123 |
def _api(token: str | None) -> Any:
|
| 124 |
try:
|
tests/test_docs_and_jobs.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The published documents and the job must describe what is actually built."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
import re
|
| 7 |
+
import tomllib
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
import yaml
|
| 11 |
+
|
| 12 |
+
from recipe.hf_jobs import DEFAULT_SCHEDULE, JOB_SCRIPT
|
| 13 |
+
from recipe.schema import CONFIG_SCHEMAS
|
| 14 |
+
|
| 15 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _frontmatter() -> dict:
|
| 19 |
+
text = (ROOT / "README.md").read_text(encoding="utf-8")
|
| 20 |
+
assert text.startswith("---\n")
|
| 21 |
+
return yaml.safe_load(text.split("---\n", 2)[1])
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_readme_declares_every_config():
|
| 25 |
+
assert {entry["config_name"] for entry in _frontmatter()["configs"]} == set(CONFIG_SCHEMAS)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_readme_has_no_unfilled_placeholders():
|
| 29 |
+
assert not re.findall(r"\{\{[A-Z_]+\}\}", (ROOT / "README.md").read_text(encoding="utf-8"))
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_job_script_declares_every_runtime_dependency():
|
| 33 |
+
"""A Job installs from the script header alone; a gap fails only in the cloud."""
|
| 34 |
+
project = tomllib.loads((ROOT / "pyproject.toml").read_text(encoding="utf-8"))["project"]
|
| 35 |
+
required = {re.split(r"[<>=]", n, maxsplit=1)[0].strip().lower() for n in project["dependencies"]}
|
| 36 |
+
for group in ("dev", "publish"):
|
| 37 |
+
required |= {
|
| 38 |
+
re.split(r"[<>=]", n, maxsplit=1)[0].strip().lower()
|
| 39 |
+
for n in project.get("optional-dependencies", {}).get(group, [])
|
| 40 |
+
}
|
| 41 |
+
header = (ROOT / JOB_SCRIPT).read_text(encoding="utf-8").split("# ///")[1]
|
| 42 |
+
declared = {m.group(1).lower() for m in re.finditer(r'"\s*([A-Za-z0-9_.-]+)\s*[<>=]', header)}
|
| 43 |
+
assert required <= declared
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_manifest_matches_the_recipe():
|
| 47 |
+
from recipe.build import PIT_DELTA_NAME
|
| 48 |
+
|
| 49 |
+
manifest = json.loads((ROOT / "manifest.json").read_text(encoding="utf-8"))
|
| 50 |
+
assert manifest["bundle_storage_data"]["table_uri"].endswith(PIT_DELTA_NAME)
|
| 51 |
+
assert manifest["source"]["schedule"] == DEFAULT_SCHEDULE
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_schedule_runs_after_the_dataset_it_reads():
|
| 55 |
+
"""Derived entirely from company-fundamentals, which rebuilds at 07:10."""
|
| 56 |
+
hour, minute = int(DEFAULT_SCHEDULE.split()[1]), int(DEFAULT_SCHEDULE.split()[0])
|
| 57 |
+
assert (hour, minute) > (7, 10), DEFAULT_SCHEDULE
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_job_specs_can_be_built():
|
| 61 |
+
"""A missing import in hf_jobs fails only when someone schedules the job."""
|
| 62 |
+
from recipe.hf_jobs import backfill_spec, job_secrets, update_spec
|
| 63 |
+
|
| 64 |
+
for spec in (update_spec(repo_id="ZipLime/corporate-actions"),
|
| 65 |
+
backfill_spec(repo_id="ZipLime/corporate-actions")):
|
| 66 |
+
assert spec.script_url.endswith(JOB_SCRIPT)
|
| 67 |
+
assert spec.script_args
|
| 68 |
+
assert set(job_secrets(hf_token="x")) == {"HF_TOKEN"}
|