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
8 changes: 4 additions & 4 deletions .evergreen/scripts/configure-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fi
# Add the latest MongoDB toolchain bin dir to PATH if it exists, so that hosts
# with an old system Python (e.g. RHEL8's 3.6) still get a modern interpreter
# for tool installs like `uv tool install rust-just`. It goes after
# PYMONGO_BIN_DIR so the pinned uv (installed there by setup_uv.py) takes
# precedence over the toolchain's uv.
# PYMONGO_BIN_DIR so the project's uv takes precedence.
if [ "Windows_NT" = "${OS:-}" ]; then
_toolchain_bin="/cygdrive/c/Python/Current/Scripts"
elif [ "$(uname -s)" == "Darwin" ]; then
Expand Down Expand Up @@ -106,8 +105,9 @@ if [ "$DRIVERS_TOOLS" = "$PROJECT_DIRECTORY/drivers-evergreen-tools" ]; then

# Write a uv config boundary into the submodule: it is vendored inside this
# project, so uv run by the tools' own scripts would otherwise walk up to
# pyproject.toml and enforce our required-version pin. Write-if-absent, so an
# upstream uv.toml fails the submodule update loudly instead of being clobbered.
# pyproject.toml and apply this project's [tool.uv] settings.
# Write-if-absent, so an upstream uv.toml fails the submodule update loudly
# instead of being clobbered.
if [ -d "${DRIVERS_TOOLS}" ] && [ ! -f "${DRIVERS_TOOLS}/uv.toml" ]; then
cat <<EOT > "${DRIVERS_TOOLS}/uv.toml"
# Written by mongo-python-driver to stop uv's config discovery here; see
Expand Down
7 changes: 4 additions & 3 deletions .evergreen/scripts/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fi
export UV_TOOL_BIN_DIR="${UV_TOOL_BIN_DIR:-$PYMONGO_BIN_DIR}"

# Ensure the bin dir is on PATH: hosts without env.sh (e.g. auth-aws-ecs) never
# export it, so a fresh pinned install there would be invisible to the probe
# export it, so a fresh uv install there would be invisible to the probe
# below and to later steps like `uv tool install` and `uv sync`.
case ":$PATH:" in
*":$PYMONGO_BIN_DIR_POSIX:"*) ;;
Expand All @@ -48,8 +48,9 @@ if [ "Windows_NT" = "${OS:-}" ]; then
_uv_setup_script="$(cygpath -m "$_uv_setup_script")"
fi

# Skip setup when the pinned uv is already installed: `setup_uv.py --check`
# compares the uv on PATH with the required-version pin, without side effects.
# Skip setup when a suitable uv is already installed: `setup_uv.py --check`
# verifies the required-version pin, or the project-controlled uv in
# UV_TOOL_BIN_DIR when unpinned, without side effects.
# A full `uv sync` here would download Python and install/build dependencies
# just to make this decision, and would run before setup-uv-python.sh sets
# UV_PYTHON, creating the environment twice on local dev.
Expand Down
2 changes: 1 addition & 1 deletion .evergreen/scripts/setup-dev-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if [ -f $HERE/test-env.sh ]; then
. $HERE/test-env.sh
fi

# The bin dir for the pinned uv/just. setup-system.sh sets it on evergreen hosts;
# The bin dir for uv/just. setup-system.sh sets it on evergreen hosts;
# default it here so local dev (without setup-system.sh) also has a usable value.
# Native (Windows) form on cygwin, like install-dependencies.sh.
export PYMONGO_BIN_DIR="${PYMONGO_BIN_DIR:-$HOME/.local/bin}"
Expand Down
42 changes: 38 additions & 4 deletions .evergreen/scripts/setup-uv-python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# UV_PYTHON_PREFERENCE - "system" so the toolchain wins over managed installs.
# A preference set by the task is left alone.
# UV_PYTHON - the Python interpreter uv uses; defaults to CPython
# 3.11 when the task does not set one.
# 3.11, or the toolchain interpreter's path when the
# toolchain has the requested version.
set -euo pipefail

HERE=$(dirname "${BASH_SOURCE:-$0}")
Expand Down Expand Up @@ -48,12 +49,17 @@ fi
# uv version in pyproject.toml.
export UV_PYTHON="${UV_PYTHON:-3.11}"

