#!/usr/bin/env bash # bin/compile 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" virtualenv() { VENV_PATH=$ROOT_DIR/src/virtualenv-1.6.4 PYTHONPATH=$VENV_PATH python $VENV_PATH/scripts/virtualenv "$@" } indent() { RE="s/^/ /" [ $(uname) == "Darwin" ] && sed -l "$RE" || sed -u "$RE" } 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 # 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 --no-site-packages . | indent # if Django, append settings if [ "$NAME" = "Python/Django" ]; then echo "-----> Django settings injection" SETTINGS_FILE=$(ls **/settings.py | head -1) PROJECT=$(dirname $SETTINGS_FILE) echo " Injecting code into $SETTINGS_FILE to read from DATABASE_URL" cat >>$SETTINGS_FILE < Installing dependencies using pip version $(bin/pip --version | awk '{print $2}')" PIP_DOWNLOAD_CACHE=$PIP_DOWNLOAD_CACHE bin/pip install --use-mirrors -r requirements.txt | indent 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