diff --git a/Pmodules/modulecmd.bash.in b/Pmodules/modulecmd.bash.in index c2ac3ee..af165f3 100644 --- a/Pmodules/modulecmd.bash.in +++ b/Pmodules/modulecmd.bash.in @@ -1845,6 +1845,13 @@ USAGE: ' subcommand_purge() { + # + # unload all loaded modules + # + # Note: + # If a Pmodule module is loaded, it will *not* be + # unloaded! + # local -r subcommand='purge' local -a args=() while (( $# > 0)); do @@ -1865,13 +1872,55 @@ subcommand_purge() { "${CMD}" "${subcommand}" \ "no arguments allowd" fi - # we cannot unset PMODULES_HOME, otherwise the module function - # would fail. - local saved_home="${PMODULES_HOME}" - "${modulecmd}" "${Shell}" "${subcommand}" + + # is a Pmodule module loaded? + # if yes, save name in variable 'pmodule' + local pmodule='' + IFS=':' + local -a lmfiles=($_LMFILES_) + unset IFS + for f in "${lmfiles[@]}"; do + if [[ $f == */${PMODULES_MODULEFILES_DIR}/Pmodules/* ]]; then + pmodule="${f##*/${PMODULES_MODULEFILES_DIR}/}" + break; + fi + done + + # run module purge + # since we might have to reload a Pmodules module, we cannot + # just run 'modulecmd ${Shell} purge' + local tmpfile=$( "${mktemp}" /tmp/Pmodules.XXXXXX ) \ + || std::die 1 "Oops: unable to create tmp file!" + local output=$("${modulecmd}" 'bash' 'purge' 2> "${tmpfile}") + + local error=$( < "${tmpfile}") + if [[ "${error}" =~ ":ERROR:" ]]; then + local s=${error%%$'\n'*} + local error_txt='failed' + std::die 3 "%s %s: %s" \ + "${CMD}" "${subcommand}" \ + "${error_txt}" + fi + if [[ "${Shell}" == "sh" ]]; then + # for sh-like shells just echo + echo "${output}" + else + # re-run with right shell + "${modulecmd}" "${Shell}" 'purge' + fi + eval "${output}" + if [[ -n "${error}" ]]; then + echo "${error}" 1>&2 + fi + reset_modulepath - reset_used_groups - PMODULES_HOME="${saved_home}" + if [[ -n "${pmodule}" ]]; then + # reload a previously loaded Pmodule module + # stderr is redirected to /dev/null, otherwise + # we may get output like + # 'unstable module has been loaded' + subcommand_load "${pmodule}" 2> /dev/null + fi export_env MODULEPATH PMODULES_HOME }