PYTHON-5203 Use uv from Python toolchain if available (#2200)

This commit is contained in:
Steven Silvester 2025-03-13 10:18:17 -05:00 committed by GitHub
parent 0351992ddb
commit 8274db2722
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 18 deletions

View File

@ -92,3 +92,24 @@ cat <<EOT > expansion.yml
DRIVERS_TOOLS: "$DRIVERS_TOOLS"
PROJECT_DIRECTORY: "$PROJECT_DIRECTORY"
EOT
# If the toolchain is available, symlink binaries to the bin dir. This has to be done
# after drivers-tools is cloned, since we might be using its binary dir.
_bin_path=""
if [ "Windows_NT" == "${OS:-}" ]; then
_bin_path="/cygdrive/c/Python/Current/Scripts"
elif [ "$(uname -s)" != "Darwin" ]; then
_bin_path="/Library/Frameworks/Python.Framework/Versions/Current/bin"
else
_bin_path="/opt/python/Current/bin"
fi
if [ -d "${_bin_path}" ]; then
_suffix=""
if [ "Windows_NT" == "${OS:-}" ]; then
_suffix=".exe"
fi
mkdir -p $PYMONGO_BIN_DIR
ln -s ${_bin_path}/just${_suffix} $PYMONGO_BIN_DIR/just${_suffix}
ln -s ${_bin_path}/uv${_suffix} $PYMONGO_BIN_DIR/uv${_suffix}
ln -s ${_bin_path}/uvx${_suffix} $PYMONGO_BIN_DIR/uvx${_suffix}
fi

View File

@ -24,10 +24,14 @@ function _pip_install() {
echo "Installing $2 using pip..."
createvirtualenv "$(find_python3)" $_VENV_PATH
python -m pip install $1
_suffix=""
if [ "Windows_NT" = "${OS:-}" ]; then
ln -s "$(which $2)" $_BIN_DIR/$2.exe
else
ln -s "$(which $2)" $_BIN_DIR/$2
_suffix=".exe"
fi
ln -s "$(which $2)" $_BIN_DIR/${2}${_suffix}
# uv also comes with a uvx binary.
if [ $2 == "uv" ]; then
ln -s "$(which uvx)" $_BIN_DIR/uvx${_suffix}
fi
echo "Installed to ${_BIN_DIR}"
echo "Installing $2 using pip... done."

View File

@ -137,7 +137,12 @@ def run_command(cmd: str | list[str], **kwargs: Any) -> None:
cmd = " ".join(cmd)
LOGGER.info("Running command '%s'...", cmd)
kwargs.setdefault("check", True)
subprocess.run(shlex.split(cmd), **kwargs) # noqa: PLW1510, S603
try:
subprocess.run(shlex.split(cmd), **kwargs) # noqa: PLW1510, S603
except subprocess.CalledProcessError as e:
LOGGER.error(e.output)
LOGGER.error(str(e))
sys.exit(e.returncode)
LOGGER.info("Running command '%s'... done.", cmd)

View File

@ -4,31 +4,29 @@ set -eu
find_python3() {
PYTHON=""
# Add a fallback system python3 if it is available and Python 3.9+.
if is_python_39 "$(command -v python3)"; then
PYTHON="$(command -v python3)"
fi
# Find a suitable toolchain version, if available.
if [ "$(uname -s)" = "Darwin" ]; then
# macos 11.00
if [ -d "/Library/Frameworks/Python.Framework/Versions/3.10" ]; then
PYTHON="/Library/Frameworks/Python.Framework/Versions/3.10/bin/python3"
# macos 10.14
elif [ -d "/Library/Frameworks/Python.Framework/Versions/3.9" ]; then
PYTHON="/Library/Frameworks/Python.Framework/Versions/3.9/bin/python3"
fi
PYTHON="/Library/Frameworks/Python.Framework/Versions/Current/bin/python3"
elif [ "Windows_NT" = "${OS:-}" ]; then # Magic variable in cygwin
PYTHON="C:/python/Python39/python.exe"
PYTHON="C:/python/Current/python.exe"
else
# Prefer our own toolchain, fall back to mongodb toolchain if it has Python 3.9+.
if [ -f "/opt/python/3.9/bin/python3" ]; then
PYTHON="/opt/python/3.9/bin/python3"
if [ -f "/opt/python/Current/bin/python3" ]; then
PYTHON="/opt/python/Current/bin/python3"
elif is_python_39 "$(command -v /opt/mongodbtoolchain/v5/bin/python3)"; then
PYTHON="/opt/mongodbtoolchain/v5/bin/python3"
elif is_python_39 "$(command -v /opt/mongodbtoolchain/v4/bin/python3)"; then
PYTHON="/opt/mongodbtoolchain/v4/bin/python3"
elif is_python_39 "$(command -v /opt/mongodbtoolchain/v3/bin/python3)"; then
PYTHON="/opt/mongodbtoolchain/v3/bin/python3"
fi
fi
# Add a fallback system python3 if it is available and Python 3.9+.
if [ -z "$PYTHON" ]; then
if is_python_39 "$(command -v python3)"; then
PYTHON="$(command -v python3)"
fi
fi
if [ -z "$PYTHON" ]; then
echo "Cannot test without python3.9+ installed!"
exit 1