_runner.py 1.32 KB
from __future__ import annotations

import importlib.util
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
SCRIPTS_DIR = ROOT / "scripts"
if str(SCRIPTS_DIR) not in sys.path:
    sys.path.insert(0, str(SCRIPTS_DIR))


def run_script(path: str) -> int:
    script_path = ROOT / path
    module_name = f"_workflow_{script_path.stem}"
    spec = importlib.util.spec_from_file_location(module_name, script_path)
    if spec is None or spec.loader is None:
        raise RuntimeError(f"Cannot load script: {script_path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    if not hasattr(module, "main"):
        raise RuntimeError(f"Script has no main(): {script_path}")
    result = module.main()
    return int(result or 0)


def run_scripts(paths: list[str]) -> int:
    for path in paths:
        print(f"\n==> Running {path}")
        code = run_script(path)
        if code != 0:
            print(f"Stopped at {path} with exit code {code}")
            return code
    return 0


def print_artifacts(paths: list[str]) -> None:
    existing = [ROOT / path for path in paths if (ROOT / path).exists()]
    if not existing:
        print("\nGenerated files: none")
        return
    print("\nGenerated files:")
    for path in existing:
        print(f"- {path.relative_to(ROOT)}")