Skip to content

Commit 4e18803

Browse files
PYTHON-5203 Use uv from Python toolchain if available (#634)
Co-authored-by: Ezra Chung <[email protected]>
1 parent 68d1eec commit 4e18803

File tree

4 files changed

+77
-33
lines changed

4 files changed

+77
-33
lines changed

.evergreen/auth_aws/activate-authawsvenv.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ activate_authawsvenv() {
3434
. ../find-python3.sh || return
3535
PYTHON=$(ensure_python3) || return
3636

37-
echo "Creating virtual environment 'authawsvenv'..."
3837
venvcreate "${PYTHON:?}" authawsvenv || return
3938

4039
local packages=(

.evergreen/find-python3.sh

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,41 @@ find_python3() (
314314
# with `2>/dev/null` to silence these messages.
315315
#
316316
# If DRIVERS_TOOLS_PYTHON is set, it will return that value. Otherwise
317-
# it will use find_python3 to return a suitable value.
317+
# it will look for the "Current" python in the python toolchain. Finally,
318+
# it will fall back to using find_python3 to return a suitable value.
318319
#
319320
ensure_python3() {
320-
declare python_binary
321-
python_binary="${DRIVERS_TOOLS_PYTHON:-}"
321+
# Use "$DRIVERS_TOOLS_PYTHON".
322+
if command -v "${DRIVERS_TOOLS_PYTHON:-}" >/dev/null; then
323+
echo "Using Python binary ${DRIVERS_TOOLS_PYTHON:?}" >&2
324+
echo "${DRIVERS_TOOLS_PYTHON:?}"
325+
return
326+
fi
327+
328+
# Use Python Toolchain.
329+
declare python_binary=""
330+
case "${OSTYPE:?}" in
331+
cygwin)
332+
python_binary="/cygdrive/c/Python/Current/python.exe"
333+
;;
334+
darwin*)
335+
python_binary="/Library/Frameworks/Python.Framework/Versions/Current/bin/python3"
336+
;;
337+
*)
338+
python_binary="/opt/python/Current/bin/python3"
339+
;;
340+
esac
341+
if command -v "${python_binary:?}" >/dev/null; then
342+
echo "Using Python binary ${python_binary:?}" >&2
343+
echo "${python_binary:?}"
344+
return
345+
fi
346+
347+
# Use find_python3.
322348
{
323-
if [ -z "${python_binary}" ]; then
324-
echo "Finding Python3 binary..."
325-
python_binary="$(find_python3 2>/dev/null)" || return
326-
echo "Finding Python3 binary... done."
327-
else
328-
echo "Using Python binary $python_binary"
329-
fi
349+
echo "Finding Python3 binary..."
350+
python_binary="$(find_python3 2>/dev/null)" || return
351+
echo "Finding Python3 binary... done."
330352
} 1>&2
331353
echo "${python_binary:?}"
332354
}

.evergreen/install-cli.sh

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ if [ -z "$BASH" ]; then
88
return 1
99
fi
1010

11-
if [ -z "${1:-}" ]; then
12-
echo "Must give a target directory!"
13-
fi
11+
TARGET_DIR="${1:?"must give a target directory!"}"
1412

1513
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
1614
. $SCRIPT_DIR/handle-paths.sh
@@ -27,33 +25,58 @@ if [ "${DOCKER_RUNNING:-}" == "true" ]; then
2725
UV_TOOL_DIR=$_root_dir/uv-tool
2826
fi
2927

30-
. ./venv-utils.sh
28+
# If uv is not on path, try see if it is available from the Python toolchain.
29+
if ! command -v uv >/dev/null; then
30+
export PATH
31+
case "${OSTYPE:?}" in
32+
cygwin)
33+
PATH="/cygdrive/c/Python/Current:${PATH:-}"
34+
;;
35+
darwin*)
36+
PATH="/Library/Frameworks/Python.Framework/Versions/Current/bin:${PATH:-}"
37+
;;
38+
*)
39+
PATH="/opt/python/Current/bin:${PATH:-}"
40+
;;
41+
esac
42+
fi
3143

