-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathbootstrap-python
More file actions
executable file
·80 lines (70 loc) · 3.18 KB
/
Copy pathbootstrap-python
File metadata and controls
executable file
·80 lines (70 loc) · 3.18 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
#!/usr/bin/env bash
# Compile script is executed from /home/vcap and buildpack is downloaded in /tmp/buildpacks/<<something>>/
# Determine the buildpack folder based on this file
BUILDPACK_PATH=$(dirname "$0")/..
BUILD_PATH=$1
CACHE_PATH=$2
# Silence warning about outdated pip version
export PIP_DISABLE_PIP_VERSION_CHECK=1
# Bootstrap and install the Python requirements from wheels.
# Minimal packaging for the buildpack includes pip and setuptools wheels.
REQUIREMENTS_PATH="${BUILDPACK_PATH}/requirements.txt"
WHEELS_PATH="${BUILDPACK_PATH}/vendor/wheels"
PIP_WHL_PATH=$(ls $WHEELS_PATH/pip*.whl 2>/dev/null | head -n1)
PIP_VERBOSITY_FLAGS=""
if [[ -z $BUILDPACK_XTRACE ]] || [[ $BUILDPACK_XTRACE == "false" ]]
then
PIP_VERBOSITY_FLAGS="--quiet"
fi
PIP_WHEELDIR="${CACHE_PATH}/pip/wheels"
mkdir -p $PIP_WHEELDIR
# Set the correct Python site-packages directory to install to
SITE_PACKAGES_PATH="${BUILD_PATH}/$(python3 -m site --user-site | cut -d '/' -f4-)"
# Invalidate the cache and copy over wheels if they are bundled
# If no wheels are included, the cache will be left alone, as dependencies are downloaded
echo " ---> Check if Python dependencies need to be cached..."
if [[ $(ls -1q $WHEELS_PATH/* 2>/dev/null | wc -l) -gt 0 ]] && [[ $(diff $WHEELS_PATH $PIP_WHEELDIR 2>/dev/null | wc -l) -gt 0 ]]
then
echo " ---> Invalidating Python wheels cache..."
rm -rf $PIP_WHEELDIR
echo " ---> Copying bundled Python dependencies to cache...";
mkdir -p $PIP_WHEELDIR
cp -rf $WHEELS_PATH/* $PIP_WHEELDIR/ 2>/dev/null
fi
echo " ---> Bootstrapping pip..."
if [ "$CF_STACK" == "cflinuxfs4" ]; then
PIP_CMD="pip3"
else
PIP_CMD="pip"
fi
if ! [[ -x "$(command -v ${PIP_CMD})" ]]
then
# pip is not included on the root FS
if [[ -n "${PIP_WHL_PATH}" ]]
then
# Bootstrap wheels are bundled in the buildpack
python3 ${PIP_WHL_PATH}/pip install $PIP_VERBOSITY_FLAGS --user --no-warn-script-location --no-index --no-build-isolation --find-links=$PIP_WHEELDIR --use-pep517 pip setuptools wheel
else
# Bootstrap wheels are not bundled in the buildpack
curl -sS https://bootstrap.pypa.io/pip/get-pip.py -o get-pip.py
python3 get-pip.py install $PIP_VERBOSITY_FLAGS --user --no-warn-script-location
fi
PIP_CMD="/home/vcap/.local/bin/pip"
else
# pip is bundled on the root FS
if [ "$CF_STACK" == "cflinuxfs4" ]; then
PIP_CMD="python3 $(dirname "$(command -v python3)")/pip3"
fi
$PIP_CMD install --upgrade --user --no-warn-script-location --use-pep517 pip setuptools wheel
fi
# Only download Python dependencies if they are not bundled
if [[ $(ls -1q $WHEELS_PATH/* 2>/dev/null | wc -l) -gt 0 ]]
then
echo " ---> Using bundled Python dependencies"
else
echo " ---> Downloading Python dependencies..."
$PIP_CMD download $PIP_VERBOSITY_FLAGS --use-pep517 -r $REQUIREMENTS_PATH --prefer-binary -d $PIP_WHEELDIR
fi
echo " ---> Installing Python dependencies to ${SITE_PACKAGES_PATH}..."
$PIP_CMD install $PIP_VERBOSITY_FLAGS --target $SITE_PACKAGES_PATH --prefer-binary --no-warn-script-location --no-index --no-build-isolation --find-links=$PIP_WHEELDIR --use-pep517 -r $REQUIREMENTS_PATH
echo " ---> Finished installing Python dependencies"