#!/bin/bash
#
#
# This is the jenkins build script for building/testing
# Bokeh.  Modified from https://github.com/ContinuumIO/blaze/blob/master/buildscripts/jenkins-build
#
# Jenkins Requirements:
#   - Anaconda should be installed in ~/anaconda
#   - Use a jenkins build matrix for multiple
#     platforms/python versions
#   - Use the XShell plugin to launch this script
#   - Call the script from the root workspace
#     directory as buildscripts/jenkins-build
#

# Require a version of Python to be selected
if [ "${PYTHON_VERSION}" == "" ]; then
 echo You must select a Python version with the PYTHON_VERSION variable.
 exit 1
fi

# Try to delete the build directory twice, with a pause in
# between, because on an OSX build machine this was failing
# on occasion.
rm -rf ${WORKSPACE}/build
sleep 1
rm -rf ${WORKSPACE}/build

if [ -d ${WORKSPACE}/build ]; then
 echo Failed to delete temporary build dir ${WORKSPACE}/build
 exit 1
fi

# Use conda to create a conda environment of the required
# python version and containing the dependencies.
export PYENV_PREFIX=${WORKSPACE}/build/pyenv
~/anaconda/bin/conda create --yes --channel http://repo.continuum.io/pkgs/dev -p ${PYENV_PREFIX} python=${PYTHON_VERSION} bokeh nose || exit 1
export PATH=${PYENV_PREFIX}/bin:${PATH}

if [ -f "${PYENV_PREFIX}/bin/python" ]; then
 export PYTHON_EXECUTABLE=${PYENV_PREFIX}/bin/python
elif [ -f "${PYENV_PREFIX}/bin/python3" ]; then
 export PYTHON_EXECUTABLE=${PYENV_PREFIX}/bin/python3
else
 echo Conda environment creation failed.
 exit 1
fi


# Build/install Blaze
${PYTHON_EXECUTABLE} setup.py install || exit 1

# Run the tests (in a different directory, so the import works properly)
mkdir tmpdir
pushd tmpdir
${PYTHON_EXECUTABLE} -c 'import bokeh;bokeh.test(xunitfile="../test_results.xml", verbosity=2, exit=1)' || exit 1
popd