diff --git a/turing/support/bash-module-env.sh b/turing/support/bash-module-env.sh new file mode 100755 index 0000000..2a6e8ac --- /dev/null +++ b/turing/support/bash-module-env.sh @@ -0,0 +1,57 @@ +# -*- bash -*- +# +# + +# Some cluster defaults (may change over time) +# Reference: /etc/profile.d/modules.sh +MODULESHOME_DEFAULT=/cm/local/apps/environment-modules/3.2.10/Modules/3.2.10 +MODULEPATH_DEFAULT=/cm/local/modulefiles:/cm/shared/modulefiles:/cm/shared/compilers + +# RestoreModuleEnv restores module environment at batch-job runtime to be +# exactly the same as the environment at the time of job submission. +# It requires the following variables to be set: +# - MODULESHOME (optional; default provided above) +# - MODULEPATH +# - LAODEDMODULES + +RestoreModuleEnv () { + local LOADEDMODULES_SAVE + : ${MODULESHOME:=$MODULESHOME_DEFAULT} + : ${MODULEPATH:=$MODULEPATH_DEFAULT} + export MODULESHOME + export MODULEPATH + + LOADEDMODULES_SAVE=$LOADEDMODULES + unset LOADEDMODULES + + source "$MODULESHOME/init/bash" + + if [ -n "$LOADEDMODULES_SAVE" ]; then + module load ${LOADEDMODULES_SAVE//:/" "} + fi + + if [ -n "$Debug" -o -n "$DEBUG_MODULES" ]; then + echo "New loaded modules: $LOADEDMODULES" + module list + fi +} + +InitModuleEnv () { + # Only do the initialization part of the module so "module" command + # is available for the script + local LOADEDMODULES_SAVE + : ${MODULESHOME:=$MODULESHOME_DEFAULT} + : ${MODULEPATH:=$MODULEPATH_DEFAULT} + export MODULESHOME + export MODULEPATH + + source "$MODULESHOME/init/bash" +} + +# For convenience: +if [ "$1" = restore ]; then + RestoreModuleEnv +elif [ "$1" = init ]; then + InitModuleEnv +fi +