Skip to content
Snippets Groups Projects
compile 2.37 KiB
#!/usr/bin/env bash
# bin/compile <build-dir> <cache-dir>

set -eo pipefail

BIN_DIR=$(cd $(dirname $0); pwd) # absolute path
ROOT_DIR=$(dirname $BIN_DIR)
BUILD_DIR=$1
CACHE_DIR=$2

NAME=$($BIN_DIR/detect $BUILD_DIR)
PIP_DOWNLOAD_CACHE=${PIP_DOWNLOAD_CACHE:-$CACHE_DIR/pip_downloads}
VIRTUALENV_DIRS="bin include lib"
VENDORED_MEMCACHED="http://cl.ly/0a191R3K160t1w1P0N25/vendor-libmemcached.tar.gz"

export PIP_DOWNLOAD_CACHE

indent() {
  RE="s/^/       /"
  [ $(uname) == "Darwin" ] && sed -l "$RE" || sed -u "$RE"
}

virtualenv() {
  python - "$@" <<EOF
import sys
sys.path.insert(0, "$ROOT_DIR/src/virtualenv-1.7")
import virtualenv
virtualenv.main()
EOF
}

cd $BUILD_DIR

# Reject a non-packaged Django app.
if [ "$NAME" = "Python" ]; then
  [ -f manage.py ] && [ -f settings.py ] && { echo " !     Django app must be in a package subdirectory"; exit 1; }
fi

# Warn a checked-in virtualenv.
if [ -d "lib" ] || [ -d "bin" ]; then
  echo " !     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
  echo " !     Checked-in virtualenv conflict."
  exit 1;
fi

# Copy artifacts out of cache if exists.
mkdir -p $CACHE_DIR
for dir in $VIRTUALENV_DIRS; do
  cp -R $CACHE_DIR/$dir . &> /dev/null || true
done

echo "-----> Preparing virtualenv version $(virtualenv --version)"
virtualenv --distribute --never-download . | indent

# Create set-aside .heroku folder.
mkdir -p .heroku

# Pylibmc support.
source $BIN_DIR/steps/pylibmc

# Activate the virtualenv.
echo "-----> Activating virtualenv"
source bin/activate

# Install mercurial, if needed.
if (grep -Fiq "hg+" requirements.txt) then
  pip install --use-mirrors mercurial | indent
fi

# Install dependencies.
echo "-----> Installing dependencies using pip version $(bin/pip --version | awk '{print $2}')"
pip install --use-mirrors -r requirements.txt | indent

# Django support.
if [ "$NAME" = "Python/Django" ] && ! [ "$DISABLE_INJECTION" ]; then
  source $BIN_DIR/steps/django
fi

# Make virtualenv relocatable.
set +e
OUT=$(virtualenv --relocatable .)
[ $? -ne 0 ] && {
  echo " !     Error making virtualenv relocatable"
  echo "$OUT" | indent
  exit 1
}
set -e

# Store new artifacts in cache.
for dir in $VIRTUALENV_DIRS; do
  rm -rf $CACHE_DIR/$dir
  cp -R $dir $CACHE_DIR/
done