-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathspider.in.lua
885 lines (755 loc) · 24.9 KB
/
spider.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
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
#!@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 readlink = posix.readlink
local stat = posix.stat
local access = posix.access
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("serializeTbl")
require("pairsByKeys")
require("fileOps")
require("modfuncs")
require("cmdfuncs")
require("deepcopy")
require("parseVersion")
require("declare")
MainControl = require("MainControl")
Cache = require("Cache")
MRC = require("MRC")
Hub = require("Hub")
BaseShell = require("BaseShell")
Shell = false
local Optiks = require("Optiks")
local Spider = require("Spider")
local Version = require("Version")
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
-- key: /opt/apps/gcc-4_8/mpich/3.1.1,
-- path: /opt/apps/gcc-4_8/mpich/3.1.1/lib, i: nil, j: nil
--------------------------------------------------------------------------
-- Check path against the *ignoreT* table. Also it must be "in" dirA if
-- dirA exists.
-- @param path input path.
local function l_keepThisPath(path, dirA, keepA, ignoreA)
dbg.start{"l_keepThisPath(",path,", dirA)"}
if (dirA) then
local match = false
for k in pairs(dirA) do
local i, j = path:find(k,1,true)
dbg.print{"key: \"",k, "\", path: \"",path,"\", i: ",i,", j: ",j,"\n"}
if ( i == 1) then
match = true
break
end
end
if (not match) then
dbg.print{"No match\n"}
dbg.fini("l_keepThisPath")
return false
end
end
for i = 1, #keepA do
if (path:find(keepA[i],1,true) == 1) then
dbg.print{"In keepA list: path:",path,"\n"}
dbg.fini("l_keepThisPath")
return true
end
end
for i = 1, #ignoreA do
if (path:find(ignoreA[i],1,true) == 1) then
dbg.print{"In ignore list: path:",path,"\n"}
dbg.print{"Keep: false\n"}
dbg.fini("l_keepThisPath")
return false
end
end
dbg.print{"Keep: true\n"}
dbg.fini("l_keepThisPath")
return true
end
--------------------------------------------------------------------------
-- Store an entry in *rmapT*
-- @param entry
-- @param tbl
-- @param rmapT
-- @param kind
local function l_add2map(entry, tbl, dirA, moduleFn, kind, rmapT)
dbg.start{"l_add2map(entry, tbl, dirA, moduleFn, kind, rmapT)"}
local keepA, ignoreA = hook.apply("reverseMapPathFilter")
dbg.printT("ignoreA",ignoreA)
if (type(keepA) ~= "table" ) then
LmodError{msg="e_No_table", name = "keepA"}
end
if (type(ignoreA) ~= "table" ) then
LmodError{msg="e_No_table", name = "ignoreA"}
end
for path in pairs(tbl) do
local attr = lfs.attributes(path)
local a = attr or {}
local keep = l_keepThisPath(path,dirA, keepA, ignoreA)
dbg.print{"path: ",path,", keep: ",keep,", attr.mode: ",a.mode,"\n"}
if (keep and attr and attr.mode == "directory") then
local t = rmapT[path] or {pkg=entry.fullName, kind = kind, moduleFn = moduleFn, flavorT = {}}
local flavorT = t.flavorT
if (entry.parentAA == nil) then
flavorT.default = true
else
for i = 1, #entry.parentAA do
local parentA = entry.parentAA[i]
local a = {"default"}
for j = 1, #parentA do
a[#a+1] = parentA[j]
end
local key = concatTbl(a,':')
flavorT[key] = true
end
end
dbg.print{"assigning rmapT for path: ",path,"\n"}
rmapT[path] = t
local p2 = realpath(path)
if (p2 and p2 ~= path) then
dbg.print{"assigning rmapT for path: ",p2,"\n"}
rmapT[p2] = deepcopy(t)
end
end
end
dbg.fini("l_add2map")
end
--------------------------------------------------------------------------
-- Build the version string.
function l_version()
local v = {}
v[#v + 1] = "\nModules based on Lua: Version " .. Version.name().."\n"
v[#v + 1] = " by Robert McLay [email protected]\n\n"
return concatTbl(v,"")
end
--------------------------------------------------------------------------
--
-- @param moduleDirA
-- @param moduleT
-- @param timestampFn
-- @param dbT
local function l_rptList(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{"l_rptList(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
local spider = Spider:new()
local tbl = spider:listModules(dbT)
for k in pairsByKeys(tbl) do
print(k)
end
dbg.fini("l_rptList")
end
local function l_rptSpiderT(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptSpiderT(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
local mrc = MRC:singleton()
local ts = { timestampFn }
local s1 = serializeTbl{name="timestampFn", value=ts, indent=true}
local s2 = mrc:export()
local s3 = serializeTbl{name="spiderT", value=spiderT, indent=true}
local s4 = serializeTbl{name="mpathMapT", value=mpathMapT, indent=true}
io.stdout:write(s1,s2,s3,s4,"\n")
dbg.fini("l_rptSpiderT")
end
local function l_buildReverseMapT(dbT)
dbg.start{"l_buildReverseMapT(dbT)"}
local reverseMapT = {}
for sn,vvv in pairs(dbT) do
for fn, entry in pairs(vvv) do
dbg.print{"sn: ",sn,", fn: ",fn,"\n"}
if (entry.pathA) then
l_add2map(entry, entry.pathA, entry.dirA, fn, "bin", reverseMapT)
end
if (entry.lpathA) then
l_add2map(entry, entry.lpathA, entry.dirA, fn, "lib", reverseMapT)
end
if (entry.dirA) then
l_add2map(entry, entry.dirA, entry.dirA, fn, "dir", reverseMapT)
end
end
end
for kk, vv in pairs(reverseMapT) do
local flavor = {}
local flavorT = vv.flavorT
for k, v in pairs(flavorT) do
flavor[#flavor+1] = k
end
vv.flavorT = nil
sort(flavor)
vv.flavor = flavor
end
dbg.fini("l_buildReverseMapT")
return reverseMapT
end
local function l_buildXALTrmapT(reverseMapT)
local rmapT = {}
for path,entry in pairs(reverseMapT) do
local value = entry.pkg
local flavor = entry.flavor[1] or ""
flavor = flavor:gsub("default:?","")
if (flavor ~= "") then
value = value .. "(" .. flavor .. ")"
end
rmapT[path] = value
end
return rmapT
end
local function l_buildLibMapA(reverseMapT)
local libT = {}
for path,v in pairs(reverseMapT) do
local kind = v.kind
if (kind == "lib") then
local attr = lfs.attributes(path)
if (attr and type(attr) == "table" and attr.mode == "directory" and
access(path,"x")) then
for file in lfs.dir(path) do
local ext = extname(file)
if (ext == ".a" or ext == ".so" or ext == ".dylib") then
libT[file] = true
end
end
end
end
end
local libA = {}
local i = 0
for k in pairs(libT) do
i = i + 1
libA[i] = k
end
return libA
end
local function l_rptReverseMapT(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptReverseMapT(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
local ts = { timestampFn }
local reverseMapT = l_buildReverseMapT(dbT)
local libA = l_buildLibMapA(reverseMapT)
local s1 = serializeTbl{name="timestampFn", value=ts,
indent=true}
local s2 = serializeTbl{name="reverseMapT",
value=reverseMapT, indent=true}
local s3 = serializeTbl{name="xlibmap", value = libA,
indent = true}
io.stdout:write(s1,s2,s3,"\n")
dbg.fini("l_rptReverseMapT")
end
local function l_rptReverseMapTJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptReverseMapTJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
if (_VERSION ~= "Lua 5.1") then
require("declare")
declare("loadstring")
loadstring = load
end
local json = require("json")
local reverseMapT = l_buildReverseMapT(dbT)
local libA = l_buildLibMapA(reverseMapT)
local t = { timestampFn = timestampFn,
reverseMapT = reverseMapT,
xlibmap = libA}
print(json.encode(t))
dbg.fini("l_rptReverseMapTJson")
end
local function l_rptXALTRmapTJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptXALTRmapTJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
if (_VERSION ~= "Lua 5.1") then
require("declare")
declare("loadstring")
loadstring = load
end
local json = require("json")
local reverseMapT = l_buildReverseMapT(dbT)
local libA = l_buildLibMapA(reverseMapT)
local rmapT = l_buildXALTrmapT(reverseMapT)
local t = { reverseMapT = rmapT,
xlibmap = libA}
print(json.encode(t))
dbg.fini("l_rptXALTRmapTJson")
end
local function l_rptSoftwarePageJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptSoftwarePageJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
if (_VERSION ~= "Lua 5.1") then
require("declare")
declare("loadstring")
loadstring = load
end
local json = require("json")
local spA = softwarePage(dbT)
print(json.encode(spA))
dbg.fini("l_rptSoftwarePageJson")
end
local function l_rptSoftwarePageLua(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptSoftwarePageLua(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
local spA = softwarePage(dbT)
local s = serializeTbl{name="spA", value=spA, indent=true}
print(s)
dbg.fini("l_rptSoftwarePageLua")
end
local function l_rptSoftwarePageXml(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptSoftwarePageXml(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
local xmlStr = xmlSoftwarePage(dbT)
print(xmlStr)
dbg.fini("l_rptSoftwarePageXml")
end
local function l_rptDbT(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptDbT(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
local ts = { timestampFn }
local s1 = serializeTbl{name="timestampFn", value=ts, indent=true}
local s2 = serializeTbl{name="dbT", value=dbT, indent=true}
local s3 = serializeTbl{name="provideByT", value=providedByT, indent=true}
io.stdout:write(s1,s2,s3,"\n")
dbg.fini("l_rptDbT")
end
local function l_rptDbTJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)
dbg.start{ "l_rptDbTJson(mpathMapT, spiderT, timestampFn, dbT, providedByT)"}
if (_VERSION ~= "Lua 5.1") then
require("declare")
declare("loadstring")
loadstring = load
end
local json = require("json")
print(json.encode(dbT))
dbg.fini("l_rptDbTJson")
end
function main()
options()
local optionTbl = optionTbl()
local pargs = optionTbl.pargs
local mpathA = {}
Shell = BaseShell:build("bash")
local hub = Hub:singleton(false)
if (optionTbl.version) then
io.stderr:write(l_version())
os.exit(0)
end
for _, v in ipairs(pargs) do
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)
if (optionTbl.debug > 0 or optionTbl.dbglvl) then
local dbgLevel = math.max(optionTbl.debug, optionTbl.dbglvl or 1)
dbg:activateDebug(dbgLevel)
end
dbg.start{"Spider main()"}
MCP = MainControl.build("spider")
mcp = MainControl.build("spider")
dbg.print{"LMOD_TRACING: ",cosmic:value("LMOD_TRACING"),"\n"}
------------------------------------------------------------------
-- initialize lmod with SitePackage and /etc/lmod/lmod_config.lua
initialize_lmod()
------------------------------------------------------------------------
-- do not colorize output from spider
colorize = plain
local mrc = MRC:singleton()
mrc:set_display_mode("spider")
-- Make sure that MRC uses $LMOD_MODULERC and ignores ~/.modulerc when building the cache
local remove_MRC_home = true
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()
if (dbg.active()) then
for k,v in pairs(spiderT) do
dbg.print{"k: ",k,"\n"}
end
end
-- This interpT converts user outputstyle into a function call.
local interpT = {
list = l_rptList,
moduleT = l_rptSpiderT,
spiderT = l_rptSpiderT,
softwarePage = l_rptSoftwarePageJson,
jsonSoftwarePage = l_rptSoftwarePageJson,
xmlSoftwarePage = l_rptSoftwarePageXml,
softwarePageLua = l_rptSoftwarePageLua,
reverseMapT = l_rptReverseMapT,
reverseMap = l_rptReverseMapT,
jsonReverseMapT = l_rptReverseMapTJson,
jsonReverseMap = l_rptReverseMapTJson,
xalt_rmapT = l_rptXALTRmapTJson,
xalt_rmap = l_rptXALTRmapTJson,
["spider-json"] = l_rptDbTJson,
dbT = l_rptDbT,
}
-- grap function and run with it.
local func = interpT[optionTbl.outputStyle]
if (func) then
func(mpathMapT, spiderT, optionTbl.timestampFn, dbT, providedByT)
end
dbg.fini()
end
function softwarePage(dbT)
local spA = {}
for name, vv in pairs(dbT) do
convertEntry(name, vv, spA)
end
return spA
end
function convertEntry(name, vv, spA)
local topKeyT = {
Category = "categories",
Description = "description",
Keyword = "keywords",
Name = "displayName",
URL = "url",
}
local keyT = {
Version = "versionName",
Description = "description",
fullName = "full",
help = "help",
parentAA = "parent",
wV = "wV",
hidden = "hidden",
family = "family",
propT = "properties",
provides = "provides",
}
local entry = {}
entry.package = name
local versionA = {}
local wV = " " -- This is the lowest possible value for a pV
local epoch = 0
local a = {}
--------------------------------------------------------
-- Sort the version by pV
for mfPath,v in pairs(vv) do
a[#a+1] = { mfPath, v.wV }
end
local function l_cmp_wV(x,y)
return x[2] < y[2]
end
sort(a,l_cmp_wV)
------------------------------------------------------------
-- Loop over version from lowest to highest version in pv
-- order.
for i = 1, #a do
local mfPath = a[i][1]
local v = vv[mfPath]
local vT = {}
vT.path = mfPath
if (v.wV > wV) then
wV = v.wV
for topKey, newKey in pairs(topKeyT) do
entry[newKey] = v[topKey]
end
entry.defaultVersionName = v.Version
end
for key, newKey in pairs(keyT) do
if (v[key]) then
vT[newKey] = v[key]
end
end
vT.canonicalVersionString = ""
if (v.Version) then
vT.canonicalVersionString = v.pV
end
if (v.wV) then
vT.markedDefault=isMarked(v.wV)
end
if (not vT.hidden) then
versionA[#versionA + 1] = vT
end
end
entry.versions = versionA
spA[#spA+1] = entry
end
local function l_Error(...)
local argA = pack(...)
for i = 1,argA.n do
stderr:write(argA[i])
end
end
local function l_prt(...)
stderr:write(...)
end
function options()
local optionTbl = optionTbl()
local usage = "Usage: spider [options] moduledir ..."
local cmdlineParser = Optiks:new{usage = usage,
version = "1.0",
error = l_Error,
prt = l_prt,
}
cmdlineParser:add_option{
name = {'-v','--version'},
dest = 'version',
action = 'store_true',
help = "Version info",
}
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",
}
cmdlineParser:add_option{
name = {'--debug'},
dest = 'dbglvl',
action = 'store',
help = "Program tracing written to stderr",
}
cmdlineParser:add_option{
name = {'-o','--output'},
dest = 'outputStyle',
action = 'store',
default = "list",
help = "Output Style: list, spiderT, dbT, reverseMapT, "..
"jsonReverseMapT, spider-json, softwarePage, "..
"jsonSoftwarePage, xmlSoftwarePage, xalt_rmapT"
}
cmdlineParser:add_option{
name = {'--timestampFn'},
dest = 'timestampFn',
action = 'store',
default = false,
help = "Absolute path to the timestamp file"
}
cmdlineParser:add_option{
name = {'-n','--no_recursion'},
dest = 'no_recursion',
action = 'store_true',
default = false,
}
cmdlineParser:add_option{
name = {'--preload'},
dest = 'preload',
action = 'store_true',
default = false,
help = "Use preloaded modules to build reverseMapT"
}
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 xmlSoftwarePage(dbT)
require("LuaXML") -- This defines xml
local translateT = { ls4 = "lonestar" }
local hostName = posix.uname("%n")
local i = hostName:find("%.") or 0
local host = hostName:sub(i+1,-1)
host = host:gsub("%..*","")
host = translateT[host] or host
local top = xml.new{
[0] = "V4RPSoftwareRP",
["xmlns:ns1"] = "https://mds.teragrid.org/2007/02/ctss"
}
local root = xml.new{
[0] = "RPSoftwareList",
Timestamp = os.date("!%Y-%m-%dT%H:%M:%SZ"),
UniqueID="localsoft4."..host..".tacc.xsede.org",
}
local resourceID = xml.new("ResourceID")
resourceID[1] = host..".tacc.xsede.org"
root:append(resourceID)
local siteID = xml.new("SiteID")
siteID[1] = "tacc.xsede.org"
root:append(siteID)
for name, vv in pairs(dbT) do
for file, v in pairs(vv) do
local xmlT = localSoftware(xml,name,v)
root:append(xmlT)
end
end
top:append(root)
return top:str()
end
function splitNV(full)
local name = full
local version = ""
local i,j = full:find(".*/")
if (j) then
name = full:sub(1,j-1)
version = full:sub(j+1)
end
return name, version
end
function findLatestV(a)
local aa = {}
if a == nil then
return "default"
end
for i = 1, #a do
local entryfull = concatTbl(a[i],":")
local b = {}
for j = 1, #a[i] do
local entry = a[i][j]
local name, version = splitNV(entry)
b[#b+1] = name .. "/" .. parseVersion(version)
end
aa[i] = { concatTbl(b,":"), entryfull}
end
table.sort(aa, function(x,y) return x[1] > y[1] end)
local result = aa[1][2]
if (result ~= "default") then
result = result:gsub("default:","")
end
return result
end
function localSoftware(xml, name, t)
dbg.start{"localSoftware(xml,",name,",t)"}
local root = xml.new("LocalSoftware")
local Name
local value = "unknown"
local domain = "unknown"
local category = t.Category or ""
for entry in category:split(",") do
entry = entry:trim()
local entryL = entry:lower()
if (value == "unknown") then
if (entryL == "library") then
value = "library"
elseif (entryL == "application") then
value = "application"
end
end
if (domain == "unknown") then
if (entryL ~= "library" and
entryL ~= "application" and
entry ~= "") then
domain = entry
end
end
end
if (value == "unknown") then
value = "application"
end
Name = xml.new("Type")
Name[1] = value
root:append(Name)
dbg.print{"Type: ",value,"\n"}
Name = xml.new("Domain")
Name[1] = domain
root:append(Name)
dbg.print{"domain: ",domain,"\n"}
Name = xml.new("Name")
Name[1] = name
root:append(Name)
dbg.print{"name: ",name,"\n"}
local Description = xml.new("Description")
Description[1] = t.Description
root:append(Description)
local Flavor = xml.new("Flavor")
Flavor[1] = t.Version
root:append(Flavor)
local Default = xml.new("Default")
local result = "no"
if (t.default) then result = "yes" end
Default[1] = result
root:append(Default)
local Handle = xml.new("Handle")
local HType = xml.new("HandleType")
HType[1] = "module"
Handle:append(HType)
local HKey = xml.new("HandleKey")
HKey[1] = t.fullName
Handle:append(HKey)
root:append(Handle)
local Context = xml.new("Context")
Context[1] = findLatestV(t.parentAA)
root:append(Context)
dbg.fini()
return root
end
main()