<commit_before>#!/bin/bash

# Script to create a virtual environment for, and (re)install, a particular lalsuite branch

LALSUITE_LOCATION=${HOME}/lscsoft/lalsuite

# current location
CURDIR=`pwd`

# get a list of the current git branch names
cd $LALSUITE_LOCATION

branches=$(git branch | cut -c 3-) # see http://stackoverflow.com/a/3846451/1862861

# command lines arguments:
#   -b branchname (the git branch name) [required]
#   -n (do not install additional python packages)
#   -p python (the path to the required python version)
#   -u uninstall previous install (rather than just doing a distclean)
#   -c only re-run 'make install' with running 'make distclean' beforehand
#   -g perform 'git clean -dxf' to remove superfluous files 
#   -v (path to the virtualenvwrapper.sh script)
#   -o compile with further optimisation
#   -h (print this info)

usage="Usage $0 -b (-n -u -c -g -p -v -o -m -h):\n\t-b\tbranchname (the git branch name) [required]\n\
\t-n\tdo not install a selection of additional python packages [optional]\n\
\t-u\tuninstall previous lalsuite install (rather than just doing a 'make distclean') [optional]\n\
\t-c\tonly re-run 'make install' without 'make distclean' beforehand [optional]\n\
\t-g\tperform 'git clean -dxf' to remove superfluous files (BE CAREFUL!) [optional]\n\
\t-p\tpython (the path to the required python executable for installing\n\t\tthe virtual environment) [optional]\n\
\t-v\tpath to the virtualenvwrapper.sh script [optional]\n\
\t-o\tcompile lalsuite with the -O3 optimisation [optional]\n\
\t-h\thelp\n"

if [[ $# -eq 0 ]]; then
  echo -e $usage
  cd $CURDUR
  exit 0
fi

nopython=0 # by default install additional python packages
pythonexe=""
isbranch=0
vewscript="jskgkgksbdkuylfzgslf" # some gibberish
optimise=0
unintsall=0
gitclean=0
distclean=1

while getopts ":b:p:v:nougch" opt; do
  case $opt in
    b)
      thisbranch=$OPTARG
      # check argument is one of the current git branches
      for branch in ${branches[@]}; do
        if [[ $branch == $thisbranch ]]; then
          isbranch=1
          break
        fi
      done

      if [[ $isbranch -eq 0 ]]; then # check if branch is found
        echo "Specified branch \"${thisbranch}\" is not in the git repo. Use one of the following:"
        for branch in ${branches[@]}; do
          echo "  $branch"
        done
        cd $CURDIR
        exit 0
      fi
      ;;
    p)
      pythonexe=$OPTARG
      if [[ ! -x "$OPTARG" ]]; then
        echo "$OPTARG: this python executable does not exist or is not executable"
        cd $CURDIR
        exit 0
      fi
      pythonexe="-p $OPTARG"
      ;;
    v)
      vewscript=$OPTARG
      if [[ ! -x "$vewscript" ]]; then
      echo "$OPTARG: this virtualenvwrapper.sh script does not exist or is not executable"
        cd $CURDIR
        exit 0
      fi
      ;;
    o)
      optimise=1
      ;;
    n)
      nopython=1
      ;;
    u)
      uninstall=1
      ;;
    c)
      distclean=0 # remove distclean
      ;;
    g)
      echo "Are you sure you really want to run 'git clean -dxf'? (y/n):"
      read -n 1 ynanswer
      if [ "$ynanswer" == "y" ]; then
        gitclean=1
      fi
      ;;
    h)
      echo -e $usage
      cd $CURDIR
      exit 0
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

if [[ $isbranch -eq 0 ]]; then
  echo -e "No branch has been given\n"
  echo -e $usage
  cd $CURDIR
  exit 0
fi
 
baseenv=${HOME}/lscsoft/.virtualenvs

# use virtualenvwrapper
export WORKON_HOME=$baseenv

# check if workon function is defined (if not then source the virtualenvwrapper script
if [ ! -n "$(type -t workon)" ] || [ ! "$(type -t workon)" = function ]; then
  # see if virtialenvwrapper.sh script has been given
  if [ -r $vewscript ]; then
    source $vewscript
  else
    # try and find script
    venvwrapper=`which virtualenvwrapper.sh`
    if [[ $? -eq 0 ]]; then
      source $venvwrapper
    else
      # could not find a virtualenvwrapper
      echo "Could not find virtualenvwrapper.sh in your PATH"
      cd $CURDIR
      exit 0
    fi
  fi
