| from __future__ import annotations |
| import json |
| import subprocess |
| import sys |
| from pathlib import Path |
|
|
| _SCRIPTS = ( |
| Path(__file__).parent.parent |
| / "app" |
| / "skill_cache" |
| / "humanitarian-data-analyst" |
| / "scripts" |
| ) |
|
|
|
|
| def run_skill_script(script_name: str, args: list[str]) -> dict | list: |
| """Run a vendored skill script and return its parsed JSON stdout. |
| |
| Raises RuntimeError with stderr if the script exits non-zero. |
| """ |
| script_path = _SCRIPTS / script_name |
| result = subprocess.run( |
| [sys.executable, str(script_path)] + args, |
| capture_output=True, |
| text=True, |
| ) |
| if result.returncode != 0: |
| raise RuntimeError( |
| f"{script_name} exited {result.returncode}:\n{result.stderr}" |
| ) |
| return json.loads(result.stdout) |
|
|
|
|
| def run_skill_script_raw(script_name: str, args: list[str]) -> str: |
| """Run a vendored skill script; return stdout as a plain string (not JSON-parsed). |
| |
| Use for scripts that write their output to a file rather than JSON to stdout |
| (e.g. read_kobo.py --slug, render_spec.py without -o). |
| Raises RuntimeError with stderr if the script exits non-zero. |
| """ |
| script_path = _SCRIPTS / script_name |
| result = subprocess.run( |
| [sys.executable, str(script_path)] + args, |
| capture_output=True, |
| text=True, |
| ) |
| if result.returncode != 0: |
| raise RuntimeError( |
| f"{script_name} exited {result.returncode}:\n{result.stderr}" |
| ) |
| return result.stdout |
|
|