Skip to content

Self-improving AI Employees for everyone

refactor(machina): consolidate subprocess wrappers in machina/run.py

trohitg170f56440156
diff --git a/machina/commands/build.py b/machina/commands/build.py
index bc235b09..a51c3cf5 100644
--- a/machina/commands/build.py
+++ b/machina/commands/build.py
@@ -13,38 +13,18 @@ from __future__ import annotations
import os
import re
import shutil
-import subprocess
import sys
from pathlib import Path
import typer
+from machina.run import capture, run
from machina.colors import console
from machina.platform_ import project_root
# ---------------------------------------------------------------- helpers
-def _run(cmd: list[str], *, cwd: Path | None = None, check: bool = True) -> int:
- """Inherit-stdio run; raises ``typer.Exit`` on failure when ``check``."""
- proc = subprocess.run(cmd, cwd=str(cwd) if cwd else None)
- if check and proc.returncode != 0:
- console.print(f"[red]Command failed:[/] {' '.join(cmd)}")
- raise typer.Exit(code=proc.returncode)
- return proc.returncode
-
-
-def _capture(cmd: list[str]) -> str | None:
- """Capture stdout; return None if the command isn't on PATH or fails."""
- try:
- out = subprocess.run(
- cmd, capture_output=True, text=True, check=True, encoding="utf-8"
- )
- return out.stdout.strip() or out.stderr.strip()
- except (FileNotFoundError, subprocess.CalledProcessError):
- return None
-
-
def _which_python() -> str | None:
"""Prefer ``python3`` so we don't pick up Python 2.x on POSIX distros."""
for cmd in ("python3", "python"):
@@ -54,7 +34,7 @@ def _which_python() -> str | None:
def _check_python(cmd: str) -> bool:
- out = _capture([cmd, "--version"])
+ out = capture([cmd, "--version"])
if not out:
return False
match = re.search(r"Python (\d+)\.(\d+)", out)
@@ -69,21 +49,21 @@ def _check_python(cmd: str) -> bool:
def _ensure_pip(python_cmd: str) -> None:
- if not _capture([python_cmd, "-m", "pip", "--version"]):
+ if not capture([python_cmd, "-m", "pip", "--version"]):
console.print(" Installing pip via ensurepip...")
- _run([python_cmd, "-m", "ensurepip", "--upgrade"])
+ run([python_cmd, "-m", "ensurepip", "--upgrade"])
def _ensure_uv(python_cmd: str) -> str:
"""Install ``uv`` via pip if missing; return the resolved version string."""
- version = _capture(["uv", "--version"])
+ version = capture(["uv", "--version"])
if version:
console.print(f" uv: {version}")
return version
_ensure_pip(python_cmd)
console.print(" Installing uv via pip...")
- _run([python_cmd, "-m", "pip", "install", "uv"])
- version = _capture(["uv", "--version"])
+ run([python_cmd, "-m", "pip", "install", "uv"])
+ version = capture(["uv", "--version"])
if not version:
console.print("[red]Error: failed to install uv. See https://docs.astral.sh/uv/[/]")
raise typer.Exit(code=1)
@@ -93,19 +73,19 @@ def _ensure_uv(python_cmd: str) -> str:
def _ensure_temporal() -> None:
"""``temporal-server`` is a global npm CLI; install if missing (non-fatal)."""
- version = _capture(["temporal-server", "--version"])
+ version = capture(["temporal-server", "--version"])
if version:
console.print(f" temporal-server: {version}")
return
console.print(" temporal-server: not found, installing globally...")
- rc = _run(["npm", "install", "-g", "temporal-server"], check=False)
+ rc = run(["npm", "install", "-g", "temporal-server"], check=False)
if rc != 0:
console.print(
" [yellow]Warning: temporal-server install failed. "
"Distributed execution unavailable.[/]"
)
return
- version = _capture(["temporal-server", "--version"])
+ version = capture(["temporal-server", "--version"])
if version:
console.print(f" temporal-server: {version}")
@@ -128,13 +108,13 @@ def build_command() -> None:
# ---- toolchain ---------------------------------------------------
console.print("[bold]Checking dependencies...[/]\n")
- node_version = _capture(["node", "--version"])
+ node_version = capture(["node", "--version"])
console.print(f" Node.js: {node_version or '[red]not found[/]'}")
if not node_version:
console.print("[red]Error: Node.js is required.[/]")
raise typer.Exit(code=1)
- npm_version = _capture(["npm", "--version"])
+ npm_version = capture(["npm", "--version"])
console.print(f" npm: {npm_version or '[red]not found[/]'}")
python_cmd = _which_python()
@@ -161,20 +141,20 @@ def build_command() -> None:
if not is_postinstall:
console.print("[1/5] Installing dependencies...")
- _run(["pnpm", "install"], cwd=root)
+ run(["pnpm", "install"], cwd=root)
else:
console.print("[1/5] Dependencies already installed by package manager")
console.print("[2/5] Building client...")
- _run(["pnpm", "--filter", "react-flow-client", "run", "build"], cwd=root)
+ run(["pnpm", "--filter", "react-flow-client", "run", "build"], cwd=root)
console.print("[3/5] Installing Python dependencies...")
if not (server_dir / ".venv").exists():
- _run(["uv", "venv"], cwd=server_dir)
- _run(["uv", "sync"], cwd=server_dir)
+ run(["uv", "venv"], cwd=server_dir)
+ run(["uv", "sync"], cwd=server_dir)
console.print("[4/5] Installing Playwright browser...")
- rc = _run(["playwright", "install", "chromium"], cwd=server_dir, check=False)
+ rc = run(["playwright", "install", "chromium"], cwd=server_dir, check=False)
if rc != 0:
console.print(
" [yellow]Warning: Playwright browser install failed. "
diff --git a/machina/commands/version.py b/machina/commands/version.py
index 90cf602d..4e7fcd87 100644
--- a/machina/commands/version.py
+++ b/machina/commands/version.py
@@ -9,13 +9,13 @@ from __future__ import annotations
import json
import re
-import subprocess
from pathlib import Path
import typer
from machina.colors import console
from machina.platform_ import project_root
+from machina.run import capture
app = typer.Typer(
@@ -31,33 +31,12 @@ _VALID_VERSION = re.compile(r"^\d+\.\d+\.\d+")
def _git_describe(root: Path) -> str | None:
"""``git describe --tags --abbrev=0``; falls back to sorted ``git tag -l``."""
- try:
- out = subprocess.run(
- ["git", "describe", "--tags", "--abbrev=0"],
- cwd=str(root),
- capture_output=True,
- text=True,
- check=True,
- )
- tag = out.stdout.strip()
- if tag:
- return tag
- except (FileNotFoundError, subprocess.CalledProcessError):
- pass
- try:
- out = subprocess.run(
- ["git", "tag", "-l", "--sort=-version:refname"],
- cwd=str(root),
- capture_output=True,
- text=True,
- check=True,
- )
- for line in out.stdout.splitlines():
- tag = line.strip()
- if tag:
- return tag
- except (FileNotFoundError, subprocess.CalledProcessError):
- pass
+ described = capture(["git", "describe", "--tags", "--abbrev=0"], cwd=root)
+ if described:
+ return described
+ listed = capture(["git", "tag", "-l", "--sort=-version:refname"], cwd=root)
+ if listed:
+ return next((line.strip() for line in listed.splitlines() if line.strip()), None)
return None
diff --git a/machina/run.py b/machina/run.py
new file mode 100644
index 00000000..603cac5b
--- /dev/null
+++ b/machina/run.py
@@ -0,0 +1,59 @@
+"""Shared subprocess helpers used across ``machina.commands``.
+
+Two patterns covering ~95% of subprocess use in this CLI:
+
+* :func:`run` -- inherit-stdio fire-and-forget; raises ``typer.Exit`` on
+ non-zero exit unless ``check=False`` is passed (matches the existing
+ build.py / start.py / dev.py call shape).
+* :func:`capture` -- run silently, return stdout (stripped) on success
+ or ``None`` on missing binary / non-zero exit. Tolerates failure by
+ design; the caller branches on truthiness.
+
+Centralising these two functions removes the per-file ``_run`` /
+``_capture`` / ``_git_describe`` duplicates that previously diverged
+in subtle ways (one had inverted ``ignore_error`` semantics, another
+swallowed stderr, etc.).
+"""
+
+from __future__ import annotations
+
+import subprocess
+from pathlib import Path
+
+import typer
+
+from machina.colors import console
+
+
+def run(
+ argv: list[str],
+ *,
+ cwd: Path | str | None = None,
+ check: bool = True,
+) -> int:
+ """Inherit-stdio run; raises :class:`typer.Exit` on non-zero when ``check``."""
+ proc = subprocess.run(argv, cwd=str(cwd) if cwd else None)
+ if check and proc.returncode != 0:
+ console.print(f"[red]Command failed:[/] {' '.join(argv)}")
+ raise typer.Exit(code=proc.returncode)
+ return proc.returncode
+
+
+def capture(argv: list[str], *, cwd: Path | str | None = None) -> str | None:
+ """Capture stdout (or stderr fallback); ``None`` if binary missing or fails.
+
+ Used for "is this tool installed and what version" queries -- always
+ tolerant, never raises.
+ """
+ try:
+ result = subprocess.run(
+ argv,
+ cwd=str(cwd) if cwd else None,
+ capture_output=True,
+ text=True,
+ check=True,
+ encoding="utf-8",
+ )
+ return result.stdout.strip() or result.stderr.strip() or None
+ except (FileNotFoundError, subprocess.CalledProcessError):
+ return None
diff --git a/machina/tests/test_build.py b/machina/tests/test_build.py
index 2da44c5b..fc660d9b 100644
--- a/machina/tests/test_build.py
+++ b/machina/tests/test_build.py
@@ -1,4 +1,4 @@
-"""Smoke tests for ``machina.commands.build``."""
+"""Smoke tests for ``machina.commands.build`` + ``machina.run`` shared helpers."""
from __future__ import annotations
@@ -7,42 +7,48 @@ from unittest.mock import MagicMock, patch
import pytest
import typer
+from machina import run as run_module
from machina.commands import build
+# --- shared helpers in machina.run -----------------------------------------
+
def test_capture_returns_none_when_command_missing():
- with patch.object(build.subprocess, "run", side_effect=FileNotFoundError):
- assert build._capture(["does-not-exist"]) is None
+ with patch.object(run_module.subprocess, "run", side_effect=FileNotFoundError):
+ assert run_module.capture(["does-not-exist"]) is None
def test_capture_returns_stdout_when_present():
fake = MagicMock(stdout="v1.2.3\n", stderr="")
- with patch.object(build.subprocess, "run", return_value=fake):
- assert build._capture(["tool", "--version"]) == "v1.2.3"
+ with patch.object(run_module.subprocess, "run", return_value=fake):
+ assert run_module.capture(["tool", "--version"]) == "v1.2.3"
+
+
+def test_run_raises_typer_exit_on_nonzero_when_check():
+ fake = MagicMock(returncode=1)
+ with patch.object(run_module.subprocess, "run", return_value=fake), \
+ pytest.raises(typer.Exit):
+ run_module.run(["false"])
+
+
+def test_run_returns_code_when_check_disabled():
+ fake = MagicMock(returncode=42)
+ with patch.object(run_module.subprocess, "run", return_value=fake):
+ assert run_module.run(["x"], check=False) == 42
+# --- build-specific helpers -------------------------------------------------
+
def test_check_python_accepts_3_12_plus():
- with patch.object(build, "_capture", return_value="Python 3.12.5"):
+ with patch.object(build, "capture", return_value="Python 3.12.5"):
assert build._check_python("python") is True
def test_check_python_rejects_3_11():
- with patch.object(build, "_capture", return_value="Python 3.11.9"):
+ with patch.object(build, "capture", return_value="Python 3.11.9"):
assert build._check_python("python") is False
def test_check_python_rejects_unparseable():
- with patch.object(build, "_capture", return_value="garbage output"):
+ with patch.object(build, "capture", return_value="garbage output"):
assert build._check_python("python") is False
-
-
-def test_run_raises_typer_exit_on_nonzero_when_check():
- fake = MagicMock(returncode=1)
- with patch.object(build.subprocess, "run", return_value=fake), pytest.raises(typer.Exit):
- build._run(["false"])
-
-
-def test_run_returns_code_when_check_disabled():
- fake = MagicMock(returncode=42)
- with patch.object(build.subprocess, "run", return_value=fake):
- assert build._run(["x"], check=False) == 42
diff --git a/machina/tests/test_version.py b/machina/tests/test_version.py
index 595254c9..f194dcf9 100644
--- a/machina/tests/test_version.py
+++ b/machina/tests/test_version.py
@@ -39,5 +39,5 @@ def test_update_package_json_preserves_trailing_newline(tmp_path: Path):
def test_git_describe_returns_none_without_git():
- with patch.object(version.subprocess, "run", side_effect=FileNotFoundError):
+ with patch.object(version, "capture", return_value=None):
assert version._git_describe(Path(".")) is None