Skip to content

Instruments

instruments

Explicit instrument commands with no environment discovery or installation.

instrument_command(command, *, interpreter, package_root, pythonpath)

Build a command from trusted inputs, never probing or bootstrapping venvs.

The interpreter path stays lexical for venv activation. PYTHONPATH contains the primary package source, then declared supplemental paths in order. Missing Python modules fail in that interpreter; other instruments must exist beside it.

Source code in packages/axm-ingot/src/axm_ingot/instruments.py
Python
def instrument_command(
    command: list[str],
    *,
    interpreter: str,
    package_root: str,
    pythonpath: tuple[str, ...],
) -> tuple[list[str], dict[str, str]]:
    """Build a command from trusted inputs, never probing or bootstrapping venvs.

    The interpreter path stays lexical for venv activation. PYTHONPATH contains
    the primary package source, then declared supplemental paths in order.
    Missing Python modules fail in that interpreter; other instruments must
    exist beside it.
    """
    python = Path(interpreter)
    if not python.is_absolute() or not python.is_file():
        raise ValueError(f"declared instrument interpreter missing: {python}")
    if not Path(package_root).is_absolute() or not command:
        raise ValueError("instrument requires an absolute package root and command")
    if any(not Path(path).is_absolute() for path in pythonpath):
        raise ValueError("instrument pythonpath must be absolute")
    prefix = _instrument_prefix(command[0], python)
    env = dict(os.environ)
    env["PYTHONPATH"] = os.pathsep.join(_source_overlay(package_root, pythonpath))
    env["PATH"] = str(python.parent) + os.pathsep + env.get("PATH", "")
    env.pop("PYTHONHOME", None)
    env["PYTHONNOUSERSITE"] = "1"
    env["PYTHONDONTWRITEBYTECODE"] = "1"
    env.pop("VIRTUAL_ENV", None)
    env.pop("UV_PROJECT_ENVIRONMENT", None)
    return [*prefix, *command[1:]], env