#!/bin/bash SCRIPT_NAME=$(basename $0) TOPLEVEL=$(git rev-parse --show-toplevel) VERSION=$(git describe|sed 's/-\([[:digit:]]\+\)-.*/\.post\1/') (return 0 2>/dev/null) && sourced=1 || sourced=0 pythonpath_prepend() { if [ -d "$1" ]; then PYTHONPATH=${PYTHONPATH//":$1:"/:} #delete all instances in the middle PYTHONPATH=${PYTHONPATH/%":$1"/} #delete any instance at the end PYTHONPATH=${PYTHONPATH/#"$1:"/} #delete any instance at the beginning PYTHONPATH="$1${PYTHONPATH:+":$PYTHONPATH"}" #prepend $1 or if $PATH is empty set to $1 export PYTHONPATH fi } # Setup or upgrade the Python virtual environment setup_virtualenv(){ VENV_PATH="$1" virtualenv --python=python3 --system-site-packages "$VENV_PATH" source "$VENV_PATH/bin/activate" pip install --upgrade pip pip install --upgrade numpy ase h5py lxml pint terminaltables pycairo pip install --upgrade sphinx status=$? deactivate [ $status -eq 0 ] || exit 1 } activate_msspec_python3_virtualenv() { VENV_PATH="$1" # do not activate virtualenv if already activated if [ x"${VIRTUAL_ENV}" == x ]; then # We are not in a virtualenv if [ x"${VENV_PATH}" != x ]; then # activate the specified venv export MSSPEC_ROOT="${TOPLEVEL}" pythonpath_prepend "$MSSPEC_ROOT/src" #PYTHONPATH="$PYTHONPATH":"$MSSPEC_ROOT"/src source "$VENV_PATH/bin/activate" else # error echo "Error: No venv specified!!" fi else # We are in a virtualenv so load only variables export MSSPEC_ROOT="${TOPLEVEL}" pythonpath_prepend "$MSSPEC_ROOT/src" fi } make_pymsspec_package() { echo "Running setuptools for pymsspec, please wait... " VERSION=$(get_package_version) DEST=${TOPLEVEL}/install_resources/packages/${VERSION} mkdir -p $DEST cd ${TOPLEVEL}/src/python/pymsspec && python setup.py sdist 2>&1 > /dev/null cp ${TOPLEVEL}/src/python/pymsspec/dist/pymsspec-${VERSION}.tar.gz ${DEST}/ rm -rf ${TOPLEVEL}/src/python/pymsspec/{*.egg-info,dist} } make_pybinding() { echo "Creating Python library for Fortran code, please wait... " activate_msspec_python3_virtualenv cd ${TOPLEVEL}/src/ && make pybinding } make_selfex() { echo "Creating the self-extractible setup program... " VERSION=$(get_package_version) DEST=${TOPLEVEL}/install_resources/packages/${VERSION} LICENSE="${TOPLEVEL}/src/CI/license.txt" REQUIREMENTS="${TOPLEVEL}/src/CI/requirements" SETUPFILE="MsSpec-${VERSION}.setup" MAKESELF=${TOPLEVEL}/src/CI/makeself/makeself.sh #pip freeze > "${REQUIREMENTS}" mkdir -p $DEST cp "${TOPLEVEL}/src/CI/install.sh" "${DEST}" chmod u+x "${DEST}/install.sh" cp "${TOPLEVEL}/src/CI/uninstall.sh" "${DEST}" cp "${TOPLEVEL}/src/CI/msspec" "${DEST}" cp "${TOPLEVEL}/src/CI/requirements" "${DEST}" cp "${TOPLEVEL}/src/CI/distro-requirements" "${DEST}" get_makeself cd "${DEST}/../" && ${MAKESELF} --license ${LICENSE} --quiet ${VERSION} ${SETUPFILE} "MsSpec - pymsspec" ./install.sh rm -rf ${SETUPFILE}.bak rm -rf "${DEST}" #rm -rf "${REQUIREMENTS}" } make_pdf() { DEST=${TOPLEVEL}/package mkdir -p ${DEST} # first activate the virtual environment activate_msspec_python3_virtualenv # then build the documentation. cd "${TOPLEVEL}/doc" make --no-print-directory latexpdf || exit 1 # remove previous pdf rm -rf "${DEST}"/*.pdf # copy the produced pdf file to the final location cp "build/latex/MsSpec-python.pdf" "${TOPLEVEL}/package/MsSpec-${VERSION}.pdf" } make_html () { make_pdf # first activate the virtual environment activate_msspec_python3_virtualenv # then build the documentation. cd "${TOPLEVEL}/doc" make --no-print-directory html || exit 1 } make_tests() { echo "generate results for unit tests..." activate_msspec_python3_virtualenv python -c "from msspec.tests import create_tests_results; create_tests_results()" } create_package() { VENV_PATH="$1" source "$VENV_PATH/bin/activate" #update_version #make_tests #make_pdf #make_pymsspec_package #make_msspecgui_package #make_fortran_package #make_selfex #cleanup deactivate } cleanup() { echo "cleaning up..." # remove results.txt file python -c "from msspec.tests import delete_results_file; delete_results_file()" VERSION=$(get_package_version) rm -f ${TOPLEVEL}/guide/MsSpec-${VERSION}.pdf } show_help () { echo "Usage: $SCRIPT_NAME [OPTIONS]" echo "List of possible options:" echo " -i Initialize a Python virtual environnment in FOLDER" echo " -d Build the html documentation within the Python " echo " virtual environnment in FOLDER" } if [ "$sourced" == 0 ]; then while getopts "hi:dt" option; do case $option in i) setup_virtualenv "$OPTARG" ;; d) make_html ;; t) make_tests ;; h) show_help ;; *) show_help ;; esac done fi