initialization of Pmodules environment reviewed

- introduce new configuration file ${PMODULES_ROOT}/config/Pmodules.conf
- move as much as possible from profile.bash and init/bash to
  modulecmd
- expose less environment variables to the user
This commit is contained in:
2021-05-26 16:43:38 +02:00
parent 9b33e1723a
commit 85c89df9f5
4 changed files with 83 additions and 67 deletions
+5
View File
@@ -35,6 +35,11 @@ unset MODULE_VERSION_STACK
unset MODULESHOME
unset PMODULES_ENV
declare -x PMODULES_MODULEFILES_DIR='modulefiles'
declare -x PMODULES_CONFIG_DIR='config'
declare -x PMODULES_DIR="${PMODULES_HOME}"
#############################################################################
# implement module comand as shell function
#
+16
View File
@@ -22,6 +22,22 @@ if {[info exists env(PMODULES_DEBUG)] && $env(PMODULES_DEBUG)} {
debug "loading libmodules"
package require base64
proc _pmodules_parse_pmodules_env { } {
debug "enter"
foreach line [split [base64::decode $::env(PMODULES_ENV)] "\n"] {
if { ![regexp -- {.* -[aAx]* (.*)=\((.*)\)} $line -> name value] } {
continue
}
switch $name {
UsedGroups {
set ::UsedGroups $value
}
}
}
}
proc module-addgroup { group } {
global env
global name
+62 -42
View File
@@ -38,10 +38,6 @@ fi
source "${libdir}/libstd.bash"
: ${ReleaseStages:=':unstable:stable:deprecated:'}
declare -r version='@PMODULES_VERSION@'
if [[ ${PMODULES_PURETCL} == yes ]]; then
declare -r modulecmd="${libexecdir}/modulecmd.tcl"
else
@@ -62,8 +58,10 @@ declare -A Subcommands
declare -A Options
declare -A Help
declare -r pmodules_config_file="${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/Pmodules.conf"
Help['version']="
Pmodules ${version} using Tcl Environment Modules @MODULES_VERSION@
Pmodules @PMODULES_VERSION@ using Tcl Environment Modules @MODULES_VERSION@
Copyright GNU GPL v2
"
@@ -89,6 +87,18 @@ export_env() {
printf "${fmt}" "$1" "${!1}"
shift
done
# :FIXME: UsedGroups can be modified in libmodule.tcl using
# append-path/remove-path! But we keep the state in PMODULES_ENV
# so we don't have to export it.
#
case "${Shell}" in
sh | bash | zsh )
echo "unset UsedGroups; "
;;
csh | tcsh )
echo "unsetenv UsedGroups; "
;;
esac
}
#
@@ -154,6 +164,42 @@ is_release() {
[[ ${ReleaseStages} =~ :$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}
g_env_must_be_saved='yes'
}
#
# (Re-)Scan available groups in given root and compute group depth's
#
# Args:
# $1: root of modulefile hierarchy
#
scan_groups () {
local -r root="$1"
local moduledir
for moduledir in ${root}/*/${PMODULES_MODULEFILES_DIR}; do
compute_group_depth "${moduledir}"
done
}
#
# Check whether argument is a group
#
@@ -993,42 +1039,6 @@ subcommand_avail() {
done
}
#
# 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}
g_env_must_be_saved='yes'
}
#
# (Re-)Scan available groups in given root and compute group depth's
#
# Args:
# $1: root of modulefile hierarchy
#
scan_groups () {
local -r root="$1"
local moduledir
for moduledir in ${root}/*/${PMODULES_MODULEFILES_DIR}; do
compute_group_depth "${moduledir}"
done
}
##############################################################################
#
# use [-a|--append|-p|--prepend] [directory|group|release...]
@@ -1397,15 +1407,19 @@ init_manpath() {
}
pmodules_init() {
source "${pmodules_config_file}" || \
std::die 3 "Oops: cannot parse config file -- %s\n" \
"${pmodules_config_file}"
declare -gx LOADEDMODULES=''
declare -gx _LMFILES_=''
declare -gx UsedGroups=''
declare -gx MODULEPATH=''
declare -Ag GroupDepths='()'
unset UseFlags
declare -Ag UseFlags=()
declare -g Version="${PMODULES_VERSION}"
reset_used_groups
reset_modulepath
reset_used_releases
@@ -2274,6 +2288,12 @@ if [[ -n ${PMODULES_ENV} ]]; then
declare -g Version="${PMODULES_VERSION}"
g_env_must_be_saved='yes'
fi
if [[ -v DefaultGroups ]] || [[ -v DefaultUsedReleaseStages ]] || [[ -v ReleaseStages ]]; then
source "${pmodules_config_file}" || \
std::die 3 "Oops: cannot parse config file -- %s\n" \
"${pmodules_config_file}"
fi
else
pmodules_init
fi
-25
View File
@@ -3,36 +3,11 @@
# The following settings are system defaults. They can be (re-)defined
# in a system wide profile or in a user's profile.
# set groups which should be available after initialization
: ${DefaultGroups:='Tools Programming'}
# set releases which should be available after initialization
: ${DefaultUsedReleaseStages:='stable'}
# set default version
: ${PMODULES_VERSION:=@PMODULES_VERSION@}
#############################################################################
# N O C H A N G E S B E L O W T H I S L I N E ! #
#
# Notes:
# - PMODULES_ROOT is derived from the location of this file.
# - Some for PMODULES_CONFIG_DIR.
# - The Pmodules software must be installed in
# ${PMODULES_ROOT}/Tools/Pmodules/${PMODULES_VERSION}
#
export DefaultGroups
export DefaultUsedReleaseStages
export PMODULES_VERSION
declare -x PMODULES_MODULEFILES_DIR='modulefiles'
declare -x ReleaseStages=':unstable:stable:deprecated:'
declare -x PMODULES_ROOT=$(cd $(dirname "${BASH_SOURCE}")/.. && pwd)
declare -x PMODULES_CONFIG_DIR=$(basename $(cd $(dirname "${BASH_SOURCE}") && pwd))
declare -x PMODULES_HOME="${PMODULES_ROOT}/Tools/Pmodules/${PMODULES_VERSION}"
declare -x PMODULES_DIR="${PMODULES_HOME}"
test -r "${PMODULES_HOME}/init/bash" && source "$_"