fi

# following http://virtualenvwrapper.readthedocs.org/en/latest/scripts.html#postactivate add an extra space to command prompt between env and prompt
echo "#!/bin/bash" > $VIRTUALENVWRAPPER_HOOK_DIR/postactivate
echo 'PS1="(`basename \"$VIRTUAL_ENV\"`) $_OLD_VIRTUAL_PS1"' >> $VIRTUALENVWRAPPER_HOOK_DIR/postactivate

if [[ $nopython -eq 0 ]]; then
  # some things to install (upgrade distribute if possible) in all virtual enviroments
  pipinstalls=("--upgrade distribute" "numpy" "scipy" "matplotlib" "corner" "astropy" "python-crontab" "h5py" "healpy" "pandas" "scotchcorner")
  postmkvirtualenv=$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv
  echo "#!/bin/bash" > $postmkvirtualenv
  for pr in "${pipinstalls[@]}"; do
    # add work around for very old pip on atlas cluster that does not have --no-cache-dir option
    if [[ $HOSTNAME == "atlas"* ]]; then
      echo "pip install $pr" >> $postmkvirtualenv
    else
      echo "pip install --no-cache-dir $pr" >> $postmkvirtualenv
    fi
  done
else # remove any previous postmkvirtualenv (so that things do not get reinstalled on new envs if not wanted)
  if [ -a $postmkvirtualenv ]; then
    > $postmkvirtualenv # empty the file
  fi
fi

# set path to virtual environment
ENV=$thisbranch

# components of lalsuite
lalsuite=("lal" "lalframe" "lalmetaio" "lalxml" "lalsimulation" "lalburst" "lalinspiral" "lalpulsar" "lalstochastic" "laldetchar" "lalinference" "lalapps")

lalsuitepy=("glue" "pylal")

# check if virtual environment already exists - if not create it otherwise activate it
if [[ ! -e $baseenv/$ENV/bin/activate ]]; then
  # create virtual environment<commit_msg>Define postmkvirtualenv file outside of if statement<commit_after>#!/bin/bash

# Script to create a virtual environment for, and (re)install, a particular lalsuite branch

LALSUITE_LOCATION=${HOME}/lscsoft/lalsuite

# current location
CURDIR=`pwd`

# get a list of the current git branch names
cd $LALSUITE_LOCATION

branches=$(git branch | cut -c 3-) # see http://stackoverflow.com/a/3846451/1862861

# command lines arguments:
#   -b branchname (the git branch name) [required]
#   -n (do not install additional python packages)
#   -p python (the path to the required python version)
#   -u uninstall previous install (rather than just doing a distclean)
#   -c only re-run 'make install' with running 'make distclean' beforehand
#   -g perform 'git clean -dxf' to remove superfluous files 
#   -v (path to the virtualenvwrapper.sh script)
#   -o compile with further optimisation
#   -h (print this info)

usage="Usage $0 -b (-n -u -c -g -p -v -o -m -h):\n\t-b\tbranchname (the git branch name) [required]\n\
\t-n\tdo not install a selection of additional python packages [optional]\n\
\t-u\tuninstall previous lalsuite install (rather than just doing a 'make distclean') [optional]\n\
\t-c\tonly re-run 'make install' without 'make distclean' beforehand [optional]\n\
\t-g\tperform 'git clean -dxf' to remove superfluous files (BE CAREFUL!) [optional]\n\
\t-p\tpython (the path to the required python executable for installing\n\t\tthe virtual environment) [optional]\n\
\t-v\tpath to the virtualenvwrapper.sh script [optional]\n\
\t-o\tcompile lalsuite with the -O3 optimisation [optional]\n\
\t-h\thelp\n"

