forked from cloudfoundry/python-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile
More file actions
executable file
·280 lines (217 loc) · 8.74 KB
/
compile
File metadata and controls
executable file
·280 lines (217 loc) · 8.74 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
# The Cloud Foundry Python Buildpack. This script accepts parameters for a build
# directory, a cache directory, and a directory for app environment variables.
# Usage:
#
# $ bin/compile <build-dir> <cache-dir> <env-path>
# Fail fast and fail hard.
set -eo pipefail
[ "$BUILDPACK_XTRACE" ] && set -o xtrace
# Prepend proper path for virtualenv hackery. This will be deprecated soon.
export PATH=:/usr/local/bin:$PATH
# Paths.
BIN_DIR=$(cd $(dirname $0); pwd) # absolute path
ROOT_DIR=$(dirname $BIN_DIR)
BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3
$ROOT_DIR/compile-extensions/bin/check_buildpack_version $ROOT_DIR $CACHE_DIR
# Use miniconda if environment.yml exists and exit
if [ -f $BUILD_DIR/environment.yml ]; then
echo "----------------- USING CONDA BUILDPACK -----------------"
$BIN_DIR/steps/conda-install $BUILD_DIR $CACHE_DIR
$ROOT_DIR/compile-extensions/bin/store_buildpack_metadata $ROOT_DIR $CACHE_DIR
exit 0
fi
$ROOT_DIR/compile-extensions/bin/check_stack_support
# The ROOT_DIR is read only so we can't install dependencies in it
# Move the ROOT_DIR contents to tmp and point ROOT_DIR to the new root
TMP_ROOT=/tmp/buildpack
rm -rf $TMP_ROOT
cp -r $ROOT_DIR $TMP_ROOT
ROOT_DIR=$TMP_ROOT
cd $ROOT_DIR
# Cloud Foundry does not have support for Logplex.
function bpwatch() {
:
}
# CF Common
BUILDPACK_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"
export BUILDPACK_PATH
source $BUILDPACK_PATH/compile-extensions/lib/common
# END CF Common
# Static configurations for virtualenv caches.
VIRTUALENV_LOC=".heroku/venv"
LEGACY_TRIGGER="lib/python2.7"
DEFAULT_PYTHON_VERSION="python-$($BUILDPACK_PATH/compile-extensions/bin/default_version_for $BUILDPACK_PATH/manifest.yml python)"
PYTHON_EXE="/app/.heroku/python/bin/python"
PIP_VERSION="9.0.1"
SETUPTOOLS_VERSION="32.1.0"
# Common Problem Warnings
export WARNINGS_LOG=$(mktemp)
export RECOMMENDED_PYTHON_VERSION=$DEFAULT_PYTHON_VERSION
# Setup bpwatch
export PATH=$PATH:$ROOT_DIR/vendor/bpwatch
LOGPLEX_KEY="t.b90d9d29-5388-4908-9737-b4576af1d4ce"
export BPWATCH_STORE_PATH=$CACHE_DIR/bpwatch.json
BUILDPACK_VERSION=v28
# Setup pip-pop (pip-diff)
export PATH=$PATH:$ROOT_DIR/vendor/pip-pop
# Support Anvil Build_IDs
[ ! "$SLUG_ID" ] && SLUG_ID="defaultslug"
[ ! "$REQUEST_ID" ] && REQUEST_ID=$SLUG_ID
[ ! "$STACK" ] && STACK=$DEFAULT_PYTHON_STACK
# Sanitizing environment variables.
unset GIT_DIR PYTHONHOME PYTHONPATH
unset RECEIVE_DATA RUN_KEY BUILD_INFO DEPLOY LOG_TOKEN DYNO
unset CYTOKINE_LOG_FILE GEM_PATH
# Setup buildpack instrumentation.
bpwatch init $LOGPLEX_KEY
bpwatch build python $BUILDPACK_VERSION $REQUEST_ID
bpwatch start compile
# Syntax sugar.
source $BIN_DIR/utils
# Import collection of warnings.
source $BIN_DIR/warnings
# we need to put a bunch of symlinks in there later
mkdir -p /app/.heroku
# Set up outputs under new context
PROFILE_PATH="$BUILD_DIR/.profile.d/python.sh"
EXPORT_PATH="$BIN_DIR/../export"
GUNICORN_PROFILE_PATH="$BUILD_DIR/.profile.d/python.gunicorn.sh"
# We'll need to send these statics to other scripts we `source`.
export BUILD_DIR CACHE_DIR BIN_DIR PROFILE_PATH EXPORT_PATH
# Prepend proper environment variables for Python use.
export PATH=/app/.heroku/python/bin:/app/.heroku/vendor/bin:$PATH
export PYTHONUNBUFFERED=1
export LANG=en_US.UTF-8
export C_INCLUDE_PATH=/app/.heroku/vendor/include:/app/.heroku/python/include:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/app/.heroku/vendor/include:/app/.heroku/python/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/app/.heroku/vendor/lib:/app/.heroku/python/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/app/.heroku/vendor/lib:/app/.heroku/python/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/app/.heroku/vendor/lib/pkg-config:/app/.heroku/python/lib/pkg-config:$PKG_CONFIG_PATH
# Switch to the repo's context.
cd $BUILD_DIR
# Warn for lack of Procfile.
if [[ ! -f Procfile ]]; then
puts-warn 'Warning: Your application is missing a Procfile. This file tells Cloud Foundry how to run your application.'
puts-warn 'Learn more: https://docs.cloudfoundry.org/buildpacks/prod-server.html#procfile'
fi
# Experimental pre_compile hook.
bpwatch start pre_compile
source $BIN_DIR/steps/hooks/pre_compile
bpwatch stop pre_compile
# If no requirements.txt file given, assume `setup.py develop` is intended.
if [ ! -f requirements.txt ]; then
echo "-e ." > requirements.txt
fi
# Sticky runtimes.
if [ -f $CACHE_DIR/.heroku/python-version ]; then
DEFAULT_PYTHON_VERSION=$(cat $CACHE_DIR/.heroku/python-version)
fi
# Stack fallback for non-declared caches.
if [ -f $CACHE_DIR/.heroku/python-stack ]; then
CACHED_PYTHON_STACK=$(cat $CACHE_DIR/.heroku/python-stack)
else
CACHED_PYTHON_STACK=$STACK
fi
# If no runtime given, assume default version.
if [ ! -f runtime.txt ]; then
echo $DEFAULT_PYTHON_VERSION > runtime.txt
fi
# Prepare the cache.
mkdir -p $CACHE_DIR
# Purge "old-style" virtualenvs.
bpwatch start clear_old_venvs
[ -d $CACHE_DIR/$LEGACY_TRIGGER ] && rm -fr $CACHE_DIR/.heroku/bin $CACHE_DIR/.heroku/lib $CACHE_DIR/.heroku/include
[ -d $CACHE_DIR/$VIRTUALENV_LOC ] && rm -fr $CACHE_DIR/.heroku/venv $CACHE_DIR/.heroku/src
bpwatch stop clear_old_venvs
# Restore old artifacts from the cache.
bpwatch start restore_cache
mkdir -p .heroku
cp -R $CACHE_DIR/.heroku/python .heroku/ &> /dev/null || true
cp -R $CACHE_DIR/.heroku/python-stack .heroku/ &> /dev/null || true
cp -R $CACHE_DIR/.heroku/python-version .heroku/ &> /dev/null || true
cp -R $CACHE_DIR/.heroku/vendor .heroku/ &> /dev/null || true
cp -R $CACHE_DIR/.heroku/venv .heroku/ &> /dev/null || true
if [[ -d $CACHE_DIR/.heroku/src ]]; then
cp -R $CACHE_DIR/.heroku/src .heroku/ &> /dev/null || true
fi
bpwatch stop restore_cache
mkdir -p $(dirname $PROFILE_PATH)
mkdir -p /app/.heroku/src
if [[ $BUILD_DIR != '/app' ]]; then
# python expects to reside in /app, so set up symlinks
# we will not remove these later so subsequent buildpacks can still invoke it
ln -nsf $BUILD_DIR/.heroku/python /app/.heroku/python
ln -nsf $BUILD_DIR/.heroku/vendor /app/.heroku/vendor
ln -nsf $BUILD_DIR/.heroku/venv /app/.heroku/venv
# Note: .heroku/src is copied in later.
fi
# Install Python.
source $BIN_DIR/steps/python
# Sanity check for setuptools/distribute.
source $BIN_DIR/steps/setuptools
# Uninstall removed dependencies with Pip.
source $BIN_DIR/steps/pip-uninstall
# Mercurial support.
source $BIN_DIR/steps/mercurial
# Pylibmc support.
source $BIN_DIR/steps/pylibmc
# Libffi support.
source $BIN_DIR/steps/cryptography
# Support for Geo libraries.
sub-env $BIN_DIR/steps/geo-libs
# GDAL support.
source $BIN_DIR/steps/gdal
# Install dependencies with Pip (where the magic happens).
source $BIN_DIR/steps/pip-install
# Support for pip install -e.
rm -fr $BUILD_DIR/.heroku/src
deep-cp /app/.heroku/src $BUILD_DIR/.heroku/src
# Django collectstatic support.
sub-env $BIN_DIR/steps/collectstatic
# Create .profile script for application runtime environment variables.
set-env PATH '$HOME/.heroku/python/bin:$PATH'
set-env PYTHONUNBUFFERED true
set-env PYTHONHOME /app/.heroku/python
set-env LIBRARY_PATH '/app/.heroku/vendor/lib:/app/.heroku/python/lib:$LIBRARY_PATH'
set-env LD_LIBRARY_PATH '/app/.heroku/vendor/lib:/app/.heroku/python/lib:$LD_LIBRARY_PATH'
set-default-env LANG en_US.UTF-8
set-default-env PYTHONHASHSEED random
set-default-env PYTHONPATH /app/
# Install sane-default script for $WEB_CONCURRENCY and $FORWARDED_ALLOW_IPS.
cp $ROOT_DIR/vendor/python.gunicorn.sh $GUNICORN_PROFILE_PATH
# Experimental post_compile hook.
bpwatch start post_compile
source $BIN_DIR/steps/hooks/post_compile
bpwatch stop post_compile
set +e
# rewrite build dir in egg links to /app so things are found at runtime
find .heroku/python/lib/python*/site-packages/ -name "*.pth" -print0 2> /dev/null | xargs -r -0 -n 1 sed -i -e "s#$(pwd)#/app#" &> /dev/null
set -e
set +e
# Support for PyPy
find .heroku/python/lib-python/*/site-packages/ -name "*.pth" -print0 2> /dev/null | xargs -r -0 -n 1 sed -i -e "s#$(pwd)#/app#" &> /dev/null
set -e
# Store new artifacts in cache.
bpwatch start dump_cache
rm -rf $CACHE_DIR/.heroku/python
rm -rf $CACHE_DIR/.heroku/python-version
rm -rf $CACHE_DIR/.heroku/python-stack
rm -rf $CACHE_DIR/.heroku/vendor
rm -rf $CACHE_DIR/.heroku/venv
rm -rf $CACHE_DIR/.heroku/src
mkdir -p $CACHE_DIR/.heroku
cp -R .heroku/python $CACHE_DIR/.heroku/
cp -R .heroku/python-version $CACHE_DIR/.heroku/
cp -R .heroku/python-stack $CACHE_DIR/.heroku/ &> /dev/null || true
cp -R .heroku/vendor $CACHE_DIR/.heroku/ &> /dev/null || true
cp -R .heroku/venv $CACHE_DIR/.heroku/ &> /dev/null || true
if [[ -d .heroku/src ]]; then
cp -R .heroku/src $CACHE_DIR/.heroku/ &> /dev/null || true
fi
bpwatch stop dump_cache
# Fin.
$ROOT_DIR/compile-extensions/bin/store_buildpack_metadata $ROOT_DIR $CACHE_DIR
bpwatch stop compile