| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# ENVML, setup environment with module then run specified command |
| 4 |
# Copyright (C) 2015-2022 CEA/DAM |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
########################################################################## |
| 20 |
|
| 21 |
|
| 22 |
typeset progpath=$0 |
| 23 |
typeset prog=${progpath##*/} |
| 24 |
typeset arg |
| 25 |
typeset subarg |
| 26 |
typeset kind_of_arg='mod' |
| 27 |
typeset -a modarglist=() |
| 28 |
typeset -a maymodarglist=() |
| 29 |
typeset -a maycmdarglist=() |
| 30 |
typeset -a cmdarglist=() |
| 31 |
|
| 32 |
|
| 33 |
# print message on stderr then exit |
| 34 |
echo_error() { |
| 35 |
echo "$prog: $1" >&2 |
| 36 |
exit 1 |
| 37 |
} |
| 38 |
|
| 39 |
arg_into_modaction() { |
| 40 |
# split module action from its argument |
| 41 |
local action="${1/=/ }" |
| 42 |
# split multiple arguments |
| 43 |
if [ "${action//;/ }" == "${action}" ]; then |
| 44 |
action="${action//&/ }" |
| 45 |
else |
| 46 |
# enable arg split over ';' to enable same behavior than envml.cmd |
| 47 |
action="${action//;/ }" |
| 48 |
fi |
| 49 |
|
| 50 |
case "$action" in |
| 51 |
purge*|restore*|unload*|switch*|load*) echo "$action" ;; |
| 52 |
*) echo "load $action" ;; |
| 53 |
esac |
| 54 |
} |
| 55 |
|
| 56 |
# print usage message |
| 57 |
echo_usage() { |
| 58 |
echo "Usage: $progpath [MODULE_ACTION]... [--] COMMAND [ARG]... |
| 59 |
Run MODULE_ACTION(s) to setup environment then run COMMAND. |
| 60 |
|
| 61 |
Syntax of supported MODULE_ACTIONs: |
| 62 |
purge unload all loaded modulefiles |
| 63 |
restore[=coll] restore module list from collection named coll or |
| 64 |
default collection if no collection name provided |
| 65 |
unload=mod1[&mod2...] remove modulefile(s) mod1, (mod2...) |
| 66 |
switch=mod1&mod2 unload modulefile mod1 and load mod2 |
| 67 |
[load=]mod1[&mod2...] load modulefile(s) mod1, (mod2...) |
| 68 |
|
| 69 |
Multiple MODULE_ACTION arguments can be specified as one argument by |
| 70 |
separating them with a colon character (:). |
| 71 |
|
| 72 |
To clearly separate command-line arguments from the module action arguments |
| 73 |
a '--' argument can be used. Without this '--' separator, first argument is |
| 74 |
considered module action and following arguments are part of command-line. |
| 75 |
|
| 76 |
Examples: |
| 77 |
$progpath restore command arg1 arg2 |
| 78 |
$progpath purge:mod1:mod2 command arg1 arg2 |
| 79 |
$progpath restore load=mod1&mod2 -- command arg1 arg2" |
| 80 |
} |
| 81 |
|
| 82 |
# command help is asked |
| 83 |
if [ $# -eq 0 ] || [ "$1" == '-h' ] || [ "$1" == '--help' ]; then |
| 84 |
echo_usage |
| 85 |
exit 0 |
| 86 |
fi |
| 87 |
|
| 88 |
# parse arguments |
| 89 |
for arg in "${@}"; do |
| 90 |
# reach separator, everything after is part of cmd |
| 91 |
if [ "$arg" == '--' ]; then |
| 92 |
kind_of_arg='cmd' |
| 93 |
else |
| 94 |
if [ "$kind_of_arg" == 'cmd' ]; then |
| 95 |
cmdarglist+=("$arg") |
| 96 |
else |
| 97 |
for subarg in ${arg//:/ }; do |
| 98 |
if [ "$kind_of_arg" == 'mod' ]; then |
| 99 |
modarglist+=("$(arg_into_modaction "$subarg")") |
| 100 |
else |
| 101 |
maymodarglist+=("$(arg_into_modaction "$subarg")") |
| 102 |
maycmdarglist+=("$subarg") |
| 103 |
fi |
| 104 |
done |
| 105 |
|
| 106 |
# after first arg, we are not sure |
| 107 |
# following args are about module env |
| 108 |
if [ "$kind_of_arg" == 'mod' ]; then |
| 109 |
kind_of_arg='maymod' |
| 110 |
fi |
| 111 |
fi |
| 112 |
fi |
| 113 |
done |
| 114 |
|
| 115 |
# if a cmd separator has been found what we thought |
| 116 |
# to be module-related is really module-related |
| 117 |
if [ "$kind_of_arg" == 'cmd' ]; then |
| 118 |
modarglist+=("${maymodarglist[@]}") |
| 119 |
# elsewhere what we thought to be module-related |
| 120 |
# is in fact command-line related |
| 121 |
else |
| 122 |
cmdarglist=("${maycmdarglist[@]}" "${cmdarglist[@]}") |
| 123 |
fi |
| 124 |
|
| 125 |
|
| 126 |
# check module function is defined |
| 127 |
if ! typeset -F module >/dev/null; then |
| 128 |
echo_error "module command not found..." |
| 129 |
fi |
| 130 |
|
| 131 |
for arg in "${modarglist[@]}"; do |
| 132 |
module "$arg" |
| 133 |
done |
| 134 |
|
| 135 |
# now execute the real command with its interpreter |
| 136 |
exec "${cmdarglist[@]}" |