diff --git a/Pmodules/modulecmd.bash.in b/Pmodules/modulecmd.bash.in index 624e7da..ca4c143 100644 --- a/Pmodules/modulecmd.bash.in +++ b/Pmodules/modulecmd.bash.in @@ -77,7 +77,10 @@ export_env() { done } +declare g_env_must_be_saved='no' + save_env() { + [[ ${g_env_must_be_saved} == 'no' ]] && return 0 local -r shell="$1" shift local s='' @@ -497,6 +500,7 @@ subcommand_load() { std::die 3 "%s %s: illegal release name -- %s\n" \ "${CMD}" 'load' "${release}" std::append_path UsedReleases "${release}" + g_env_must_be_saved='yes' fi fi local found='' @@ -967,7 +971,8 @@ get_group_depth () { # We set it to 0 (even this might be wrong). # :FIXME: better solution? (( depth < 0 )) && (( depth = 0 )) - GroupDepths[$group]=${depth} + GroupDepths[$group]=${depth} + g_env_must_be_saved='yes' } # @@ -1125,8 +1130,10 @@ subcommand_use() { if is_release "${arg}"; then # releases are always *appended* std::append_path UsedReleases "${arg}" + g_env_must_be_saved='yes' elif [[ "${arg}" =~ "flag=" ]]; then std::append_path UseFlags "${arg/flag=}" + g_env_must_be_saved='yes' elif [[ "${arg}" =~ "overlay=" ]]; then local overlay="${arg/overlay=}" [[ -d "${overlay}" ]] || \ @@ -1142,6 +1149,7 @@ subcommand_use() { std::prepend_path MODULEPATH "${dir}" fi done + g_env_must_be_saved='yes' fi elif [[ ! ${arg} =~ */* ]] && is_group "${arg}"; then @@ -1150,6 +1158,7 @@ subcommand_use() { "${CMD}" "${FUNCNAME[0]##*_}" "${arg}" fi std::append_path UsedGroups "${arg}" + g_env_must_be_saved='yes' for overlay in "${!Overlays[@]}"; do for group in ${UsedGroups//:/ }; do local dir="${overlay}/" @@ -1171,7 +1180,7 @@ subcommand_use() { shift done - export_env ${g_shell} MODULEPATH UsedGroups + export_env ${g_shell} MODULEPATH } local -a args=() @@ -1252,7 +1261,8 @@ subcommand_unuse() { elif [[ "${arg}" =~ "flag=" ]]; then std::remove_path UseFlags "${arg/flag=}" - + g_env_must_be_saved='yes' + elif [[ "${arg}" =~ "overlay=" ]]; then local overlay="${arg/overlay=}" [[ -d "${overlay}" ]] || \ @@ -1272,6 +1282,7 @@ subcommand_unuse() { std::remove_path MODULEPATH "${dir}" fi done + g_env_must_be_saved='yes' fi elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then @@ -1282,27 +1293,24 @@ subcommand_unuse() { "${arg}" fi std::remove_path UsedGroups "${arg}" - dirs_to_remove+=( ${modulefiles_dir} ) - + g_env_must_be_saved='yes' + std::remove_path MODULEPATH "${modulefiles_dir}" + elif [[ -d ${arg} ]]; then local normalized_dir=$(cd "${arg}" && pwd) - dirs_to_remove+=( ${normalized_dir} ) + std::remove_path MODULEPATH "${normalized_dir}" + elif [[ ${arg} =~ ^${PMODULES_ROOT} ]]; then std::die 3 "%s %s: illegal directory -- %s\n." \ "${CMD}" "${FUNCNAME[0]##*_}" "${arg}" + else std::die 3 "%s %s: not a directory -- %s\n" \ "${CMD}" "${FUNCNAME[0]##*_}" "${arg}" fi shift done - - declare -g UsedGroups - export_env ${g_shell} UsedGroups - [[ ${#dirs_to_remove[@]} == 0 ]] && return - for dir in "${dirs_to_remove[@]}"; do - "${modulecmd}" "${g_shell}" "${subcommand}" "${dir}" - done + export_env "${g_shell}" 'MODULEPATH' } ############################################################################## @@ -1360,6 +1368,7 @@ reset_used_groups() { for group in ${PMODULES_DEFAULT_GROUPS}; do std::append_path UsedGroups "${group}" done + g_env_must_be_saved='yes' } reset_used_releases() { @@ -1367,6 +1376,7 @@ reset_used_releases() { for r in ${PMODULES_DEFAULT_RELEASES//:/ }; do std::append_path UsedReleases "${r}" done + g_env_must_be_saved='yes' } init_path() { @@ -1451,7 +1461,7 @@ subcommand_purge() { "${modulecmd}" "${g_shell}" "${subcommand}" reset_modulepath reset_used_groups - export_env ${g_shell} MODULEPATH UsedGroups + export_env ${g_shell} MODULEPATH } ##############################################################################