File size: 2,055 Bytes
a8b4b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# Self-heal the amaru venv. Runs at every workflow start; no-ops if the venv
# is already present and importable. Idempotent.
#
# Important: we deliberately verify by file presence inside the venv's own
# site-packages, NOT by `python -c "import …"`. A bare import can succeed
# against PEP 370 user-site or system packages even when the venv is empty,
# which produced silent under-installs in the past.
set -euo pipefail

cd "$(dirname "$0")/.."

VENV=".venv"
PYBIN="$VENV/bin/python"
SITE="$VENV/lib/python3.11/site-packages"

required_paths=(
  "$PYBIN"
  "$SITE/fastapi"
  "$SITE/uvicorn"
  "$SITE/typing_extensions.py"
  "$SITE/anyio"
  "$SITE/pydantic"
)

needs_bootstrap=0
for p in "${required_paths[@]}"; do
  if [[ ! -e "$p" ]]; then
    needs_bootstrap=1
    break
  fi
done

# Modern pip (PEP 660) editable installs do NOT create amaru.egg-link.
# They create _editable_impl_amaru.pth + an amaru-*.dist-info directory.
# Accept either layout; rebuild only if neither marker is present.
if [[ "$needs_bootstrap" -eq 0 ]]; then
  if [[ ! -e "$SITE/_editable_impl_amaru.pth" ]] \
     && [[ -z "$(compgen -G "$SITE/amaru-*.dist-info" || true)" ]] \
     && [[ ! -e "$SITE/amaru.egg-link" ]]; then
    needs_bootstrap=1
  fi
fi

if [[ "$needs_bootstrap" -eq 1 ]]; then
  echo "[amaru bootstrap] (re)creating .venv"
  rm -rf "$VENV"
  python3 -m venv "$VENV"
  # --break-system-packages is required because the Nix-provided python is
  # marked PEP 668 externally-managed; the flag is harmless inside our own
  # venv (we are not modifying /nix/store).
  # --ignore-installed forces pip to write into the venv even when a
  # package is already present in the (read-only) Nix store. Without it,
  # pip will try to upgrade-in-place inside /nix/store and fail with EACCES.
  env PIP_USER=0 "$VENV/bin/pip" install --no-user --quiet --break-system-packages --ignore-installed \
    -e ".[dev]" \
    "typing_extensions>=4.0" >/dev/null
  echo "[amaru bootstrap] ready"
else
  echo "[amaru bootstrap] .venv healthy, skipping"
fi