/
compile
executable file
·102 lines (75 loc) · 3.07 KB
/
compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env bash
set -euo pipefail
function log() {
echo "-----> $*"
}
function indent() {
sed -e 's/^/ /'
}
export BUILD_DIR="$1"
export ENV_DIR="$3"
BUILDPACK_VARIABLES="DISABLE_POETRY_CREATE_RUNTIME_FILE PYTHON_RUNTIME_VERSION"
shopt -s nullglob
for env_file in "$ENV_DIR"/POETRY_* ; do
export "$(basename "$env_file")=$(cat "$env_file" 2>/dev/null)"
done
for env_file in $BUILDPACK_VARIABLES ; do
[ -f "$ENV_DIR/$env_file" ] && export "$(basename "$env_file")=$(cat "$ENV_DIR/$env_file" 2>/dev/null)"
done
if [ -z "${POETRY_VERSION:-}" ] ; then
export POETRY_VERSION=1.1.13
log "No Poetry version specified in POETRY_VERSION config var. Defaulting to $POETRY_VERSION."
else
log "Using Poetry version from POETRY_VERSION config var: $POETRY_VERSION"
fi
log "Generate requirements.txt with Poetry"
log "Install Poetry"
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py \
| sed -e 's/allowed_executables = \["python", "python3"\]/allowed_executables = ["python3"]/' \
| python3 \
| indent
PATH_TO_POETRY_ENV="$HOME/.poetry/env"
if [ -f "$PATH_TO_POETRY_ENV" ] ; then
# shellcheck source=/dev/null
. "$PATH_TO_POETRY_ENV"
fi
REQUIREMENTS_FILE="requirements.txt"
log "Export $REQUIREMENTS_FILE from Poetry"
cd "$BUILD_DIR"
EXPORT_PARAMETERS=(--without-hashes --with-credentials)
if [ "${POETRY_EXPORT_DEV_REQUIREMENTS:-0}" != "0" ] ; then
log "Enable exporting dev requirements to $REQUIREMENTS_FILE"
EXPORT_PARAMETERS+=(--dev)
fi
# pip can't handle vcs & editable dependencies when requiring hashes (https://github.com/pypa/pip/issues/4995)
# poetry exports local dependencies as editable by default (https://github.com/python-poetry/poetry/issues/897)
poetry export -f "$REQUIREMENTS_FILE" "${EXPORT_PARAMETERS[@]}" > "$REQUIREMENTS_FILE.orig"
sed -e 's/^-e //' < "$REQUIREMENTS_FILE.orig" > "$REQUIREMENTS_FILE" && rm "$REQUIREMENTS_FILE.orig"
RUNTIME_FILE="runtime.txt"
log "Export Python version from Poetry to Heroku $RUNTIME_FILE file"
if [ "${DISABLE_POETRY_CREATE_RUNTIME_FILE:-0}" != "0" ] ; then
log "Skip generation of $RUNTIME_FILE file from poetry.lock"
exit 0
fi
if [ -f "$RUNTIME_FILE" ] ; then
log "$RUNTIME_FILE found, delete this file from your repository!" >&2
exit 1
fi
if [ -z "${PYTHON_RUNTIME_VERSION:-}" ] ; then
log "Read Python version from poetry.lock"
PYTHON_RUNTIME_VERSION="$(sed -n -e '/^\[metadata\]/,/^\[/p' poetry.lock | sed -n -e 's/^python-versions\s*=\s*//p' | tr -d \"\')"
else
log "Force Python version to $PYTHON_RUNTIME_VERSION, because PYTHON_RUNTIME_VERSION is set!"
fi
# Version is only valid if exact interpreter version is specified
#
# ^3.8 -> not valid
# 3.8 -> not valid
# 3.8.1 -> valid
if [[ "$PYTHON_RUNTIME_VERSION" =~ ^[2-9](\.[0-9]+){2}$ ]] ; then
log "Write $PYTHON_RUNTIME_VERSION into $RUNTIME_FILE"
echo "python-$PYTHON_RUNTIME_VERSION" > "$RUNTIME_FILE"
else
log "$PYTHON_RUNTIME_VERSION is not valid, please specify an exact Python version (e.g. 3.8.1) in your pyproject.toml (and thus poetry.lock)" >&2
exit 1
fi