forked from heroku/heroku-buildpack-python
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcompile
More file actions
executable file
·225 lines (173 loc) · 5.83 KB
/
Copy pathcompile
File metadata and controls
executable file
·225 lines (173 loc) · 5.83 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
#!/usr/bin/env bash
# This script serves as the
# [**Python Buildpack**](https://github.com/heroku/heroku-buildpack-python)
# compiler.
#
# A [buildpack](http://devcenter.heroku.com/articles/buildpacks) is an
# adapter between a Python application and Heroku's runtime.
#
# You can intreract with the Heroku API directly with [heroku.py](https://github.com/heroku/heroku.py/).
#
# See also: [Release history](/changelog.html), [Detection](/detect.html).
#
# ## Usage
# Compiling an app into a slug is simple:
#
# $ bin/compile <build-dir> <cache-dir>
# ## Assumptions
#
# This buildpack makes the following assumptions:
#
# - The desired Python VM is available on the base system.
# - Library dependencies are available on the base system.
# - Django applications should not require any platform-specific configuration.
# <hr />
# ## Context
# Fail fast and fail hard.
set -eo pipefail
# 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
# The detected application type (`Python`|`Python/Django`).
NAME=$($BIN_DIR/detect $BUILD_DIR)
# Where to store the Pip download cache.
CACHED_DIRS=".heroku"
PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-$CACHE_DIR/pip_downloads}
# Static configurations for virtualenv caches.
LEGACY_VIRTUALENV_LOC="."
MODERN_VIRTUALENV_LOC=".heroku/venv"
LEGACY_VIRTUALENV_DIRS="bin include lib"
LEGACY_VIRTUALENV_TRIGGER="lib/python2.7"
# Python version. This will be used in the future to specify custom Pythons.
PYTHON_VERSION="2.7.2"
PYTHON_EXE="python2.7"
# Sanitizing environment variables.
unset GIT_DIR
unset PYTHON_HOME
unset PYTHONPATH
# We'll need to send these statics to other scripts we `source`.
export PIP_DOWNLOAD_CACHE BUILD_DIR CACHE_DIR BIN_DIR
# Syntax sugar.
indent() {
RE="s/^/ /"
[ $(uname) == "Darwin" ] && sed -l "$RE" || sed -u "$RE"
}
function virtualenv (){
python "$ROOT_DIR/vendor/virtualenv-1.7/virtualenv.py" "$@"
}
function puts-step (){
echo "-----> $@"
}
function puts-warn (){
echo " ! $@"
}
# ## Build Time
#
# Switch to the repo's context.
cd $BUILD_DIR
# Experimental pre_compile hook.
source $BIN_DIR/steps/hooks/pre_compile
# ### Sanity Checks
#
# Just a little peace of mind.
# If no requirements given, assume `setup.py develop`.
if [ ! -f requirements.txt ]; then
puts-step "No requirements.txt provided; assuming dist package."
echo "-e ." > requirements.txt
fi
# ### The Cache
mkdir -p $CACHE_DIR
# Nice defaults.
LEGACY_VIRTUALENV=false
VIRTUALENV_LOC=$MODERN_VIRTUALENV_LOC
# Support "old-style" virtualenvs.
if [ -d $CACHE_DIR/$LEGACY_VIRTUALENV_TRIGGER ]; then
LEGACY_VIRTUALENV=true
VIRTUALENV_LOC=$LEGACY_VIRTUALENV_LOC
CACHED_DIRS=$LEGACY_VIRTUALENV_DIRS
# Warn for a checked-in virtualenv.
if [ -d "lib" ] || [ -d "bin" ]; then
puts-warn "You have a virtualenv checked in. You should ignore the appropriate paths in your repo. See http://devcenter.heroku.com/articles/gitignore for more info.";
fi
# Reject a conflicting checked-in virtualenv.
if [ -f "lib/python2.7" ]; then
puts-warn "Checked-in virtualenv conflict."
exit 1;
fi
fi
# Restore old artifacts from the cache.
for dir in $CACHED_DIRS; do
cp -R $CACHE_DIR/$dir . &> /dev/null || true
done
set +e
# Create set-aside `.heroku` folder.
mkdir .heroku &> /dev/null
HEROKU_DIR_STATUS=$?
# This is a new app, disable injection.
[ $HEROKU_DIR_STATUS -eq 0 ] && {
touch .heroku/injection_disabled
}
set -e
# ### Virtualenv Setup
#
# Create the virtualenv. Rebuild if corrupt.
# TODO: Bootstrap a bottled Python VM...
set +e
puts-step "Preparing Python interpreter ($PYTHON_VERSION)"
puts-step "Creating Virtualenv version $(virtualenv --version)"
# Try to create the virtualenv.
OUT=$(virtualenv --python $PYTHON_EXE --distribute --never-download --prompt='(venv) ' $VIRTUALENV_LOC 2>&1)
# If there's an error, purge and recreate.
[ $? -ne 0 ] && {
puts-warn "Virtualenv corrupt, rebuilding."
for dir in $VIRTUALENV_DIRS; do
rm -fr $dir &> /dev/null || true
done
OUT=$(virtualenv --python $PYTHON_EXE --distribute --never-download --prompt='(venv) ' $VIRTUALENV_LOC )
}
echo "$OUT" | indent
set -e
# Pylibmc support.
# See [`bin/steps/pylibmc`](pylibmc.html).
source $BIN_DIR/steps/pylibmc
# Activate the Virtualenv.
puts-step "Activating virtualenv"
source $VIRTUALENV_LOC/bin/activate
# Install Mercurial if it appears to be required.
if (grep -Fiq "hg+" requirements.txt) then
pip install --use-mirrors mercurial | indent
fi
# Install dependencies with Pip.
puts-step "Installing dependencies using pip version $(pip --version | awk '{print $2}')"
pip install --use-mirrors -r requirements.txt --src ./.heroku/src | indent
# Do additional application hackery if applications appears to be a Django app.
# Optionally, disable all Django-specific changes with `DISABLE_INJECTION` env.
#
# See [`bin/steps/django`](django.html).
if [ "$NAME" = "Python/Django" ]; then
source $BIN_DIR/steps/django/init
fi
# Make Virtualenv's paths relative for portability.
set +e
OUT=$(virtualenv --python $PYTHON_EXE --relocatable $VIRTUALENV_LOC)
[ $? -ne 0 ] && {
puts-warn "Error making virtualenv relocatable"
echo "$OUT" | indent
exit 1
}
set -e
# ### Finalize
#
# Store new artifacts in cache.
for dir in $CACHED_DIRS; do
rm -rf $CACHE_DIR/$dir
cp -R $dir $CACHE_DIR/
done
# ### Fin.
# Experimental post_compile hook.
source $BIN_DIR/steps/hooks/post_compile
# <a href="http://github.com/heroku/heroku-buildpack-python"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://d3nwyuy0nl342s.cloudfront.net/img/7afbc8b248c68eb468279e8c17986ad46549fb71/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub"></a>