Newer
Older
# The Heroku Python Buildpack. This script accepts parameters for a build
# directory, a cache directory, and a directory for app environment variables.
# Warning: there are a few hacks in this script to accommodate excellent builds
# on Heroku. No guarantee for external compatibility is made. However,
# everything should work fine outside of the Heroku environment, if the
# environment is setup correctly.
# $ bin/compile <build-dir> <cache-dir> <env-path>
# Standard Library.
export BPLOG_PREFIX="buildpack.python"
export BUILDPACK_LOG_FILE=${BUILDPACK_LOG_FILE:-/dev/null}
[ "$BUILDPACK_XTRACE" ] && set -o xtrace
# Prepend proper path for virtualenv hackery. This will be deprecated soon.
BIN_DIR=$(cd "$(dirname "$0")"; pwd) # absolute path
ROOT_DIR=$(dirname "$BIN_DIR")
DEFAULT_PYTHON_VERSION="python-3.6.3"
LATEST_3="python-3.6.3"
DEFAULT_PYTHON_STACK="cedar-14"
export DEFAULT_PYTHON_VERSION DEFAULT_PYTHON_STACK PIP_UPDATE LATEST_2 LATEST_3
WARNINGS_LOG=$(mktemp)
export WARNINGS_LOG
export RECOMMENDED_PYTHON_VERSION=$DEFAULT_PYTHON_VERSION
# Setup vendored tools and pip-pop (pip-diff)
export PATH=$PATH:$ROOT_DIR/vendor/:$ROOT_DIR/vendor/pip-pop
[ ! "$STACK" ] && STACK=$DEFAULT_PYTHON_STACK
# Sanitizing environment variables.
unset RECEIVE_DATA RUN_KEY BUILD_INFO DEPLOY LOG_TOKEN
unset CYTOKINE_LOG_FILE GEM_PATH
# shellcheck source=bin/utils
source "$BIN_DIR/utils"
# shellcheck source=bin/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"
GUNICORN_PROFILE_PATH="$BUILD_DIR/.profile.d/python.gunicorn.sh"
WEB_CONCURRENCY_PROFILE_PATH="$BUILD_DIR/.profile.d/WEB_CONCURRENCY.sh"
# We'll need to send these statics to other scripts we `source`.
export BUILD_DIR CACHE_DIR BIN_DIR PROFILE_PATH EXPORT_PATH
export PATH=/app/.heroku/python/bin:/app/.heroku/vendor/bin:$PATH
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
# Warn for lack of Procfile.
if [[ ! -f Procfile ]]; then
puts-warn 'Warning: Your application is missing a Procfile. This file tells Heroku how to run your application.'
puts-warn 'Learn more: https://devcenter.heroku.com/articles/procfile'
fi
# Prepare the cache.
# Restore old artifacts from the 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
if [[ -d "$CACHE_DIR/.heroku/src" ]]; then
cp -R "$CACHE_DIR/.heroku/src" .heroku/ &> /dev/null || true
# shellcheck source=bin/steps/hooks/pre_compile
source "$BIN_DIR/steps/hooks/pre_compile"
if [ -f "$CACHE_DIR/.heroku/python-version" ]; then
DEFAULT_PYTHON_VERSION=$(cat "$CACHE_DIR/.heroku/python-version")
# Stack fallback for non-declared caches.
if [ -f "$CACHE_DIR/.heroku/python-stack" ]; then
CACHED_PYTHON_STACK=$(cat "$CACHE_DIR/.heroku/python-stack")
CACHED_PYTHON_STACK=$STACK
# shellcheck source=bin/steps/pipenv-python-version
source "$BIN_DIR/steps/pipenv-python-version"
# If no runtime given, assume default version.
if [ ! -f runtime.txt ]; then
echo "$DEFAULT_PYTHON_VERSION" > runtime.txt
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
# Note: .heroku/src is copied in later.
# shellcheck source=bin/steps/python
source "$BIN_DIR/steps/python"
# shellcheck source=bin/steps/pipenv
source "$BIN_DIR/steps/pipenv"
# If no requirements.txt file given, assume `setup.py develop` is intended.
echo "-e ." > requirements.txt
fi
# shellcheck source=bin/steps/eggpath-fix
source "$BIN_DIR/steps/eggpath-fix"
# shellcheck source=bin/steps/mercurial
source "$BIN_DIR/steps/mercurial"
# shellcheck source=bin/steps/pylibmc
source "$BIN_DIR/steps/pylibmc"
# shellcheck source=bin/steps/cryptography
source "$BIN_DIR/steps/cryptography"
# shellcheck source=bin/steps/gdal
source "$BIN_DIR/steps/gdal"
# Uninstall removed dependencies with Pip.
let start=$(nowms)
# shellcheck source=bin/steps/pip-uninstall
source "$BIN_DIR/steps/pip-uninstall"
mtime "pip.uninstall.time" "${start}"
# Install dependencies with Pip (where the magic happens).
let start=$(nowms)
# shellcheck source=bin/steps/pip-install
source "$BIN_DIR/steps/pip-install"
mtime "pip.install.time" "${start}"
# In CI, $BUILD_DIR is /app.
if [[ ! "$BUILD_DIR" == "/app" ]]; then
rm -fr "$BUILD_DIR/.heroku/src"
deep-cp /app/.heroku/src "$BUILD_DIR/.heroku/src"
# 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/WEB_CONCURRENCY.sh" "$WEB_CONCURRENCY_PROFILE_PATH"
cp "$ROOT_DIR/vendor/python.gunicorn.sh" "$GUNICORN_PROFILE_PATH"
# shellcheck source=bin/steps/hooks/post_compile
source "$BIN_DIR/steps/hooks/post_compile"
# shellcheck source=bin/steps/eggpath-fix2
source "$BIN_DIR/steps/eggpath-fix2"
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/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/src "$CACHE_DIR/.heroku/" &> /dev/null || true
# Measure the size of the Python installation.