Debian Codegraph @

script/envml.cmd

raw
1 @echo off
2 setlocal enabledelayedexpansion
3
4 :: ENVML, setup environment with module then run specified command
5 :: Copyright (C) 2015-2021 CEA/DAM
6 ::
7 :: This program is free software: you can redistribute it and/or modify
8 :: it under the terms of the GNU General Public License as published by
9 :: the Free Software Foundation, either version 2 of the License, or
10 :: (at your option) any later version.
11 ::
12 :: This program is distributed in the hope that it will be useful,
13 :: but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :: GNU General Public License for more details.
16 ::
17 :: You should have received a copy of the GNU General Public License
18 :: along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ::
20
21 set progpath=%~dpnx0
22 set prog=%~nx0
23 set kind_of_arg=mod
24 set modarglist=
25 set maymodarglist=
26 set maycmdarglist=
27 set cmdarglist=
28
29
30 :: command help is asked
31 if "x%~1" equ "x" goto:echo_usage&goto:end_script
32 if "%~1" equ "-h" goto:echo_usage&goto:end_script
33 if "%~1" equ "--help" goto:echo_usage&goto:end_script
34
35 :: parse arguments
36 set ERRORLEVEL=
37 call :parse_args %*
38 if %ERRORLEVEL% NEQ 0 exit /B %ERRORLEVEL%&goto:end_script
39
40 :: if a cmd separator has been found what we thought
41 :: to be module-related is really module-related
42 if "%kind_of_arg%" equ "cmd" (
43 set modarglist=%modarglist%;%maymodarglist%
44 ) else (
45 set cmdarglist=%maycmdarglist% %cmdarglist%
46 )
47
48 :: initialize Modules if not already done
49 if not defined MODULES_CMD (
50 call "%~dp0..\init\cmd.cmd"
51 )
52
53 :: load all required modules
54 call :load_mod "%modarglist%"
55
56 :: now execute the real command with its interpreter
57 if "x%cmdarglist%" equ "x" goto:end_script
58
59 set CALLARG=
60 set t=%cmdarglist:"=%
61 if "%t:.bat=%" neq "%t%" set "CALLARG=call "
62
63 %CALLARG%%cmdarglist% %maymodarglist%
64
65 :end_script
66 endlocal &set ERRORLEVEL=%ERRORLEVEL%
67
68 goto:eof
69
70
71 :: print message on stderr then exit
72 :echo_error
73 echo %prog%: %~1 >&2
74 exit /B 1
75 goto:eof
76
77 :arg_into_mod
78 set MODACTION=%~1
79 if "!kind_of_arg!" equ "mod" (
80 set modarglist=!modarglist!;%~1
81 ) else (
82 set maymodarglist=!maymodarglist!;%~1
83 set maycmdarglist=!maycmdarglist! %~1
84 )
85 call :decompose_mod "%%~2"
86
87 goto:eof
88
89
90 :arg_into_modaction
91
92 for /F "tokens=1,* delims==" %%I in ("%~1") do (
93 if "!kind_of_arg!" equ "mod" (
94 set modarglist=!modarglist!;!MODACTION! %%~I
95 ) else (
96 set maymodarglist=!maymodarglist!;!MODACTION! %%~I
97 set maycmdarglist=!maycmdarglist! %%~I
98 )
99 call :decompose_mod "%%~J"
100 )
101
102 goto:eof
103
104 :decompose_mod
105
106 for /F "tokens=1,* delims=;" %%I in ("%~1") do (
107 if "!kind_of_arg!" equ "mod" (
108 set modarglist=!modarglist! %%~I
109 ) else (
110 set maycmdarglist=!maycmdarglist! %%~I
111 )
112 if "%%~J" neq "" call :decompose_mod "%%~J"
113 )
114
115 goto:eof
116
117
118 :: print usage message
119 :echo_usage
120
121 echo(Usage: %progpath% [MODULE_ACTION]... [--] COMMAND [ARG]...
122 echo(Run MODULE_ACTION(s) to setup environment then run COMMAND.
123 echo.
124 echo(Syntax of supported MODULE_ACTIONs:
125 echo( purge unload all loaded modulefiles
126 echo( restore[=coll] restore module list from collection named coll or
127 echo( default collection if no collection name provided
128 echo( unload=mod1[;mod2...] remove modulefile(s) mod1, (mod2...)
129 echo( switch=mod1;mod2 unload modulefile mod1 and load mod2
130 echo( [load=]mod1[;mod2...] load modulefile(s) mod1, (mod2...)
131 echo.
132 echo(To clearly separate command-line arguments from the module action arguments
133 echo(a '--' argument can be used. Without this '--' separator, first argument is
134 echo(considered module action and following arguments are part of command-line.
135 echo.
136 echo(Examples:
137 echo( %progpath% restore command arg1 arg2
138 echo( %progpath% purge:mod1:mod2 command arg1 arg2
139 echo( %progpath% restore load=mod1;mod2 -- command arg1 arg2
140
141 goto:eof
142
143
144 :: parse command-line arguments
145 :parse_args
146
147 :parse_start
148 if "x%~1" equ "x" goto:parse_end
149 if "%~1" equ "--" set kind_of_arg=cmd&shift&goto:parse_start
150 if "!kind_of_arg!" equ "cmd" set cmdarglist=%cmdarglist% "%~1"&shift&goto:parse_start
151
152 if "%~1" equ "purge" set action=%~1&shift&call :arg_into_mod "%~1"&goto:parse_start
153 if "%~1" equ "restore" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
154 if "%~1" equ "unload" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
155 if "%~1" equ "switch" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
156 if "%~1" equ "load" set action=%~1&shift&call :arg_into_mod "%~1" "%~2"&shift&goto:parse_start
157
158 :: replace & by ; to avoid batch incompatibility
159 set "ARG0=%~1"
160 set ARG0=!ARG0:^&=;!
161
162 call :arg_into_modaction "!ARG0!"
163 shift
164
165 goto:parse_start
166
167 :parse_end
168 goto:eof
169
170 :load_mod
171
172 for /F "tokens=1,* delims=;" %%I in ("%~1") do (
173 if defined MODULE_DEBUG echo "module %%~I"
174 module %%~I
175 if !ERRORLEVEL! NEQ 0 exit /B !ERRORLEVEL!
176 if "%%~J" neq "" call :load_mod "%%~J"
177 )
178
179 goto:eof