Commit 1ab70662 authored by gsell's avatar gsell
Browse files

Pmodules/libpmodules.bash,modulecmd.bash.in:

- refactor function names with a namespace
parent 525b7694
...@@ -5,33 +5,63 @@ if [[ -z ${bindir} ]]; then ...@@ -5,33 +5,63 @@ if [[ -z ${bindir} ]]; then
bindir=$(cd "${bindir}"/.. && pwd)"/bin" bindir=$(cd "${bindir}"/.. && pwd)"/bin"
fi fi
log() { pmodules::get_options() {
"${bindir}/getopt" "$@"
}
pmodules::check_env_vars() {
[[ -n "${PMODULES_ROOT}" ]] &&
[[ -n "${PMODULES_CONFIG_DIR}" ]] &&
[[ -n "${PMODULES_TEMPLATES_DIR}" ]] &&
[[ -n "${PMODULES_HOME}" ]] &&
[[ -n "${PMODULES_VERSION}" ]] || std::die 1 "
Error: the module environment you are going to use as source has not been
initialized properly!"
}
pmodules::check_directories() {
local -r src_prefix="$1"
[[ -d "${src_prefix}" ]] &&
[[ -d "${src_prefix}/${PMODULES_CONFIG_DIR}" ]] &&
[[ -d "${src_prefix}/${PMODULES_TEMPLATES_DIR}" ]] &&
[[ -d "${src_prefix}/Tools/Pmodules/${PMODULES_VERSION}" ]] || std::die 1 "
Error: the module environment '${src_prefix}' has not been initialized properly!"
}
pmodules::check_env() {
pmodules::check_env_vars
pmodules::check_directories "${PMODULES_ROOT}"
}
std::log() {
local -ri fd=$1 local -ri fd=$1
local -r fmt="$2\n" local -r fmt="$2\n"
shift 2 shift 2
printf -- "$fmt" "$@" 1>&$fd printf -- "$fmt" "$@" 1>&$fd
} }
info() { std::info() {
log 2 "$1" "${@:2}" std::log 2 "$1" "${@:2}"
} }
error() { std::error() {
log 2 "$1" "${@:2}" std::log 2 "$1" "${@:2}"
} }
debug() { std::debug() {
[[ ${PMODULES_DEBUG} ]] || return 0 [[ ${PMODULES_DEBUG} ]] || return 0
log 2 "$@" std::log 2 "$@"
} }
die() { std::die() {
local -ri ec=$1 local -ri ec=$1
shift shift
if [[ -n $@ ]]; then if [[ -n $@ ]]; then
local -r fmt=$1 local -r fmt=$1
shift shift
log 2 "$fmt" "$@" std::log 2 "$fmt" "$@"
fi fi
exit $ec exit $ec
} }
...@@ -41,7 +71,7 @@ die() { ...@@ -41,7 +71,7 @@ die() {
# #
# $1: prompt # $1: prompt
# #
get_YN_answer() { std::get_YN_answer() {
local -r prompt="$1" local -r prompt="$1"
local ans local ans
read -p "${prompt}" ans read -p "${prompt}" ans
...@@ -56,7 +86,7 @@ get_YN_answer() { ...@@ -56,7 +86,7 @@ get_YN_answer() {
# #
# return normalized abolute pathname # return normalized abolute pathname
# $1: filename # $1: filename
get_abspath() { std::get_abspath() {
local -r fname=$1 local -r fname=$1
[[ -r "${fname}" ]] || return 1 [[ -r "${fname}" ]] || return 1
if [[ -d ${fname} ]]; then if [[ -d ${fname} ]]; then
...@@ -67,36 +97,7 @@ get_abspath() { ...@@ -67,36 +97,7 @@ get_abspath() {
fi fi
} }
get_options() { std::append_path () {
"${bindir}/getopt" "$@"
}
check_pmodules_env_vars() {
[[ -n "${PMODULES_ROOT}" ]] &&
[[ -n "${PMODULES_CONFIG_DIR}" ]] &&
[[ -n "${PMODULES_TEMPLATES_DIR}" ]] &&
[[ -n "${PMODULES_HOME}" ]] &&
[[ -n "${PMODULES_VERSION}" ]] || die 1 "
Error: the module environment you are going to use as source has not been
initialized properly!"
}
check_pmodules_directories() {
local -r src_prefix="$1"
[[ -d "${src_prefix}" ]] &&
[[ -d "${src_prefix}/${PMODULES_CONFIG_DIR}" ]] &&
[[ -d "${src_prefix}/${PMODULES_TEMPLATES_DIR}" ]] &&
[[ -d "${src_prefix}/Tools/Pmodules/${PMODULES_VERSION}" ]] || die 1 "
Error: the module environment '${src_prefix}' has not been initialized properly!"
}
check_pmodules_env() {
check_pmodules_env_vars
check_pmodules_directories "${PMODULES_ROOT}"
}
append_path () {
local -r P=$1 local -r P=$1
local -r d=$2 local -r d=$2
...@@ -109,7 +110,7 @@ append_path () { ...@@ -109,7 +110,7 @@ append_path () {
fi fi
} }
prepend_path () { std::prepend_path () {
local -r P=$1 local -r P=$1
local -r d=$2 local -r d=$2
...@@ -122,7 +123,7 @@ prepend_path () { ...@@ -122,7 +123,7 @@ prepend_path () {
fi fi
} }
remove_path() { std::remove_path() {
local -r P=$1 local -r P=$1
local -r d=$2 local -r d=$2
local new_path='' local new_path=''
...@@ -135,6 +136,19 @@ remove_path() { ...@@ -135,6 +136,19 @@ remove_path() {
eval ${P}="${new_path:1}" eval ${P}="${new_path:1}"
} }
#
# split file name
#
std::split_fname() {
local -r savedIFS="${IFS}"
IFS='/'
local std__split_fname_result__=( $(echo "${@: -1}") )
IFS=${savedIFS}
eval $1=\(\"\${std__split_fname_result__[@]}\"\)
if (( $# >= 3 )); then
eval $2=${#std__split_fname_result__[@]}
fi
}
# Local Variables: # Local Variables:
# mode: sh # mode: sh
......
...@@ -476,7 +476,7 @@ subcommand_generic0() { ...@@ -476,7 +476,7 @@ subcommand_generic0() {
local -r subcommand=$1 local -r subcommand=$1
shift shift
local opts='' local opts=''
opts=$(get_options -- '' "$@") || subcommand_help_${subcommand} opts=$(pmodules::get_options -- '' "$@") || subcommand_help_${subcommand}
eval set -- "${opts}" eval set -- "${opts}"
while (( $# > 0 )); do while (( $# > 0 )); do
case $1 in case $1 in
...@@ -495,7 +495,7 @@ subcommand_generic1() { ...@@ -495,7 +495,7 @@ subcommand_generic1() {
local -r subcommand=$1 local -r subcommand=$1
shift shift
local opts='' local opts=''
opts=$(get_options -- '' "$@") || subcommand_help_${subcommand} opts=$(pmodules::get_options -- '' "$@") || subcommand_help_${subcommand}
eval set -- "${opts}" eval set -- "${opts}"
local args=() local args=()
while (( $# > 0 )); do while (( $# > 0 )); do
...@@ -522,7 +522,7 @@ subcommand_generic1plus() { ...@@ -522,7 +522,7 @@ subcommand_generic1plus() {
local -r subcommand=$1 local -r subcommand=$1
shift shift
local opts='' local opts=''
opts=$(get_options -- '' "$@") || subcommand_help_${subcommand} opts=$(pmodules::get_options -- '' "$@") || subcommand_help_${subcommand}
eval set -- "${opts}" eval set -- "${opts}"
local args=() local args=()
while (( $# > 0 )); do while (( $# > 0 )); do
...@@ -545,7 +545,7 @@ subcommand_generic1or2() { ...@@ -545,7 +545,7 @@ subcommand_generic1or2() {
local -r subcommand=$1 local -r subcommand=$1
shift shift
local opts='' local opts=''
opts=$(get_options -- '' "$@") || subcommand_help_${subcommand} opts=$(pmodules::get_options -- '' "$@") || subcommand_help_${subcommand}
eval set -- "${opts}" eval set -- "${opts}"
local args=() local args=()
while (( $# > 0 )); do while (( $# > 0 )); do
...@@ -631,7 +631,7 @@ subcommand_load() { ...@@ -631,7 +631,7 @@ subcommand_load() {
if [[ -f ${m} ]]; then if [[ -f ${m} ]]; then
if [[ "${m:0:1}" != "/" ]]; then if [[ "${m:0:1}" != "/" ]]; then
# convert to absolte path if relative # convert to absolte path if relative
m=$(get_abspath "${m}") m=$(std::get_abspath "${m}")
fi fi
is_modulefile "${m}" || return 2 is_modulefile "${m}" || return 2
is_loadable "${m}" || return 3 is_loadable "${m}" || return 3
...@@ -683,10 +683,10 @@ subcommand_load() { ...@@ -683,10 +683,10 @@ subcommand_load() {
for rel in "${rels[@]}"; do for rel in "${rels[@]}"; do
eval $( subcommand_use "${rel}" ) eval $( subcommand_use "${rel}" )
if is_available "${m}"; then if is_available "${m}"; then
info "${m}: is ${rel}! If you want to load this module, run" std::info "${m}: is ${rel}! If you want to load this module, run"
info "\tmodule use ${rel}" std::info "\tmodule use ${rel}"
info "before running" std::info "before running"
info "\tmodule load ${m}" std::info "\tmodule load ${m}"
exit 42 exit 42
fi fi
done done
...@@ -706,22 +706,22 @@ subcommand_load() { ...@@ -706,22 +706,22 @@ subcommand_load() {
fi fi
n+=1 n+=1
done < <(subcommand_search "${m}" -a --no-header 2>&1) done < <(subcommand_search "${m}" -a --no-header 2>&1)
info "${CMD} load: module unavailable -- ${m}" std::info "${CMD} load: module unavailable -- ${m}"
if (( n > 0 )); then if (( n > 0 )); then
# :FIXME: output group # :FIXME: output group
info "\nBut the following modules chain(s) are available in the hierarchy:" std::info "\nBut the following modules chain(s) are available in the hierarchy:"
for ((i=n-1; i >=0; i--)); do for ((i=n-1; i >=0; i--)); do
if [[ "${loadable[i]}" == "no" ]]; then if [[ "${loadable[i]}" == "no" ]]; then
info "${output[i]}\t# ${release[i]}" std::info "${output[i]}\t# ${release[i]}"
else else
info "${output[i]}" std::info "${output[i]}"
fi fi
done done
fi fi
} }
local opts local opts
opts=$(get_options -o fsvw -l force -l silent -l verbose -l warn -- "$@") || \ opts=$(pmodules::get_options -o fsvw -l force -l silent -l verbose -l warn -- "$@") || \
subcommand_help_load subcommand_help_load
eval set -- "${opts}" eval set -- "${opts}"
local args=() local args=()
...@@ -803,12 +803,12 @@ subcommand_load() { ...@@ -803,12 +803,12 @@ subcommand_load() {
fi fi
if [[ -n ${release} ]]; then if [[ -n ${release} ]]; then
is_release "${release}" || die 3 "${CMD} load: illegal release name." is_release "${release}" || die 3 "${CMD} load: illegal release name."
append_path UsedReleases "${release}" std::append_path UsedReleases "${release}"
fi fi
fi fi
if is_available "${m}"; then if is_available "${m}"; then
if [[ ${verbosity_lvl} != silent ]] && [[ ${release} != stable ]]; then if [[ ${verbosity_lvl} != silent ]] && [[ ${release} != stable ]]; then
info "Warning: the ${release} module '${m}' has been loaded." std::info "Warning: the ${release} module '${m}' has been loaded."
fi fi
"${modulecmd}" "${shell}" ${opts} load "${m}" "${modulecmd}" "${shell}" ${opts} load "${m}"
else else
...@@ -910,7 +910,7 @@ subcommand_avail() { ...@@ -910,7 +910,7 @@ subcommand_avail() {
esac esac
printf "%-20s\t%s\n" "${mod}" "${out}" 1>&2 printf "%-20s\t%s\n" "${mod}" "${out}" 1>&2
done done
info "" std::info ""
} }
# #
...@@ -930,7 +930,7 @@ subcommand_avail() { ...@@ -930,7 +930,7 @@ subcommand_avail() {
esac esac
printf "%-20s\t%s\n" "${mod}" "${out}" 1>&2 printf "%-20s\t%s\n" "${mod}" "${out}" 1>&2
done done
info "" std::info ""
} }
human_readable_output() { human_readable_output() {
...@@ -970,7 +970,7 @@ subcommand_avail() { ...@@ -970,7 +970,7 @@ subcommand_avail() {
printf -- "\n\n" 1>&2 printf -- "\n\n" 1>&2
} }
local opts='' local opts=''
opts=$(get_options -o hlt -l human -l long -l terse -- "$@") || subcommand_help_avail opts=$(pmodules::get_options -o hlt -l human -l long -l terse -- "$@") || subcommand_help_avail
eval set -- "${opts}" eval set -- "${opts}"
local pattern=() local pattern=()
local output_function='' local output_function=''
...@@ -1068,42 +1068,42 @@ subcommand_use() { ...@@ -1068,42 +1068,42 @@ subcommand_use() {
print_info() { print_info() {
local f local f
local r local r
info "Used groups:" std::info "Used groups:"
for f in ${used_groups//:/ }; do for f in ${used_groups//:/ }; do
info "\t${f}" std::info "\t${f}"
done done
info "\nUnused groups:" std::info "\nUnused groups:"
local _group local _group
for _group in "${Groups[@]}"; do for _group in "${Groups[@]}"; do
local -i depth=${HierarchyDepths[${_group}]} local -i depth=${HierarchyDepths[${_group}]}
if ! is_used_group "${_group}" && (( depth == 0 )); then if ! is_used_group "${_group}" && (( depth == 0 )); then
info "\t${_group}" std::info "\t${_group}"
fi fi
done done
info "\nUsed releases:" std::info "\nUsed releases:"
for r in ${UsedReleases//:/ }; do for r in ${UsedReleases//:/ }; do
info "\t${r}" std::info "\t${r}"
done done
info "\nUnused releases:" std::info "\nUnused releases:"
for r in ${PMODULES_DEFINED_RELEASES//:/ }; do for r in ${PMODULES_DEFINED_RELEASES//:/ }; do
if ! is_used_release $r; then if ! is_used_release $r; then
info "\t${r}" std::info "\t${r}"
fi fi
done done
info "\nAdditonal directories in MODULEPATH:" std::info "\nAdditonal directories in MODULEPATH:"
let n=0 let n=0
for (( i=0; i<${#modulepath[@]}; i++)); do for (( i=0; i<${#modulepath[@]}; i++)); do
if [[ ! ${modulepath[i]} =~ ${PMODULES_ROOT} ]]; then if [[ ! ${modulepath[i]} =~ ${PMODULES_ROOT} ]]; then
info "\t${modulepath[i]}" std::info "\t${modulepath[i]}"
let n+=1 let n+=1
fi fi
done done
if (( n == 0 )); then if (( n == 0 )); then
info "\tnone" std::info "\tnone"
fi fi
info "\n" std::info "\n"
} }
use () { use () {
...@@ -1127,12 +1127,12 @@ subcommand_use() { ...@@ -1127,12 +1127,12 @@ subcommand_use() {
subcommand_switches='' subcommand_switches=''
elif is_release "${arg}"; then elif is_release "${arg}"; then
# releases are always *appended* # releases are always *appended*
append_path UsedReleases "${arg}" std::append_path UsedReleases "${arg}"
elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then
if (( ${HierarchyDepths[$arg]} != 0 )); then if (( ${HierarchyDepths[$arg]} != 0 )); then
die 3 "${CMD} ${0##_}: cannot add group ${arg} to module path" die 3 "${CMD} ${0##_}: cannot add group ${arg} to module path"
fi fi
append_path PMODULES_USED_GROUPS "${arg}" std::append_path PMODULES_USED_GROUPS "${arg}"
dirs_to_add+=( ${modulefiles_dir} ) dirs_to_add+=( ${modulefiles_dir} )
elif [[ ${arg} =~ ^${PMODULES_ROOT} ]]; then elif [[ ${arg} =~ ^${PMODULES_ROOT} ]]; then
die 3 "${CMD} ${0##_}: illegal directory: ${arg}" die 3 "${CMD} ${0##_}: illegal directory: ${arg}"
...@@ -1157,7 +1157,7 @@ subcommand_use() { ...@@ -1157,7 +1157,7 @@ subcommand_use() {
} }
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
print_info std::print_info
else else
use "$@" use "$@"
fi fi
...@@ -1168,7 +1168,7 @@ subcommand_use() { ...@@ -1168,7 +1168,7 @@ subcommand_use() {
# #
subcommand_unuse() { subcommand_unuse() {
local opts='' local opts=''
opts=$(get_options -- '' "$@") || subcommand_help_unuse opts=$(pmodules::get_options -- '' "$@") || subcommand_help_unuse
eval set -- "${opts}" eval set -- "${opts}"
local dirs_to_remove=() local dirs_to_remove=()
while (( $# > 0)); do while (( $# > 0)); do
...@@ -1187,12 +1187,12 @@ subcommand_unuse() { ...@@ -1187,12 +1187,12 @@ subcommand_unuse() {
# ... # ...
local modulefiles_dir="${PMODULES_ROOT}/${arg}/${PMODULES_MODULEFILES_DIR}" local modulefiles_dir="${PMODULES_ROOT}/${arg}/${PMODULES_MODULEFILES_DIR}"
if is_release "${arg}"; then if is_release "${arg}"; then
remove_path UsedReleases "${arg}" std::remove_path UsedReleases "${arg}"
elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then
if (( ${HierarchyDepths[$arg]} != 0 )); then if (( ${HierarchyDepths[$arg]} != 0 )); then
die 3 "${CMD} ${0##_}: cannot remove group ${arg} from module path" die 3 "${CMD} ${0##_}: cannot remove group ${arg} from module path"
fi fi
remove_path PMODULES_USED_GROUPS "${arg}" std::remove_path PMODULES_USED_GROUPS "${arg}"
dirs_to_remove+=( ${modulefiles_dir} ) dirs_to_remove+=( ${modulefiles_dir} )
elif [[ -d ${arg} ]]; then elif [[ -d ${arg} ]]; then
local normalized_dir=$(cd "${arg}" && pwd) local normalized_dir=$(cd "${arg}" && pwd)
...@@ -1243,7 +1243,7 @@ subcommand_purge() { ...@@ -1243,7 +1243,7 @@ subcommand_purge() {
# #
subcommand_list() { subcommand_list() {
local opts='' local opts=''
opts=$(get_options -o hlt -l human -l long -l terse -- "$@") || subcommand_help_list opts=$(pmodules::get_options -o hlt -l human -l long -l terse -- "$@") || subcommand_help_list
eval set -- "${opts}" eval set -- "${opts}"
local opts='' local opts=''
while (( $# > 0 )); do while (( $# > 0 )); do
...@@ -1286,14 +1286,14 @@ pmodules_init() { ...@@ -1286,14 +1286,14 @@ pmodules_init() {
declare -g _LMFILES_='' declare -g _LMFILES_=''
for group in ${PMODULES_DEFAULT_GROUPS}; do for group in ${PMODULES_DEFAULT_GROUPS}; do
append_path MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" std::append_path MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}"
append_path PMODULES_USED_GROUPS "${group}" std::append_path PMODULES_USED_GROUPS "${group}"
done done
declare -ag Groups='()' declare -ag Groups='()'
declare -Ag HierarchyDepths='()' declare -Ag HierarchyDepths='()'
declare -g UsedReleases='' declare -g UsedReleases=''
for r in ${PMODULES_DEFAULT_RELEASES//:/ }; do for r in ${PMODULES_DEFAULT_RELEASES//:/ }; do
append_path UsedReleases "${r}" std::append_path UsedReleases "${r}"
done done
} }
...@@ -1303,7 +1303,7 @@ pmodules_init() { ...@@ -1303,7 +1303,7 @@ pmodules_init() {
# #
subcommand_clear() { subcommand_clear() {
local opts='' local opts=''
opts=$(get_options -- '' "$@") || subcommand_help_${subcommand} opts=$(pmodules::get_options -- '' "$@") || subcommand_help_${subcommand}
eval set -- "${opts}" eval set -- "${opts}"
while (( $# > 0 )); do while (( $# > 0 )); do
case $1 in case $1 in
...@@ -1393,13 +1393,13 @@ subcommand_search() { ...@@ -1393,13 +1393,13 @@ subcommand_search() {
out+="$d/" out+="$d/"
done done
out+="${line[0]}" out+="${line[0]}"
info "${out}" std::info "${out}"
done < <(sort -k 1,1 -k 4,4 -k 5,5 "${tmpfile}" | awk "${with_modules}") done < <(sort -k 1,1 -k 4,4 -k 5,5 "${tmpfile}" | awk "