-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild
More file actions
executable file
·104 lines (94 loc) · 3.9 KB
/
build
File metadata and controls
executable file
·104 lines (94 loc) · 3.9 KB
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
103
104
#!/usr/bin/env bash
set -eo pipefail
echo "---> Python Buildpack"
# 1. GET ARGS
layers_dir=$1
plan_path=$3
arch=$(dpkg --print-architecture) # amd64 arm64
# 2. DOWNLOAD PYTHON
python_layer_dir="${layers_dir}/python"
mkdir -p "${python_layer_dir}"
# determine python version provided during detection
python_version=$(< "${plan_path}" yj -t | jq -r '.entries[] | select(.name == "python") | .version')
remote_python_version="not found"
if [[ -f "${python_layer_dir}.toml" ]]; then
remote_python_version=$(< "${python_layer_dir}.toml" yj -t | jq -r .metadata 2>/dev/null || echo 'not found')
fi
if [[ "${python_version}" == "${remote_python_version}" ]] ; then
echo "---> Reusing python"
else
echo "---> Downloading and extracting Python ${python_version}"
python_url="https://buildpacks.drycc.cc/python/${CNB_STACK_ID}/python-${python_version}-${arch}.tar.gz"
wget -q -O - "${python_url}" | tar -xzf - -C "${python_layer_dir}"
cat > "${python_layer_dir}.toml" <<EOL
cache = true
launch = true
metadata = "${python_version}"
EOL
# shellcheck disable=SC2006
for file in `(ls "${python_layer_dir}/bin/")`
do
if [ "${file}" == 'pip' ]; then
sed -ir 's/\#\!\/usr\/local\/bin\/python/\#\!\/usr\/bin\/env\ python/g' "${python_layer_dir}/bin/${file}"
fi
done
if [ ! -s "${python_layer_dir}"/profile.d/python.sh ]; then
mkdir "${python_layer_dir}"/profile.d
cat > "${python_layer_dir}"/profile.d/python.sh <<EOL
export PATH="${python_layer_dir}/bin:${PATH}"
export C_INCLUDE_PATH="${python_layer_dir}/include:${C_INCLUDE_PATH}"
export CPLUS_INCLUDE_PATH="${python_layer_dir}/include:${C_INCLUDE_PATH}"
export LIBRARY_PATH="${python_layer_dir}/lib:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${python_layer_dir}/lib:${LD_LIBRARY_PATH}"
export PKG_CONFIG_PATH="${python_layer_dir}/lib/pkg-config:${PKG_CONFIG_PATH}"
EOL
fi
fi
rm -f "${python_layer_dir}"/profile.d/pip.sh
if [ "${PIP_INDEX_URL}" ]; then
tee >> "${python_layer_dir}"/profile.d/pip.sh << EOF
python -m pip config set global.index-url "${PIP_INDEX_URL}"
EOF
fi
if [ "${PIP_EXTRA_INDEX_URL}" ]; then
tee >> "${python_layer_dir}"/profile.d/pip.sh << EOF
python -m pip config set global.extra-index-url "${PIP_INDEX_URL}"
EOF
fi
# 3. MAKE python and pip AVAILABLE TO THIS SCRIPT
export PATH="${python_layer_dir}/bin:${PATH}"
export C_INCLUDE_PATH="${python_layer_dir}/include:${C_INCLUDE_PATH}"
export CPLUS_INCLUDE_PATH="${python_layer_dir}/include:${C_INCLUDE_PATH}"
export LIBRARY_PATH="${python_layer_dir}/lib:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH="${python_layer_dir}/lib:${LD_LIBRARY_PATH}"
export PKG_CONFIG_PATH="${python_layer_dir}/lib/pkg-config:${PKG_CONFIG_PATH}"
# Compares previous requirements checksum to the current requirements
requirements_layer_dir="${layers_dir}/requirements"
local_requirements_checksum=$(sha256sum requirements.txt | cut -d ' ' -f 1 || echo 'not found')
remote_requirements_checksum='not found'
if [[ -f "${requirements_layer_dir}.toml" ]]; then
remote_requirements_checksum=$(< "${requirements_layer_dir}.toml" yj -t | jq -r .metadata 2>/dev/null || echo 'not found')
fi
mkdir -p "${requirements_layer_dir}"
if [ "${PIP_INDEX_URL}" ]; then
echo "---> Setting pip index-url with ${PIP_INDEX_URL}"
python -m pip config set global.index-url "${PIP_INDEX_URL}"
fi
if [ "${PIP_EXTRA_INDEX_URL}" ]; then
echo "---> Setting pip extra-index-url with ${PIP_EXTRA_INDEX_URL}"
python -m pip config set global.extra-index-url "${PIP_EXTRA_INDEX_URL}"
fi
if [[ -f requirements.txt && "${local_requirements_checksum}" == "${remote_requirements_checksum}" ]] ; then
echo "---> Reusing requirements"
else
echo "---> Installing requirements with pip"
python -m pip install -r requirements.txt \
--exists-action=w --src="${python_layer_dir}"/src \
--disable-pip-version-check --no-cache-dir
cat > "${requirements_layer_dir}.toml" <<EOL
cache = true
launch = true
metadata = "${local_requirements_checksum}"
EOL
fi
generate-launch.sh "${layers_dir}"