Skip to content

Commit

Permalink
Detect Python version from pyproject.toml generated by Poetry
Browse files Browse the repository at this point in the history
If no runtime.txt is present, generate it using the pyproject.toml file
generated by Poetry. The Python version is specified in the section
`[tool.poetry.dependencies]` under the `python` key.

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.
  • Loading branch information
cjolowicz committed Oct 3, 2019
1 parent f97d9ff commit 01e023e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions bin/steps/poetry-python-version
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 01e023e

Please sign in to comment.