| |
| """ |
| Registry Runner for Elizabeth Datasets |
| |
| - Validates and (optionally) runs scripts defined in a script registry YAML |
| - Default registry: /data/elizabeth-datasets/script_registry.yaml |
| |
| Usage: |
| python etl/registry_runner.py --validate |
| python etl/registry_runner.py --validate --registry /path/to/registry.yaml |
| python etl/registry_runner.py --list |
| |
| Notes: |
| - Execution is implemented as no-op placeholders; extend as needed per environment |
| """ |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import hashlib |
| import os |
| import subprocess |
| import sys |
| from dataclasses import dataclass |
| from pathlib import Path |
| from typing import Any, Dict, List |
|
|
| import yaml |
|
|
|
|
| DEFAULT_REGISTRY = Path("/data/elizabeth-datasets/script_registry.yaml") |
|
|
|
|
| @dataclass |
| class ScriptEntry: |
| name: str |
| filename: str |
| path: str |
| version: str | None = None |
| hash: str | None = None |
| dependencies: List[str] | None = None |
| status: str | None = None |
|
|
|
|
| def load_registry(path: Path) -> List[ScriptEntry]: |
| with open(path, "r", encoding="utf-8") as f: |
| data = yaml.safe_load(f) |
| scripts = [] |
| for item in data.get("scripts", []): |
| scripts.append(ScriptEntry( |
| name=item.get("name"), |
| filename=item.get("filename"), |
| path=item.get("path"), |
| version=item.get("version"), |
| hash=item.get("hash"), |
| dependencies=item.get("dependencies"), |
| status=item.get("status"), |
| )) |
| return scripts |
|
|
|
|
| def sha256_file(p: Path) -> str: |
| h = hashlib.sha256() |
| with open(p, "rb") as f: |
| for chunk in iter(lambda: f.read(8192), b""): |
| h.update(chunk) |
| return h.hexdigest() |
|
|
|
|
| def validate_registry(scripts: List[ScriptEntry]) -> List[Dict[str, Any]]: |
| results: List[Dict[str, Any]] = [] |
| for s in scripts: |
| entry: Dict[str, Any] = {"name": s.name, "path": s.path} |
| p = Path(s.path) |
| entry["exists"] = p.exists() |
| if p.exists(): |
| try: |
| entry["sha256"] = sha256_file(p) |
| entry["hash_match"] = (s.hash is None) or (entry["sha256"] == s.hash) |
| except Exception as e: |
| entry["error"] = str(e) |
| else: |
| entry["sha256"] = None |
| entry["hash_match"] = False |
|
|
| |
| missing_deps = [] |
| for dep in s.dependencies or []: |
| if shutil_which(dep) is None: |
| missing_deps.append(dep) |
| entry["missing_deps"] = missing_deps |
| results.append(entry) |
| return results |
|
|
|
|
| def shutil_which(cmd: str) -> str | None: |
| from shutil import which |
| return which(cmd) |
|
|
|
|
| def print_table(rows: List[Dict[str, Any]]) -> None: |
| for r in rows: |
| status = "OK" if (r.get("exists") and r.get("hash_match") and not r.get("missing_deps")) else "WARN" |
| print(f"- {r.get('name')}: {status} | exists={r.get('exists')} hash_match={r.get('hash_match')} missing_deps={r.get('missing_deps')}") |
|
|
|
|
| def list_scripts(scripts: List[ScriptEntry]) -> None: |
| for s in scripts: |
| print(f"- {s.name} :: {s.path} :: deps={s.dependencies}") |
|
|
|
|
| def main(argv: List[str] | None = None) -> int: |
| p = argparse.ArgumentParser(description="Elizabeth registry runner") |
| p.add_argument("--registry", default=str(DEFAULT_REGISTRY), help="Path to script_registry.yaml") |
| p.add_argument("--validate", action="store_true", help="Validate files and hashes") |
| p.add_argument("--list", action="store_true", help="List scripts") |
| args = p.parse_args(argv) |
|
|
| reg_path = Path(args.registry) |
| if not reg_path.exists(): |
| print(f"Registry not found: {reg_path}") |
| return 2 |
| scripts = load_registry(reg_path) |
| if args.list: |
| list_scripts(scripts) |
| if args.validate: |
| rows = validate_registry(scripts) |
| print_table(rows) |
| if not (args.list or args.validate): |
| p.print_help() |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|
|
|