Files
Pmodules/Pmodules/libpmodules.bash.in
T
gsell 8c247f673a code cleanup
there is one important change:
The keys in Dir2OverlayMap are now the modulefiles root + group name.
Example: New '/opt/psi/Tools', old '/opt/psi/Tools/modulefiles'
2022-06-13 11:06:37 +02:00

167 lines
4.2 KiB
Bash

#!/bin/bash
declare PMODULES_MODULEFILES_DIR='modulefiles'
declare PMODULES_CONFIG_DIR='config'
declare -A GroupDepths=()
declare -A Subcommands=()
declare -A Options=()
declare -A Help=()
declare -a Overlays=()
declare -A OverlayInfo
declare -a UsedOverlays
declare -A Dir2OverlayMap
declare -r ol_normal='n'
declare -r ol_hiding='h'
declare -r ol_replacing='r'
# initialize help text of 'module --version'
Help['version']="
Pmodules @PMODULES_VERSION@ using Tcl Environment Modules
Copyright GNU GPL v2
"
#
# display help text for command given in $1
#
print_help() {
echo -e "${Help[$1]}" 1>&2
std::die 1
}
#
# compute depth of modulefile directory.
#
# Args:
# $1: absolute path of a modulefile directory
#
compute_group_depth () {
local -r dir=$1
test -d "${dir}" || return 1
local group=${dir%/*}
local group=${group##*/}
[[ -n "${GroupDepths[${group}]}" ]] && return 0
local -i depth=$(${find} "${dir}" -depth \( -type f -o -type l \) \
-printf "%d" -quit 2>/dev/null)
(( depth-=2 ))
# if a group doesn't contain a modulefile, depth is negativ
# :FIXME: better solution?
(( depth < 0 )) && (( depth = 0 ))
GroupDepths[$group]=${depth}
}
#
# (Re-)Scan available groups in given overlays and compute group depth's
#
# Args:
# $@: overlay names
#
scan_groups () {
local ol
for ol in "$@"; do
local mod_root="${OverlayInfo[${ol}:mod_root]}"
local dir
for dir in ${mod_root}/*/${PMODULES_MODULEFILES_DIR}; do
compute_group_depth "${dir}"
Dir2OverlayMap[${dir%/${PMODULES_MODULEFILES_DIR}*}]="${ol}"
done
done
}
pm::read_config(){
local -a config_files=()
_get_config_files(){
#
# return array with overlay configuration files
#
# Args:
# $1 [upvar] result
if [[ -v PMODULES_OVERLAYS_DEF ]]; then
test -r "${PMODULES_OVERLAYS_DEF}" || \
std::die 3 \
"%s -- %s" \
"overlay definition file is not readable" \
"$_"
config_files+=("${PMODULES_OVERLAYS_DEF}")
fi
if [[ -r "${HOME}/.Pmodules/Pmodules.yaml" ]]; then
config_files+=("${HOME}/.Pmodules/Pmodules.yaml")
fi
test -r "${PMODULES_HOME%%/Tools*}/config/Pmodules.yaml" || \
std::die 3 \
"%s %s -- %s" \
"base overlay definition file" \
"does not exist or is not readable" \
"$_"
config_files+=("${PMODULES_HOME%%/Tools*}/config/Pmodules.yaml")
}
_get_ol_names(){
${yq} -Ne eval-all '. as $item ireduce ({}; . *+ $item) |.Overlays|keys()' "${config_files[@]}" | awk '{print $2}'
}
_get_config_files
eval $(std::parse_yaml "${config_files[-1]}" 'cfg_')
[[ -v cfg_DefaultGroups ]] && DefaultGroups="${cfg_DefaultGroups}"
[[ -v cfg_DefaultReleaseStages ]] && DefaultReleaseStages="${cfg_DefaultReleaseStages}"
[[ -v cfg_ReleaseStages ]] && ReleaseStages="${cfg_ReleaseStages}"
unset ${!cfg_*}
#
# loop over all overlays and save config in OverlayInfo and Dir2OverlayMap
#
# - at least install_root must be specified
# - modulefiles_root default so install_root
# - the type defaults to ${ol_normal}
#
Overlays=( $(_get_ol_names) )
local ol=''
for ol in "${Overlays[@]}"; do
eval $(${yq} -Ne eval-all \
". as \$item ireduce ({}; . *+ \$item) |.Overlays.${ol}" \
"${config_files[@]}" | \
sed 's/: /=/; s/\(.*\)/local \1/')
[[ -n ${install_root} ]] || \
std::die 3 \
"install_root missing for overlay -- ${ol}"
[[ -d ${install_root} ]] || \
std::die 3 \
"Invalid installation root directory for overlay '${ol}' -- ${install_root}"
OverlayInfo[${ol}:inst_root]="${install_root}"
: ${modulefiles_root:=${install_root}}
[[ -d ${modulefiles_root} ]] || \
std::die 3 \
"Invalid modulefiles root directory for overlay '${ol}' -- ${modulefiles_root}"
OverlayInfo[${ol}:mod_root]="${modulefiles_root}"
Dir2OverlayMap[${modulefiles_root}]="${ol}"
: ${type:=${ol_normal}}
case ${type} in
${ol_normal} | ${ol_replacing} | ${ol_hiding} )
:
;;
* )
std::die 3 "Invalid type for overlay '${ol}' -- ${type}"
;;
esac
OverlayInfo[${ol}:type]="${type}"
OverlayInfo[${ol}:used]='no'
unset type modulefiles_root install_root
done
}
# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End: