| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 4 | # |
| 5 | # Pick a Python interpreter for buildkite scripts: prefer a local |
| 6 | # ``python3`` if it is recent enough (>= 3.12), otherwise fall back to |
| 7 | # a one-shot Docker container running ``python:3-slim``. After |
| 8 | # ``select_python`` returns, ``$PYTHON`` is set in the caller's shell |
| 9 | # and is safe to use as a command (e.g. ``$PYTHON some_script.py``). |
| 10 | # |
| 11 | # The 3.12 threshold matches what the existing nightly-index work |
| 12 | # expects -- typing features used by ``generate-nightly-index.py``. |
| 13 | # This helper does not pin the *minor* version; if you need stricter |
| 14 | # reproducibility (e.g. relying on auditwheel internals), invoke |
| 15 | # Docker yourself with a pinned tag rather than calling this. |
| 16 | |
| 17 | if [[ -n "${_SELECT_PYTHON_LIB_SOURCED:-}" ]]; then |
| 18 | return 0 |
| 19 | fi |
| 20 | _SELECT_PYTHON_LIB_SOURCED=1 |
| 21 | |
| 22 | # Sets ``PYTHON`` in the caller's shell and exports it. Idempotent -- |
| 23 | # calling twice is safe and the second call simply re-runs the probe. |
| 24 | select_python() { |
| 25 | local py="${PYTHON_PROG:-python3}" |
| 26 | local has_new_python |
| 27 | has_new_python=$("$py" -c \ |
| 28 | "print(1 if __import__('sys').version_info >= (3,12) else 0)" \ |
| 29 | 2>/dev/null || echo 0) |
| 30 | if [[ "$has_new_python" -eq 0 ]]; then |
| 31 | # ``-u $(id -u):$(id -g)`` so files created via the container |
| 32 | # end up owned by the host user, not root. |
| 33 | docker pull python:3-slim |
| 34 | PYTHON="docker run --rm -u $(id -u):$(id -g) -v $(pwd):/app -w /app python:3-slim python3" |
| 35 | else |
| 36 | PYTHON="$py" |
| 37 | fi |
| 38 | export PYTHON |
| 39 | echo "Using python interpreter: $PYTHON" |
| 40 | echo "Python version: $($PYTHON --version)" |
| 41 | } |