Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/microsandbox-uv-pip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Reinstalling in a microsandbox no longer stops at "Getting uv".** A sandbox whose volume an earlier install had
built in asked the backend's virtualenv, which has no pip, for uv and failed with "No module named pip". It now asks
the image's own Python. Rerun the install line to pick it up.
7 changes: 4 additions & 3 deletions microsandbox/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"options": {
"cwd": "{projectRoot}",
"commands": [
"sh -n run.sh && sh -n dns.sh && sh -n test-dns.sh && sh -n programs.sh && sh -n test-programs.sh && sh -n wipe.sh && sh -n test-wipe.sh",
"uvx --from shellcheck-py shellcheck -s sh -x run.sh dns.sh test-dns.sh programs.sh test-programs.sh wipe.sh test-wipe.sh"
"sh -n run.sh && sh -n dns.sh && sh -n test-dns.sh && sh -n programs.sh && sh -n test-programs.sh && sh -n wipe.sh && sh -n test-wipe.sh && sh -n uv.sh && sh -n test-uv.sh",
"uvx --from shellcheck-py shellcheck -s sh -x run.sh dns.sh test-dns.sh programs.sh test-programs.sh wipe.sh test-wipe.sh uv.sh test-uv.sh"
],
"parallel": false
}
Expand All @@ -22,7 +22,8 @@
"commands": [
"docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-dns.sh",
"docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-programs.sh",
"docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-wipe.sh"
"docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-wipe.sh",
"docker run --rm -v \"$PWD:/lens:ro\" mirror.gcr.io/library/python:3.12-slim-trixie sh /lens/microsandbox/test-uv.sh"
],
"parallel": false
}
Expand Down
11 changes: 5 additions & 6 deletions microsandbox/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ if [ ! -x "$OPT/node/bin/node" ]; then
tar -xJf "/tmp/lens/$file" -C "$OPT/node" --strip-components 1
rm "/tmp/lens/$file"
fi
if [ ! -x "$OPT/bin/uv" ] || [ ! -x "$OPT/bin/uvx" ]; then # uvx: the MCP runner's, for servers from PyPI
log "Getting uv..."
python3 -m pip install -q --no-cache-dir --target /tmp/lens/uv "uv>=0.8,<0.9"
cp /tmp/lens/uv/bin/uv /tmp/lens/uv/bin/uvx "$OPT/bin/" && rm -rf /tmp/lens/uv
fi
# uv, and uvx: the MCP runner's, for servers from PyPI
# shellcheck source=SCRIPTDIR/uv.sh
. "$CODE/microsandbox/uv.sh"
get_uv "$OPT/bin"

# --- Build: the backend and the web app, again only when the code changed (the edition doesn't change them) -------
build_id="$(cat "$CODE/.lens-build" 2>/dev/null || echo unknown)"
Expand All @@ -136,7 +135,7 @@ if [ "$(cat "$OPT/build-id" 2>/dev/null)" != "$build_id" ]; then
(cd "$OPT/backend" &&
export UV_PROJECT_ENVIRONMENT="$OPT/venv" UV_PYTHON_DOWNLOADS=never UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy \
UV_HTTP_TIMEOUT=300 UV_HTTP_RETRIES=5 UV_CACHE_DIR="$OPT/uv-cache" &&
retry uv sync --frozen --no-dev --python "$(command -v python3)" \
retry uv sync --frozen --no-dev --python "$(image_python)" \
$(for e in ${EXTRAS:-}; do printf -- '--extra %s ' "$e"; done))

web() { (cd "$WEB_SRC" && exec env COREPACK_ENABLE_DOWNLOAD_PROMPT=0 COREPACK_HOME="$OPT/corepack" "$@"); }
Expand Down
43 changes: 43 additions & 0 deletions microsandbox/test-uv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh
# Tests uv.sh: getting uv and uvx with the backend's virtualenv, which has no pip, first in PATH, as on a sandbox whose
# volume an earlier install built in. Run it in the image run.sh runs in:
#
# docker run --rm -v "$PWD:/lens:ro" python:3.12-slim-trixie sh /lens/microsandbox/test-uv.sh
set -eu

here="$(cd "$(dirname "$0")" && pwd)"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT

fail() {
printf 'FAIL: %s\n' "$*" >&2
exit 1
}
ok() { printf 'ok: %s\n' "$*"; }
logged=""
log() { logged="$logged$*;"; }

# shellcheck source=SCRIPTDIR/uv.sh
. "$here/uv.sh"
mkdir -p /tmp/lens "$work/bin"

# the virtualenv a build made, without pip, first in PATH as run.sh puts it
python3 -m venv --without-pip "$work/venv"
PATH="$work/bin:$work/venv/bin:$PATH"
[ "$(command -v python3)" = "$work/venv/bin/python3" ] || fail "the virtualenv's python3 isn't first in PATH"
if python3 -m pip --version >/dev/null 2>&1; then fail "the virtualenv has pip, so this tests nothing"; fi
"$(image_python)" -m pip --version >/dev/null || fail "image_python names $(image_python), which has no pip either"
ok "image_python names a Python with pip ($(image_python)) with the virtualenv first in PATH"

# an install from before uvx was needed: uv, no uvx
printf '#!/bin/sh\n' >"$work/bin/uv" && chmod +x "$work/bin/uv"
get_uv "$work/bin" || fail "get_uv failed with the virtualenv first in PATH"
"$work/bin/uv" --version >/dev/null || fail "uv doesn't run"
"$work/bin/uvx" --version >/dev/null || fail "uvx doesn't run"
case "$logged" in *"Getting uv..."*) ;; *) fail "get_uv didn't say what it was doing" ;; esac
ok "get_uv got uv and uvx where uvx was missing"

logged=""
get_uv "$work/bin" || fail "get_uv failed with both there"
[ -z "$logged" ] || fail "get_uv downloaded again with both there"
ok "get_uv keeps uv and uvx that are there"
21 changes: 21 additions & 0 deletions microsandbox/uv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# uv and uvx in the sandbox (docs/microsandbox.md), sourced by run.sh, which defines log() and fetch(). test-uv.sh tests
# it.
#
# uv comes from PyPI through the image's pip. The backend's virtualenv, which uv made without pip, is first in PATH
# once a build has made it, so its python3 can't install anything ("No module named pip"): an install made before uvx
# was needed has uv and the virtualenv but not uvx, and got stuck at "Getting uv". image_python names the image's own.

# image_python: the Python the image ships, the same with the virtualenv in PATH or not
image_python() {
python3 -c 'import sys; print(getattr(sys, "_base_executable", "") or sys.executable)'
}

# get_uv BIN: uv and uvx into BIN, unless both are there
get_uv() {
[ -x "$1/uv" ] && [ -x "$1/uvx" ] && return 0
log "Getting uv..."
py="$(image_python)"
rm -rf /tmp/lens/uv
"$py" -m pip install -q --no-cache-dir --disable-pip-version-check --root-user-action=ignore --target /tmp/lens/uv "uv>=0.8,<0.9" &&
cp /tmp/lens/uv/bin/uv /tmp/lens/uv/bin/uvx "$1/" && rm -rf /tmp/lens/uv
}
Loading