Spaces:
Paused
Paused
File size: 1,476 Bytes
751ad26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | from __future__ import annotations
import json
import os
import shlex
import subprocess
import sys
from pathlib import Path
from typing import Any
from engines.live_request import default_dotcache_runner_script
def run_dotcache_live(request: dict[str, Any]) -> tuple[dict[str, Any], list[str]]:
return _run_subprocess_runner(
env_var="DOTCACHE_SPACE_DOTCACHE_RUNNER_CMD",
request=request,
runner_name="dotcache",
)
def _run_subprocess_runner(*, env_var: str, request: dict[str, Any], runner_name: str) -> tuple[dict[str, Any], list[str]]:
command = os.getenv(env_var)
if command:
argv = shlex.split(command)
else:
repo_root = Path(__file__).resolve().parents[1]
argv = [sys.executable, str(default_dotcache_runner_script(repo_root))]
completed = subprocess.run(
argv,
input=json.dumps(request),
text=True,
capture_output=True,
check=False,
)
logs = [
f"{runner_name} runner command: {' '.join(argv)}",
f"{runner_name} runner exit code: {completed.returncode}",
]
if completed.stderr.strip():
logs.append(completed.stderr.strip())
if completed.returncode != 0:
raise RuntimeError("\n".join(logs))
try:
payload = json.loads(completed.stdout)
except json.JSONDecodeError as exc:
raise RuntimeError(f"Live {runner_name} runner returned invalid JSON.") from exc
return payload, logs
|