Merge branch '34-initialisation-cleanup' into 'master'

Resolve "initialisation cleanup"

Closes #34

See merge request Pmodules/src!4
This commit is contained in:
2019-04-30 13:23:10 +02:00
4 changed files with 178 additions and 228 deletions
+27 -117
View File
@@ -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 <pattern>
#
# To replace a dir:
# std::replace_path PATH <pattern> /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
+25 -14
View File
@@ -28,7 +28,6 @@ proc module-addgroup { group } {
global version
debug "called with arg $group"
set Implementation [file join {*}$::implementation]
set GROUP [string toupper $group]
regsub -- "-" ${GROUP} "_" GROUP
@@ -41,10 +40,14 @@ proc module-addgroup { group } {
if { [module-info mode load] } {
debug "mode is load"
prepend-path MODULEPATH $::PmodulesRoot/$group/$::PmodulesModulfilesDir/$Implementation
prepend-path PMODULES_USED_GROUPS $group
prepend-path MODULEPATH [file join \
$::PmodulesRoot \
$group \
$::PmodulesModulfilesDir \
{*}$::variant]
prepend-path UsedGroups $group
debug "mode=load: new MODULEPATH=$env(MODULEPATH)"
debug "mode=load: new PMODULES_USED_GROUPS=$env(PMODULES_USED_GROUPS)"
debug "mode=load: new UsedGroups=$env(UsedGroups)"
} elseif { [module-info mode remove] } {
set GROUP [string toupper $group]
debug "remove hierarchical group '${GROUP}'"
@@ -65,14 +68,22 @@ proc module-addgroup { group } {
debug "no orphan modules to unload"
}
debug "mode=remove: $env(MODULEPATH)"
remove-path MODULEPATH $::PmodulesRoot/$group/$::PmodulesModulfilesDir/$Implementation
debug "mode=remove: $env(PMODULES_USED_GROUPS)"
remove-path PMODULES_USED_GROUPS $group
remove-path MODULEPATH [file join \
$::PmodulesRoot \
$group \
$::PmodulesModulfilesDir \
{*}$::variant]
debug "mode=remove: $env(UsedGroups)"
remove-path UsedGroups $group
}
if { [module-info mode switch2] } {
debug "mode=switch2"
append-path MODULEPATH $::PmodulesRoot/$group/$::PmodulesModulfilesDir/[module-info name]
append-path PMODULES_USED_GROUPS ${group}
append-path MODULEPATH [file join \
$::PmodulesRoot \
$group \
$::PmodulesModulfilesDir \
[module-info name]]
append-path UsedGroups ${group}
}
}
@@ -304,7 +315,7 @@ proc _pmodules_init_global_vars { } {
global V_RELEASE
global V_PKG
global implementation
global variant
global PREFIX # prefix of package
debug "$::ModulesCurrentModulefile"
@@ -314,7 +325,8 @@ proc _pmodules_init_global_vars { } {
set pmodules_root [file split $::PmodulesRoot]
set pmodules_root_num_dirs [llength $pmodules_root]
set modulefile_root [file join {*}[lrange $modulefile 0 [expr $pmodules_root_num_dirs - 1]]]
set modulefile_root [file join {*}[lrange $modulefile 0 \
[expr $pmodules_root_num_dirs - 1]]]
if { $::PmodulesRoot != $modulefile_root } {
debug "stop sourcing: ${::PmodulesRoot} != $modulefile_root"
return
@@ -330,11 +342,10 @@ proc _pmodules_init_global_vars { } {
lassign [split $V -] V_PKG tmp
set V_RELEASE [lindex [split $tmp _] 0]
lassign [split $V_PKG .] V_MAJOR V_MINOR V_PATCHLVL
set implementation [lrange $rel_modulefile 2 end]
set prefix "$pmodules_root $group [lreverse_n $implementation 2]"
set variant [lrange $rel_modulefile 2 end]
set prefix "$pmodules_root $group [lreverse_n $variant 2]"
set PREFIX [file join {*}$prefix]
debug "PREFIX=$PREFIX"
debug "group of module $name: $group"
}
+35 -8
View File
@@ -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 <pattern>
#
# To replace a dir:
# std::replace_path PATH <pattern> /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
#
+91 -89
View File
@@ -37,7 +37,6 @@ declare verbosity_lvl=${PMODULES_VERBOSITY:-'verbose'}
shopt -s nullglob
declare -a Groups='()'
declare -A GroupDepths='()'
declare current_modulefile=''
declare g_shell=''
@@ -77,7 +76,7 @@ pbuild::save_env() {
pbuild::export_env ${shell} PMODULES_ENV
}
trap 'pbuild::save_env ${g_shell} Groups GroupDepths UsedReleases UseFlags PMODULES_DEFAULT_GROUPS PMODULES_DEFINED_RELEASES PMODULES_DEFAULT_RELEASES' EXIT
trap 'pbuild::save_env ${g_shell} GroupDepths UsedReleases UseFlags UsedGroups PMODULES_DEFAULT_GROUPS PMODULES_DEFINED_RELEASES PMODULES_DEFAULT_RELEASES' EXIT
print_version() {
echo "
@@ -485,15 +484,11 @@ is_group () {
[[ -n ${group} ]] && [[ -n ${GroupDepths[${group}]} ]] && return 0
# not yet cached or not a group
if [[ -d "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" ]]; then
Groups+=( "${group}" )
get_group_depths "${PMODULES_ROOT}" "${group}"
fi
return 1
get_group_depths "${PMODULES_ROOT}" "${group}"
}
is_used_group() {
[[ :${PMODULES_USED_GROUPS}: =~ :$1: ]]
[[ :${UsedGroups}: =~ :$1: ]]
}
module_is_loaded() {
@@ -1077,28 +1072,14 @@ subcommand_avail() {
done
}
# get available groups
# $1: root of modulefile hierarchy
#
get_groups () {
local -r root="$1"
{
cd "${root}"
# for some unknown reason [A-Z]* doesn't work on (some?) SL6 systems
for f in [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*; do
Groups+=( $f )
done
};
}
# compute depths or passed group
# Note: cwd must be Pmodules root directory
# $1: group
#
get_group_depth2 () {
get_group_depth () {
local -r group="$1"
local -r dir="${group}/${PMODULES_MODULEFILES_DIR}"
test -d "${dir}" || return 0
test -d "${dir}" || return 1
local tmp=$(find "${dir}" -depth -type f -o -type l 2>/dev/null| head -1)
local -a tmp2=( ${tmp//\// } )
local depth=${#tmp2[@]}
@@ -1110,20 +1091,6 @@ get_group_depth2 () {
GroupDepths[$group]=${depth}
}
# compute depths or passed group
# $1: root
# $2: group
#
get_group_depth () {
local -r root="$1"
local -r group="$2"
{
cd "${root}"
get_group_depth2 "${group}"
}
}
#
# Compute depth for all known groups
# $1: root of modulefile hierarchy
@@ -1132,8 +1099,8 @@ get_group_depths () {
{
cd "${root}"
local group
for group in "${Groups[@]}"; do
get_group_depth2 "${group}"
for group in [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*; do
get_group_depth "${group}"
done
};
}
@@ -1152,8 +1119,7 @@ rescan_groups() {
# for some unknown reason [A-Z]* doesn't work with some bash versions
for group in [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*; do
if [[ -z "${GroupDepths[${group}]}" ]]; then
Groups+=( "${group}" )
get_group_depth2 "${group}"
get_group_depth "${group}"
fi
done
};
@@ -1163,8 +1129,7 @@ rescan_groups() {
# use [-a|--append|-p|--prepend] [directory|group|release...]
#
subcommand_use() {
if (( ${#Groups[@]} == 0 )); then
get_groups "${PMODULES_ROOT}"
if (( ${#GroupDepths[@]} == 0 )); then
get_group_depths "${PMODULES_ROOT}"
fi
local saved_IFS=${IFS};
@@ -1176,12 +1141,12 @@ subcommand_use() {
local f
local r
std::info "Used groups:\n"
for f in ${PMODULES_USED_GROUPS//:/ }; do
for f in ${UsedGroups//:/ }; do
std::info "\t${f}\n"
done
std::info "\nUnused groups:\n"
local _group
for _group in "${Groups[@]}"; do
for _group in "${!GroupDepths[@]}"; do
local -i depth=${GroupDepths[${_group}]}
if ! is_used_group "${_group}" && (( depth == 0 )); then
std::info "\t${_group}\n"
@@ -1219,7 +1184,7 @@ subcommand_use() {
}
use () {
declare -g PMODULES_USED_GROUPS
declare -g UsedGroups
declare -g MODULEPATH
local dirs_to_add=()
@@ -1243,16 +1208,11 @@ subcommand_use() {
elif [[ "${arg}" =~ "flag=" ]]; then
std::append_path UseFlags "${arg/flag=}"
elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then
if [[ -z "${GroupDepths[$arg]}" ]]; then
# not yet cached group
Groups+=( "${arg}" )
get_group_depth "${PMODULES_ROOT}" "${arg}"
fi
if (( ${GroupDepths[$arg]} != 0 )); then
std::die 3 "%s %s: cannot add group to module path -- %s\n" \
"${CMD}" "${FUNCNAME[0]##*_}" "${arg}"
fi
std::append_path PMODULES_USED_GROUPS "${arg}"
std::append_path UsedGroups "${arg}"
dirs_to_add+=( ${modulefiles_dir} )
elif [[ ${arg} =~ ^${PMODULES_ROOT} ]]; then
std::die 3 "%s %s: illegal directory -- %s\n" \
@@ -1273,7 +1233,7 @@ subcommand_use() {
std::prepend_path MODULEPATH "${dir}"
fi
done
pbuild::export_env ${g_shell} MODULEPATH PMODULES_USED_GROUPS
pbuild::export_env ${g_shell} MODULEPATH UsedGroups
}
local opts=()
@@ -1339,7 +1299,7 @@ subcommand_unuse() {
std::die 3 "%s %s: cannot remove group from module path -- %s\n" \
"${CMD}" "${FUNCNAME[0]##*_}" "${arg}"
fi
std::remove_path PMODULES_USED_GROUPS "${arg}"
std::remove_path UsedGroups "${arg}"
dirs_to_remove+=( ${modulefiles_dir} )
elif [[ -d ${arg} ]]; then
local normalized_dir=$(cd "${arg}" && pwd)
@@ -1354,8 +1314,8 @@ subcommand_unuse() {
shift
done
declare -g PMODULES_USED_GROUPS
pbuild::export_env ${g_shell} PMODULES_USED_GROUPS
declare -g UsedGroups
pbuild::export_env ${g_shell} UsedGroups
[[ ${#dirs_to_remove[@]} == 0 ]] && return
for dir in "${dirs_to_remove[@]}"; do
subcommand_generic1 unuse "${dir}"
@@ -1378,20 +1338,40 @@ 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() {
UsedGroups=''
local group
for group in ${PMODULES_DEFAULT_GROUPS}; do
std::append_path UsedGroups "${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
pbuild::export_env ${g_shell} MODULEPATH PMODULES_USED_GROUPS
reset_modulepath
reset_used_groups
pbuild::export_env ${g_shell} MODULEPATH UsedGroups
}
##############################################################################
@@ -1428,25 +1408,49 @@ 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 PMODULES_USED_GROUPS=''
declare -g MODULEPATH=''
declare -g _LMFILES_=''
declare -gx LOADEDMODULES=''
declare -gx _LMFILES_=''
declare -gx UsedGroups=''
declare -gx 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
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
pbuild::export_env "${g_shell}" \
LOADEDMODULES \
_LMFILES_ \
MODULEPATH \
PATH
}
##############################################################################
@@ -1470,7 +1474,7 @@ subcommand_clear() {
esac
done
pmodules_init
pbuild::export_env ${g_shell} LOADEDMODULES PMODULES_USED_GROUPS MODULEPATH _LMFILES_
pbuild::export_env ${g_shell} LOADEDMODULES MODULEPATH _LMFILES_
}
##############################################################################
@@ -1544,14 +1548,14 @@ subcommand_search() {
# write results to a temporary file for later processing
local -r tmpfile=$( "${mktemp}" /tmp/Pmodules.XXXXXX ) \
|| std::die 1 "Oops: unable to create tmp file!\n"
local _group
local group
# loop over all groups
for _group in "${Groups[@]}"; do
for group in "${!GroupDepths[@]}"; do
# loop over all directories which can be added to
# MODULEPATH inside current group
local depth=${GroupDepths[${_group}]}
local depth=${GroupDepths[${group}]}
local mpaths=( $(find \
"${src_prefix}/${_group}/modulefiles" \
"${src_prefix}/${group}/modulefiles" \
-type d \
-mindepth ${depth} -maxdepth ${depth} \
2>/dev/null))
@@ -1577,7 +1581,7 @@ subcommand_search() {
[[ ${#mods[@]} == 0 ]] && continue
for (( i=0; i<${#mods[@]}; i+=2 )); do
printf "${fmt}" ${mods[i]} "${mods[i+1]}" \
${_group} "${requires}" >> "${tmpfile}"
${group} "${requires}" >> "${tmpfile}"
done
done
done
@@ -1657,8 +1661,7 @@ subcommand_search() {
modules+=( '' )
fi
if (( ${#Groups[@]} == 0 )) || [[ ${src_prefix} != ${PMODULES_ROOT} ]]; then
get_groups "${src_prefix}"
if (( ${#GroupDepths[@]} == 0 )) || [[ ${src_prefix} != ${PMODULES_ROOT} ]]; then
get_group_depths "${src_prefix}"
fi
@@ -1862,8 +1865,7 @@ if [[ -z "${subcommand}" ]]; then
std::die 1 "${CMD}: no sub-command specified.\n"
fi
if (( ${#Groups[@]} == 0 )); then
get_groups "${PMODULES_ROOT}"
if (( ${#GroupDepths[@]} == 0 )); then
get_group_depths "${PMODULES_ROOT}"
fi