Skip to content

Commit 01e023e

Browse files
committed
Detect Python version from pyproject.toml generated by Poetry
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.
1 parent f97d9ff commit 01e023e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

bin/compile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ export CACHED_PYTHON_STACK
221221
# shellcheck source=bin/steps/pipenv-python-version
222222
source "$BIN_DIR/steps/pipenv-python-version"
223223

224+
# Poetry Python version support.
225+
# Detect the version of Python requested from a pyproject.toml.
226+
# Convert it to a runtime.txt file.
227+
228+
# shellcheck source=bin/steps/poetry-python-version
229+
source "$BIN_DIR/steps/poetry-python-version"
230+
224231
# If no runtime was provided by the user, assume the default Python runtime version.
225232
if [ ! -f runtime.txt ]; then
226233
echo "$DEFAULT_PYTHON_VERSION" > runtime.txt

bin/steps/poetry-python-version

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
3+
# Determine Python version from pyproject.toml, if no runtime.txt is present.
4+
if [ ! -f $BUILD_DIR/runtime.txt ] &&
5+
[ -f $BUILD_DIR/pyproject.toml ] &&
6+
[ -f $BUILD_DIR/poetry.lock ]; then
7+
# The Python version is specified in the section [tool.poetry.dependencies]
8+
# under the `python` key.
9+
beg='^ *\[tool.poetry.dependencies\]'
10+
end='^ *\['
11+
pat='^ *python *= *"\([^"]*\)"'
12+
13+
PYTHON=$(sed -n "/$beg/,/$end/s/$pat/\\1/p" pyproject.toml)
14+
15+
# We only handle a few simple cases here. If the Python version constraints
16+
# are any more complicated, the user needs to provide an explicit
17+
# runtime.txt.
18+
case $PYTHON in
19+
'^2.7' | '^2.7.'* | '~2.7' | '~2.7.'* | '2.7.*' | "${LATEST_27#python-}")
20+
echo "$LATEST_27" > "$BUILD_DIR/runtime.txt"
21+
;;
22+
23+
'^3.4' | '^3.4.'* | '~3.4' | '~3.4.'* | '3.4.*' | "${LATEST_34#python-}")
24+
echo "$LATEST_34" > "$BUILD_DIR/runtime.txt"
25+
;;
26+
27+
'^3.5' | '^3.5.'* | '~3.5' | '~3.5.'* | '3.5.*' | "${LATEST_35#python-}")
28+
echo "$LATEST_35" > "$BUILD_DIR/runtime.txt"
29+
;;
30+
31+
'^3.6' | '^3.6.'* | '~3.6' | '~3.6.'* | '3.6.*' | "${LATEST_36#python-}")
32+
echo "$LATEST_36" > "$BUILD_DIR/runtime.txt"
33+
;;
34+
35+
'^3.7' | '^3.7.'* | '~3.7' | '~3.7.'* | '3.7.*' | "${LATEST_37#python-}")
36+
echo "$LATEST_37" > "$BUILD_DIR/runtime.txt"
37+
;;
38+
esac
39+
fi

0 commit comments

Comments
 (0)