File: envml.cmd

package info (click to toggle)
modules 5.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,996 kB
  • sloc: exp: 79,667; sh: 6,142; tcl: 5,895; makefile: 1,478; ansic: 474; python: 272; csh: 202; perl: 47; ruby: 44; lisp: 13
file content (179 lines) | stat: -rwxr-xr-x 5,004 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
@echo off
setlocal enabledelayedexpansion

:: ENVML, setup environment with module then run specified command
:: Copyright (C) 2015-2021 CEA/DAM
::
:: This program is free software: you can redistribute it and/or modify
:: it under the terms of the GNU General Public License as published by
:: the Free Software Foundation, either version 2 of the License, or
:: (at your option) any later version.
::
:: This program is distributed in the hope that it will be useful,
:: but WITHOUT ANY WARRANTY; without even the implied warranty of
:: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
:: GNU General Public License for more details.
::
:: You should have received a copy of the GNU General Public License
:: along with this program.  If not, see <http://www.gnu.org/licenses/>.
::

set progpath=%~dpnx0
set prog=%~nx0
set kind_of_arg=mod
set modarglist=
set maymodarglist=
set maycmdarglist=
set cmdarglist=


:: command help is asked
if "x%~1" equ "x" goto:echo_usage&goto:end_script
if "%~1" equ "-h"  goto:echo_usage&goto:end_script
if "%~1" equ "--help"  goto:echo_usage&goto:end_script

:: parse arguments
set ERRORLEVEL=
call :parse_args %*
if %ERRORLEVEL% NEQ 0 exit /B %ERRORLEVEL%&goto:end_script

:: if a cmd separator has been found what we thought
:: to be module-related is really module-related
if "%kind_of_arg%" equ "cmd" (
    set modarglist=%modarglist%;%maymodarglist%
) else (
    set cmdarglist=%maycmdarglist% %cmdarglist%
)

:: initialize Modules if not already done
if not defined MODULES_CMD (
   call "%~dp0..\init\cmd.cmd"
)

:: load all required modules
call :load_mod "%modarglist%"

:: now execute the real command with its interpreter
if "x%cmdarglist%" equ "x" goto:end_script

set CALLARG=
set t=%cmdarglist:"=%
if "%t:.bat=%" neq "%t%" set "CALLARG=call "

%CALLARG%%cmdarglist% %maymodarglist%

:end_script
endlocal &set ERRORLEVEL=%ERRORLEVEL%

goto:eof


:: print message on stderr then exit
:echo_error
    echo %prog%: %~1 >&2
    exit /B 1
goto:eof

:arg_into_mod
set MODACTION=%~1
if "!kind_of_arg!" equ "mod" (
    set modarglist=!modarglist!;%~1
) else (
    set maymodarglist=!maymodarglist!;%~1
    set maycmdarglist=!maycmdarglist! %~1
)
call :decompose_mod "%%~2"

goto:eof


:arg_into_modaction

for /F "tokens=1,* delims==" %%I in ("%~1") do (
    if "!kind_of_arg!" equ "mod" (
        set modarglist=!modarglist!;!MODACTION! %%~I
    ) else (
        set maymodarglist=!maymodarglist!;!MODACTION! %%~I
        set maycmdarglist=!maycmdarglist! %%~I
    )
    call :decompose_mod "%%~J"
)

goto:eof

:decompose_mod

for /F "tokens=1,* delims=;" %%I in ("%~1") do (
    if "!kind_of_arg!" equ "mod" (
        set modarglist=!modarglist! %%~I
    ) else (
        set maycmdarglist=!maycmdarglist! %%~I
    )
    if "%%~J" neq "" call :decompose_mod "%%~J"
)

goto:eof


:: print usage message
:echo_usage

echo(Usage: %progpath% [MODULE_ACTION]... [--] COMMAND [ARG]...
echo(Run MODULE_ACTION(s) to setup environment then run COMMAND.
echo.
echo(Syntax of supported MODULE_ACTIONs:
echo(  purge                  unload all loaded modulefiles
echo(  restore[=coll]         restore module list from collection named coll or
echo(                         default collection if no collection name provided
echo(  unload=mod1[;mod2...]  remove modulefile(s) mod1, (mod2...)
echo(  switch=mod1;mod2       unload modulefile mod1 and load mod2
echo(  [load=]mod1[;mod2...]  load modulefile(s) mod1, (mod2...)
echo.
echo(To clearly separate command-line arguments from the module action arguments
echo(a '--' argument can be used. Without this '--' separator, first argument is
echo(considered module action and following arguments are part of command-line.
echo.
echo(Examples:
echo(  %progpath% restore command arg1 arg2
echo(  %progpath% purge:mod1:mod2 command arg1 arg2
echo(  %progpath% restore load=mod1;mod2 -- command arg1 arg2

goto:eof


:: parse command-line arguments
:parse_args

:parse_start
if "x%~1" equ "x" goto:parse_end
if "%~1" equ "--" set kind_of_arg=cmd&shift&goto:parse_start
if "!kind_of_arg!" equ "cmd" set cmdarglist=%cmdarglist% "%~1"&shift&goto:parse_start

if "%~1" equ "purge" set action=%~1&shift&call :arg_into_mod "%~1"&goto:parse_start
if "%~1" equ "restore" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
if "%~1" equ "unload" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
if "%~1" equ "switch" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
if "%~1" equ "load" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start

:: replace & by ; to avoid batch incompatibility
set "ARG0=%~1"
set ARG0=!ARG0:^&=;!

call :arg_into_modaction "!ARG0!"
shift

goto:parse_start

:parse_end
goto:eof

:load_mod

for /F "tokens=1,* delims=;" %%I in ("%~1") do (
    if defined MODULE_DEBUG echo "module %%~I"
    module %%~I
	if !ERRORLEVEL! NEQ 0 exit /B !ERRORLEVEL!
    if "%%~J" neq "" call :load_mod "%%~J"
)

goto:eof