diff --git a/bin/compile b/bin/compile index 64eca306f..06794d3f8 100755 --- a/bin/compile +++ b/bin/compile @@ -221,6 +221,13 @@ export CACHED_PYTHON_STACK # shellcheck source=bin/steps/pipenv-python-version source "$BIN_DIR/steps/pipenv-python-version" +# Poetry Python version support. +# Detect the version of Python requested from a pyproject.toml. +# Convert it to a runtime.txt file. + +# shellcheck source=bin/steps/poetry-python-version +source "$BIN_DIR/steps/poetry-python-version" + # If no runtime was provided by the user, assume the default Python runtime version. if [ ! -f runtime.txt ]; then echo "$DEFAULT_PYTHON_VERSION" > runtime.txt diff --git a/bin/steps/poetry-python-version b/bin/steps/poetry-python-version new file mode 100644 index 000000000..6a19f987c --- /dev/null +++ b/bin/steps/poetry-python-version @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +# Determine Python version from pyproject.toml, if no runtime.txt is present. +if [ ! -f $BUILD_DIR/runtime.txt ] && + [ -f $BUILD_DIR/pyproject.toml ] && + [ -f $BUILD_DIR/poetry.lock ]; then + # The Python version is specified in the section [tool.poetry.dependencies] + # under the `python` key. + beg='^ *\[tool.poetry.dependencies\]' + end='^ *\[' + pat='^ *python *= *"\([^"]*\)"' + + PYTHON=$(sed -n "/$beg/,/$end/s/$pat/\\1/p" pyproject.toml) + + # We only handle a few simple cases here. If the Python version constraints + # are any more complicated, the user needs to provide an explicit + # runtime.txt. + case $PYTHON in + '^2.7' | '^2.7.'* | '~2.7' | '~2.7.'* | '2.7.*' | "${LATEST_27#python-}") + echo "$LATEST_27" > "$BUILD_DIR/runtime.txt" + ;; + + '^3.4' | '^3.4.'* | '~3.4' | '~3.4.'* | '3.4.*' | "${LATEST_34#python-}") + echo "$LATEST_34" > "$BUILD_DIR/runtime.txt" + ;; + + '^3.5' | '^3.5.'* | '~3.5' | '~3.5.'* | '3.5.*' | "${LATEST_35#python-}") + echo "$LATEST_35" > "$BUILD_DIR/runtime.txt" + ;; + + '^3.6' | '^3.6.'* | '~3.6' | '~3.6.'* | '3.6.*' | "${LATEST_36#python-}") + echo "$LATEST_36" > "$BUILD_DIR/runtime.txt" + ;; + + '^3.7' | '^3.7.'* | '~3.7' | '~3.7.'* | '3.7.*' | "${LATEST_37#python-}") + echo "$LATEST_37" > "$BUILD_DIR/runtime.txt" + ;; + esac +fi