if [[ $# -eq 0 ]]; then
  echo -e $usage
  cd $CURDUR
  exit 0
fi

nopython=0 # by default install additional python packages
pythonexe=""
isbranch=0
vewscript="jskgkgksbdkuylfzgslf" # some gibberish
optimise=0
unintsall=0
gitclean=0
distclean=1

while getopts ":b:p:v:nougch" opt; do
  case $opt in
    b)
      thisbranch=$OPTARG
      # check argument is one of the current git branches
      for branch in ${branches[@]}; do
        if [[ $branch == $thisbranch ]]; then
          isbranch=1
          break
        fi
      done

      if [[ $isbranch -eq 0 ]]; then # check if branch is found
        echo "Specified branch \"${thisbranch}\" is not in the git repo. Use one of the following:"
        for branch in ${branches[@]}; do
          echo "  $branch"
        done
        cd $CURDIR
        exit 0
      fi
      ;;
    p)
      pythonexe=$OPTARG
      if [[ ! -x "$OPTARG" ]]; then
        echo "$OPTARG: this python executable does not exist or is not executable"
        cd $CURDIR
        exit 0
      fi
      pythonexe="-p $OPTARG"
      ;;
    v)
      vewscript=$OPTARG
      if [[ ! -x "$vewscript" ]]; then
      echo "$OPTARG: this virtualenvwrapper.sh script does not exist or is not executable"
        cd $CURDIR
        exit 0
      fi
      ;;
    o)
      optimise=1
      ;;
    n)
      nopython=1
      ;;
    u)
      uninstall=1
      ;;
    c)
      distclean=0 # remove distclean
      ;;
    g)
      echo "Are you sure you really want to run 'git clean -dxf'? (y/n):"
      read -n 1 ynanswer
      if [ "$ynanswer" == "y" ]; then
        gitclean=1
      fi
      ;;
    h)
      echo -e $usage
      cd $CURDIR
      exit 0
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

if [[ $isbranch -eq 0 ]]; then
  echo -e "No branch has been given\n"
  echo -e $usage
  cd $CURDIR
  exit 0
fi
 
baseenv=${HOME}/lscsoft/.virtualenvs

# use virtualenvwrapper
export WORKON_HOME=$baseenv

# check if workon function is defined (if not then source the virtualenvwrapper script
if [ ! -n "$(type -t workon)" ] || [ ! "$(type -t workon)" = function ]; then
  # see if virtialenvwrapper.sh script has been given
  if [ -r $vewscript ]; then
    source $vewscript
  else
    # try and find script
    venvwrapper=`which virtualenvwrapper.sh`
    if [[ $? -eq 0 ]]; then
      source $venvwrapper
    else
      # could not find a virtualenvwrapper
      echo "Could not find virtualenvwrapper.sh in your PATH"
      cd $CURDIR
      exit 0
    fi
  fi
fi

# following http://virtualenvwrapper.readthedocs.org/en/latest/scripts.html#postactivate add an extra space to command prompt between env and prompt
echo "#!/bin/bash" > $VIRTUALENVWRAPPER_HOOK_DIR/postactivate
echo 'PS1="(`basename \"$VIRTUAL_ENV\"`) $_OLD_VIRTUAL_PS1"' >> $VIRTUALENVWRAPPER_HOOK_DIR/postactivate

postmkvirtualenv=$VIRTUALENVWRAPPER_HOOK_DIR/postmkvirtualenv

if [[ $nopython -eq 0 ]]; then
  # some things to install (upgrade distribute if possible) in all virtual enviroments
  pipinstalls=("--upgrade distribute" "numpy" "scipy" "matplotlib" "corner" "astropy" "python-crontab" "h5py" "healpy" "pandas" "scotchcorner")
  echo "#!/bin/bash" > $postmkvirtualenv
  for pr in "${pipinstalls[@]}"; do
    # add work around for very old pip on atlas cluster that does not have --no-cache-dir option
    if [[ $HOSTNAME == "atlas"* ]]; then
      echo "pip install $pr" >> $postmkvirtualenv
    else
      echo "pip install --no-cache-dir $pr" >> $postmkvirtualenv
    fi
  done
else # remove any previous postmkvirtualenv (so that things do not get reinstalled on new envs if not wanted)
  if [ -a $postmkvirtualenv ]; then
    > $postmkvirtualenv # empty the file
  fi
fi

# set path to virtual environment
ENV=$thisbranch

# components of lalsuite
lalsuite=("lal" "lalframe" "lalmetaio" "lalxml" "lalsimulation" "lalburst" "lalinspiral" "lalpulsar" "lalstochastic" "laldetchar" "lalinference" "lalapps")

lalsuitepy=("glue" "pylal")

# check if virtual environment already exists - if not create it otherwise activate it
if [[ ! -e $baseenv/$ENV/bin/activate ]]; then
  # create virtual environment
{'fraction_non_alphanumeric': 0.11234976484932939, 'fraction_numerical': 0.008186727051036406, 'mean_word_length': 2.919112627986348, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4003660', 'n_tokens_mistral': 3971, 'n_tokens_neox': 3823, 'n_words': 1301}