Files
computer-playbook/scripts/meta/env/python.sh
T
kevinveenbirkenbachandClaude Opus 4.7 3fe215aaa6 fix(test-runner): source load.sh + prefer project venv over ambient VIRTUAL_ENV
`make test-unit` (docker runner) reached run.sh inside the container via
`bash --login`, which never sources the project's env-loader. PYTHON stayed
unset, so the test invocation fell back to the system python3 — which has
none of the runtime deps (ruamel, ansible, bcrypt, requests, certifi,
humanfriendly), producing 132+ ModuleNotFoundError errors.

run.sh now sources scripts/meta/env/load.sh itself (idempotent via
INFINITO_ENV_LOADED), so the env layer applies regardless of how the shell
was spawned.

While load.sh's detect.sh (now scripts/meta/env/python.sh) correctly walks
/opt/venvs and ~/.venvs to find the venv, an ambient VIRTUAL_ENV from a
different project (e.g. /root/.venvs/pkgmgr in pkgmgr's container) would
hijack the resolution. python.sh now scans the project-named venv candidates
first and only falls back to VIRTUAL_ENV when no project venv exists.

Also moves scripts/meta/python/detect.sh to scripts/meta/env/python.sh so the
env-related helpers live under one directory; updates the load.sh source
pragma and the lint-test path that scans it for INFINITO_* registrations.

`make test-unit` goes from 1262 tests / 132 errors to 1999 tests / 0 errors.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-21 14:55:10 +02:00

44 lines
1.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# shellcheck shell=bash
#
# SPOT for venv + Python interpreter resolution. Exports VENV, PYTHON,
# PIP, PYTHONPATH. Sourced; re-entrant.
VENV_NAME=infinito
if [[ -z "${VENV:-}" ]]; then
for _venv_candidate in "/opt/venvs/${VENV_NAME}" "${HOME}/.venvs/${VENV_NAME}"; do
if [[ -x "${_venv_candidate}/bin/python" ]]; then
VENV="${_venv_candidate}"
break
fi
done
unset _venv_candidate
fi
if [[ -n "${VIRTUAL_ENV:-}" ]]; then
VENV_BASE="$(dirname "${VIRTUAL_ENV}")"
elif [[ -d /opt && -w /opt ]]; then
VENV_BASE=/opt/venvs
else
VENV_BASE="${HOME}/.venvs"
fi
VENV_FALLBACK="${VENV_BASE%/}/${VENV_NAME}"
VENV="${VENV:-${VIRTUAL_ENV:-${VENV_FALLBACK}}}"
if [[ -x "${VENV}/bin/python" ]]; then
PYTHON="${VENV}/bin/python"
elif command -v python3 >/dev/null 2>&1; then
PYTHON=python3
else
echo "detect.sh: no python3 on PATH and no venv at ${VENV}/bin/python" >&2
# shellcheck disable=SC2317
return 1 2>/dev/null || exit 1
fi
PIP="${PYTHON} -m pip"
PYTHONPATH="${PYTHONPATH:-.}"
export VENV PYTHON PIP PYTHONPATH