From 29248571727653b44cc4f89874c3fbe1f42a49d9 Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Thu, 9 Nov 2017 16:03:37 +0100 Subject: [PATCH] support for (t)csh added, closing #21 --- Pmodules/csh | 14 ++-- Pmodules/modulecmd.bash.in | 144 ++++++++++++++++++------------------- Pmodules/profile.csh.in | 82 ++++++++------------- build | 6 +- 4 files changed, 113 insertions(+), 133 deletions(-) diff --git a/Pmodules/csh b/Pmodules/csh index 93c2d64..00d781e 100644 --- a/Pmodules/csh +++ b/Pmodules/csh @@ -3,7 +3,7 @@ if ($?tcsh) then else set modules_shell="csh" endif -set exec_prefix = $PMODULES_HOME/bin' +set exec_prefix = "${PMODULES_HOME}/bin" set prefix="" set postfix="" @@ -34,10 +34,14 @@ unset exec_prefix unset prefix unset postfix - -if (! $?MODULEPATH ) then - setenv MODULEPATH `sed -n 's/[ #].*$//; /./H; $ { x; s/^\n//; s/\n/:/g; p; }' ${MODULESHOME}/init/.modulespath` -endif +setenv MODULEPATH +foreach group ( ${PMODULES_DEFAULT_GROUPS} ) + if ( "${MODULEPATH}" == "" ) then + setenv MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" + else + setenv MODULEPATH "${MODULEPATH}:${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" + endif +end if (! $?LOADEDMODULES ) then setenv LOADEDMODULES "" diff --git a/Pmodules/modulecmd.bash.in b/Pmodules/modulecmd.bash.in index ba612ea..2ee1448 100755 --- a/Pmodules/modulecmd.bash.in +++ b/Pmodules/modulecmd.bash.in @@ -51,8 +51,8 @@ pbuild::export_env() { bash | zsh ) local -r fmt="export %s=\"%s\"; " ;; - tcsh ) - local -r fmt="setenv %s \"%s\"" + csh | tcsh ) + local -r fmt="setenv %s \"%s\"; " ;; * ) std::die 1 "${shell} is an unsupported shell" @@ -734,47 +734,22 @@ subcommand_load() { # Args: # none output_load_hints() { - local -ra rels=( ${PMODULES_DEFINED_RELEASES//:/ } ) - for rel in "${rels[@]}"; do - eval $( subcommand_use "${rel}" ) - if is_available "${m}"; then - std::info "${m}: is ${rel}! If you want to load this module, run" - std::info "\tmodule use ${rel}" - std::info "before running" - std::info "\tmodule load ${m}" - exit 42 - fi - done - local something_found='no' - local -a output=() - local -a release=() - local -a loadable=() - local -i i=0 + local output="The module '${m}' cannot be loaded!\n" + output+="Try with one of the following command(s):\n\n" + local release='' local -i n=0 while read -a line; do - output[n]="module load ${line[@]:3} ${line[0]}" - release[n]=${line[1]} - if [[ ":${UsedReleases}:" =~ "${release[n]}" ]]; then - loadable[n]='yes' - else - loadable[n]='no' + release=${line[1]} + if [[ ! ":${UsedReleases}:" =~ "${release}" ]]; then + output+="module use ${release}; " fi - n+=1 + output+="module load ${line[@]:3} ${line[0]}\n" + let n+=1 done < <(subcommand_search "${m}" -a --no-header 2>&1) if (( n == 0 )); then - std::info "${CMD} load: module unavailable -- ${m}" + std::info "The module '${m}' doesn't exist!" else - # :FIXME: output group - std::info "\nThe module '${m}' is not available with the " \ - "currently loaded modules!" \ - "You may try: " - for ((i=n-1; i >=0; i--)); do - if [[ "${loadable[i]}" == "no" ]]; then - std::info "${output[i]}\t# ${release[i]}" - else - std::info "${output[i]}" - fi - done + std::info "${output}" fi } @@ -784,18 +759,20 @@ subcommand_load() { [[ -z ${dep} ]] && continue [[ ${dep:0:1} == \# ]] && continue module_is_loaded "${dep}" && continue - local output=$( subcommand_load "${dep}") - echo ${output} + local output=$( subcommand_load --internal "${dep}") + + #echo ${output} eval ${output} done < "${fname}" } local opts - opts=$(pmodules::get_options -o fsvw -l force -l silent -l verbose -l warn -- "$@") || \ + opts=$(pmodules::get_options -o fsvwi -l force -l silent -l verbose -l warn -l internal -- "$@") || \ subcommand_help_load eval set -- "${opts}" local args=() opts='' + local shell="${g_shell}" while (($# > 0)); do case $1 in -f | --force ) @@ -810,6 +787,9 @@ subcommand_load() { -w | --warn ) verbosity_lvl='warn' ;; + -i | --internal ) + shell='bash' + ;; -- ) ;; * ) @@ -904,7 +884,7 @@ subcommand_load() { load_dependencies "${dependency_file}" fi fi - "${modulecmd}" "${g_shell}" ${opts} load "${m}" + "${modulecmd}" "${shell}" ${opts} load "${m}" if [[ ${verbosity_lvl} != silent ]] && [[ ${release} != stable ]]; then std::info "Warning: the ${release} module '${m}' has been loaded." fi @@ -1291,27 +1271,21 @@ subcommand_use() { use () { local dirs_to_add=() - local subcommand_switches='' while (( $# > 0)); do - if [[ "$1" == "--" ]]; then - shift - continue - fi - arg=$1 + arg="$1" # if is release - # ... + # ... # elif is group - # ... + # ... # elif matches modulepath root - # ... + # ... # elif is directory - # ... + # ... + # else + # error + # local modulefiles_dir="${PMODULES_ROOT}/${arg}/${PMODULES_MODULEFILES_DIR}" - if [[ ${arg} == -a ]] || [[ ${arg} == --append ]]; then - subcommand_switches='--append' - elif [[ ${arg} == -p ]] || [[ ${arg} == --prepend ]]; then - subcommand_switches='' - elif is_release "${arg}"; then + if is_release "${arg}"; then # releases are always *appended* std::append_path UsedReleases "${arg}" elif [[ ! ${arg} =~ */* ]] && [[ -d ${modulefiles_dir} ]]; then @@ -1328,8 +1302,7 @@ subcommand_use() { elif [[ ${arg} =~ ^${PMODULES_ROOT} ]]; then std::die 3 "${CMD} ${FUNCNAME[0]##*_}: illegal directory: ${arg}" elif [[ -d ${arg} ]]; then - local normalized_dir=$(cd "${arg}" && pwd) - dirs_to_add+=( ${normalized_dir} ) + dirs_to_add+=( "$(cd "${arg}" && pwd)" ) else std::die 3 "${CMD} ${FUNCNAME[0]##*_}: neither a directory, release or group: ${arg}" fi @@ -1338,21 +1311,43 @@ subcommand_use() { declare -g PMODULES_USED_GROUPS pbuild::export_env ${g_shell} PMODULES_USED_GROUPS - [[ ${#dirs_to_add[@]} == 0 ]] && return - for dir in "${dirs_to_add[@]}"; do - subcommand_generic1 use ${subcommand_switches} "${dir}" + if [[ ${opt_append} == yes ]]; then + std::append_path MODULEPATH "${dir}" + else + std::prepend_path MODULEPATH "${dir}" + fi done } local opts='' + local opt_append='no' + local -a args=() + opts=$(pmodules::get_options -o 'ap' -l 'append' -l 'prepend' -- "$@") || subcommand_help_use eval set -- "${opts}" - if [[ $# == 1 ]]; then + while (( $# > 0)); do + case "$1" in + -a | --append ) + opt_append='yes' + ;; + -p | --prepend ) + opt_append='no' + ;; + -- ) + ;; + * ) + args+=( "$1" ) + ;; + esac + shift + done + + if (( ${#args[@]} == 0 )); then print_info else - use "$@" + use "${args[@]}" fi } @@ -1477,10 +1472,10 @@ subcommand_list() { ############################################################################## pmodules_init() { - declare -g PMODULES_DEFAULT_GROUPS='' - declare -g PMODULES_DEFAULT_RELEASES='' + #declare -g PMODULES_DEFAULT_GROUPS='' + #declare -g PMODULES_DEFAULT_RELEASES='' - source "${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.${g_shell}" + #source "${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.${g_shell}" declare -g LOADEDMODULES='' declare -g PMODULES_USED_GROUPS='' @@ -1866,21 +1861,24 @@ subcommand_initclear() { subcommand_generic0 initclear "$@" } -if [[ -n ${PMODULES_ENV} ]]; then - eval "$("${base64}" -d <<< "${PMODULES_ENV}" 2>/dev/null)" -else - pmodules_init -fi - case "$1" in - bash | tcsh | zsh ) + bash | zsh ) declare g_shell="$1" ;; + csh | tcsh ) + declare g_shell='csh' + ;; * ) std::die 1 "${CMD}: unsupported shell -- $1" ;; esac shift +if [[ -n ${PMODULES_ENV} ]]; then + eval "$("${base64}" -d <<< "${PMODULES_ENV}" 2>/dev/null)" +else + pmodules_init +fi + declare -A Subcommands=(\ [add]="subcommand_load" \ diff --git a/Pmodules/profile.csh.in b/Pmodules/profile.csh.in index a50d29e..c4e4f78 100755 --- a/Pmodules/profile.csh.in +++ b/Pmodules/profile.csh.in @@ -1,69 +1,43 @@ #!/bin/tcsh -setenv PMODULES_ROOT "/opt/psi" - +setenv PMODULES_DEFAULT_GROUPS 'Tools Programming' +setenv PMODULES_DEFAULT_RELEASES 'stable' if ( ! $?PMODULES_VERSION ) then setenv PMODULES_VERSION "@PMODULES_VERSION@" endif -setenv PMODULES_HOME "$PMODULES_ROOT/Tools/Pmodules/$PMODULES_VERSION" +if ( ${PMODULES_VERSION} == "" ) then + setenv PMODULES_VERSION "@PMODULES_VERSION@" +endif + +############################################################################# +# N O C H A N G E S B E L O W T H I S L I N E ! # +############################################################################# + +setenv PMODULES_MODULEFILES_DIR 'modulefiles' +setenv PMODULES_DEFINED_RELEASES ':unstable:stable:deprecated:' + +setenv PMODULES_ROOT '/opt/psi' +setenv PMODULES_CONFIG_DIR 'config' +setenv PMODULES_HOME "${PMODULES_ROOT}/Tools/Pmodules/${PMODULES_VERSION}" setenv PMODULES_DIR "${PMODULES_HOME}" -setenv PMODULES_CONFIG_DIR 'config' -setenv PMODULES_MODULEFILES_DIR 'modulefiles' -setenv PMODULES_TEMPLATES_DIR 'templates' +############################################################################ +# some sanity checks +# + +# fixme: add some checks -setenv PMODULES_DEFAULT_GROUPS 'Tools Programming' -setenv PMODULES_DEFINED_RELEASES ':unstable:stable:deprecated:' -setenv PMODULES_DEFAULT_RELEASES 'stable' ############################################################################ -# check configuration +# inititialize Pmodules for csh # -if ( ! $?PMODULES_PREFIX ) then - echo "Oops: Pmodules prefix not set." - return 1 -endif - -if ( "$PMODULES_PREFIX" == "" ) then - echo "Oops: Pmodules prefix set to empty string!" -endif -if ( ! -d ${PMODULES_PREFIX} ) then - echo "Oops: ${PMODULES_PREFIX}: Set as Pmodules prefix, but this is not a directory!" - return 1 -endif - -if ( ! $?PMODULES_VERSION ) then - echo "Oops: Pmodules version not set!" - return 1 -endif -if ( "$PMODULES_VERSION" == "" ) then - echo "Oops: Pmodules version set to empty string!" - return 1 -endif - -if ( $?PMODULES_HOME ) then - echo "Oops: Pmodules home not set!" - return 1 -endif -if ( "$PMODULES_HOME" == "" ) then - echo "Oops: Pmodules home set to empty string!" - return 1 -fi -if ( ! -d "$PMODULES_HOME" ) then - echo "Oops: $PMODULES_HOME: Set as Pmodules home, but this is not a directory!" - return 1 -endif - -############################################################################ -# inititialize Pmodules for bash -# -set _init_csh="$PMODULES_HOME/init/csh" -if ( ! -r "$_init_csh" ) then +set _init_csh="${PMODULES_HOME}/init/csh" +if ( ! -r "${_init_csh}" ) then echo "Oops: cannot initialize Modules!" - echo "$_init_csh: File does not exist or is not readable." - return 1 -fi -source "$_init_csh" + echo "${_init_csh}: File does not exist or is not readable." +else + source "${_init_csh}" +endif unset _init_csh # Local Variables: diff --git a/build b/build index 0bb494c..ba96cb3 100755 --- a/build +++ b/build @@ -155,7 +155,7 @@ pmodules::compile() { fi if [[ ! -e "${PMODULES_HOME}/libexec/modulecmd.bin" ]] || [[ ${force} == 'yes' ]]; then - build Modules "${MODULES_VERSION}" --compile + build Modules "${MODULES_VERSION}" fi echo "Done..." } @@ -258,6 +258,9 @@ pmodules::install() { test -e "${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.bash" || \ install -m 0755 "${SRC_DIR}/profile.bash" "$_" + test -e "${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.csh" || \ + install -m 0755 "${SRC_DIR}/profile.csh" "$_" + test -e "${PMODULES_ROOT}/${PMODULES_CONFIG_DIR}/profile.zsh" || \ install -m 0755 "${SRC_DIR}/profile.zsh" "$_" @@ -270,6 +273,7 @@ pmodules::install() { install -m 0644 "${SRC_DIR}/bash" "${PMODULES_HOME}/init" install -m 0644 "${SRC_DIR}/bash_completion" "${PMODULES_HOME}/init" + install -m 0644 "${SRC_DIR}/csh" "${PMODULES_HOME}/init" install -m 0644 "${SRC_DIR}/zsh" "${PMODULES_HOME}/init" install -m 0644 "${SRC_DIR}/libpmodules.bash" "${PMODULES_HOME}/lib"