diff --git a/Pmodules/bash b/Pmodules/bash index 976cd75..893730f 100644 --- a/Pmodules/bash +++ b/Pmodules/bash @@ -8,8 +8,34 @@ if [ ${BASH_VERSINFO:-0} -lt 3 ]; then return 1 fi +############################################################################ +# some sanity checks +# +if [[ ! -d ${PMODULES_ROOT} ]]; then + echo "Oops: ${PMODULES_ROOT}: Set as Pmodules root, but this is not a directory." 1>&2 + return 1 +fi +if [[ ! -d ${PMODULES_HOME} ]]; then + echo "Oops: ${PMODULES_HOME}: Set as Pmodules home, but this is not a directory." 1>&2 + return 1 +fi + ############################################################################# -# implement module comand as function +# initialize bash completion +# +if [[ -r "${PMODULES_HOME}/init/bash_completion" ]]; then + source "${PMODULES_HOME}/init/bash_completion" +fi + +############################################################################# +# legacy... +# +unset MODULE_VERSION +unset MODULE_VERSION_STACK +unset MODULESHOME + +############################################################################# +# implement module comand as shell function # module() { unset BASH_ENV @@ -56,122 +82,6 @@ module() { } export -f module -############################################################################# -# helper functions -# -std::append_path () { - local -r P="$1" - local -r d="$2" - - if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then - if [[ -z ${!P} ]]; then - eval $P=\"${d}\" - else - eval $P=\"${!P}:${d}\" - fi - fi -} - -# -# Replace or remove a directory in a path variable. -# -# To remove a dir: -# std::replace_path PATH -# -# To replace a dir: -# std::replace_path PATH /replacement/path -# -# Args: -# $1 name of the shell variable to set (e.g. PATH) -# $2 a grep pattern identifying the element to be removed/replaced -# $3 the replacement string (use "" for removal) -# -# Based on solution published here: -# https://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts -# -std::replace_path () { - local -r path="$1" - local -r removepat="$2" - local -r replacestr="$3" - - local -r removestr=$(echo "${!path}" | tr ":" "\n" | grep -m 1 "^$removepat\$") - export $path="$(echo "${!path}" | tr ":" "\n" | sed "s:^${removestr}\$:${replacestr}:" | - sed '/^\s*$/d' | tr "\n" ":" | sed -e 's|^:||' -e 's|:$||')" -} - -save_env() { - local s='' - while (( $# > 0 )); do - s+="$( typeset -p $1 );" - shift - done - echo export PMODULES_ENV=$( "${PMODULES_HOME}/sbin/base64" --wrap=0 <<< "$s" ) -} - -############################################################################ -# some sanity checks -# -if [[ ! -d ${PMODULES_ROOT} ]]; then - echo "Oops: ${PMODULES_ROOT}: Set as Pmodules root, but this is not a directory." 1>&2 - return 1 -fi -if [[ ! -d ${PMODULES_HOME} ]]; then - echo "Oops: ${PMODULES_HOME}: Set as Pmodules home, but this is not a directory." 1>&2 - return 1 -fi - -############################################################################# -# setup environment -# -declare -x LOADEDMODULES='' -declare -x _LMFILES_='' -declare -x MODULEPATH='' -for group in ${PMODULES_DEFAULT_GROUPS//:/ }; do - std::append_path MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" -done -declare -x UsedReleases='' -for r in ${PMODULES_DEFAULT_RELEASES//:/ }; do - std::append_path UsedReleases "${r}" -done - -eval $(save_env UsedReleases PMODULES_DEFAULT_RELEASES PMODULES_DEFAULT_GROUPS PMODULES_DEFINED_RELEASES) -unset UsedReleases -unset PMODULES_DEFAULT_RELEASES -unset PMODULES_DEFAULT_GROUPS -unset PMODULES_DEFINED_RELEASES - -std::replace_path PATH "${PMODULES_HOME%/*}/.*" -std::replace_path MANPATH "${PMODULES_HOME%/*}/.*" -std::append_path PATH "${PMODULES_HOME}/bin" - -if [[ -r /etc/man.config ]]; then - declare _manconf='/etc/man.config' -elif [[ -r /etc/man.conf ]]; then - declare _manconf='/etc/man.conf' -fi -if [[ -n ${_manconf} ]]; then - while read name value rest; do - std::append_path MANPATH "${value}" - done < <(grep "^MANPATH\s" "${_manconf}") - unset _manconf -else - std::append_path MANPATH "${PMODULES_HOME}/share/man" - std::append_path MANPATH "/usr/share/man" -fi - -############################################################################# -# initialize bash completion -# -if [[ -r "${PMODULES_HOME}/init/bash_completion" ]]; then - source "${PMODULES_HOME}/init/bash_completion" -fi - -############################################################################# -# legacy... -# -unset MODULE_VERSION -unset MODULE_VERSION_STACK -unset MODULESHOME # Local Variables: # mode: sh diff --git a/Pmodules/libstd.bash b/Pmodules/libstd.bash index 1773f17..ad7f0e6 100644 --- a/Pmodules/libstd.bash +++ b/Pmodules/libstd.bash @@ -67,16 +67,16 @@ std::get_abspath() { } std::append_path () { - local -r P=$1 - local -r d=$2 + local -r P="$1" + local -r d="$2" if ! echo ${!P} | egrep -q "(^|:)${d}($|:)" ; then - if [[ -z ${!P} ]]; then - eval $P=${d} - else - eval $P=${!P}:${d} - fi - fi + if [[ -z ${!P} ]]; then + eval $P=\"${d}\" + else + eval $P=\"${!P}:${d}\" + fi + fi } std::prepend_path () { @@ -105,6 +105,33 @@ std::remove_path() { eval ${P}="${new_path:1}" } +# +# Replace or remove a directory in a path variable. +# +# To remove a dir: +# std::replace_path PATH +# +# To replace a dir: +# std::replace_path PATH /replacement/path +# +# Args: +# $1 name of the shell variable to set (e.g. PATH) +# $2 a grep pattern identifying the element to be removed/replaced +# $3 the replacement string (use "" for removal) +# +# Based on solution published here: +# https://stackoverflow.com/questions/273909/how-do-i-manipulate-path-elements-in-shell-scripts +# +std::replace_path () { + local -r path="$1" + local -r removepat="$2" + local -r replacestr="$3" + + local -r removestr=$(echo "${!path}" | tr ":" "\n" | grep -m 1 "^$removepat\$") + export $path="$(echo "${!path}" | tr ":" "\n" | sed "s:^${removestr}\$:${replacestr}:" | + sed '/^\s*$/d' | tr "\n" ":" | sed -e 's|^:||' -e 's|:$||')" +} + # # split file name # diff --git a/Pmodules/modulecmd.bash.in b/Pmodules/modulecmd.bash.in index 3605424..290d7af 100755 --- a/Pmodules/modulecmd.bash.in +++ b/Pmodules/modulecmd.bash.in @@ -1378,19 +1378,39 @@ subcommand_refresh() { subcommand_generic0 refresh "$@" } +reset_modulepath() { + MODULEPATH='' + local group + local root="${PMODULES_ROOT}" + for group in ${PMODULES_DEFAULT_GROUPS}; do + local dir="${root}/${group}/${PMODULES_MODULEFILES_DIR}" + [[ -d "${dir}" ]] && std::prepend_path MODULEPATH "${dir}" + done +} + +reset_used_groups() { + PMODULES_USED_GROUPS='' + local group + for group in ${PMODULES_DEFAULT_GROUPS}; do + std::append_path PMODULES_USED_GROUPS "${group}" + done +} + +reset_used_releases() { + declare -g UsedReleases='' + for r in ${PMODULES_DEFAULT_RELEASES//:/ }; do + std::append_path UsedReleases "${r}" + done +} + ############################################################################## # # purge # subcommand_purge() { subcommand_generic0 purge "$@" - PMODULES_USED_GROUPS='' - MODULEPATH='' - - for group in ${PMODULES_DEFAULT_GROUPS}; do - std::append_path MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" - std::append_path PMODULES_USED_GROUPS "${group}" - done + reset_modulepath + reset_used_groups pbuild::export_env ${g_shell} MODULEPATH PMODULES_USED_GROUPS } @@ -1428,25 +1448,45 @@ subcommand_list() { "${modulecmd}" "${g_shell}" list "${opts[@]}" } +init_path() { + std::replace_path PATH "${PMODULES_HOME%/*}/.*" + std::append_path PATH "${PMODULES_HOME}/bin" +} + +init_manpath() { + std::replace_path MANPATH "${PMODULES_HOME%/*}/.*" + + if [[ -r /etc/man.config ]]; then + declare _manconf='/etc/man.config' + elif [[ -r /etc/man.conf ]]; then + declare _manconf='/etc/man.conf' + fi + if [[ -n ${_manconf} ]]; then + while read name value rest; do + std::append_path MANPATH "${value}" + done < <(grep "^MANPATH\s" "${_manconf}") + unset _manconf + else + std::append_path MANPATH "${PMODULES_HOME}/share/man" + std::append_path MANPATH "/usr/share/man" + fi +} + ############################################################################## pmodules_init() { declare -g LOADEDMODULES='' + declare -g _LMFILES_='' declare -g PMODULES_USED_GROUPS='' declare -g MODULEPATH='' - declare -g _LMFILES_='' - for group in ${PMODULES_DEFAULT_GROUPS}; do - std::append_path MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" - std::append_path PMODULES_USED_GROUPS "${group}" - done declare -ag Groups='()' declare -Ag GroupDepths='()' - declare -g UsedReleases='' declare -g UseFlags=() - for r in ${PMODULES_DEFAULT_RELEASES//:/ }; do - std::append_path UsedReleases "${r}" - done - + reset_modulepath + reset_used_groups + reset_used_releases + init_path + init_manpath } ##############################################################################