32-
if [ ! -d $SCRIPT_DIR/venv ]; then
44+
if command -V uv 2>/dev/null; then
45+
# Ensure there is a venv available for backward compatibility.
46+
uv venv venv
47+
else
48+
# If uv is still not available, we need a venv.
49+
. ./venv-utils.sh
3350

34-
. ./find-python3.sh
51+
# Create and activate `venv` via `venvcreate` or `venvactivate`.
52+
if [ ! -d "$SCRIPT_DIR/venv" ]; then
3553

36-
echo "Ensuring python binary..."
37-
PYTHON=$(ensure_python3 2>/dev/null)
38-
echo "Ensuring python binary... done."
54+
. ./find-python3.sh
3955

40-
echo "Creating virtual environment 'venv'..."
41-
venvcreate "${PYTHON:?}" venv
42-
echo "Creating virtual environment 'venv'... done."
43-
else
44-
venvactivate venv
45-
fi
56+
echo "Ensuring python binary..."
57+
PYTHON="$(ensure_python3 2>/dev/null)"
58+
echo "Ensuring python binary... done."
4659

47-
if ! command -v uv >/dev/null; then
60+
echo "Creating virtual environment 'venv'..."
61+
venvcreate "${PYTHON:?}" venv
62+
echo "Creating virtual environment 'venv'... done."
63+
else
64+
venvactivate venv
65+
fi
66+
67+
# Install uv into the newly created venv.
4868
UV_UNMANAGED_INSTALL=1 python -m pip install -q --force-reinstall uv
69+
70+
# Ensure a working uv binary is present.
71+
command -V uv
4972
fi
5073

51-
command -V uv # Ensure a working uv binary is present.
74+
[[ -d venv ]] # venv should exist by this point.
5275

5376
# Store paths to binaries for use outside of current working directory.
54-
python_binary="$(which python)"
77+
python_binary="$(uv run --no-project python -c 'import sys;print(sys.executable)')"
5578

56-
pushd $1 > /dev/null
79+
pushd "$TARGET_DIR" > /dev/null
5780

5881
# Add support for MongoDB 3.6, which was dropped in pymongo 4.11.
5982
EXTRA_ARGS=""
@@ -66,14 +89,14 @@ fi
6689
if [ "Windows_NT" == "${OS:-}" ]; then
6790
TMP_DIR=$(cygpath -m "$(mktemp -d)")
6891
PATH="$SCRIPT_DIR/venv/Scripts:$PATH"
69-
UV_TOOL_BIN_DIR=${TMP_DIR} uv tool install ${EXTRA_ARGS} --force --editable .
92+
UV_TOOL_BIN_DIR=${TMP_DIR} uv tool install ${EXTRA_ARGS} --with certifi --force --editable .
7093
filenames=$(ls ${TMP_DIR})
7194
for filename in $filenames; do
7295
mv $TMP_DIR/$filename "$1/${filename//.exe/}"
7396
done
7497
rm -rf $TMP_DIR
7598
else
76-
UV_TOOL_BIN_DIR=$(pwd) uv tool install -q ${EXTRA_ARGS} --python "${python_binary}" --force --editable .
99+
UV_TOOL_BIN_DIR=$(pwd) uv tool install -q ${EXTRA_ARGS} --python "${python_binary}" --with certifi --force --editable .
77100
fi
78101

79102
popd > /dev/null

.evergreen/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ PATH=$PATH
2828
EOF
2929

3030
# Set the python binary to use.
31-
DRIVERS_TOOLS_PYTHON=$(find_python3 2>/dev/null)
31+
DRIVERS_TOOLS_PYTHON="$(ensure_python3 2>/dev/null)"
3232
echo "DRIVERS_TOOLS_PYTHON=$DRIVERS_TOOLS_PYTHON" >> $DRIVERS_TOOLS/.env
3333

3434
# Install the clis in this folder.

0 commit comments

Comments
 (0)