-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathSpider.lua
1550 lines (1367 loc) · 45.6 KB
/
Spider.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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
_G._DEBUG = false -- Required by the new lua posix
local posix = require("posix")
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-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.
--
--------------------------------------------------------------------------
require("myGlobals")
require("string_utils")
require("fileOps")
require("pairsByKeys")
require("utils")
require("serializeTbl")
require("deepcopy")
require("loadModuleFile")
require("sandbox")
local Banner = require("Banner")
local M = {}
local MRC = require("MRC")
local ModuleA = require("ModuleA")
local MT = require("MT")
local MName = require("MName")
local ReadLmodRC = require("ReadLmodRC")
local concatTbl = table.concat
local cosmic = require("Cosmic"):singleton()
local dbg = require("Dbg"):dbg()
local getenv = os.getenv
local hook = require("Hook")
local i18n = require("i18n")
local lfs = require("lfs")
local access = posix.access
local sort = table.sort
KeyT = {Description=true, Name=true, URL=true, Version=true, Category=true, Keyword=true}
function M.new(self)
local o = {}
setmetatable(o,self)
self.__index = self
o.__name = false
return o
end
local function l_nothing()
end
local function l_process(kind, value)
if (value == nil) then return end
local moduleStack = optionTbl().moduleStack
local iStack = #moduleStack
local moduleT = moduleStack[iStack].moduleT
local a = moduleT[kind] or {}
for path in value:split(":") do
path = path_regularize(path)
a[path] = 1
end
moduleT[kind] = a
end
function processLPATH(value)
l_process("lpathA",value)
end
function processPATH(value)
l_process("pathA",value)
end
function processDIR(value)
l_process("dirA",value)
end
local function l_processNewModulePATH(path)
dbg.start{"l_processNewModulePATH(path)"}
local optionTbl = optionTbl()
local dirStk = optionTbl.dirStk
local mpath_new = path_regularize(path)
dirStk[#dirStk+1] = mpath_new
local mpathMapT = optionTbl.mpathMapT
local moduleStack = optionTbl.moduleStack
local iStack = #moduleStack
local mpath_old = moduleStack[iStack].mpath
local moduleT = moduleStack[iStack].moduleT
local fullName = moduleStack[iStack].fullName
local t = mpathMapT[mpath_new] or {}
if (mpath_new ~= mpath_old) then
t[fullName] = mpath_old
mpathMapT[mpath_new] = t
moduleT.changeMPATH = true
end
dbg.printT("mpathMapT",mpathMapT)
dbg.fini("l_processNewModulePATH")
end
function Spider_dynamic_mpath()
local optionTbl = optionTbl()
local moduleStack = optionTbl.moduleStack
local iStack = #moduleStack
local moduleT = moduleStack[iStack].moduleT
moduleT.changeMPATH = true
end
function Spider_append_path(kind, t)
local name = t[1]
local value = t[2]
if (name == "MODULEPATH") then
dbg.start{kind, "(\"",name, "\" = \"", value, "\")"}
l_processNewModulePATH(value)
dbg.fini(kind)
elseif (name == "PATH") then
dbg.start{kind, "(\"",name, "\" = \"", value, "\")"}
if (value ~= ".") then
processPATH(value)
end
dbg.fini(kind)
elseif (name == "LD_LIBRARY_PATH" or name == "CRAY_LD_LIBRARY_PATH" ) then
dbg.start{kind, "(\"",name, "\" = \"", value, "\")"}
processLPATH(value)
dbg.fini(kind)
elseif (name == "PKG_CONFIG_PATH") then
local i = value:find("/lib/pkgconfig$")
if (i) then
dbg.start{kind, "(\"",name, "\" = \"", value, "\")"}
processLPATH(value:sub(1,i+3))
dbg.fini(kind)
end
end
end
local shellNm = "bash"
local function l_loadMe(entryT, moduleStack, iStack, myModuleT, mt, mList, mpath, sn, msg)
local shell = _G.Shell
local tracing = cosmic:value("LMOD_TRACING")
local fn = entryT.fn
local sn = entryT.sn
local fullName = entryT.fullName
local version = entryT.version
moduleStack[iStack] = { mpath = mpath, sn = sn, fullName = fullName, moduleT = myModuleT, fn = fn}
local mname = MName:new("entryT", entryT)
mt:add(mname, "pending")
if (tracing == "yes") then
tracing_msg{msg, fullName, " (fn: ", fn or "nil", ")"}
end
loadModuleFile{file=fn, help=true, shell=shellNm, reportErr=false,
mList = mList, forbiddenT = {}}
hook.apply("load_spider",{fn = fn, modFullName = fullName, sn = sn})
mt:setStatus(sn, "active")
reset_env()
end
local function l_findModules(mpath, mt, mList, sn, v, moduleT)
local entryT
local moduleStack = optionTbl().moduleStack
local iStack = #moduleStack
if (next(v.fileT) ~= nil) then
for fullName, vv in pairs(v.fileT) do
vv.Version = extractVersion(fullName, sn)
entryT = { fn = vv.fn, sn = sn, userName = fullName, fullName = fullName,
version = vv.Version }
l_loadMe(entryT, moduleStack, iStack, vv, mt, mList, mpath, sn, "Spider Loading: ")
end
end
if (next(v.dirT) ~= nil) then
for name, vv in pairs(v.dirT) do
l_findModules(mpath, mt, mList, sn, vv)
end
end
end
local function l_findChangeMPATH_modules(mpath, mt, mList, sn, v, moduleT)
local entryT
local moduleStack = optionTbl().moduleStack
local iStack = #moduleStack
if (next(v.fileT) ~= nil) then
for fullName, vv in pairs(v.fileT) do
if (vv.changeMPATH == true) then
vv.Version = extractVersion(fullName, sn)
entryT = { fn = vv.fn, sn = sn, userName = fullName, fullName = fullName,
version = vv.Version }
l_loadMe(entryT, moduleStack, iStack, vv, mt, mList, mpath, sn,"Spider Loading again: ")
end
end
end
if (next(v.dirT) ~= nil) then
for name, vv in pairs(v.dirT) do
l_findChangeMPATH_modules(mpath, mt, mList, sn, vv)
end
end
end
function M.searchSpiderDB(self, strA, dbT, providedByT)
dbg.start{"Spider:searchSpiderDB({",concatTbl(strA,","),"},spider, dbT)"}
local optionTbl = optionTbl()
if (not optionTbl.regexp) then
for i = 1, strA.n do
strA[i] = strA[i]:caseIndependent()
end
end
local kywdT = {}
for sn, vvv in pairs(dbT) do
kywdT[sn] = {}
local t = kywdT[sn]
for fn, vv in pairs(vvv) do
local whatisS = concatTbl(vv.whatis or {},"\n"):lower()
local found = false
local help = vv.help or ""
for i = 1,strA.n do
local str = strA[i]
if (sn:find(str) or whatisS:find(str) or help:find(str)) then
found = true
break
end
if (vv.propT and next(vv.propT) ~= nil) then
for propN,v in pairs(vv.propT) do
for k in pairs(v) do
if (k:find(str)) then
dbg.print{"k: ",k,"\n"}
found = true
break
end
end
end
end
end
if (found) then
t[fn] = vv
end
end
if (next(kywdT[sn]) == nil) then
kywdT[sn] = nil
end
end
local kywdExtsT = {}
for sn, vv in pairs(providedByT) do
for i = 1, strA.n do
local str = strA[i]
if (sn:find(str)) then
kywdExtsT[sn] = vv
end
end
end
dbg.fini("Spider:searchSpiderDB")
return kywdT, kywdExtsT
end
function M.findAllModules(self, mpathA, spiderT, mpathMapT)
dbg.start{"Spider:findAllModules(",concatTbl(mpathA,", "),")"}
spiderT.version = LMOD_CACHE_VERSION
local tracing = cosmic:value("LMOD_TRACING")
local dynamicCache = (cosmic:value("LMOD_DYNAMIC_SPIDER_CACHE") ~= "no")
local mt = deepcopy(MT:singleton())
local maxdepthT = mt:maxDepthT()
local optionTbl = optionTbl()
local moduleDirT = {}
optionTbl.moduleStack = {{}}
optionTbl.dirStk = {}
optionTbl.mpathMapT = {}
local mList = ""
local exit = os.exit
os.exit = l_nothing
local mcp_old = mcp
dbg.print{"Setting mcp to ", mcp:name(),"\n"}
mcp = MainControl.build("spider")
sandbox_set_os_exit(l_nothing)
if (tracing == "no" and not dbg.active()) then
dbg.print{"Turning off stdio\n"}
turn_off_stdio()
end
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
local dirStk = optionTbl.dirStk
for i = 1,#mpathA do
local mpath = mpathA[i]
if (isDir(mpath)) then
dirStk[#dirStk+1] = path_regularize(mpath)
end
end
local seenT = {}
while(#dirStk > 0) do
repeat
-- Pop top of dirStk
local mpath = dirStk[#dirStk]
dirStk[#dirStk] = nil
-- skip mpath if directory does not exist
-- or can not be read
local attr = lfs.attributes(mpath)
if (not attr or attr.mode ~= "directory" or
(not access(mpath,"rx"))) then break end
-- skip mpath directory if already walked.
if (seenT[mpath] or not isDir(mpath)) then break end
if (spiderT[mpath] == nil ) then
dbg.print{"Running l_findModules on: ", mpath,"\n"}
if (tracing == "yes") then
tracing_msg{"Full spider search on ",mpath}
end
local moduleA = ModuleA:__new({mpath}, maxdepthT):moduleA()
local T = moduleA[1].T
for sn, v in pairs(T) do
l_findModules(mpath, mt, mList, sn, v)
end
spiderT[mpath] = moduleA[1].T
elseif (dynamicCache) then
dbg.print{"Running l_findChangeMPATH_modules on: ", mpath,"\n"}
if (tracing == "yes") then
tracing_msg{"dynamic spider search on ",mpath}
end
for sn, v in pairs(spiderT[mpath]) do
l_findChangeMPATH_modules(mpath, mt, mList, sn, v)
end
end
seenT[mpath] = true
until true
end
dbg.print{"Resetting os.exit back\n"}
os.exit = exit
sandbox_set_os_exit(exit)
if (tracing == "no" and not dbg.active()) then
turn_on_stdio()
dbg.print{"stderr back on\n"}
end
local t = optionTbl.mpathMapT
if (next(t) ~= nil) then
for k,v in pairs(t) do
mpathMapT[k] = v
end
end
dbg.printT("mpathMapT",mpathMapT)
mcp = mcp_old
dbg.print{"Setting mcp to ", mcp:name(),"\n"}
dbg.fini("Spider:findAllModules")
end
function extend(a,b)
local tblInsert = table.insert
for i = 1,#b do
tblInsert(a, b[i])
end
return a
end
function reverse(a)
local n = #a
for i = 1, n/2 do
a[i], a[n] = a[n], a[i]
n = n - 1
end
return a
end
function copy(a)
local b = {}
for k,v in pairs(a) do
b[k] = v
end
return b
end
------------------------------------------------------------
-- Convert the mpathMapT to parentT.
-- So if the mpathMapT looks like
-- mpathMapT = {
-- ["%ProjDir%/Compiler/gcc/5.9"] = {
-- ["gcc/5.9.3"] = "%ProjDir%/Core",
-- ["gcc/5.9.2"] = "%ProjDir%/Core",
-- },
-- ["%ProjDir%/MPI/gcc/5.9/mpich/17.200"] = {
-- ["mpich/17.200.1"] = "%ProjDir%/Compiler/gcc/5.9",
-- ["mpich/17.200.2"] = "%ProjDir%/Compiler/gcc/5.9",
-- },
-- ["%ProjDir%/MPI/gcc/5.9/mpich/17.200/petsc/3.4"] = {
-- ["petsc/3.4-cxx"] = "%ProjDir%/MPI/gcc/5.9/mpich/17.200",
-- ["petsc/3.4-cmplx"] = "%ProjDir%/MPI/gcc/5.9/mpich/17.200",
-- },
-- }
-- Then parentT looks like:
-- parentT = {
-- ['%ProjDir%/Compiler/gcc/5.9'] =
-- {
-- {"gcc/5.9.3"},
-- {"gcc/5.9.2"},
-- },
--
-- ['%ProjDir%/Compiler/gcc/5.9/mpich/17.200'] =
-- {
-- {"gcc/5.9.3", "mpich/17.200.1"},
-- {"gcc/5.9.3", "mpich/17.200.2"},
-- {"gcc/5.9.2", "mpich/17.200.1"},
-- {"gcc/5.9.2", "mpich/17.200.2"},
-- },
-- ["%ProjDir%/MPI/gcc/5.9/mpich/17.200/petsc/3.4"] =
-- {
-- {"gcc/5.9.3", "mpich/17.200.1", "petsc/3.4-cxx" },
-- {"gcc/5.9.3", "mpich/17.200.2", "petsc/3.4-cxx" },
-- {"gcc/5.9.2", "mpich/17.200.1", "petsc/3.4-cxx" },
-- {"gcc/5.9.2", "mpich/17.200.2", "petsc/3.4-cxx" },
-- {"gcc/5.9.3", "mpich/17.200.1", "petsc/3.4-cmplx"},
-- {"gcc/5.9.3", "mpich/17.200.2", "petsc/3.4-cmplx"},
-- {"gcc/5.9.2", "mpich/17.200.1", "petsc/3.4-cmplx"},
-- {"gcc/5.9.2", "mpich/17.200.2", "petsc/3.4-cmplx"},
-- },
-- }
--
------------------------------------------------------------------------
-- The logic for this routine was originally written by Kenneth Hoste in
-- python after yours truly couldn't work it out.
local function l_build_parentT(keepT, mpathMapT)
dbg.start{"l_build_parentT(keepT, mpathMapT)"}
dbg.printT("keepT",keepT)
dbg.printT("mpathMapT",mpathMapT)
local function l_build_parentT_helper( mpath, fullNameA, fullNameT)
dbg.start{"l_build_parentT_helper(mpath, fullNameA, fullNameT)"}
dbg.print{"mpath: ",mpath,"\n"}
dbg.printT("fullNameA: ",fullNameA)
dbg.printT("fullNameT: ",fullNameT)
local resultA
if (not mpathMapT[mpath]) then
resultA = { fullNameA }
else
resultA = {}
for fullName, mpath2 in pairs(mpathMapT[mpath]) do
if (not fullNameT[fullName]) then
local tmpA = copy(fullNameA)
local tmpT = copy(fullNameT)
if (keepT[mpath2]) then
tmpA[#tmpA+1] = fullName
tmpT[fullName] = true
end
resultA = extend(resultA, l_build_parentT_helper(mpath2, tmpA, tmpT))
end
end
end
dbg.printT("resultA",resultA)
dbg.fini("l_build_parentT_helper")
return resultA
end
local parentT = {}
for mpath, v in pairs(mpathMapT) do
parentT[mpath] = {}
local A = parentT[mpath]
for fullName, mpath2 in pairs(v) do
A = extend(A, l_build_parentT_helper(mpath2, {fullName}, {[fullName] = true}))
end
end
for mpath, AA in pairs(parentT) do
for i = 1, #AA do
reverse(AA[i])
end
end
dbg.printT("parentT",parentT)
dbg.fini("l_build_parentT")
return parentT
end
local function l_build_mpathParentT(mpathMapT)
dbg.start{"l_build_mpathParentT(mpathMapT)"}
local mpathParentT = {}
dbg.printT("mpathMapT",mpathMapT)
for mpath, vv in pairs(mpathMapT) do
local a = mpathParentT[mpath] or {}
for k, v in pairs(vv) do
local found = false
for i = 1,#a do
if (a[i] == v) then
found = true
break
end
end
if (not found) then
a[#a+1] = v
end
end
mpathParentT[mpath] = a
end
dbg.fini("l_build_mpathParentT")
return mpathParentT
end
-- mpathParentT = {
-- ["%ProjDir%/Compiler/gcc/5.9"] = { "%ProjDir%/Core", "%ProjDir%/Core2"},
-- ["%ProjDir%/MPI/gcc/5.9/mpich/17.200"] = { "%ProjDir%/Compiler/gcc/5.9"},
-- }
local function l_search_mpathParentT(mpath, keepT, mpathParentT)
local a = mpathParentT[mpath]
if (not a) then
return false
end
local found = false
for i = 1,#a do
mpath = a[i]
if (keepT[mpath] or l_search_mpathParentT(mpath, keepT, mpathParentT)) then
return true
end
end
return false
end
local function l_build_keepT(mpathA, mpathParentT, spiderT)
local keepT = {}
for i = 1,#mpathA do
keepT[mpathA[i]] = true
end
for mpath, vv in pairs(spiderT) do
if (mpath ~= 'version') then
if (not keepT[mpath] and l_search_mpathParentT(mpath, keepT, mpathParentT)) then
keepT[mpath] = true
end
end
end
return keepT
end
local dbT_keyA = { 'Description', 'Category', 'URL', 'Version', 'whatis', 'dirA',
'family','pathA', 'lpathA', 'propT','help','pV','wV','provides'}
function M.buildDbT(self, mpathMapT, spiderT, dbT)
dbg.start{"Spider:buildDbT(mpathMapT,spiderT, dbT)"}
dbg.printT("mpathMapT",mpathMapT)
local mpathParentT = l_build_mpathParentT(mpathMapT)
dbg.printT("spiderT",spiderT)
dbg.printT("mpathParentT",mpathParentT)
local mpathA = {}
for k, v in pairs(spiderT) do
if (k ~= "version") then
mpathA[#mpathA + 1] = k
end
end
dbg.printT("mpathA",mpathA)
local keepT = l_build_keepT(mpathA, mpathParentT, spiderT)
local parentT = l_build_parentT(keepT, mpathMapT)
local mrc = MRC:singleton()
local function l_cmp(a,b)
return a[1] > b[1]
end
local function l_buildDbT_helper(mpath, sn, v, T)
local kind = false
if (next(v.fileT) ~= nil) then
for fullName, vv in pairs(v.fileT) do
local t = {}
for i = 1,#dbT_keyA do
local key = dbT_keyA[i]
t[key] = vv[key]
end
if (parentT[mpath] and next(parentT[mpath]) ~= nil) then
dbg.printT("parentAA",parentT[mpath])
sort(parentT[mpath], l_cmp)
end
t.parentAA = parentT[mpath]
t.mpath = vv.mpath
t.fullName = fullName
local resultT = mrc:isVisible{fullName=fullName, sn=sn, fn=vv.fn, mpathA=mpathA, mpath = vv.mpath}
t.hidden = not resultT.isVisible
kind = resultT.moduleKindT.kind
if (not vv.dot_version and (kind ~= "hard")) then
T[vv.fn] = t
end
end
end
if (next(v.dirT) ~= nil) then
for name, vv in pairs(v.dirT) do
l_buildDbT_helper(mpath, sn, vv, T)
end
end
end
dbg.printT("mpathA", mpathA)
dbg.printT("mpathMapT", mpathMapT)
dbg.printT("mpathParentT",mpathParentT)
dbg.printT("keepT", keepT)
dbg.printT("parentT", parentT)
if (next(spiderT) == nil) then
dbg.print{"empty spiderT\n"}
dbg.fini("Spider:buildDbT")
return
end
for mpath, vv in pairs(spiderT) do
dbg.print{"mpath: ",mpath, ", keepT[mpathT]: ",tostring(keepT[mpath]),"\n"}
if (mpath ~= 'version' and keepT[mpath]) then
for sn, v in pairs(vv) do
local T = dbT[sn] or {}
l_buildDbT_helper(mpath, sn, v, T)
dbT[sn] = T
end
end
end
dbg.printT("dbT", dbT)
dbg.fini("Spider:buildDbT")
end
function M.buildProvideByT(self, dbT, providedByT)
dbg.start{"Spider:buildProvideByT(dbT, providedByT)"}
local mrc = MRC:singleton()
for sn, vv in pairs(dbT) do
for fullPath, v in pairs(vv) do
local resultT = mrc:isVisible{fullName=v.fullName, sn=sn, fn=fullPath, mpath=v.mpath}
local hidden = not resultT.isVisible
if (v.provides ~= nil) then
local providesA = v.provides
for i = 1, #providesA do
local fullName = providesA[i]
local _,_, sn = fullName:find("^([^/]*)")
local T = providedByT[sn] or {}
local A = T[fullName] or {}
local parentAA = v.parentAA
if (parentAA == nil) then
A[#A+1] = {fullName = v.fullName, pV = v.pV, hidden = hidden,
my_name = fullName, mpath = v.mpath}
else
for j = 1,#parentAA do
local hierStr = concatTbl(parentAA[j]," ")
A[#A+1] = {fullName = v.fullName .. " (" .. hierStr .. ")", pV = v.pV,
hidden = hidden, my_name = fullName, mpath = v.mpath}
end
end
T[fullName] = A
providedByT[sn] = T
end
end
end
end
local function l_cmp(a, b)
if (a.pV > b.pV) then
return true
elseif (a.pV == b.pV) then
return a.fullName > b.fullName
else
return false
end
end
for sn, vv in pairs(providedByT) do
for fullName, v in pairs(vv) do
sort(v, l_cmp)
end
end
dbg.printT("providedByT",providedByT)
dbg.fini("Spider:buildProvideByT")
end
function M.Level0_terse(self,dbT, providedByT)
dbg.start{"Spider:Level0_terse()"}
local mrc = MRC:singleton()
local t = {}
local a = {}
mrc:set_display_mode("spider")
for sn, vv in pairs(dbT) do
for fn, v in pairs(vv) do
local resultT = mrc:isVisible{fullName=v.fullName,sn=sn,fn=fn, mpath = v.mpath}
if (resultT.isVisible) then
local forbiddenT = mrc:isForbidden{fullName=v.fullName, sn=sn, fn=fn, mpath = v.mpath}
if (sn == v.fullName) then
t[sn] = decorateModule(sn, resultT, forbiddenT)
else
-- print out directory name (e.g. gcc) for tab completion.
t[sn] = sn .. "/"
local key = sn .. "/" .. v.pV
t[key] = decorateModule(v.fullName, resultT, forbiddenT)
end
end
end
end
for sn, vv in pairs(providedByT) do
t[sn] = t[sn] or sn .. "/"
for fullName, A in pairs(vv) do
t[fullName] = t[fullName] or fullName
end
end
for k,v in pairsByKeys(t) do
a[#a+1] = v
end
a[#a+1] = ""
dbg.fini("Spider:Level0_terse")
return concatTbl(a,"\n")
end
function M.Level0(self, dbT, providedByT)
local a = {}
if (optionTbl().terse) then
return self:Level0_terse(dbT, providedByT)
end
local ia = 0
local banner = Banner:singleton()
local border = banner:border(0)
ia = ia+1; a[ia] = "\n"
ia = ia+1; a[ia] = border
ia = ia+1; a[ia] = i18n("m_Spider_Title", {})
ia = ia+1; a[ia] = border
self:Level0Helper(dbT, providedByT, a)
return concatTbl(a,"")
end
--------------------------------------------------------------------------
-- Convert both argument to lower case and compare.
-- @param a input string
-- @param b input string
local function l_case_independent_cmp_by_name(a,b)
local a_lower = a:lower()
local b_lower = b:lower()
if (a_lower == b_lower ) then
return a < b
else
return a_lower < b_lower
end
end
local function l_computeColor(resultT, forbiddenT)
local fT = forbiddenT
if (not fT or next(fT) == nil) then
fT = {forbiddenState = "normal"}
end
if (fT.forbiddenState ~= "normal") then
return fT.forbiddenState
end
if (resultT.moduleKindT.kind ~= "normal") then
return "hidden"
end
return false
end
function M.Level0Helper(self, dbT, providedByT, a)
local t = {}
local optionTbl = optionTbl()
local mrc = MRC:singleton()
local show_hidden = mrc:show_hidden()
local term_width = TermWidth() - 4
local banner = Banner:singleton()
for sn, vv in pairs(dbT) do
for fn,v in pairsByKeys(vv) do
local resultT = mrc:isVisible{fullName=v.fullName,sn=sn,fn=fn, mpath = v.mpath}
if (resultT.isVisible) then
local forbiddenT = mrc:isForbidden{fullName=v.fullName,sn=sn,fn=fn, mpath = v.mpath}
if (t[sn] == nil) then
t[sn] = { Description = v.Description, versionA = { }, name = sn}
end
local color = l_computeColor(resultT, forbiddenT)
dbg.print{"fullName: ",v.fullName,", color: ",color,"\n"}
dbg.printT("resultT",resultT)
dbg.printT("forbiddenT",forbiddenT or {})
t[sn].versionA[v.pV] = colorize(color, v.fullName)
end
end
end
local have_providedBy = false
if (next(providedByT) ~= nil) then
for sn, vv in pairs(providedByT) do
for fullName, A in pairs(vv) do
local isVisible = show_hidden
if (not show_hidden) then
for i = 1, #A do
if (not A[i].hidden) then
isVisible = true
break
end
end
end
if (isVisible) then
have_providedBy = true
local version = extractVersion(fullName,sn)
local pV = parseVersion(version)
if ( t[sn] == nil) then
t[sn] = {versionA = { }, name = sn}
end
t[sn].versionA[pV] = colorize("blue",fullName) .. " (E)"
end
end
end
end
local ia = #a
local cmp = (cosmic:value("LMOD_CASE_INDEPENDENT_SORTING") == "yes") and
l_case_independent_cmp_by_name or nil
for k,v in pairsByKeys(t,cmp) do
local len = 0
ia = ia + 1; a[ia] = " " .. v.name .. ":"
len = len + a[ia]:len()
for kk,full in pairsByKeys(v.versionA) do
ia = ia + 1; a[ia] = " " .. full; len = len + a[ia]:len() + 1
if (len > term_width) then
a[ia] = " ..."
ia = ia + 1; a[ia] = ","
break;
end
ia = ia + 1; a[ia] = ","
end
a[ia] = "\n" -- overwrite the last comma
if (v.Description) then
ia = ia + 1; a[ia] = v.Description:fillWords(" ", term_width)
ia = ia + 1; a[ia] = "\n"
end
ia = ia + 1; a[ia] = "\n"
end
if (have_providedBy) then
ia = ia + 1
a[ia] = i18n("m_ProvidedBy")
end
local border = banner:border(0)
ia = ia+1; a[ia] = i18n("m_Spider_Tail", {border=border})
end
function M.setExactMatch(self, name)
self.__name = name
end
function M.getExactMatch(self)
return self.__name
end
function M.spiderSearch(self, dbT, providedByT, userSearchPat, helpFlg)
dbg.start{"Spider:spiderSearch(dbT,providedByT,\"",userSearchPat,"\",",helpFlg,")"}
local mrc = MRC:singleton()
local optionTbl = optionTbl()
mrc:set_display_mode("spider")
local show_hidden = mrc:show_hidden()
dbg.print{"show_hidden: ",show_hidden,"\n"}
--dbg.printT("dbT",dbT)
local origUserSearchPat = userSearchPat
if (not optionTbl.regexp) then
userSearchPat = userSearchPat:caseIndependent()
end
------------------------------------------------------------
-- Match rules in dbT
--
-- 1) Matching original user search pattern in dbT
-- Check for possibles. Count
-- 2) Matching one Full only -> Level 2
--
-- 3) Matching sn but there is only 1 fullName -> Level 2
--
-- 4) Matching sn or full with multiple versions -> Level1
local a = {}
local matchT = {}
local T = dbT[origUserSearchPat]
local TT = providedByT[origUserSearchPat]
if (T and next(T) ~= nil) then dbg.printT("dbT->T",T) else dbg.print{"no T\n"} end
if (TT and next(TT) ~= nil) then dbg.printT("providedBy->TT",TT) else dbg.print{"no TT\n"} end
local look4poss = false
if (T or TT) then
-- Must check for any valid modulefiles or providesBy
dbg.print{"Have T or TT\n"}
local found = true
if (not show_hidden) then
found = false
if (T) then
dbg.print{"Have T\n"}
for fn, v in pairs(T) do
local resultT = mrc:isVisible{fullName=v.fullName,fn=fn,sn=origUserSearchPat, mpath=v.mpath}
if (resultT.isVisible) then
found = true
break
end
end
end
if (TT and not found) then
dbg.print{"Have TT\n"}
for fullName, A in pairs(TT) do
for i = 1,#A do
if (not A[i].hidden) then
found = true
break
end
end
end
end
end
if (found) then
matchT[origUserSearchPat] = origUserSearchPat
look4poss = true
end
else
dbg.print{"Do not have T or TT\n"}
-- If here then no exact match has been found in either dbT or providedByT, so
-- Step 1 copy all sn and fullNames to fullA
local aT = {}
local bT = {}
dbg.print{"userSearchPat: ",userSearchPat,"\n"}
local fullA = {}
for sn, vv in pairs(dbT) do
for fn, v in pairs(vv) do
local resultT = mrc:isVisible{fullName=v.fullName,sn=sn,fn=fn, mpath = v.mpath}