# Print the Python toolchain bin dir for a version like "3.11" or "3.14t".
function _toolchain_dir() {
# Print the Python toolchain bin dir for a version like "3.11" or "3.14t",
# without checking that it exists.
function _toolchain_bin_dir() {
local version="$1" dir
# Only plain CPython versions live in the toolchain.
[[ "$version" =~ ^[0-9]+\.[0-9]+t?$ ]] || return 1
if [ "Windows_NT" = "${OS:-}" ]; then
if [ -n "${PYMONGO_TOOLCHAIN_ROOT:-}" ]; then
# Test override of the unix toolchain root; the <version>/bin layout under
# it is unchanged.
dir="${PYMONGO_TOOLCHAIN_ROOT}/${version}/bin"
elif [ "Windows_NT" = "${OS:-}" ]; then
local winver="${version%t}"
winver="${winver//./}"
if [ -n "${IS_WIN32:-}" ]; then
Expand All @@ -70,6 +76,25 @@ function _toolchain_dir() {
else
dir="/opt/python/${version}/bin"
fi
echo "$dir"
}

# Print the toolchain interpreter file for a version like "3.15t", without
# checking that it exists. Windows interpreters are exe files: native uv
# needs the exact file name.
function _interpreter_file() {
local dir="$1" version="$2"
if [ "Windows_NT" = "${OS:-}" ]; then
echo "$dir/python${version}.exe"
else
echo "$dir/python${version}"
fi
}

# Print the toolchain bin dir for the version when it exists on this host.
function _toolchain_dir() {
local dir
dir=$(_toolchain_bin_dir "$1") || return 1
[ -d "$dir" ] || return 1
echo "$dir"
}
Expand Down Expand Up @@ -115,6 +140,15 @@ if [[ "$UV_PYTHON" == */* || "$UV_PYTHON" == *\\* ]]; then
_skip_ensure=1
elif _dir=$(_toolchain_dir "$UV_PYTHON"); then
_search_path="$_dir"
# Use the toolchain interpreter by explicit path when the task did not set its
# own preference (PYTHON-6135): uv uses the path as-is, so provisioning cannot
# fail on uv's interpreter discovery (it aborts on interpreters it cannot
# inspect, like RHEL8's /usr/bin/python3 3.6) or on a missing download.
_file=$(_interpreter_file "$_dir" "$UV_PYTHON")
if [ -z "${UV_PYTHON_PREFERENCE:-}" ] && [ -x "$_file" ]; then
export UV_PYTHON="$_file"
_skip_ensure=1
fi
fi

# Point uv at the toolchain Python when there is one and the task did not set
Expand Down
90 changes: 74 additions & 16 deletions .evergreen/scripts/setup_uv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/usr/bin/env python3
"""Bootstrap the pinned uv/just for the test environment.
"""Bootstrap uv/just for the test environment.

install-dependencies.sh probes with `setup_uv.py --check`, sources ensure-uv.sh
(which finds or installs uv), then runs this script for the rest. Only the
standard library is used.

The project always controls a uv in UV_TOOL_BIN_DIR: the pinned version when
[tool.uv] required-version is set, the latest uv otherwise. A uv found
elsewhere on PATH may be invisible to later steps, so setup never accepts it
as-is.

This runs under the host's system python3, which may be older than our minimum supported version.
"""

Expand All @@ -26,7 +31,7 @@


def required_uv_pin() -> str:
"""Return [tool.uv] required-version, e.g. '==0.12.12' ('' if absent)."""
"""Return [tool.uv] required-version, e.g. '==1.2.3' ('' if absent)."""
pattern = re.compile(r"required-version\s*=\s*['\"]?([^'\"\s]+)['\"]?")
in_uv = False
for line in (ROOT / "pyproject.toml").read_text().splitlines():
Expand Down Expand Up @@ -65,21 +70,25 @@ def _install_uv_astral() -> None:
_add_path(os.environ["UV_TOOL_BIN_DIR"])


def _pin_uv(uv_pin: str) -> None:
"""Install the pinned uv via uv tool install --force.
def _uv_exe() -> str:
"""The uv binary name on this platform."""
return "uv.exe" if os.name == "nt" else "uv"


UV_TOOL_BIN_DIR and UV_TOOL_DIR are inherited from the environment (set by
install-dependencies.sh / ensure-uv.sh, in native form on Windows). uv tool
install writes the binary into UV_TOOL_BIN_DIR and the tool venv into
UV_TOOL_DIR; --force lets it overwrite an existing install.
def _install_uv(pin: str) -> None:
"""Install uv into UV_TOOL_BIN_DIR with `uv tool install --force`.

Windows will not overwrite a running executable, so when the current uv is
already the target bin dir, run the install from a copy of the binary.
Installs the pinned version when pin is set ("==0.12.12"), the latest uv
otherwise (pin ""). Windows will not overwrite a running executable, so
when the uv driving the install is the binary being replaced, run the
install from a copy of it.
"""
uv_path = shutil.which("uv")
bin_dir = os.environ["UV_TOOL_BIN_DIR"]
overwrite_self = uv_path is not None and _same_path(uv_path, str(Path(bin_dir, _uv_exe())))
tool = uv_path
tmp_uv = None
if os.name == "nt":
if overwrite_self:
tmp_uv = Path(tempfile.mkdtemp()) / Path(uv_path).name
shutil.copy2(uv_path, tmp_uv)
tool = str(tmp_uv)
Expand All @@ -93,7 +102,7 @@ def _pin_uv(uv_pin: str) -> None:
"-q",
"--force",
"--from",
f"uv{uv_pin}",
f"uv{pin}",
"uv",
],
check=True,
Expand Down Expand Up @@ -133,9 +142,51 @@ def _write_env() -> None:
ENV_SH.write_bytes(("\n".join(keep) + "\n").encode())


def _pinned_uv_installed() -> bool:
"""Return True when the uv on PATH already matches the required-version pin."""
def _cygdrive(path: str) -> str:
"""Map a native Windows path to its cygwin /cygdrive form."""
match = re.match(r"^([A-Za-z]):[/\\](.*)$", path)
if not match:
return path
drive, rest = match.groups()
return "/cygdrive/" + drive.lower() + "/" + rest.replace("\\", "/")


def _same_path(a: str, b: str) -> bool:
"""Compare two paths, tolerating forms realpath cannot unify.

On cygwin, which() may report a /cygdrive form while UV_TOOL_BIN_DIR is
native ("C:/...").
"""
try:
if os.path.realpath(a) == os.path.realpath(b):
return True
except OSError:
pass
return any(
os.path.normcase(os.path.normpath(form_a)) == os.path.normcase(os.path.normpath(form_b))
for form_a in (a, _cygdrive(a))
for form_b in (b, _cygdrive(b))
)


def _project_uv_installed() -> bool:
"""Return True when the uv on PATH is the one this setup put in UV_TOOL_BIN_DIR."""
uv_path = shutil.which("uv")
bin_dir = os.environ.get("UV_TOOL_BIN_DIR")
if not uv_path or not bin_dir:
return False
return _same_path(uv_path, str(Path(bin_dir, _uv_exe())))


def _required_uv_installed() -> bool:
"""Return True when the uv on PATH already satisfies the setup requirement.

With a required-version pin, the uv on PATH must match it exactly; without
a pin, it must be the project-controlled uv in UV_TOOL_BIN_DIR.
"""
pin = required_uv_pin()
if not pin:
return _project_uv_installed()
if not pin.startswith("=="):
# Only exact pins are checked here; anything else falls back to setup.
return False
Expand All @@ -154,7 +205,7 @@ def main() -> int:
# there would download Python and install dependencies just to decide
# whether setup is needed.
if "--check" in sys.argv[1:]:
return 0 if _pinned_uv_installed() else 1
return 0 if _required_uv_installed() else 1

bin_dir = os.environ["UV_TOOL_BIN_DIR"]
Path(bin_dir).mkdir(parents=True, exist_ok=True)
Expand All @@ -166,7 +217,14 @@ def main() -> int:

uv_pin = required_uv_pin()
if uv_pin:
_pin_uv(uv_pin)
_install_uv(uv_pin)
elif not _project_uv_installed():
# Unpinned: still install the latest uv into UV_TOOL_BIN_DIR, so the
# project controls the uv that later steps find on PATH. A uv found
# elsewhere (the vendored submodule's, the host's) may be missing from
# those steps' PATH entirely, and may lack behavior the project relies
# on, such as provisioning newer CPythons.
_install_uv("")

_write_env()
return 0
Expand Down
Loading
Loading