-
Notifications
You must be signed in to change notification settings - Fork 122
/
modfuncs.lua
772 lines (652 loc) · 23 KB
/
modfuncs.lua
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
--------------------------------------------------------------------------
-- All the functions that are "Lmod" functions are in
-- this file. Since the behavior of many of the Lmod functions (such as
-- setenv) function when the user is doing a load, unload, show, many of
-- these function they do the following:
--
-- a) They validate their arguments.
-- b) mcp:<function>(...)
--
-- The variable mcp is the master control program object. It gets
-- constructed in the various modes Lmod gets run in. The modes include
-- load, unload, show, etc. See MC_Load.lua and the other MC_*.lua files
-- As well as the base class MasterControl.lua for more details.
--
-- See tools/Dbg.lua for details on how this debugging tool works.
-- @module modfuncs
require("strict")
--------------------------------------------------------------------------
-- Lmod License
--------------------------------------------------------------------------
--
-- Lmod is licensed under the terms of the MIT license reproduced below.
-- This means that Lmod is free software and can be used for both academic
-- and commercial purposes at absolutely no cost.
--
-- ----------------------------------------------------------------------
--
-- Copyright (C) 2008-2017 Robert McLay
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject
-- to the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
-- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
--------------------------------------------------------------------------
require("myGlobals")
require("colorize")
require("utils")
require("string_utils")
require("parseVersion")
require("TermWidth")
local BeautifulTbl = require("BeautifulTbl")
local MName = require("MName")
local Version = require("Version")
local dbg = require("Dbg"):dbg()
local hook = require("Hook")
local max = math.max
local _concatTbl = table.concat
local pack = (_VERSION == "Lua 5.1") and argsPack or table.pack -- luacheck: compat
--------------------------------------------------------------------------
-- Special table concat function that knows about strings and numbers.
-- @param aa Input array
-- @param sep output separator.
local function concatTbl(aa,sep)
if (not dbg.active()) then
return ""
end
local a = {}
for i = 1, #aa do
local v = aa[i]
local vType = type(v)
if ( vType == "string") then
a[i] = v
elseif (vType == "number") then
a[i] = tostring(v)
else
a[i] = vType
end
end
return _concatTbl(a, sep)
end
--------------------------------------------------------------------------
-- Validate a function with only string arguments.
-- @param cmdName The command which is getting its arguments validated.
local function validateStringArgs(cmdName, ...)
local arg = pack(...)
for i = 1, arg.n do
local v = arg[i]
if (type(v) ~= "string") then
mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
return false
end
end
return true
end
--------------------------------------------------------------------------
-- Validate a function with only string table.
-- @param cmdName The command which is getting its arguments validated.
local function validateStringTable(n, cmdName, t)
n = max(n,#t)
for i = 1, n do
local v = t[i]
if (type(v) ~= "string") then
mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
return false
end
end
if (t.priority ~= nil) then
local valid = false
if (t.priority == 0) then
valid = true
elseif (t.priority >= 10) then
valid = true
end
if (not valid) then
mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
return false
end
end
return true
end
--------------------------------------------------------------------------
-- Validate a function with only string module names table.
-- @param cmdName The command which is getting its arguments validated.
local function validateArgsWithValue(cmdName, ...)
local arg = pack(...)
for i = 1, arg.n -1 do
local v = arg[i]
if (type(v) ~= "string") then
mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
return false
end
end
local v = arg[arg.n]
if (type(v) ~= "string" and type(v) ~= "number" and type(v) ~= "boolean") then
mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
return false
end
return true
end
--------------------------------------------------------------------------
-- Validate a function with only string module names table.
-- @param cmdName The command which is getting its arguments validated.
local function validateModules(cmdName, ...)
local arg = pack(...)
dbg.print{"cmd: ",cmdName, " arg.n: ",arg.n,"\n"}
local allGood = true
local fn = false
for i = 1, arg.n do
local v = arg[i]
if (type(v) == "string") then
allGood = true
elseif (type(v) == "table" and v.__waterMark == "MName") then
allGood = true
else
allGood = false
fn = myFileName()
break
end
end
if (not allGood) then
mcp:report{msg="e_Args_Not_Strings", fn = myFileName(), cmdName = cmdName}
end
return allGood
end
--------------------------------------------------------------------------
-- The load function. It can be found in the following forms:
-- "load('name'); load('name/1.2'); load(atleast('name','3.2'))",
function load_module(...)
dbg.start{"load_module(",concatTbl({...},", "),")"}
if (not validateModules("load",...)) then return {} end
dbg.print{"mcp:name(): ",mcp:name(),"\n"}
local b = mcp:load_usr(MName:buildA(mcp:MNameType(), ...))
dbg.fini("load_module")
return b
end
--- PATH functions ---
--------------------------------------------------------------------------
-- convert arguments into a table if necessary.
local function convert2table(...)
local arg = pack(...)
local t = {}
if (arg.n == 1 and type(arg[1]) == "table" ) then
t = arg[1]
t[1] = t[1]:trim()
else
t[1] = arg[1]:trim()
t[2] = arg[2]
t.delim = arg[3]
end
t.priority = tonumber(t.priority or "0")
return t
end
--------------------------------------------------------------------------
-- Prepend a value to a path like variable.
function prepend_path(...)
local t = convert2table(...)
dbg.start{"prepend_path(",concatTbl(t,", "),")"}
if (not validateStringTable(2, "prepend_path",t)) then return end
mcp:prepend_path(t)
dbg.fini("prepend_path")
end
--------------------------------------------------------------------------
-- Append a value to a path like variable.
function append_path(...)
local t = convert2table(...)
dbg.start{"append_path(",concatTbl(t,", "),")"}
if (not validateStringTable(2, "append_path",t)) then return end
mcp:append_path(t)
dbg.fini("append_path")
end
--------------------------------------------------------------------------
-- Remove a value from a path like variable.
function remove_path(...)
local t = convert2table(...)
dbg.start{"remove_path(",concatTbl(t,", "),")"}
if (not validateStringTable(2, "remove_path",t)) then return end
mcp:remove_path(t)
dbg.fini("remove_path")
end
--- Set Environment functions ----
--------------------------------------------------------------------------
-- Set the value of environment variable and maintain a stack.
function pushenv(...)
dbg.start{"pushenv(",concatTbl({...},", "),")"}
if (not validateArgsWithValue("pushenv",...)) then return end
mcp:pushenv(...)
dbg.fini("pushenv")
return
end
--------------------------------------------------------------------------
-- Set the value of environment variable.
function setenv(...)
dbg.start{"setenv(",concatTbl({...},", "),")"}
if (not validateArgsWithValue("setenv",...)) then return end
mcp:setenv(...)
dbg.fini("setenv")
return
end
--------------------------------------------------------------------------
-- Unset the value of environment variable.
function unsetenv(...)
dbg.start{"unsetenv(",concatTbl({...},", "),")"}
if (not validateArgsWithValue("unsetenv",...)) then return end
mcp:unsetenv(...)
dbg.fini("unsetenv")
return
end
--------------------------------------------------------------------------
-- Put a command in stdout so it will get executed.
-- @param t the command table.
function execute(t)
dbg.start{"execute(...)"}
if (type(t) ~= "table" or not t.cmd or type(t.modeA) ~= "table") then
mcp:report{msg="e_Execute_Msg", fn = myFileName()}
return
end
local b = mcp:execute(t)
dbg.fini("execute")
return b
end
--------------------------------------------------------------------------
-- This function allows only module to claim the name. It is a
-- generalized prereq/conflict function.
function family(...)
dbg.start{"family(",concatTbl({...},", "),")"}
if (not validateStringArgs("family",...)) then return end
mcp:family(...)
dbg.fini("family")
end
--------------------------------------------------------------------------
-- Provide a list of loaded modules for sites to use
function loaded_modules()
dbg.start{"loaded_modules()"}
local a = mcp:loaded_modules()
dbg.fini{"loaded_modules"}
return a
end
--- Inherit function ---
--------------------------------------------------------------------------
-- This function finds the same named module in the MODULEPATH and
-- loads it.
function inherit(...)
dbg.start{"inherit(",concatTbl({...},", "),")"}
mcp:inherit(...)
dbg.fini("inherit")
end
--------------------------------------------------------------------------
-- Return the mode.
function mode()
local b = mcp:mode()
return b
end
--------------------------------------------------------------------------
-- Return true if in spider mode. Use mode function instead.
function is_spider()
dbg.start{"is_spider()"}
local b = mcp:is_spider()
dbg.fini("is_spider")
return b
end
--------------------------------------------------------------------------
-- Return true if the module is loaded.
-- @param m module name
function isloaded(m)
dbg.start{"isloaded(",m,")"}
if (not validateStringArgs("isloaded",m)) then return false end
local mname = MName:new("mt", m)
dbg.fini("isloaded")
return mname:isloaded()
end
function myFileName()
return mcp:myFileName()
end
--------------------------------------------------------------------------
-- Return the full name of the module
function myModuleFullName()
return mcp:myModuleFullName()
end
--------------------------------------------------------------------------
-- Return the name of the module (w/o) version.
function myModuleName()
return mcp:myModuleName()
end
--------------------------------------------------------------------------
-- Return the name of the module that the user specified.
function myModuleUsrName()
return mcp:myModuleUsrName()
end
--------------------------------------------------------------------------
-- Return the version of the module.
function myModuleVersion()
return mcp:myModuleVersion()
end
--------------------------------------------------------------------------
-- Return true if the module is in the pending state for a load.
-- @param m module name
function isPending(m)
if (not validateStringArgs("isPending",m)) then return false end
local mname = MName:new("mt", m)
return mname:isPending()
end
--------------------------------------------------------------------------
-- Report an error and quit.
function LmodError(...)
local b = mcp:error(...)
return b
end
--------------------------------------------------------------------------
-- Report a warning and continue operation.
function LmodWarning(...)
local b = mcp:warning(...)
return b
end
--------------------------------------------------------------------------
-- Print a message
function LmodMessage(...)
local b = mcp:message(...)
return b
end
---------------------------------------------------------------------------
-- Return the version of Lmod.
function LmodVersion()
return Version.tag()
end
-------------------------------------------------------------------------
-- Return shell that invoked Lmod.
function myShellName()
return mcp:myShellName()
end
function myShellType()
return mcp:myShellType()
end
--------------------------------------------------------------------------
-- The whatis database function.
function whatis(...)
dbg.start{"whatis(",concatTbl({...},", "),")"}
if (not validateStringArgs("whatis",...)) then return end
mcp:whatis(...)
dbg.fini("whatis")
end
--------------------------------------------------------------------------
-- the help function.
function help(...)
dbg.start{"help(...)"}
if (not validateStringArgs("help",...)) then return end
mcp:help(...)
dbg.fini("help")
end
function userInGroup(group)
local grps = capture("groups")
local found = false
local userId = capture("id -u")
local isRoot = tonumber(userId) == 0
for g in grps:split("[ \n]") do
if (g == group or isRoot) then
found = true
break
end
end
return found
end
--------------------------------------------------------------------------
-- Convert version to canonical so that it can be used in a comparison.
function convertToCanonical(s)
return parseVersion(s)
end
--- Prereq / Conflict ---
--------------------------------------------------------------------------
-- Test to see if a prereq module is loaded. Fail if it is not.
-- If more than one module is listed then it is an and condition.
function prereq(...)
dbg.start{"prereq(",concatTbl({...},", "),")"}
if (not validateModules("prereq", ...)) then return end
mcp:prereq(MName:buildA("mt", ...))
dbg.fini("prereq")
end
--------------------------------------------------------------------------
-- Test to see if any of prereq modules are loaded. Fail if it is not.
-- If more than one module is listed then it is an or condition.
function prereq_any(...)
dbg.start{"prereq_any(",concatTbl({...},", "),")"}
if (not validateModules("prereq_any",...)) then return end
mcp:prereq_any(MName:buildA("mt",...))
dbg.fini("conflict")
end
--------------------------------------------------------------------------
-- Test to see if a conflict module is loaded. Fail if it is loaded.
function conflict(...)
dbg.start{"conflict(",concatTbl({...},", "),")"}
if (not validateStringArgs("conflict",...)) then return end
mcp:conflict(MName:buildA("mt",...))
dbg.fini()
end
--------------------------------------------------------------------------
-- A load and prereq modifier. It is used like this:
-- load(atleast("gcc","4.8"))
-- @param m module name
-- @param is the starting version
function atleast(m, is)
dbg.start{"atleast(",m,", ",is,")"}
local mname = MName:new("load", m, "atleast", is)
dbg.fini("atleast")
return mname
end
--------------------------------------------------------------------------
-- A load and prereq modifier. It is used like this:
-- load(between("gcc","4.8","4.9"))
-- @param m module name
-- @param is the starting version
-- @param ie the ending version.
function between(m,is,ie)
dbg.start{"between(",m,is,ie,")"}
local mname = MName:new("load", m, "between", is, ie)
dbg.fini("between")
return mname
end
--------------------------------------------------------------------------
-- Load the latest version available. This will ignore defaults.
-- @param m module name
function latest(m)
dbg.start{"latest(",m,")"}
local mname = MName:new("load", m, "latest")
dbg.fini("latest")
return mname
end
--- Set Alias/Shell functions ---
--------------------------------------------------------------------------
-- Set an alias for bash and csh
function set_alias(...)
dbg.start{"set_alias(",concatTbl({...},", "),")"}
if (not validateArgsWithValue("set_alias",...)) then return end
mcp:set_alias(...)
dbg.fini("set_alias")
end
--------------------------------------------------------------------------
-- Unset an alias for bash and csh
function unset_alias(...)
dbg.start{"unset_alias(",concatTbl({...},", "),")"}
if (not validateStringArgs("unset_alias",...)) then return end
mcp:unset_alias(...)
dbg.fini("unset_alias")
end
--------------------------------------------------------------------------
-- Set an shell function for bash and an alias for csh
function set_shell_function(...)
dbg.start{"set_shell_function(",concatTbl({...},", "),")"}
if (not validateStringArgs("set_shell_function",...)) then return end
mcp:set_shell_function(...)
dbg.fini()
end
--------------------------------------------------------------------------
-- Unset an shell function for bash and an alias for csh
function unset_shell_function(...)
dbg.start{"unset_shell_function(",concatTbl({...},", "),")"}
if (not validateStringArgs("unset_shell_function",...)) then return end
mcp:unset_shell_function(...)
dbg.fini("unset_shell_function")
end
--- Property functions ----
--------------------------------------------------------------------------
-- Add a property to a module.
function add_property(...)
dbg.start{"add_property(",concatTbl({...},", "),")"}
if (not validateStringArgs("add_property",...)) then return end
mcp:add_property(...)
dbg.fini("add_property")
end
--------------------------------------------------------------------------
-- Remove a property to a module.
function remove_property(...)
dbg.start{"remove_property(",concatTbl({...},", "),")"}
if (not validateStringArgs("remove_property",...)) then return end
mcp:remove_property(...)
dbg.fini("remove_property")
end
--------------------------------------------------------------------------
-- Return the hierarchy based on the file name.
-- @param pkgName the full module name
-- @param levels the number of levels to return.
function hierarchyA(pkgName, levels)
local fn = myFileName():gsub("%.lua$","")
if (levels < 1) then
return {}
end
-- Remove pkgName from end of string by using the
-- "plain" matching via string.find function
pkgName = path_regularize(pkgName:gsub("%.lua$",""))
local i,j = fn:find(pkgName,1,true)
if (j == fn:len()) then
fn = fn:sub(1,i-1)
end
fn = path_regularize(fn)
j = 0
local numEntries = 0
while (j) do
j = pkgName:find("/",j+1)
numEntries = numEntries + 1
end
local a = {}
for dir in fn:split("/") do
a[#a + 1] = dir
end
local b = {}
local n = #a
for ia = 1, levels do
local bb = {}
for ja = 1, numEntries do
local idx = n - numEntries + ja
bb[ja] = a[idx]
end
b[ia] = _concatTbl(bb,'/')
n = n - numEntries
end
return b
end
--------------------------------------------------------------------------
-- Report the modulefiles stack for error report.
function moduleStackTraceBack(msg)
local FrameStk = require("FrameStk")
local frameStk = FrameStk:singleton()
msg = msg or "While processing the following module(s):\n"
if (frameStk:empty()) then return "" end
local aa = {}
aa[1] = { " ", "Module fullname", "Module Filename"}
aa[2] = { " ", "---------------", "---------------"}
local a = frameStk:traceBack()
for i = 1,#a do
local mname = a[i]
aa[#aa+1] = {" ",mname:fullName() or "" , mname:fn() or ""}
end
local bt = BeautifulTbl:new{tbl=aa}
local bb = {}
bb[#bb+1] = msg
bb[#bb+1] = bt:build_tbl()
return _concatTbl(bb,"")
end
--------------------------------------------------------------------------
-- Write "false" to stdout and exit.
function LmodErrorExit()
io.stdout:write("\nfalse\n")
os.exit(1)
end
--------------------------------------------------------------------------
-- Print msgs, traceback then exit.
function LmodSystemError(...)
MasterControl:error(...)
end
--------------------------------------------------------------------------
-- The try-load function. It can be found in the following forms:
-- "try_load('name'); try_load('name/1.2'); try_load(atleast('name','3.2'))",
-- The only difference between 'load' and 'try_load' is that a 'try_load'
-- will not produce a warning if the specified modulefile(s) do not exist.
function try_load(...)
dbg.start{"try_load(",concatTbl({...},", "),")"}
if (not validateModules("try_load",...)) then return {} end
local b = mcp:try_load(MName:buildA(mcp:MNameType(), ...))
dbg.fini("try_load")
return b
end
--------------------------------------------------------------------------
-- The unload function reads a module file and reverses all the commands
-- in the modulefile. It is not an error to unload a module which is
-- not loaded. The reverse of an unload is a no-op.
function unload(...)
dbg.start{"unload(",concatTbl({...},", "),")"}
if (not validateStringArgs("unload",...)) then return {} end
local b = mcp:unload(MName:buildA("mt",...))
dbg.fini("unload")
return b
end
--------------------------------------------------------------------------
-- This function always loads and never unloads.
function always_load(...)
dbg.start{"always_load(",concatTbl({...},", "),")"}
if (not validateModules("always_load",...)) then return {} end
local b = mcp:always_load(MName:buildA("load",...))
dbg.fini("always_load")
return b
end
--------------------------------------------------------------------------
-- This function always unloads and never loads. The reverse of this
-- function is a no-op.
function always_unload(...)
dbg.start{"always_unload(",concatTbl({...},", "),")"}
if (not validateStringArgs("always_unload",...)) then return {} end
local b = mcp:unload(MName:buildA("mt",...))
dbg.fini("always_unload")
return b
end
function depends_on(...)
dbg.start{"depends_on(",concatTbl({...},", "),")"}
if (not validateStringArgs("depends_on",...)) then return {} end
local b = mcp:depends_on(MName:buildA(mcp:MNameType(),...))
dbg.fini("depends_on")
end
--- subprocess function ---
function subprocess(cmd)
dbg.start{"subprocess(",cmd,")"}
local p = io.popen(cmd)
if p == nil then
return nil
end
local ret = p:read("*all")
p:close()
return ret
end