From 756c00b921a60620791732b9d02aceea28fa09e2 Mon Sep 17 00:00:00 2001 From: Achim Gsell Date: Thu, 9 Jan 2025 09:49:15 +0100 Subject: [PATCH] modulecmd: derive prefix from directory of init scripts --- Pmodules/Pmodules.py | 17 ++++++++++------- Pmodules/bash | 39 +++++++++++++++++++-------------------- Pmodules/csh | 4 ++-- Pmodules/zsh | 40 ++++++++++++++++------------------------ 4 files changed, 47 insertions(+), 53 deletions(-) diff --git a/Pmodules/Pmodules.py b/Pmodules/Pmodules.py index 4f16601..6092ef4 100644 --- a/Pmodules/Pmodules.py +++ b/Pmodules/Pmodules.py @@ -1,13 +1,16 @@ -import os, re, subprocess +# +# :FIXME: +# - add error handling +# - needs testing! +# +import subprocess, pathlib def module(*args): - os.environ['PMODULES_MODULEFILES_DIR']='modulefiles' - pm_home=os.environ['PMODULES_HOME'] - os.environ['PMODULES_DIR']=pm_home - modulecmd=os.path.join(pm_home, 'bin', 'modulecmd') + dir=pathlib.Path(__file__).parent + modulecmd=pathlib.PurePath.joinpath(dir, '..', 'bin', 'modulecmd').resolve() if type(args[0]) == type([]): - args = args[0] + cmd = [modulecmd, 'python'] + args[0] else: cmd = [modulecmd, 'python'] + list(args) - (output, error) = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate() + (output, error) = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate() exec(output) diff --git a/Pmodules/bash b/Pmodules/bash index 1c6f198..4beb6c0 100644 --- a/Pmodules/bash +++ b/Pmodules/bash @@ -8,19 +8,11 @@ if [ ${BASH_VERSINFO:-0} -lt 3 ]; then return 1 fi -############################################################################ -# some sanity checks -# -if [[ ! -d ${PMODULES_HOME} ]]; then - echo "Oops: ${PMODULES_HOME}: Set as Pmodules home, but this is not a directory." 1>&2 - return 1 -fi - ############################################################################# # initialize bash completion # -if [[ -r "${PMODULES_HOME}/init/bash_completion" ]]; then - source "${PMODULES_HOME}/init/bash_completion" +if [[ -r "${BASH_SOURCE[0]%/*}/bash_completion" ]]; then + source "${BASH_SOURCE[0]%/*}/bash_completion" fi ############################################################################# @@ -30,23 +22,30 @@ unset MODULE_VERSION unset MODULE_VERSION_STACK unset MODULESHOME -declare -x PMODULES_DIR="${PMODULES_HOME}" - ############################################################################# # implement module comand as shell function # -module() { - unset BASH_ENV - local output="$("${PMODULES_HOME}/bin/modulecmd" bash "$@")" - eval "${output}" -} +declare -- _dname_=$(dirname "${BASH_SOURCE[0]}") +declare -- PMODULES_HOME="$(cd "${_dname_}/.." && pwd -L)" +unset _dname_ + +declare -- _cmd_="module() { + unset BASH_ENV + local output=\"\$(\"${PMODULES_HOME}/bin/modulecmd\" bash \"\$@\")\" + eval \"\${output}\" +}" + +eval "$(echo "${_cmd_}")" export -f module -modbuild(){ - "${PMODULES_HOME}/bin/modbuild" "$@" -} +_cmd_="modbuild(){ + \"${PMODULES_HOME}/bin/modbuild\" \"\$@\" +}" +eval "$(echo "${_cmd_}")" export -f modbuild +unset _cmd_ + # Local Variables: # mode: sh # sh-basic-offset: 8 diff --git a/Pmodules/csh b/Pmodules/csh index b9ac99f..5f179ae 100644 --- a/Pmodules/csh +++ b/Pmodules/csh @@ -40,9 +40,9 @@ if (! $?DefaultGroups ) then endif foreach group ( ${DefaultGroups} ) if ( "${MODULEPATH}" == "" ) then - setenv MODULEPATH "${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" + setenv MODULEPATH "${PMODULES_ROOT}/${group}/modulefiles" else - setenv MODULEPATH "${MODULEPATH}:${PMODULES_ROOT}/${group}/${PMODULES_MODULEFILES_DIR}" + setenv MODULEPATH "${MODULEPATH}:${PMODULES_ROOT}/${group}/modulefiles" endif end diff --git a/Pmodules/zsh b/Pmodules/zsh index b6b800b..994cace 100644 --- a/Pmodules/zsh +++ b/Pmodules/zsh @@ -1,19 +1,9 @@ #!/bin/zsh -############################################################################ -# some sanity checks -# -if [[ ! -d ${PMODULES_HOME} ]]; then - echo "Oops: ${PMODULES_HOME}: Set as Pmodules home, but this is not a directory." 1>&2 - return 1 -fi - ############################################################################# # initialize zsh completion # -#if [[ -r "${PMODULES_HOME}/init/bash_completion" ]]; then -# source "${PMODULES_HOME}/init/bash_completion" -#fi +# :FIXME: ############################################################################# # legacy... @@ -21,27 +11,29 @@ fi unset MODULE_VERSION unset MODULE_VERSION_STACK unset MODULESHOME -unset PMODULES_ENV - -declare -x PMODULES_DIR="${PMODULES_HOME}" - ############################################################################# # implement module comand as shell function # -module() { - eval $("${PMODULES_HOME}/bin/modulecmd" bash "$@") -} +declare -- PMODULES_HOME=$0:A:h:h + +declare -- _cmd_="module() { + unset BASH_ENV + local output=\"\$(\"${PMODULES_HOME}/bin/modulecmd\" zsh \"\$@\")\" + eval \"\${output}\" +}" + +eval "$(echo "${_cmd_}")" export -f module -modbuild(){ - ( - eval $("${PMODULES_HOME}/bin/modulecmd" bash load System:bash) - "${PMODULES_HOME}/bin/modbuild" "$@" - ) -} +_cmd_="modbuild(){ + \"${PMODULES_HOME}/bin/modbuild\" \"\$@\" +}" +eval "$(echo "${_cmd_}")" export -f modbuild +unset _cmd_ + # Local Variables: # mode: sh # sh-basic-offset: 8