-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathloadModuleFile.lua
More file actions
193 lines (172 loc) · 6.59 KB
/
Copy pathloadModuleFile.lua
File metadata and controls
193 lines (172 loc) · 6.59 KB
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
--------------------------------------------------------------------------
-- This module is the only file that actually reads and causes the
-- module file to be evaluated. If the file name has a ".lua" extension
-- then it is a Lua modulefile. Otherwise it is considered to be a TCL
-- modulefile.
-- @module loadModuleFile
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-2025 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("strict")
require("myGlobals")
require("fileOps")
require("sandbox")
require("string_utils")
require("utils")
local hook = require("Hook")
local dbg = require("Dbg"):dbg()
local concatTbl = table.concat
local getenv = os.getenv
local timer = require("Timer"):singleton()
local s_mfileCountT = {}
------------------------------------------------------------------------
-- loadModuleFile(t): read a modulefile in via sandbox_run
-- @param t The input table naming the file to be loaded plus other
-- things like the current list of modules and the shell.
function loadModuleFile(t)
dbg.start{"loadModuleFile(",t.file,")"}
--dbg.print{"mcp: ",mcp,"\n"}
local myType = extname(t.file)
local forbiddenT = t.forbiddenT or {}
local status = true
local lmodBrk = false
local func
local msg
local whole = nil
local userName
-- If the user is requesting an unload, don't complain if the file
-- has disappeared.
if (mode() == "unload" and not isFile(t.file)) then
dbg.fini("loadModuleFile")
return not lmodBrk
end
if (forbiddenT.forbiddenState == "forbid" ) then
if (forbiddenT.message) then
LmodError{msg="e_Forbidden", noTraceBack=true, literal_msg = forbiddenT.message,
fullName = myModuleFullName()}
else
LmodError{msg="e_Forbidden", noTraceBack=true, fullName = myModuleFullName()}
end
end
-- Check for infinite loop
if (mcp:mode() == "load" and t.file) then
s_mfileCountT[t.file] = ( s_mfileCountT[t.file] or 0) + 1
if (s_mfileCountT[t.file] > 500) then
LmodError{msg="e_Inf_Loop",file=t.file, fullName = myModuleFullName(), count = 500}
end
end
if (myType == ".lua") then
-- Read in lua module file into a [[whole]] string.
local f = io.open(t.file)
if (f) then
whole = f:read("*all")
dbg.start{"ModuleFile"}
dbg.print{whole}
dbg.fini("ModuleFile")
f:close()
end
else
dbg.print{"Reading tcl file\n"}
userName = myModuleUsrName()
local fullName = myModuleFullName()
-- Build argument list then call tcl2lua translator
-- Capture results into [[whole]] string.
local s = t.mList or ""
local A = {}
local mode = mcp:tcl_mode()
A[#A + 1] = "-l"
A[#A + 1] = "\"" .. s .. "\""
A[#A + 1] = "-f"
A[#A + 1] = fullName
A[#A + 1] = "-m"
A[#A + 1] = mode
A[#A + 1] = "-u"
A[#A + 1] = userName
A[#A + 1] = "-s"
A[#A + 1] = t.shell
local ldlib = getenv("LD_LIBRARY_PATH")
if (ldlib) then
A[#A + 1] = "-L"
A[#A + 1] = "\"" .. ldlib .. "\""
end
local ld_preload = getenv("LD_PRELOAD")
if (ld_preload) then
A[#A + 1] = "-P"
A[#A + 1] = "\"" .. ld_preload .. "\""
end
if (t.help) then
A[#A + 1] = "-h"
end
A[#A + 1] = path_regularize(t.file)
whole, status = runTCLprog(pathJoin(cmdDir(),"tcl2lua.tcl"),concatTbl(A," "))
if (not status) then
local n = userName or ""
msg = "Non-zero status returned"
LmodError{msg="e_Unable_2_Load", name = n, fn = t.file, message = msg}
end
end
-- Use the sandbox to evaluate modulefile text.
if (whole) then
-- dynamic additions via hook
if ( hook.exists("decorate_module") ) then
local additional_lines = hook.apply("decorate_module", {path=myFileName(), name=myModuleName(), version=myModuleVersion(), contents=whole}) or {}
whole = whole .. "\n" .. concatTbl(additional_lines, "\n")
end
status, msg = sandbox_run(whole)
else
status = nil
msg = "Empty or non-existent file"
end
-- report any errors
-- Note: When a module calls LmodBreak() msg will be a table
-- so do not error out then.
if (not status and t.reportErr and type(msg) ~= "table") then
local n = userName or ""
LmodError{msg="e_Unable_2_Load", name = n, fn = t.file, message = msg}
end
if (type(msg) == "table" and msg.code == "LmodBreak") then
lmodBrk = true
end
if (forbiddenT.forbiddenState == "nearly" ) then
if (forbiddenT.nearlymessage) then
LmodMessage{msg="w_Nearly_Forbidden_w_msg",literal_msg = forbiddenT.nearlymessage,
after=forbiddenT.after}
else
LmodMessage{msg="w_Nearly_Forbidden", after=forbiddenT.after}
end
end
--dbg.print{"mcp: ",mcp,"\n"}
dbg.fini("loadModuleFile")
return not lmodBrk
end