Skip to content
Snippets Groups Projects
compile 8.18 KiB
Newer Older
Kenneth Reitz's avatar
Kenneth Reitz committed
#!/usr/bin/env bash
# The Heroku Python Buildpack. This script accepts parameters for a build
Kenneth Reitz's avatar
Kenneth Reitz committed
# 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,
Kenneth Reitz's avatar
Kenneth Reitz committed
# everything should work fine outside of the Heroku environment, if the
# environment is setup correctly.

Kenneth Reitz's avatar
Kenneth Reitz committed
#
#     $ bin/compile <build-dir> <cache-dir> <env-path>
Kenneth Reitz's avatar
Kenneth Reitz committed

# Fail fast and fail hard.
Noah Zoschke's avatar
Noah Zoschke committed
set -eo pipefail
Kenneth Reitz's avatar
Kenneth Reitz committed
# Standard Library.
export BPLOG_PREFIX="buildpack.python"
export BUILDPACK_LOG_FILE=${BUILDPACK_LOG_FILE:-/dev/null}

[ "$BUILDPACK_XTRACE" ] && set -o xtrace

Kenneth Reitz's avatar
Kenneth Reitz committed
# Prepend proper path for virtualenv hackery. This will be deprecated soon.
Kenneth Reitz's avatar
Kenneth Reitz committed
export PATH=:/usr/local/bin:$PATH
Kenneth Reitz's avatar
Kenneth Reitz committed

Kenneth Reitz's avatar
Kenneth Reitz committed
# Paths.
BIN_DIR=$(cd "$(dirname "$0")"; pwd) # absolute path
ROOT_DIR=$(dirname "$BIN_DIR")
Noah Zoschke's avatar
Noah Zoschke committed
BUILD_DIR=$1
CACHE_DIR=$2
ENV_DIR=$3
export BUILD_DIR CACHE_DIR ENV_DIR

Kenneth Reitz's avatar
Kenneth Reitz committed
# Python defaults
DEFAULT_PYTHON_VERSION="python-3.6.3"
LATEST_3="python-3.6.3"
Kenneth Reitz's avatar
Kenneth Reitz committed
LATEST_2="python-2.7.14"

DEFAULT_PYTHON_STACK="cedar-14"
PIP_UPDATE="9.0.1"

Kenneth Reitz's avatar
Kenneth Reitz committed
export DEFAULT_PYTHON_VERSION DEFAULT_PYTHON_STACK PIP_UPDATE LATEST_2 LATEST_3
# Common Problem Warnings
WARNINGS_LOG=$(mktemp)
export WARNINGS_LOG
export RECOMMENDED_PYTHON_VERSION=$DEFAULT_PYTHON_VERSION
Kenneth Reitz's avatar
Kenneth Reitz committed
# Setup vendored tools and pip-pop (pip-diff)
export PATH=$PATH:$ROOT_DIR/vendor/:$ROOT_DIR/vendor/pip-pop
Kenneth Reitz's avatar
Kenneth Reitz committed
# Support Anvil Build_IDs
[ ! "$SLUG_ID" ] && SLUG_ID="defaultslug"
Kenneth Reitz's avatar
Kenneth Reitz committed
[ ! "$REQUEST_ID" ] && REQUEST_ID=$SLUG_ID
[ ! "$STACK" ] && STACK=$DEFAULT_PYTHON_STACK
Kenneth Reitz's avatar
Kenneth Reitz committed

# Sanitizing environment variables.
unset GIT_DIR PYTHONHOME PYTHONPATH
unset RECEIVE_DATA RUN_KEY BUILD_INFO DEPLOY LOG_TOKEN
unset CYTOKINE_LOG_FILE GEM_PATH
Kenneth Reitz's avatar
Kenneth Reitz committed

Kenneth Reitz's avatar
Kenneth Reitz committed
# Syntax sugar.
# shellcheck source=bin/utils
source "$BIN_DIR/utils"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Import collection of warnings.
# shellcheck source=bin/warnings
source "$BIN_DIR/warnings"
Kenneth Reitz's avatar
Kenneth Reitz committed

# we need to put a bunch of symlinks in there later
mkdir -p /app/.heroku
Kenneth Reitz's avatar
Kenneth Reitz committed

