|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate _data/loaf-index.json from loaf.yaml manifests across all Loaf_* repos |
| 4 | +in the feastorg GitHub organization. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + python3 scripts/generate_loaf_index.py |
| 8 | +
|
| 9 | +Environment: |
| 10 | + GITHUB_TOKEN — required; a token with read access to the feastorg org. |
| 11 | + GITHUB_TOKEN is available automatically in Actions. |
| 12 | +
|
| 13 | +Output: |
| 14 | + _data/loaf-index.json |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import json |
| 20 | +import os |
| 21 | +import sys |
| 22 | +from datetime import datetime, timezone |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +try: |
| 26 | + import requests |
| 27 | +except ImportError: |
| 28 | + sys.exit("Error: 'requests' not installed. Run: pip install requests") |
| 29 | + |
| 30 | +try: |
| 31 | + import yaml |
| 32 | +except ImportError: |
| 33 | + sys.exit("Error: 'pyyaml' not installed. Run: pip install pyyaml") |
| 34 | + |
| 35 | +ORG = "feastorg" |
| 36 | +OUTPUT = Path("_data/loaf-index.json") |
| 37 | +API = "https://api.github.com" |
| 38 | + |
| 39 | +# Fields to extract from each loaf.yaml (all nullable) |
| 40 | +EXTRACT = [ |
| 41 | + ("id", lambda m: m.get("id")), |
| 42 | + ("name", lambda m: m.get("name")), |
| 43 | + ("status", lambda m: m.get("status")), |
| 44 | + ("role", lambda m: m.get("role")), |
| 45 | + ("summary", lambda m: m.get("summary")), |
| 46 | + ("slice_slots", lambda m: (m.get("interconnect") or {}).get("slice_slots")), |
| 47 | + ("bus_type", lambda m: ((m.get("interconnect") or {}).get("bus") or {}).get("type")), |
| 48 | + ("hw_version", lambda m: (m.get("version") or {}).get("hardware")), |
| 49 | + ("hw_gen_current", lambda m: (m.get("hardware") or {}).get("hw_gen_current")), |
| 50 | + ("pcb_layers", lambda m: ((m.get("hardware") or {}).get("pcb") or {}).get("layers")), |
| 51 | + ("schema_version", lambda m: m.get("schema_version")), |
| 52 | + ("tags", lambda m: (m.get("metadata") or {}).get("tags", [])), |
| 53 | + ("updated", lambda m: (m.get("metadata") or {}).get("updated")), |
| 54 | +] |
| 55 | + |
| 56 | +STATUS_ORDER = ["released", "validated", "prototype", "concept", "deprecated"] |
| 57 | +ROLE_ORDER = ["backplane", "hybrid", "controller"] |
| 58 | + |
| 59 | + |
| 60 | +def gh_session() -> requests.Session: |
| 61 | + token = os.environ.get("GITHUB_TOKEN", "") |
| 62 | + if not token: |
| 63 | + sys.exit("Error: GITHUB_TOKEN environment variable not set.") |
| 64 | + s = requests.Session() |
| 65 | + s.headers.update( |
| 66 | + { |
| 67 | + "Authorization": f"Bearer {token}", |
| 68 | + "Accept": "application/vnd.github+json", |
| 69 | + "X-GitHub-Api-Version": "2022-11-28", |
| 70 | + } |
| 71 | + ) |
| 72 | + return s |
| 73 | + |
| 74 | + |
| 75 | +def list_loaf_repos(session: requests.Session) -> list[str]: |
| 76 | + """Return names of all public Loaf_* repos in the org.""" |
| 77 | + repos = [] |
| 78 | + page = 1 |
| 79 | + while True: |
| 80 | + r = session.get( |
| 81 | + f"{API}/orgs/{ORG}/repos", |
| 82 | + params={"type": "public", "per_page": 100, "page": page}, |
| 83 | + ) |
| 84 | + r.raise_for_status() |
| 85 | + batch = r.json() |
| 86 | + if not batch: |
| 87 | + break |
| 88 | + repos.extend(repo["name"] for repo in batch if repo["name"].startswith("Loaf_")) |
| 89 | + page += 1 |
| 90 | + return sorted(repos) |
| 91 | + |
| 92 | + |
| 93 | +def fetch_manifest(session: requests.Session, repo: str) -> dict | None: |
| 94 | + """Fetch and parse loaf.yaml from the repo's default branch.""" |
| 95 | + url = f"https://raw.githubusercontent.com/{ORG}/{repo}/main/loaf.yaml" |
| 96 | + r = session.get(url) |
| 97 | + if r.status_code == 404: |
| 98 | + print(f" SKIP {repo}: no loaf.yaml", flush=True) |
| 99 | + return None |
| 100 | + r.raise_for_status() |
| 101 | + try: |
| 102 | + return yaml.safe_load(r.text) |
| 103 | + except yaml.YAMLError as e: |
| 104 | + print(f" WARN {repo}: YAML parse error — {e}", flush=True) |
| 105 | + return None |
| 106 | + |
| 107 | + |
| 108 | +def extract(manifest: dict, repo: str) -> dict: |
| 109 | + entry = {"repo": repo} |
| 110 | + for key, fn in EXTRACT: |
| 111 | + try: |
| 112 | + entry[key] = fn(manifest) |
| 113 | + except Exception: |
| 114 | + entry[key] = None |
| 115 | + entry["url"] = f"https://feastorg.github.io/{repo}/" |
| 116 | + return entry |
| 117 | + |
| 118 | + |
| 119 | +def sort_key(entry: dict) -> tuple: |
| 120 | + status_rank = ( |
| 121 | + STATUS_ORDER.index(entry["status"]) if entry["status"] in STATUS_ORDER else 99 |
| 122 | + ) |
| 123 | + role_rank = ROLE_ORDER.index(entry["role"]) if entry["role"] in ROLE_ORDER else 99 |
| 124 | + return (status_rank, role_rank, entry.get("id") or "") |
| 125 | + |
| 126 | + |
| 127 | +def main() -> None: |
| 128 | + session = gh_session() |
| 129 | + |
| 130 | + print(f"Listing Loaf_* repos in {ORG}...", flush=True) |
| 131 | + repos = list_loaf_repos(session) |
| 132 | + print(f"Found {len(repos)} repos.", flush=True) |
| 133 | + |
| 134 | + loaves = [] |
| 135 | + for repo in repos: |
| 136 | + print(f" Fetching {repo}...", flush=True) |
| 137 | + manifest = fetch_manifest(session, repo) |
| 138 | + if manifest is None: |
| 139 | + continue |
| 140 | + loaves.append(extract(manifest, repo)) |
| 141 | + |
| 142 | + loaves.sort(key=sort_key) |
| 143 | + |
| 144 | + summary: dict[str, int] = {} |
| 145 | + for entry in loaves: |
| 146 | + key = entry.get("status") or "unknown" |
| 147 | + summary[key] = summary.get(key, 0) + 1 |
| 148 | + |
| 149 | + output = { |
| 150 | + "generated": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), |
| 151 | + "total": len(loaves), |
| 152 | + "summary": summary, |
| 153 | + "loaves": loaves, |
| 154 | + } |
| 155 | + |
| 156 | + OUTPUT.parent.mkdir(parents=True, exist_ok=True) |
| 157 | + OUTPUT.write_text(json.dumps(output, indent=2, ensure_ascii=False) + "\n") |
| 158 | + print(f"\nWrote {len(loaves)} loaves to {OUTPUT}", flush=True) |
| 159 | + for status, count in sorted(summary.items()): |
| 160 | + print(f" {status}: {count}", flush=True) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments