-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathcheck_module_tree_syntax.in.lua
401 lines (346 loc) · 12.5 KB
/
check_module_tree_syntax.in.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
#!@path_to_lua@
-- -*- lua -*-
--------------------------------------------------------------------------
-- Use command name to add the command directory to the package.path
-- @script spider
--------------------------------------------------------------------------
-- 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-2018 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.
--
--------------------------------------------------------------------------
local sys_lua_path = "@sys_lua_path@"
if (sys_lua_path:sub(1,1) == "@") then
sys_lua_path = package.path
end
local sys_lua_cpath = "@sys_lua_cpath@"
if (sys_lua_cpath:sub(1,1) == "@") then
sys_lua_cpath = package.cpath
end
package.path = sys_lua_path
package.cpath = sys_lua_cpath
_G._DEBUG = false
local arg_0 = arg[0]
local posix = require("posix")
local access = posix.access
local getuid = posix.getuid
local readlink = posix.readlink
local stat = posix.stat
local stderr = io.stderr
local st = stat(arg_0)
while (st.type == "link") do
local lnk = readlink(arg_0)
if (arg_0:find("/") and (lnk:find("^/") == nil)) then
local dir = arg_0:gsub("/[^/]*$","")
lnk = dir .. "/" .. lnk
end
arg_0 = lnk
st = stat(arg_0)
end
local ia,ja = arg_0:find(".*/")
local LuaCommandName_dir = "./"
if (ia) then
LuaCommandName_dir = arg_0:sub(1,ja)
end
package.path = LuaCommandName_dir .. "../tools/?.lua;" ..
LuaCommandName_dir .. "../tools/?/init.lua;" ..
LuaCommandName_dir .. "../shells/?.lua;" ..
LuaCommandName_dir .. "?.lua;" ..
sys_lua_path
package.cpath = LuaCommandName_dir .. "../lib/?.so;"..
sys_lua_cpath
function cmdDir()
return LuaCommandName_dir
end
require("strict")
require("myGlobals")
require("utils")
require("colorize")
require("pairsByKeys")
require("fileOps")
require("modfuncs")
require("deepcopy")
require("parseVersion")
require("TermWidth")
_G.MainControl = require("MainControl")
Shell = false
local BaseShell = require("BaseShell")
local Cache = require("Cache")
local FrameStk = require("FrameStk")
local MRC = require("MRC")
local MName = require("MName")
local MT = require("MT")
local Hub = require("Hub")
local ModuleA = require("ModuleA")
local Optiks = require("Optiks")
local Spider = require("Spider")
local concatTbl = table.concat
local cosmic = require("Cosmic"):singleton()
local dbg = require("Dbg"):dbg()
local hook = require("Hook")
local i18n = require("i18n")
local lfs = require("lfs")
local sort = table.sort
local pack = (_VERSION == "Lua 5.1") and argsPack or table.pack -- luacheck: compat
local function l_nothing()
end
function walk_spiderT(spiderT, mt, mList, errorT)
dbg.start{"walk_spiderT(spiderT, mList, errorT)"}
local mrc = MRC:singleton()
local function l_walk_moduleA_helper(mpath, sn, v)
dbg.print{"in l_walk_moduleA_helper(",mpath,",",sn,")\n"}
if (next(v.defaultA) ~= nil and #v.defaultA > 1) then
too_many_defaultA_entries(mpath, sn, v.defaultA, errorT.defaultA)
end
if (next(v.fileT) ~= nil) then
for fullName, vv in pairs(v.fileT) do
local resultT = mrc:isVisible{fullName=fullName,sn=sn,fn=vv.fn, mpath = vv.mpath}
if (resultT.isVisible) then
check_syntax(mpath, mt, mList, sn, vv.fn, fullName, errorT.syntaxA)
end
end
end
if (next(v.dirT) ~= nil) then
for name, vv in pairs(v.dirT) do
l_walk_moduleA_helper(mpath, name, vv)
end
end
end
for mpath, vv in pairs(spiderT) do
if (mpath ~= "version") then
for sn, v in pairs(vv) do
l_walk_moduleA_helper(mpath, sn, v)
end
end
end
dbg.fini("walk_spiderT")
end
function too_many_defaultA_entries(mpath, sn, defaultA, errorA)
errorA[#errorA + 1] = pathJoin(mpath,sn)
end
local my_errorMsg = nil
function check_syntax(mpath, mt, mList, sn, fn, fullName, errorA)
dbg.start{"check_syntax(mpath=\"",mpath,"\", mList=\"",mList,"\", sn=\"",sn,"\", fn= \"",fn,"\", fullName=\"",fullName,"\"...)"}
local shell = _G.Shell
local tracing = cosmic:value("LMOD_TRACING")
local frameStk = FrameStk:singleton()
local function l_loadMe(entryT)
dbg.start{"loadMe(entryT, moduleStack, iStack, myModuleT)"}
local shellNm = "bash"
local mname = MName:new("entryT", entryT)
frameStk:push(mname)
mt:add(mname, "pending")
if (tracing == "yes") then
tracing_msg{"check_syntax Loading: ", fullName, " (fn: ", fn or "nil",")"}
end
loadModuleFile{file=entryT.fn, shell=shellNm, help=true, reportErr=true, mList = mList}
mt = frameStk:mt()
mt:setStatus(sn, "active")
frameStk:pop()
dbg.fini("loadMe")
end
local entryT = { fn = fn, sn = sn, userName = fullName, fullName = fullName,
version = extractVersion(fullName, sn)}
my_errorMsg = nil
l_loadMe(entryT)
if (my_errorMsg) then
errorA[#errorA + 1] = my_errorMsg
end
dbg.fini("check_syntax")
end
function check_syntax_error_handler(self, ...)
dbg.start{"check_syntax_error_handler(self, ...)"}
local twidth = TermWidth()
local argA = pack(...)
local kind = "lmoderror"
local label = "Error:"
local errMsg = ""
if (argA.n == 1 and type(argA[1]) == "table") then
local t = argA[1]
local key = t.msg
if (key == "e_Unable_2_Load") then key = "e_Unable_2_Load_short" end
if (key == "e_Args_Not_Strings") then key = "e_Args_Not_Strings_short" end
local msg = i18n(key, t) or "Unknown Error Message"
msg = hook.apply("errWarnMsgHook", kind, key, msg, t) or msg
errMsg = buildMsg(twidth, pack(label, msg))
else
errMsg = buildMsg(twidth, pack(label, ...))
end
my_errorMsg = "ModuleName: "..myModuleFullName()..", Fn: "..myFileName() .. " " .. errMsg
dbg.print{"Setting my_errorMsg to : ",my_errorMsg,"\n"}
dbg.fini("check_syntax_error_handler")
end
local function l_OptError(...)
local argA = pack(...)
for i = 1,argA.n do
io.stderr:write(argA[i])
end
end
local function l_prt(...)
io.stderr:write(...)
end
function options()
local optionTbl = optionTbl()
local usage = "Usage: check_module_tree_syntax [options] moduledir ..."
local cmdlineParser = Optiks:new{usage = usage,
version = "1.0",
error = l_OptError,
prt = l_prt,
}
cmdlineParser:add_option{
name = {'-D'},
dest = 'debug',
action = 'count',
help = "Program tracing written to stderr",
}
cmdlineParser:add_option{
name = {"-T", "--trace" },
dest = "trace",
action = "store_true",
help = "Tracing",
}
local optTbl, pargs = cmdlineParser:parse(arg)
if (optionTbl.trace) then
cosmic:assign("LMOD_TRACING", "yes")
end
for v in pairs(optTbl) do
optionTbl[v] = optTbl[v]
end
optionTbl.pargs = pargs
Use_Preload = optionTbl.preload
end
function main()
options()
local optionTbl = optionTbl()
local pargs = optionTbl.pargs
local mpathA = {}
local errorT = { defaultA = {}, syntaxA = {} }
Shell = BaseShell:build("bash")
------------------------------------------------------------------
-- initialize lmod with SitePackage and /etc/lmod/lmod_config.lua
initialize_lmod()
local mrc = MRC:singleton()
mrc:set_display_mode("spider")
dbg.set_prefix(colorize("red","Lmod"))
local hub = Hub:singleton(false)
for i = 1,#pargs do
local v = pargs[i]
for path in v:split(":") do
local my_path = path_regularize(path)
if (my_path:sub(1,1) ~= "/") then
stderr:write("Each path in MODULEPATH must be absolute: ",path,"\n")
os.exit(1)
end
mpathA[#mpathA+1] = my_path
end
end
local mpath = concatTbl(mpathA,":")
posix.setenv("MODULEPATH",mpath,true)
setenv_lmod_version() -- push Lmod version info into env for modulefiles.
if (optionTbl.debug > 0 or optionTbl.dbglvl) then
local dbgLevel = math.max(optionTbl.debug, optionTbl.dbglvl or 1)
dbg:activateDebug(dbgLevel)
end
dbg.start{"module_tree_check main()"}
_G.mcp = MainControl.build("spider")
_G.MCP = MainControl.build("spider")
local remove_MRC_home = getuid() < 501
local cache = Cache:singleton{dontWrite = true, quiet = true, buildCache = true,
buildFresh = true, noMRC=true}
local spider = Spider:new()
local spiderT, dbT,
mpathMapT, providedByT = cache:build()
_G.MCP = MainControl.build("checkSyntax")
_G.mcp = MainControl.build("checkSyntax")
mcp.error = check_syntax_error_handler
MCP.error = check_syntax_error_handler
mcp.report = check_syntax_error_handler
MCP.report = check_syntax_error_handler
Shell:setActive(false)
setSyntaxMode(true)
local mList = ""
local tracing = cosmic:value("LMOD_TRACING")
local mt = deepcopy(MT:singleton())
local maxdepthT = mt:maxDepthT()
local moduleDirT = {}
optionTbl.moduleStack = {}
optionTbl.dirStk = {}
optionTbl.mpathMapT = {}
local exit = os.exit
sandbox_set_os_exit(l_nothing)
if (Use_Preload) then
local a = {}
mList = getenv("LOADEDMODULES") or ""
for mod in mList:split(":") do
local i = mod:find("/[^/]*$")
if (i) then
a[#a+1] = mod:sub(1,i-1)
end
a[#a+1] = mod
end
mList = concatTbl(a,":")
end
if (tracing == "no" and not dbg.active()) then
turn_off_stdio()
end
walk_spiderT(spiderT, mt, mList, errorT)
sandbox_set_os_exit(exit)
if (tracing == "no" and not dbg.active()) then
turn_on_stdio()
end
local ierr = 0
if (next(errorT.defaultA) ~= nil) then
io.stderr:write("\nThe following directories have more than one marked default file:\n",
"-----------------------------------------------------------------\n")
table.sort(errorT.defaultA)
for i = 1,#errorT.defaultA do
ierr = ierr + 1
io.stderr:write(" ",errorT.defaultA[i],"\n")
end
io.stderr:write("\n")
end
if (next(errorT.syntaxA) ~= nil) then
io.stderr:write("\nThe following modulefile(s) have syntax errors:\n",
"-----------------------------------------------\n")
table.sort(errorT.syntaxA)
for i = 1,#errorT.syntaxA do
ierr = ierr + 1
io.stderr:write(" ",errorT.syntaxA[i],"\n")
end
io.stderr:write("\n")
end
dbg.fini("module_tree_check main")
if (ierr > 0) then
print("\n",colorize("red","There were ".. tostring(ierr) .. " possible errors found!"),"\n")
os.exit(1)
end
print("\nNo errors found\n")
end
main()