# Set up outputs under new context
PROFILE_PATH="$BUILD_DIR/.profile.d/python.sh"
Kenneth Reitz's avatar
Kenneth Reitz committed
EXPORT_PATH="$BIN_DIR/../export"
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`.
Kenneth Reitz's avatar
Kenneth Reitz committed
export BUILD_DIR CACHE_DIR BIN_DIR PROFILE_PATH EXPORT_PATH
Kenneth Reitz's avatar
Kenneth Reitz committed
# Prepend proper environment variables for Python use.
export PATH=/app/.heroku/python/bin:/app/.heroku/vendor/bin:$PATH
Kenneth Reitz's avatar
Kenneth Reitz committed
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
Kenneth Reitz's avatar
Kenneth Reitz committed

# 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 Heroku how to run your application.'
  puts-warn 'Learn more: https://devcenter.heroku.com/articles/procfile'
fi

mkdir -p "$CACHE_DIR"

# 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
Kenneth Reitz's avatar
Kenneth Reitz committed
# Experimental pre_compile hook.
# shellcheck source=bin/steps/hooks/pre_compile
source "$BIN_DIR/steps/hooks/pre_compile"
Kenneth Reitz's avatar
Kenneth Reitz committed

Kenneth Reitz's avatar
Kenneth Reitz committed
# Sticky runtimes.
if [ -f "$CACHE_DIR/.heroku/python-version" ]; then
  DEFAULT_PYTHON_VERSION=$(cat "$CACHE_DIR/.heroku/python-version")
Kenneth Reitz's avatar
Kenneth Reitz committed
fi
Kenneth Reitz's avatar
Kenneth Reitz committed

# Stack fallback for non-declared caches.
if [ -f "$CACHE_DIR/.heroku/python-stack" ]; then
  CACHED_PYTHON_STACK=$(cat "$CACHE_DIR/.heroku/python-stack")
export CACHED_PYTHON_STACK

# Pipenv Python version support.
# shellcheck source=bin/steps/pipenv-python-version
source "$BIN_DIR/steps/pipenv-python-version"
Kenneth Reitz's avatar
Kenneth Reitz committed
# If no runtime given, assume default version.
if [ ! -f runtime.txt ]; then
  echo "$DEFAULT_PYTHON_VERSION" > runtime.txt
Kenneth Reitz's avatar
Kenneth Reitz committed
fi

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
    # Note: .heroku/src is copied in later.
Kenneth Reitz's avatar
Kenneth Reitz committed
# Install Python.
Kenneth Reitz's avatar
Kenneth Reitz committed
let start=$(nowms)
# shellcheck source=bin/steps/python
source "$BIN_DIR/steps/python"
Kenneth Reitz's avatar
Kenneth Reitz committed
mtime "python.install.time" "${start}"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Pipenv support.
# shellcheck source=bin/steps/pipenv
source "$BIN_DIR/steps/pipenv"
Kenneth Reitz's avatar
Kenneth Reitz committed

# If no requirements.txt file given, assume `setup.py develop` is intended.
Kenneth Reitz's avatar
Kenneth Reitz committed
if [ ! -f requirements.txt ] && [ ! -f Pipfile ]; then
  echo "-e ." > requirements.txt
fi

Kenneth Reitz's avatar
Kenneth Reitz committed
# Fix egg-links.
# shellcheck source=bin/steps/eggpath-fix
source "$BIN_DIR/steps/eggpath-fix"
Kenneth Reitz's avatar
Kenneth Reitz committed

# Mercurial support.
# shellcheck source=bin/steps/mercurial
source "$BIN_DIR/steps/mercurial"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Pylibmc support.
# shellcheck source=bin/steps/pylibmc
source "$BIN_DIR/steps/pylibmc"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Libffi support.
# shellcheck source=bin/steps/cryptography
source "$BIN_DIR/steps/cryptography"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Support for Geo libraries.
# shellcheck source=bin/steps/geo-libs
Kenneth Reitz's avatar
Kenneth Reitz committed
sub_env "$BIN_DIR/steps/geo-libs"
Kenneth Reitz's avatar
Kenneth Reitz committed
# GDAL support.
# shellcheck source=bin/steps/gdal
source "$BIN_DIR/steps/gdal"
Kenneth Reitz's avatar
Kenneth Reitz committed

# 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}"

Kenneth Reitz's avatar
Kenneth Reitz committed
# Support for NLTK corpora.
Kenneth Reitz's avatar
Kenneth Reitz committed
let start=$(nowms)
Kenneth Reitz's avatar
Kenneth Reitz committed
sub_env "$BIN_DIR/steps/nltk"
Kenneth Reitz's avatar
Kenneth Reitz committed
mtime "nltk.download.time" "${start}"
Kenneth Reitz's avatar
Kenneth Reitz committed

# Support for pip install -e.
Kenneth Reitz's avatar
Kenneth Reitz committed
# 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"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Django collectstatic support.
Kenneth Reitz's avatar
Kenneth Reitz committed
let start=$(nowms)
Kenneth Reitz's avatar
Kenneth Reitz committed
sub_env "$BIN_DIR/steps/collectstatic"
Kenneth Reitz's avatar
Kenneth Reitz committed
mtime "collectstatic.time" "${start}"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Create .profile script for application runtime environment variables.
Kenneth Reitz's avatar
Kenneth Reitz committed
set_env PATH "\$HOME/.heroku/python/bin:\$PATH"
set_env PYTHONUNBUFFERED true
set_env PYTHONHOME /app/.heroku/python
Kenneth Reitz's avatar
Kenneth Reitz committed
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"
Kenneth Reitz's avatar
Kenneth Reitz committed
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"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Experimental post_compile hook.
# shellcheck source=bin/steps/hooks/post_compile
source "$BIN_DIR/steps/hooks/post_compile"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Fix egg-links, again.
# shellcheck source=bin/steps/eggpath-fix2
source "$BIN_DIR/steps/eggpath-fix2"
Kenneth Reitz's avatar
Kenneth Reitz committed
# Store new artifacts in cache.
Kenneth Reitz's avatar
Kenneth Reitz committed

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
Kenneth Reitz's avatar
Kenneth Reitz committed
if [[ -d .heroku/src ]]; then
  cp -R .heroku/src "$CACHE_DIR/.heroku/" &> /dev/null || true
Kenneth Reitz's avatar
Kenneth Reitz committed
fi
Kenneth Reitz's avatar
Kenneth Reitz committed

# Measure the size of the Python installation.
# shellcheck disable=SC2119
Kenneth Reitz's avatar
Kenneth Reitz committed
mmeasure 'python.size' "$(measure-size)"