mirror of
https://github.com/kennethreitz/heroku-buildpack-python.git
synced 2026-06-05 23:10:16 +00:00
245 lines
6.1 KiB
Bash
Executable File
245 lines
6.1 KiB
Bash
Executable File
#!/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
|
|
|
|
# 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.
|
|
VIRTUALENV_LOC=".heroku/venv"
|
|
LEGACY_TRIGGER="lib/python2.7"
|
|
PROFILE_PATH="$BUILD_DIR/.profile.d/python.sh"
|
|
|
|
# Python version. This will be used in the future to specify custom Pythons.
|
|
DEFAULT_PYTHON_VERSION="python-2.6.3"
|
|
PYTHON_EXE="/app/.heroku/python/bin/python"
|
|
|
|
# Sanitizing environment variables.
|
|
unset GIT_DIR PYTHONHOME PYTHONPATH LD_LIBRARY_PATH LIBRARY_PATH
|
|
|
|
# We'll need to send these statics to other scripts we `source`.
|
|
export PIP_DOWNLOAD_CACHE BUILD_DIR CACHE_DIR BIN_DIR PROFILE_PATH
|
|
|
|
# Syntax sugar.
|
|
source $BIN_DIR/utils
|
|
|
|
# Directory Hacks for path consistiency.
|
|
APP_DIR='/app'
|
|
TMP_APP_DIR=$CACHE_DIR/tmp_app_dir
|
|
|
|
ls -a $APP_DIR
|
|
ls -a $APP_DIR/tmp
|
|
|
|
echo 'touching things...'
|
|
touch $app_dir/tmp/newfile
|
|
mkdir -p $app_dir/tmp/newdir
|
|
|
|
# Copy Anvil app dir to temporary storage...
|
|
mkdir -p $TMP_APP_DIR
|
|
deep-mv $APP_DIR $TMP_APP_DIR
|
|
|
|
# Copy Application code in.
|
|
deep-mv $BUILD_DIR $APP_DIR
|
|
|
|
# Set new context.
|
|
ORIG_BUILD_DIR=$BUILD_DIR
|
|
BUILD_DIR=$APP_DIR
|
|
|
|
# Prepend proper path buildpack use.
|
|
export PATH=$BUILD_DIR/.heroku/python/bin:$PATH
|
|
# ls $BUILD_DIR
|
|
|
|
# ## Build Time
|
|
#
|
|
|
|
# Switch to the repo's context.
|
|
cd $BUILD_DIR
|
|
|
|
# Experimental pre_compile hook.
|
|
source $BIN_DIR/steps/hooks/pre_compile
|
|
|
|
# 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
|
|
|
|
# If no runtime given, assume default version.
|
|
if [ ! -f runtime.txt ]; then
|
|
puts-step "No runtime.txt provided; assuming $DEFAULT_PYTHON_VERSION."
|
|
echo $DEFAULT_PYTHON_VERSION > runtime.txt
|
|
fi
|
|
|
|
# ### The Cache
|
|
mkdir -p $CACHE_DIR
|
|
[ ! "$(ls -A $CACHE_DIR)" ] && export FRESH_APP=1
|
|
|
|
# Purge "old-style" virtualenvs.
|
|
[ -d $CACHE_DIR/$LEGACY_TRIGGER ] && rm -fr $CACHE_DIR/*
|
|
|
|
# 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
|
|
set -e
|
|
|
|
# Buildpack profile init script
|
|
mkdir -p $(dirname $PROFILE_PATH)
|
|
|
|
# ### Virtualenv Setup
|
|
#
|
|
|
|
# Create the virtualenv. Rebuild if corrupt.
|
|
# TODO: Bootstrap a bottled Python VM...
|
|
|
|
set +e
|
|
PYTHON_VERSION=$(cat runtime.txt)
|
|
puts-step "Preparing Python runtime ($PYTHON_VERSION)"
|
|
|
|
# Install Python.
|
|
if [ -f .heroku/python-version ]; then
|
|
if [ ! $(cat .heroku/python-version) = $PYTHON_VERSION ]; then
|
|
puts-step "Found $(cat .heroku/python-version), removing."
|
|
rm -fr .heroku/python
|
|
else
|
|
SKIP_INSTALL=1
|
|
fi
|
|
fi
|
|
|
|
|
|
if [ ! "$SKIP_INSTALL" ]; then
|
|
puts-step "Installing runtime $PYTHON_VERSION"
|
|
curl http://envy-versions.s3.amazonaws.com/$PYTHON_VERSION.tar.bz2 | tar jx
|
|
mv python .heroku/python
|
|
|
|
# Record for future reference.
|
|
echo $PYTHON_VERSION > .heroku/python-version
|
|
|
|
# Prepare it for the real world
|
|
# curl -O http://python-distribute.org/distribute_setup.py
|
|
echo path
|
|
echo $PATH
|
|
ls /app/.heroku/python/bin
|
|
hash -r
|
|
which python
|
|
python $ROOT_DIR/vendor/distribute-0.6.31/setup.py install
|
|
echo
|
|
echo
|
|
echo
|
|
ls /app/.heroku/python/bin
|
|
echo
|
|
echo
|
|
|
|
hash -r
|
|
python $ROOT_DIR/vendor/pip-1.2.1/setup.py install
|
|
fi
|
|
|
|
|
|
# puts-step "Creating Virtualenv ($(virtualenv --version))"
|
|
|
|
# # Try to create the virtualenv.
|
|
# OUT=$(virtualenv --python $PYTHON_EXE --distribute --never-download --prompt='(venv) ' $VIRTUALENV_LOC 2>&1)
|
|
|
|
# [ $? -ne 0 -o -n "$CLEAN_VIRTUALENV" ] && {
|
|
# if [ -n "$CLEAN_VIRTUALENV" ]
|
|
# then echo " ! CLEAN_VIRTUALENV set, rebuilding virtualenv."
|
|
# else echo " ! Virtualenv corrupt, rebuilding."
|
|
# fi
|
|
|
|
# rm -fr $VIRTUALENV_LOC &> /dev/null || true
|
|
|
|
# OUT=$(virtualenv --python $PYTHON_EXE --distribute --never-download --prompt='(venv) ' $VIRTUALENV_LOC )
|
|
# }
|
|
# echo "$OUT" | cleanup | indent
|
|
set -e
|
|
|
|
# Pylibmc support.
|
|
# See [`bin/steps/pylibmc`](pylibmc.html).
|
|
source $BIN_DIR/steps/pylibmc
|
|
|
|
# Install Mercurial if it appears to be required.
|
|
if (grep -Fiq "hg+" requirements.txt) then
|
|
pip install --use-mirrors mercurial | cleanup | indent
|
|
fi
|
|
|
|
# Install dependencies with Pip.
|
|
puts-step "Installing dependencies using pip ($(pip --version | awk '{print $2}'))"
|
|
pip install --use-mirrors -r requirements.txt --exists-action=w --src=./.heroku/src | cleanup | indent
|
|
|
|
# Django collectstatic support.
|
|
source $BIN_DIR/steps/collectstatic
|
|
|
|
# ### Finalize
|
|
#
|
|
python --version
|
|
|
|
# Store new artifacts in cache.
|
|
for dir in $CACHED_DIRS; do
|
|
rm -rf $CACHE_DIR/$dir
|
|
cp -R $dir $CACHE_DIR/
|
|
done
|
|
|
|
# Set context environment variables.
|
|
set-env PATH '$HOME/.heroku/venv/bin:$PATH'
|
|
set-default-env PYTHONUNBUFFERED true
|
|
set-default-env LIBRARY_PATH /app/.heroku/vendor/lib
|
|
set-default-env LD_LIBRARY_PATH /app/.heroku/vendor/lib
|
|
set-default-env LANG en_US.UTF-8
|
|
set-default-env PYTHONHASHSEED random
|
|
set-default-env PYTHONHOME /app/.heroku/venv/
|
|
set-default-env PYTHONPATH /app/
|
|
|
|
|
|
# ### Fin.
|
|
|
|
deep-mv $BUILD_DIR $ORIG_BUILD_DIR
|
|
deep-mv $TMP_APP_DIR $APP_DIR
|
|
|
|
|
|
# Experimental post_compile hook.
|
|
source $BIN_DIR/steps/hooks/post_compile
|