Debian Codegraph @

proj_mgmt/joinBase64Results

raw
1 #!/usr/bin/env lua
2 -- -*- lua -*-
3 --------------------------------------------------------------------------
4 -- Convert base64 encoded Strings into the module table. This is used
5 -- to generate test results.
6 -- @script joinBase64Results
7
8 --------------------------------------------------------------------------
9 -- Lmod License
10 --------------------------------------------------------------------------
11 --
12 -- Lmod is licensed under the terms of the MIT license reproduced below.
13 -- This means that Lmod is free software and can be used for both academic
14 -- and commercial purposes at absolutely no cost.
15 --
16 -- ----------------------------------------------------------------------
17 --
18 -- Copyright (C) 2008-2018 Robert McLay
19 --
20 -- Permission is hereby granted, free of charge, to any person obtaining
21 -- a copy of this software and associated documentation files (the
22 -- "Software"), to deal in the Software without restriction, including
23 -- without limitation the rights to use, copy, modify, merge, publish,
24 -- distribute, sublicense, and/or sell copies of the Software, and to
25 -- permit persons to whom the Software is furnished to do so, subject
26 -- to the following conditions:
27 --
28 -- The above copyright notice and this permission notice shall be
29 -- included in all copies or substantial portions of the Software.
30 --
31 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
33 -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
35 -- BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
36 -- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
37 -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 -- THE SOFTWARE.
39 --
40 --------------------------------------------------------------------------
41
42
43 local cmd = arg[0]
44
45 local i,j = cmd:find(".*/")
46 local cmd_dir = "./"
47 if (i) then
48 cmd_dir = cmd:sub(1,j)
49 end
50 package.path = cmd_dir .. "../tools/?.lua;" ..
51 cmd_dir .. "../tools/?/init.lua;" ..
52 cmd_dir .. "?.lua;" ..
53 package.path
54
55 _G._DEBUG = false
56 local posix = require("posix")
57
58 require("strict")
59
60 --------------------------------------------------------------------------
61 -- This is 5.1 Lua function to cover the table.pack function
62 -- that is in Lua 5.2 and later.
63
64 local function l_argsPack(...)
65 local arg = { n = select("#", ...), ...}
66 return arg
67 end
68 pack = (_VERSION == "Lua 5.1") and l_argsPack or table.pack
69
70 require("string_utils")
71 require("serializeTbl")
72 require("declare")
73 local base64 = require("base64")
74 local cosmic = require("Cosmic"):singleton()
75 local concatTbl = table.concat
76 local decode64 = base64.decode64
77 local load = (_VERSION == "Lua 5.1") and loadstring or load
78
79 cosmic:init{name = "LMOD_LD_LIBRARY_PATH", default = false, assignV = false}
80 cosmic:init{name = "LMOD_LD_PRELOAD", default = false, assignV = false}
81
82
83 local function l_grab(line, b)
84 local eqIdx = line:find("=")
85 local scIdx = line:find(";")
86 b[#b+1] = line:sub(eqIdx+1,scIdx-1):gsub("\"","")
87 end
88
89 local function l_grabCsh(line,b)
90 local a = {}
91 for v in line:split(' ') do
92 a[#a + 1] = v
93 end
94 b[#b+1] = a[3]:gsub(";","")
95 end
96
97 local function l_grabFish(line,b)
98 local a = {}
99 for v in line:split(' ') do
100 a[#a + 1] = v
101 end
102 b[#b+1] = a[5]:gsub(";","")
103 end
104
105
106
107 declare("_ModuleTable_",false)
108 declare("_SettargTable_",false)
109 function fixB64(name, s)
110 if (s == nil) then return "" end
111 local func, msg = load(s)
112 if (func) then
113 func()
114 else
115 io.stderr:write("joinBase64Results:fixB64:Found syntax error: ",msg,"\n")
116 io.stderr:write("--------------\n")
117 io.stderr:write(s,"\n")
118 io.stderr:write("--------------\n")
119 return ""
120 end
121
122 local mt = _G[name]
123
124 for k,v in pairs(mt) do
125 if (k:sub(1,2) == "c_") then
126 mt[k] = nil
127 end
128 end
129
130 local mT = mt.mT
131 for sn, v in pairs(mT) do
132 local mcmdT_64 = mT[sn].mcmdT_64
133 if (mcmdT_64 and next(mcmdT_64) ~= nil) then
134 local t = {}
135 for script64, mcmdA_64 in pairsByKeys(mcmdT_64) do
136 local a = {}
137 for i = 1,#mcmdA_64 do
138 a[i] = decode64(mcmdA_64[i])
139 end
140 local script = decode64(script64)
141 t[script] = a
142 end
143 mT[sn].mcmdT = t
144 mT[sn].mcmdT_64 = nil
145 end
146 end
147
148 local s = serializeTbl{indent=false, name=name, value=mt}
149 return s:gsub("[\n]",""):gsub(" = ","="):gsub(" *"," "):gsub(", ",",")
150 end
151
152 function sectionEnd(sType, name, A, a1, a2, a3)
153 local b = {}
154 if (#a2 > 0) then
155 local s64 = concatTbl(a2,"")
156 local s = decode64(s64)
157 local v = s
158 if (name == "_ModuleTable_") then
159 v = fixB64(name,s)
160 end
161 if (sType == "bash") then
162 b[1] = name .."='" .. v .. "';"
163 b[2] = "export ".. name ..";"
164 elseif (sType == "csh") then
165 b[1] = "setenv " .. name .. " '" .. v .. "';"
166 elseif (sType == "fish") then
167 b[1] = "set -x -g " .. name .. " '" .. v .. "';"
168 end
169 end
170 joinArrays(A, a1, b, a3)
171 a1 = {}
172 a2 = {}
173 a3 = {}
174 return a1, a2, a3
175 end
176
177 function joinArrays(A,...)
178 local argA = pack(...)
179 for i = 1, argA.n do
180 local b = argA[i]
181 for j = 1, #b do
182 A[#A + 1] = b[j]
183 end
184 end
185 end
186
187 function joinB64_fish(name, whole)
188 local A = {}
189 local a1 = {}
190 local a2 = {}
191 local a3 = {}
192 local a = a1
193 local state = "init"
194 for s in whole:split("\n") do
195 if (s:find("^set %-x %-g ".. name .. "%d%d%d_")) then
196 if (state == "init") then
197 state = "mt"
198 end
199 a = a3
200 l_grabFish(s, a2)
201 elseif (state == "init") then
202 a[#a + 1] = s
203 else
204 a1, a2, a3 = sectionEnd("fish",name.."_",A,a1,a2,a3)
205 state = "init"
206 a = a1
207 a[#a+1] = s
208 end
209 end
210 sectionEnd("fish",name.."_",A,a1,a2,a3)
211 return A
212 end
213
214 function joinB64_bash(name, whole)
215 local A = {}
216 local a1 = {}
217 local a2 = {}
218 local a3 = {}
219 local a = a1
220 local haveMT = false
221
222 for s in whole:split("\n") do
223 local state = "init"
224 if (s:find("^" .. name .."%d%d%d_=")) then
225 if (state == "init") then
226 state = "mt"
227 end
228 a = a3
229 l_grab(s, a2)
230 haveMT = true
231 elseif (s:find("^export " .. name ) or
232 s:find("^unset " .. name ) or
233 s:find("^" .. name .. "_Sz")) then
234 state = "ignore"
235 elseif (haveMT and s:find("^===========================")) then
236 a1, a2, a3 = sectionEnd("bash", name.."_", A, a1, a2, a3)
237 state = "init"
238 a = a1
239 haveMT = false
240 end
241 if (state == "init") then
242 a[#a + 1] = s
243 end
244 end
245 sectionEnd("bash", name.."_", A, a1, a2, a3)
246 return A
247 end
248
249 function joinB64_csh(name, whole)
250 local A = {}
251 local a1 = {}
252 local a2 = {}
253 local a3 = {}
254 local a = a1
255 local state = "init"
256 for s in whole:split("\n") do
257 if (s:find("^setenv ".. name .. "%d%d%d_")) then
258 if (state == "init") then
259 state = "mt"
260 end
261 a = a3
262 l_grabCsh(s, a2)
263 elseif (state == "init") then
264 a[#a + 1] = s
265 else
266 a1, a2, a3 = sectionEnd("csh", name.."_", A,a1,a2,a3)
267 state = "init"
268 a = a1
269 a[#a+1] = s
270 end
271 end
272 sectionEnd("csh", name.."_",A,a1,a2,a3)
273 return A
274 end
275
276 function lmod_stack(shell, whole)
277 local pattT =
278 {
279 csh = {"^(setenv __LMOD_STACK[^ ]*)%s+([^;]*);?", " "},
280 bash = {"^(__LMOD_STACK[^=]*)=([^;]*);", "="},
281 fish = {"^(set -x -g __LMOD_STACK[^ ]*)%s+\"(.*)\"", " "},
282 }
283 local a = {}
284 local patt = pattT[shell][1]
285 local sep = pattT[shell][2]
286 for s in whole:split("\n") do
287 local i,j, name, str = s:find(patt)
288 if (not i) then
289 a[#a + 1] = s
290 else
291 local b = {}
292 b[#b+1] = name
293 b[#b+1] = sep
294 for v in str:split(":") do
295 v = (v ~= "false") and decode64(v) or "false"
296 b[#b+1] = v:multiEscaped()
297 b[#b+1] = ":"
298 end
299 b[#b] = ";"
300 a[#a+1] = concatTbl(b,"")
301 end
302 end
303 return concatTbl(a,"\n")
304 end
305
306 function main()
307 local resultA
308 local resultB
309 local joinB64 = joinB64_csh
310 local shell = "csh"
311 if ( arg[1]:sub(1,1) == "-") then
312 if (arg[1] == "-bash") then
313 joinB64 = joinB64_bash
314 shell = "bash"
315 end
316 if (arg[1] == "-fish") then
317 joinB64 = joinB64_fish
318 shell = "fish"
319 end
320 table.remove(arg,1)
321 end
322
323 local fn = arg[1]
324 local f = io.open(fn)
325 if (f) then
326 local whole = f:read("*all")
327 f:close()
328 local s = lmod_stack(shell,whole)
329 resultA = joinB64("_ModuleTable", s)
330 local s = concatTbl(resultA,"\n")
331 resultB = joinB64("_SettargTable", s)
332 fn = arg[2]
333 f = io.open(fn,"w")
334 for _,v in ipairs(resultB) do
335 if (v:len() > 0) then
336 f:write(v,"\n")
337 end
338 end
339 f:close()
340 end
341
342 end
343
344 main()