Files
Pmodules/Pmodules/libpmodules.bash.in
T

177 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=()
# initialize help text of 'module --version'
Help['version']="
Pmodules @PMODULES_VERSION@ using Tcl Environment Modules @MODULES_VERSION@
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:
# $1: array of overlays
#
scan_groups () {
local -r overlays=( "$@" )
local overlay
for overlay in "${overlays[@]}"; do
local moduledir
for moduledir in ${overlay}/*/${PMODULES_MODULEFILES_DIR}; do
compute_group_depth "${moduledir}"
done
done
}
get_ol_config_files(){
#
# return array with overlay configuration files
#
# Args:
# $1 [upvar] result
local files=()
if [[ -v PMODULES_OVERLAYS_DEF ]]; then
test -r "${PMODULES_OVERLAYS_DEF}" || \
std::die 3 \
"%s -- %s" \
"overlay definition file is not readable" \
"$_"
files+=("${PMODULES_OVERLAYS_DEF}")
fi
if [[ -r "${HOME}/.Pmodules/Pmodules.yaml" ]]; then
files+=("${HOME}/.Pmodules/Pmodules.yaml")
fi
test -r "${PMODULES_ROOT}/config/Pmodules.yaml" || \
std::die 3 \
"%s %s -- %s" \
"base overlay definition file" \
"does not exist or is not readable" \
"$_"
files+=("${PMODULES_ROOT}/config/Pmodules.yaml")
std::upvar "$1" "${files[@]}"
}
get_ol_install_root(){
# $1 overlay name
# ${@:2} files
yq -Ne e ".overlays.$1.install_root" - < <(cat "${@:2}") 2>/dev/null
}
get_ol_modulefiles_root(){
# $1 overlay name
# ${@:2} files
yq -Ne e ".overlays.$1.modulefiles_root" - < <(cat "${@:2}") 2>/dev/null
}
get_ol_type(){
# $1 overlay name
# ${@:2} files
yq -Ne e ".overlays.$1.type" - < <(cat "${@:2}") 2>/dev/null
}
get_ol_names(){
# ${@} files
yq -Ne e ".overlays|keys" - < <(cat "$@") 2>/dev/null | cut -b3-
}
get_ol_info(){
# Args:
# $1 [in] overlay name or directory plus optional type
# $2 [upvar] overlay name
# $3 [upvar] overlay type
# $4 [upvar] overlay modulefiles root directory
# $5 [upvar] overlay prefix for software installation
#
local name_or_dir="${1%:*}"
local ol_name=''
local ol_install_root=''
local ol_modulefiles_root=''
local ol_type=''
local files=()
get_ol_config_files files
ol_install_root=$(get_ol_install_root "${name_or_dir}" "${files[@]}")
if (( $? == 0 )); then
# "$name_of_dir" is an overlay name
ol_name="${name_or_dir}"
else
# "$name_or_dir"" might be is a directory
local -a names=( $(get_ol_names "${files[@]}") )
for name in "${names[@]}"; do
ol_install_root=$(get_ol_install_root \
"$name_or_dir" \
"${files[@]}") || continue
# found a matching overlay name
ol_name="${name}"
break
done
fi
if [[ -n "${ol_name}" ]]; then
ol_modulefiles_root=$(get_ol_modulefiles_root \
"${ol_name}" \
"${files[@]}") || \
ol_modulefiles_root="${root_dir}"
ol_type=$(get_ol_type \
"${ol_name}" \
"${files[@]}") || \
ol_type="${ol_normal}"
elif [[ "${name_or_dir}" == 'default' ]]; then
ol_name='default'
ol_type="${ol_normal}"
ol_modulefiles_root="${PMODULES_ROOT}"
ol_install_root="${PMODULES_ROOT}"
else
return 1
fi
[[ -n "$2" ]] && std::upvar "$2" "${ol_name}"
[[ -n "$3" ]] && std::upvar "$3" "${ol_type}"
[[ -n "$4" ]] && std::upvar "$4" "${ol_install_root}"
[[ -n "$5" ]] && std::upvar "$5" "${ol_modulefiles_root}"
}
# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End: