tsemekwes.bun
1# Copyright © 2017-2026 Juancarlo Añez (apalala@gmail.com) 2# SPDX-License-Identifier: Apache-2.0 3from __future__ import annotations 4 5import json 6import subprocess 7import sys 8from pathlib import Path 9from subprocess import CompletedProcess 10from typing import Any 11 12BUNDIST = "bundist" 13 14 15def _project_root() -> Path: 16 return Path(__file__).resolve().parent.parent.parent 17 18 19tsemekwes_js = _project_root() / BUNDIST / "tsemekwes.js" 20 21 22def _cmd(args: list[str]) -> list[str]: 23 return [ 24 sys.executable, 25 "-m", 26 "pybun", 27 "run", 28 str(tsemekwes_js), 29 "--quiet", 30 *args, 31 ] 32 33 34def run(args: list[str], output: str | None = None) -> str: 35 """Run the CLI with args; if output is set, write results to that file.""" 36 if output is not None: 37 args = ["-o", output, *args] 38 cp = subprocess.run(_cmd(args), capture_output=True, text=True) 39 return check_output(cp) 40 41 42def exec(args: list[str]) -> int: 43 """Replace the current process with the CLI.""" 44 import os 45 46 os.execv(sys.executable, _cmd(args)) 47 48 49def check_output(cp: CompletedProcess) -> str: 50 """Return stdout on success, raise ValueError on non-zero exit.""" 51 if cp.returncode != 0: 52 msg = cp.stderr.strip() or f"exit {cp.returncode}" 53 raise ValueError(f"tsemekwes error: {msg}") 54 return cp.stdout.strip() 55 56 57def run_json(args: list[str]) -> Any: 58 """Run the CLI and parse stdout as JSON.""" 59 result = run(args) 60 if result.returncode != 0: 61 msg = result.stderr.strip() or f"exit {result.returncode}" 62 raise RuntimeError(f"tsemekwes error: {msg}") 63 return json.loads(result.stderr.strip())
BUNDIST =
'bundist'
tsemekwes_js =
PosixPath('/Users/apalala/art/tsemekwes/bundist/tsemekwes.js')
def
run(args: list[str], output: str | None = None) -> str:
35def run(args: list[str], output: str | None = None) -> str: 36 """Run the CLI with args; if output is set, write results to that file.""" 37 if output is not None: 38 args = ["-o", output, *args] 39 cp = subprocess.run(_cmd(args), capture_output=True, text=True) 40 return check_output(cp)
Run the CLI with args; if output is set, write results to that file.
def
exec(args: list[str]) -> int:
43def exec(args: list[str]) -> int: 44 """Replace the current process with the CLI.""" 45 import os 46 47 os.execv(sys.executable, _cmd(args))
Replace the current process with the CLI.
def
check_output(cp: subprocess.CompletedProcess) -> str:
50def check_output(cp: CompletedProcess) -> str: 51 """Return stdout on success, raise ValueError on non-zero exit.""" 52 if cp.returncode != 0: 53 msg = cp.stderr.strip() or f"exit {cp.returncode}" 54 raise ValueError(f"tsemekwes error: {msg}") 55 return cp.stdout.strip()
Return stdout on success, raise ValueError on non-zero exit.
def
run_json(args: list[str]) -> Any:
58def run_json(args: list[str]) -> Any: 59 """Run the CLI and parse stdout as JSON.""" 60 result = run(args) 61 if result.returncode != 0: 62 msg = result.stderr.strip() or f"exit {result.returncode}" 63 raise RuntimeError(f"tsemekwes error: {msg}") 64 return json.loads(result.stderr.strip())
Run the CLI and parse stdout as JSON.