Skip to content

Commit 6ce7f53

Browse files
committed
Manage Python versions
* Add script for creating venvs in a way that * uses uv without requiring it to be installed * controls the used Python version with .python-version file * Use the script in Makefile and action setup * Be sure to treat Windows as the special case it is with regards to venv bin directory Signed-off-by: Jussi Kukkonen <[email protected]>
1 parent da06893 commit 6ce7f53

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.14

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ all:
44
@echo "Run my targets individually!"
55

66
.venv/pyvenv.cfg: requirements/dev.txt
7-
uv venv
7+
setup/create-venv.sh .venv
88
. ./.venv/bin/activate && \
99
uv pip install -r requirements/dev.txt
1010

setup/create-venv.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# uv allows us to easily manage python versions for our venvs... but uv is not always available.
2+
# Assume that there is *some* Python available and use a bootstrap venv using the system python to
3+
# 1. install uv & the python we want (see .python-version)
4+
# 2. create the actual venv with the installed python
5+
# 3. install uv in the actual venv
6+
# Now the actual venv is ready to use
7+
8+
if [ -z "$1" ]; then
9+
echo "Error: No virtual environment path provided." >&2
10+
echo "Usage: $0 <venv_path>" >&2
11+
exit 1
12+
fi
13+
14+
ENV=$1
15+
16+
BOOTSTRAP_ENV=$(mktemp -d)
17+
python3 -m venv --clear "$BOOTSTRAP_ENV"
18+
19+
# Annoying: Windows venvs use a different structure, for unknown reasons.
20+
if [ -d "$BOOTSTRAP_ENV/bin" ]; then
21+
BIN="$BOOTSTRAP_ENV/bin"
22+
else
23+
BIN="$BOOTSTRAP_ENV/Scripts"
24+
fi
25+
26+
27+
. "$BIN/activate" && pip install uv && uv venv --clear "$ENV" && VIRTUAL_ENV="$ENV" uv pip install uv
28+
touch "$ENV/bootstrap"
29+
rm -r "$BOOTSTRAP_ENV"

setup/setup.bash

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ if [[ "${0}" == "${BASH_SOURCE[0]}" ]]; then
3838
fi
3939

4040
# Check the Python version, making sure it's new enough (3.9+)
41-
# The installation step immediately below will technically catch this,
42-
# but doing it explicitly gives us the opportunity to produce a better
43-
# error message.
4441
vers=$(python -V | cut -d ' ' -f2)
4542
maj_vers=$(cut -d '.' -f1 <<< "${vers}")
4643
min_vers=$(cut -d '.' -f2 <<< "${vers}")
@@ -51,19 +48,20 @@ min_vers=$(cut -d '.' -f2 <<< "${vers}")
5148
# `actions/setup-python`, then we might be using the distribution's Python and
5249
# therefore be subject to PEP 668. We use a virtual environment unconditionally
5350
# to prevent that kind of confusion.
54-
python -m venv "${GITHUB_ACTION_PATH}/.action-env"
51+
"${GITHUB_ACTION_PATH}/setup/create-venv.sh" "${GITHUB_ACTION_PATH}/.action-env"
5552

5653
# Annoying: Windows venvs use a different structure, for unknown reasons.
5754
if [[ -d "${GITHUB_ACTION_PATH}/.action-env/bin" ]]; then
58-
VENV_PYTHON_PATH="${GITHUB_ACTION_PATH}/.action-env/bin/python"
55+
BIN="${GITHUB_ACTION_PATH}/.action-env/bin"
5956
else
60-
VENV_PYTHON_PATH="${GITHUB_ACTION_PATH}/.action-env/Scripts/python"
57+
BIN="${GITHUB_ACTION_PATH}/.action-env/Scripts"
6158
fi
6259

63-
"${VENV_PYTHON_PATH}" -m pip install --requirement "${GITHUB_ACTION_PATH}/requirements/main.txt"
60+
. "$BIN/activate" && uv pip install --requirement "${GITHUB_ACTION_PATH}/requirements/main.txt"
6461

65-
debug "sigstore-python: $("${VENV_PYTHON_PATH}" -m sigstore --version)"
62+
63+
debug "sigstore-python: $("$BIN/python" -m sigstore --version)"
6664

6765
# Finally, propagate VENV_PYTHON_PATH so we can actually kick-start
6866
# the extension from it.
69-
echo "venv-python-path=${VENV_PYTHON_PATH}" >> "${GITHUB_OUTPUT}"
67+
echo "venv-python-path=${BIN}/python" >> "${GITHUB_OUTPUT}"

0 commit comments

Comments
 (0)