|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Cloud Foundry Python Conda Buildpack |
| 3 | +# Copyright (c) 2014-2015 the original author or authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +echo "-----> Starting compile step..." |
| 19 | +BUILD_DIR=$1 |
| 20 | +CACHE=$2 |
| 21 | + |
| 22 | +BASH=$(which bash) |
| 23 | +WGET=$(which wget) |
| 24 | +CONDA_HOME="$1/.conda" |
| 25 | +CONDA_BIN="$CONDA_HOME/bin" |
| 26 | +RUNTIME="$BUILD_DIR/runtime.txt" |
| 27 | + |
| 28 | +# Get the runtime version and download appropriate Miniconda |
| 29 | +if [ -e $RUNTIME ]; then |
| 30 | + PYTHON_VERSION=$(cut -d- -f2 $RUNTIME) |
| 31 | + if [ ${PYTHON_VERSION:0:1} -eq 3 ]; then |
| 32 | + PYTHON_MAJOR_VERSION=3 |
| 33 | + else |
| 34 | + PYTHON_MAJOR_VERSION="" |
| 35 | + fi |
| 36 | + MINICONDA_FILE="Miniconda$PYTHON_MAJOR_VERSION-latest-Linux-x86_64.sh" |
| 37 | +else |
| 38 | + MINICONDA_FILE="Miniconda-latest-Linux-x86_64.sh" |
| 39 | +fi |
| 40 | + |
| 41 | +MINICONDA_URI="http://repo.continuum.io/miniconda/$MINICONDA_FILE" |
| 42 | +MINICONDA_CACHE="$CACHE/$MINICONDA_FILE" |
| 43 | + |
| 44 | +PROFILE_PATH="$BUILD_DIR/.profile.d/conda.sh" |
| 45 | + |
| 46 | +echo "-----> Preparing Python Environment..." |
| 47 | +if [ ! -e $MINICONDA_CACHE ] ; then |
| 48 | + echo "-----> Downloading Miniconda..." |
| 49 | + if [ ! -d $CACHE ]; then mkdir $CACHE; fi |
| 50 | + $WGET -q -O $MINICONDA_CACHE $MINICONDA_URI |
| 51 | + chmod +x $MINICONDA_CACHE |
| 52 | +fi |
| 53 | +if [ -e $CONDA_HOME ]; then rm -rf $CONDA_HOME; fi |
| 54 | +# Install miniconda |
| 55 | +$MINICONDA_CACHE -b -p $CONDA_HOME #&> /dev/null |
| 56 | + |
| 57 | +echo "-----> Installing Dependencies..." |
| 58 | +$CONDA_BIN/conda update --yes --quiet conda |
| 59 | +$CONDA_BIN/conda install --yes --quiet pip |
| 60 | + |
| 61 | +# Default Conda env is root |
| 62 | +CONDA_ENV="root" |
| 63 | + |
| 64 | +# Install application dependencies |
| 65 | +echo "-----> Installing conda environment from environment.yml..." |
| 66 | +$CONDA_BIN/conda env update -n root -f "$BUILD_DIR/environment.yml" |
| 67 | + |
| 68 | +$CONDA_BIN/conda clean -pt |
| 69 | + |
| 70 | +# |
| 71 | +echo "-----> Fixing paths..." |
| 72 | +grep -rlI $BUILD_DIR $BUILD_DIR | xargs sed -i.bak "s|$BUILD_DIR|/home/vcap/app|g" |
| 73 | +# |
| 74 | + |
| 75 | +# Add Conda path to profile |
| 76 | +mkdir -p $(dirname $PROFILE_PATH) |
| 77 | +echo "export PATH=$HOME/app/.conda/bin:\$PATH" >> $PROFILE_PATH |
| 78 | + |
| 79 | +if test -f $BUILD_DIR/runtime.txt && grep 'python=' $BUILD_DIR/environment.yml; then |
| 80 | + echo "WARNING: you have specified the version of Python runtime both in 'runtime.txt and 'environment.yml'. You should remove one of the two versions" |
| 81 | +fi |
| 82 | + |
| 83 | +echo "-----> Finished compile step" |
0 commit comments