-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathMT.lua
1627 lines (1432 loc) · 47.6 KB
/
MT.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
--------------------------------------------------------------------------
-- This class controls the ModuleTable. The ModuleTable is how Lmod
-- communicates what modules are loaded or inactive and so on between
-- module commands.
--
-- @classmod MT
_G._DEBUG = false
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("deepcopy")
require("declare")
require("utils")
require("serializeTbl")
local ColumnTable = require("ColumnTable")
local M = {}
local MRC = require("MRC")
local ReadLmodRC = require("ReadLmodRC")
local base64 = require("base64")
local concatTbl = table.concat
local cosmic = require("Cosmic"):singleton()
local decode64 = base64.decode64
local encode64 = base64.encode64
local dbg = require("Dbg"):dbg()
local floor = math.floor
local getenv = os.getenv
local hook = require("Hook")
local i18n = require("i18n")
local load = (_VERSION == "Lua 5.1") and loadstring or load
local min = math.min
local posix_setenv = posix.setenv
local s_loadOrder = 0
local s_mt = false
local s_name = "_ModuleTable_"
local s_familyA = false
local sort = table.sort
local strfmt = string.format
local abs = math.abs
function M.name(self)
return s_name
end
local function l_mt_version()
return 3
end
local function l_new(self, s, restoreFn)
dbg.start{"MT l_new(s,restoreFn:",restoreFn,")"}
local o = {}
o.c_rebuildTime = false
o.c_shortTime = false
o.mT = {}
o.MTversion = l_mt_version()
o.family = {}
o.mpathA = {}
o.depthT = {}
o.__conflictT = {}
o.__stickyA = {}
o.__loadT = {}
o.__changeMPATH = false
setmetatable(o, self)
self.__index = self
local currentMPATH = getenv("MODULEPATH")
dbg.print{"currentMPATH: ",currentMPATH,"\n"}
if (not s) then
if (currentMPATH) then
local clearDblSlash = true
o.mpathA = path2pathA(currentMPATH,':',clearDblSlash)
o.systemBaseMPATH = concatTbl(o.mpathA,":")
end
local maxdepth = cosmic:value("LMOD_MAXDEPTH")
o.depthT = paired2pathT(maxdepth)
dbg.print{"LMOD_MAXDEPTH: ",maxdepth,"\n"}
dbg.print{"s is nil\n"}
dbg.fini("MT l_new")
return o
end
declare(s_name)
local func, msg = load(s)
local ok
if (func) then
ok, msg = pcall(func)
else
ok = false
end
local _ModuleTable_ = _G[s_name] or _G._ModuleTable_
------------------------------------------------------------------------
-- Do not call LmodError or LmodSystemError here. It leads to an
-- endless loop !!
if (not ok or type(_ModuleTable_) ~= "table" ) then
if (restoreFn) then
io.stderr:write(i18n("e_coll_corrupt",{fn=restoreFn}))
LmodErrorExit()
else
io.stderr:write(i18n("e_MT_corrupt",{}))
LmodErrorExit()
end
end
if (_ModuleTable_.version == 2) then
o:__convertMT(_ModuleTable_)
else
for k,v in pairs(_ModuleTable_) do
o[k] = v
end
------------------------------------------------------------------
-- Convert the list of MName constructors in to an array of mnames
-- to hold the conflicts.
if (o.conflictT and next(o.conflictT) ~= nil) then
local MName = require("MName")
local cT = {}
local tt = o.conflictT
for sn, vv in pairs(tt) do
local a = {}
for i = 1,#vv do
local t = tt[sn][i]
a[i] = MName:new(t.sType, t.userName, t.action, t.is, t.ie)
end
cT[sn] = a
end
o.__conflictT = cT
end
o.conflictT = nil
end
-- remove any mcmdT connected to a mT entry
local mT = o.mT
for sn, entry in pairs(mT) do
entry.mcmdT = nil
end
local icount = 0
for k in pairs(o.mT) do
icount = icount + 1
end
s_loadOrder = icount
------------------------------------------------------------
-- No matter what the MODULEPATH was before use the current
-- environment value. Unless it is a module restore
if (not restoreFn) then
local clearDblSlash = true
o.mpathA = path2pathA(currentMPATH,':',clearDblSlash)
end
dbg.fini("MT l_new")
return o
end
function M.singleton(self, t)
t = t or {}
if (t.testing) then
dbg.print{"Clearing s_mt\n"}
s_mt = false
__removeEnvMT()
end
if (not s_mt) then
dbg.start{"MT:singleton()"}
s_mt = l_new(self, getMT())
if (dbg.active()) then
dbg.print{"s_mt: ",s_mt:serializeTbl("pretty") }
end
dbg.fini("MT:singleton")
end
return s_mt
end
function M.__clearMT(self, t)
if (t.testing == true) then
dbg.print{"Clearing s_mt\n"}
s_mt = false
end
end
function M.__convertMT(self, v2)
self.MTversion = 3;
self.c_rebuildTime = v2.c_rebuildTime
self.c_shortTime = v2.c_shortTime
self.depthT = {}
self.family = v2.family
self.mpathA = v2.mpathA
self.systemBaseMPATH = v2.systemBaseMPATH
local mT = {}
for sn, vv in pairs(v2.mT) do
local v = {}
v.fn = vv.FN
v.fullName = vv.fullName
v.hash = vv.hash
v.loadOrder = vv.loadOrder
v.propT = vv.propT
v.status = vv.status
v.wV = vv.wV
v.userName = (vv.default == 1) and sn or v.fullName
mT[sn] = v
end
self.mT = mT
end
function __removeEnvMT()
local SzStr = "_ModuleTable_Sz_"
local piece = "_ModuleTable%03d_"
local nblks = tonumber(getenv(SzStr) or "") or 0
posix_setenv(SzStr,nil, true)
for i = 1, nblks do
local envNm = strfmt(piece,i)
posix_setenv(envNm, nil, true)
end
end
--------------------------------------------------------------------------
-- Return the original MT from bottom of stack.
function M.add(self, mname, status, loadOrder)
local mT = self.mT
local sn = mname:sn()
assert(sn)
local entry = mT[sn] or {}
local old_status = entry.status
loadOrder = loadOrder == nil and -1 or loadOrder
-- Issue #604: If old_status is "inactive" then ref_count must be nil.
-- The ref_count will be bumped back up by the loading of
-- modules that depend on the dependent modules.
local ref_count = mname:ref_count()
if (old_status == "inactive" and status ~= "inactive" and mname:get_depends_on_flag()) then
ref_count = 0
end
mT[sn] = {
fullName = mname:fullName(),
fn = mname:fn(),
userName = mname:userName(),
stackDepth = mname:stackDepth(),
origUserName = mname:origUserName(),
moduleKindT = mname:moduleKindT{},
forbiddenT = mname:forbiddenT{},
ref_count = ref_count,
depends_on_anyA = mname:get_depends_on_anyA(),
status = status,
loadOrder = loadOrder,
propT = {},
wV = mname:wV() or false,
}
if (status ~= "inactive" and old_status ~= "inactive") then
self:safely_incr_ref_count(mname)
end
--dbg.print{"MT:add: sn: ", sn, ", status: ",status,", old_status: ",old_status,", ref_count: ",mT[sn].ref_count,"\n"}
end
--------------------------------------------------------------------------
-- Report the contents of the collection. Return an empty array if the
-- collection is not found.
function M.reportContents(self, t)
dbg.start{"mt:reportContents(",t.fn,")"}
local a = {}
if (not t.fn) then
dbg.fini("mt:reportContents")
return a
end
local f = io.open(t.fn,"r")
if (not f) then
dbg.fini("mt:reportContents")
return a
end
local s = f:read("*all")
local l_mt = l_new(self, s, t.fn)
local pin_versions = cosmic:value("LMOD_PIN_VERSIONS")
local kind = (pin_versions == "no") and "userName" or "fullName"
local activeA = l_mt:list(kind, "active")
for i = 1, #activeA do
a[#a+1] = activeA[i].name
end
f:close()
dbg.fini("mt:reportContents")
return a
end
------------------------------------------------------------------------
-- Mark Changing MODULEPATH
function M.changeMPATH(self)
return self.__changeMPATH
end
function M.set_MPATH_change_flag(self)
dbg.print{"MT:set_MPATH_change_flag(self)\n"}
self.__changeMPATH = true
end
function M.reset_MPATH_change_flag(self)
self.__changeMPATH = false
end
function M.add_actionA(self, sn, action, value)
local entry = self.mT[sn]
if (entry ~= nil) then
local a = entry.actionA or {}
a[#a+1] = action .. "(\"MODULEPATH\",\"" .. value .."\")"
entry.actionA = a
end
end
function M.get_actionA(self, sn)
local entry = self.mT[sn]
local actionA = {}
if (entry ~= nil) then
actionA = entry.actionA or {}
end
return actionA
end
function M.add_sh2mf_cmds(self, sn, script, mcmdA)
local entry = self.mT[sn]
if (entry ~= nil) then
local a64 = {}
for i =1, #mcmdA do
a64[i] = encode64(mcmdA[i])
end
if (entry.mcmdT_64 == nil) then
entry.mcmdT_64 = {}
end
local script64 = encode64(script)
entry.mcmdT_64[script64] = a64
end
end
function M.get_sh2mf_cmds(self, sn, script)
local entry = self.mT[sn]
if (entry ~= nil and entry.mcmdT_64 ~= nil
and next(entry.mcmdT_64) ~= nil) then
local script64 = encode64(script)
local a64 = entry.mcmdT_64[script64]
if ( a64 == nil ) then return nil end
local a = {}
for i = 1,#a64 do
a[i] = decode64(a64[i])
end
return a
end
return nil
end
function M.setStatus(self, sn, status)
local entry = self.mT[sn]
if (entry ~= nil) then
entry.status = status
if (status == "active") then
s_loadOrder = s_loadOrder + 1
entry.loadOrder = s_loadOrder
end
end
end
function M.status(self, sn)
local entry = self.mT[sn]
if (entry ~= nil) then
return entry.status
end
return nil
end
function M.exists(self, sn, fullName)
local entry = self.mT[sn]
if (entry == nil) then
return false
end
return (fullName == nil) or (entry.status ~= "inactive" and entry.fullName == fullName)
end
function M.moduleKindT(self, sn)
local entry = self.mT[sn]
if (entry ~= nil) then
return entry.moduleKindT
end
return nil
end
--------------------------------------------------------------------------
-- Set the rebuild and short time for the cache
-- @param long The long time before rebuilding user cache.
-- @param short The short time before rebuilding user cache.
function M.setRebuildTime(self, long, short)
dbg.start{"MT:setRebuildTime(long: ",long,", short: ",short,")",level=2}
self.c_rebuildTime = long
self.c_shortTime = short
dbg.fini("MT:setRebuildTime")
end
--------------------------------------------------------------------------
-- Set the load order by using MT:list()
-- @param self An MT object.
local function l_setLoadOrder(self)
local a = self:list("short","active")
local mT = self.mT
local sz = #a
for i = 1,sz do
local sn = a[i]
mT[sn].loadOrder = i
end
end
function M.serializeTbl(self, state)
local make_pretty = (state == "pretty")
local mt = deepcopy(self)
local rTest = optionTbl().rt
if (rTest) then
mt.c_rebuildTime = false
mt.c_shortTime = false
end
l_setLoadOrder(mt)
if (next(self.__conflictT) ~= nil) then
local cT = mt.__conflictT
local tt = {}
for sn,vv in pairs(cT) do
local a = {}
for i=1,#vv do
local mname = vv[i]
a[i] = mname:print()
end
tt[sn] = a
end
mt.conflictT = tt
end
if (make_pretty) then
local mT = mt.mT
for sn, v in pairs(mT) do
local mcmdT_64 = mT[sn].mcmdT_64
if (mcmdT_64 and next(mcmdT_64) ~= nil) then
local t = {}
for script64, mcmdA_64 in pairsByKeys(mcmdT_64) do
local a = {}
for i = 1,#mcmdA_64 do
a[i] = decode64(mcmdA_64[i])
end
local script = decode64(script64)
t[script] = a
end
mT[sn].mcmdT = t
if (rTest) then
mT[sn].mcmdT_64 = nil
end
end
end
end
local s = serializeTbl{indent = make_pretty, name = self.name(), value = mt}
return s
end
function M.encodeMT(self)
local s = self:serializeTbl()
return build_MT_envT(s)
end
--------------------------------------------------------------------------
-- Clear the entry for *sn* from the module table.
-- @param self An MT object.
-- @param sn The short name.
function M.remove(self, sn)
local mT = self.mT
mT[sn] = nil
end
local function l_build_AB(a,b, loadOrder, name, value)
if (loadOrder > 0) then
a[#a+1] = { loadOrder, name, value }
else
b[#b+1] = { abs(loadOrder), name, value }
end
return a, b
end
--------------------------------------------------------------------------
-- Return a array of modules currently in MT. The list is
-- always sorted in loadOrder.
--
-- There are three kinds of returns for this member function.
-- mt:list("userName",...) returns an object containing an table
-- which has the short, full, etc.
-- mt:list("fullName",...) returns the list modules with their
-- fullNames.
-- mt:list("both",...) returns the short and full name of
-- mt:list(... , ...) returns a simply array of names.
-- @param self An MT object
-- @param kind
-- @param status
-- @return An array of modules matching the kind and status
function M.list(self, kind, status)
local mT = self.mT
local a = {}
local b = {}
dbg.print{"MT:list(kind: ",kind,", status:",status,")\n"}
if (kind == "short" or kind == "sn") then
for k, v in pairs(mT) do
if ((status == "any" or status == v.status) and
(v.status ~= "pending")) then
a, b = l_build_AB(a, b, v.loadOrder , k, k)
end
end
elseif (kind == "userName" or kind == "fullName") then
for k, v in pairs(mT) do
if ((status == "any" or status == v.status) and
(v.status ~= "pending")) then
local obj = { sn = k, fullName = v.fullName, userName = v.userName,
name = v[kind], fn = v.fn, loadOrder = v.loadOrder,
stackDepth = v.stackDepth, ref_count = v.ref_count,
depends_on_anyA = v.depends_on_anyA, displayName = v.fullName,
origUserName = v.origUserName or false,
moduleKindT = v.moduleKindT or {},
forbiddenT = v.forbiddenT or {},
}
a, b = l_build_AB(a, b, v.loadOrder, v[kind], obj )
end
end
elseif (kind == "fullName_Meta") then
for k, v in pairs(mT) do
if ((status == "any" or status == v.status) and
(v.status ~= "pending") and
(v.stackDepth == 0)) then
local obj = { sn = k, fullName = v.fullName, userName = v.userName,
name = v.fullName, fn = v.fn, loadOrder = v.loadOrder,
stackDepth = v.stackDepth, ref_count = v.ref_count,
depends_on_anyA = v.depends_on_anyA, displayName = v.fullName,
origUserName = v.origUserName or false,
moduleKindT = v.moduleKindT or {},
forbiddenT = v.forbiddenT or {},
}
a, b = l_build_AB(a, b, v.loadOrder, v.fullName, obj )
end
end
elseif (kind == "both") then
for k, v in pairs(mT) do
if ((status == "any" or status == v.status) and
(v.status ~= "pending")) then
a, b = l_build_AB(a, b, v.loadOrder, v.userName, v.userName )
if (v.userName ~= k) then
a, b = l_build_AB(a, b, v.loadOrder, k, k)
end
if (v.userName ~= v.fullName) then
a, b = l_build_AB(a, b, v.loadOrder, v.fullName, v.fullName )
end
end
end
end
local function l_loadOrder_cmp(x,y)
if (x[1] == y[1]) then
return x[2] < y[2]
else
return x[1] < y[1]
end
end
sort (a, l_loadOrder_cmp)
sort (b, l_loadOrder_cmp)
local B = {}
for i = 1, #a do
B[i] = a[i][3]
end
for i = 1, #b do
B[#B+1] = b[i][3]
end
a = nil -- finished w/ a.
b = nil -- finished w/ b.
return B
end
function M.empty(self)
local mT = self.mT
return next(mT) == nil
end
--------------------------------------------------------------------------
-- add a property to an active module.
-- @param self An MT object.
-- @param sn The short name
-- @param name the property name
-- @param value the value for the property name.
function M.add_property(self, sn, name, value)
dbg.start{"MT:add_property(\"",sn,"\", \"",name,"\", \"",value,"\")"}
local mT = self.mT
local entry = mT[sn]
if (entry == nil) then
LmodError{msg="e_No_Mod_Entry", routine = "MT:add_property()",name = sn}
end
local readLmodRC = ReadLmodRC:singleton()
local propT = entry.propT
propT[name] = propT[name] or {}
local t = propT[name]
readLmodRC:validPropValue(name, value, t)
entry.propT[name] = t
dbg.fini("MT:add_property")
end
--------------------------------------------------------------------------
-- Remove a property to an active module.
-- @param self An MT object.
-- @param sn The short name
-- @param name the property name
-- @param value the value for the property name.
function M.remove_property(self, sn, name, value)
dbg.start{"MT:remove_property(\"",sn,"\", \"",name,"\", \"",value,"\")"}
local mT = self.mT
local entry = mT[sn]
if (entry == nil) then
LmodError{msg="e_No_Mod_Entry", routine = "MT:remove_property()",name = sn}
end
local readLmodRC = ReadLmodRC:singleton()
local propDisplayT = readLmodRC:propT()
local propKindT = propDisplayT[name]
if (propKindT == nil) then
LmodError{msg="e_No_PropT_Entry", routine = "MT:remove_property()", location = "entry", name = name}
end
local validT = propKindT.validT
if (validT == nil) then
LmodError{msg="e_No_PropT_Entry", routine = "MT:remove_property()", location = "validT table", name = name}
end
local propT = entry.propT or {}
local t = propT[name] or {}
for v in value:split(":") do
if (validT[v] == nil) then
LmodError{msg="e_No_ValidT_Entry", routine = "MT:remove_property()", name = name, value = value}
end
t[v] = nil
end
entry.propT = propT
entry.propT[name] = t
dbg.fini("MT:remove_property")
end
--------------------------------------------------------------------------
-- List the fullname with possible property
-- @param self An MT object.
-- @param idx The index in the list.
-- @param sn The short name
-- @param style How to colorize.
-- @param legendT The legend table.
function M.list_w_property(self, idx, sn, style, legendT)
dbg.start{"MT:list_w_property(\"",sn,"\", \"",style,"\")"}
local mT = self.mT
local entry = mT[sn]
local mrc = MRC:singleton()
if (entry == nil) then
LmodError{msg="e_No_Mod_Entry", routine = "MT:list_w_property()", name = sn}
end
local resultA = colorizePropA(style, self, {fullName=entry.fullName, origUserName=entry.origUserName, sn=sn, fn=entry.fn},
mrc, entry.propT, legendT, entry.forbiddenT)
dbg.print{"resultA: ",resultA[1]," ",resultA[2],"\n"}
if (resultA[2]) then
resultA[2] = "(" .. resultA[2] .. ")"
end
local cstr = string.format("%3d)",idx)
table.insert(resultA, 1, cstr)
dbg.fini("MT:list_w_property")
return resultA
end
--------------------------------------------------------------------------
-- Return the value of this property or nil.
-- @param self An MT object.
-- @param sn The short name.
-- @param propName The property name.
-- @param propValue The property value.
function M.haveProperty(self, sn, propName, propValue)
local entry = self.mT[sn]
if (entry == nil or entry.propT == nil or entry.propT[propName] == nil ) then
return nil
end
return entry.propT[propName][propValue]
end
--------------------------------------------------------------------------
-- Does the *sn* exist with a particular *status*.
-- @param self An MT object.
-- @param sn the short module name.
-- @param status The status.
-- @return existence.
function M.have(self, sn, status)
local entry = self.mT[sn]
if (entry == nil) then
return false
end
return ((status == "any") or (status == entry.status))
end
function M.find_possible_sn(self, userName)
local sn_match = false
local sn = userName
while true do
if (self:exists(sn)) then
sn_match = true
break
end
local idx = sn:match("^.*()/")
if (idx == nil) then break end
sn = sn:sub(1,idx-1)
end
if (not sn_match) then
sn = userName
end
return sn_match, sn
end
function M.lookup_w_userName(self,userName)
-- Check if userName is a sn
local sn_match, sn = self:find_possible_sn(userName)
if (not sn_match) then return false end
-- Case 1: userName -> sn ?
if (userName == sn) then
return sn
end
-- Case 2: userName -> fullName ?
local fullName = self:fullName(sn)
if (userName == fullName) then
return sn
end
-- Case 3: Partial match?
local partial_match = ("^"..userName:escape().."/"):gsub('//+','/')
if (fullName:find(partial_match)) then
return sn
end
return false
end
function M.userName(self, sn)
local entry = self.mT[sn]
if (entry == nil) then
return nil
end
return entry.userName
end
function M.fullName(self, sn)
local entry = self.mT[sn]
if (entry == nil) then
return nil
end
return entry.fullName
end
function M.wV(self, sn)
local entry = self.mT[sn]
if (entry == nil) then
return nil
end
return entry.wV
end
function M.fn(self, sn)
local entry = self.mT[sn]
if (entry == nil) then
return nil
end
return entry.fn
end
function M.version(self,sn)
local entry = self.mT[sn]
if (entry == nil) then
return nil
end
return extractVersion(entry.fullName, sn)
end
function M.stackDepth(self,sn)
local entry = self.mT[sn]
if (entry == nil) then
return nil
end
return entry.stackDepth or 0
end
function M.safely_incr_ref_count(self,mname)
local sn = mname:sn()
assert(sn)
local entry = self.mT[sn]
if (entry == nil) then
dbg.print{"MT:safely_incr_ref_count(): Did not find: ",sn,"\n"}
return
end
local depends_on_flag = mname:get_depends_on_flag()
if (not depends_on_flag and not entry.ref_count) then
dbg.print{"MT:safely_incr_ref_count(): depends_on_flag not set ",sn,"\n"}
return
end
entry.ref_count = (entry.ref_count or 0) + 1
dbg.print{"MT:safely_incr_ref_count(): stackDepth > 0, sn: ",sn,", new ref_count: ",entry.ref_count,"\n"}
return
end
function M.decr_ref_count(self,sn)
local entry = self.mT[sn]
if (entry == nil or not entry.ref_count) then
dbg.print{"MT:decr_ref_count(): sn: ",sn, ", ref_count: nil\n"}
return nil
end
local ref_count = entry.ref_count - 1
entry.ref_count = ref_count
dbg.print{"MT:decr_ref_count(): sn: ",sn, ", ref_count: ",ref_count,"\n"}
return ref_count
end
function M.get_ref_count(self,sn)
local entry = self.mT[sn]
if (entry == nil or not entry.ref_count) then
return nil
end
dbg.print{"MT:get_ref_count(): sn: ",sn, ", ref_count: ",entry.ref_count,"\n"}
return entry.ref_count
end
function M.get_depends_on_anyA(self,sn)
local entry = self.mT[sn]
if (entry == nil or not entry.ref_count) then
return nil
end
return entry.depends_on_anyA
end
function M.save_depends_on_any(self, sn, child_sn)
local entry = self.mT[sn]
assert(entry)
local anyA = entry.depends_on_anyA or {}
anyA[#anyA + 1] = child_sn
entry.depends_on_anyA = anyA
end
function M.pop_depends_on_any(self, sn)
local entry = self.mT[sn]
assert(entry)
if (not (entry.depends_on_anyA and next(entry.depends_on_anyA) ~= nil)) then
return nil
end
local child_sn = table.remove(entry.depends_on_anyA,1)
return child_sn
end
function M.pop_depends_on_any_ck(self,sn)
dbg.start{"MT:pop_depends_on_any_ck(sn: \"",sn,"\")"}
local entry = self.mT[sn]
assert(entry)
if (not (entry.depends_on_anyA and next(entry.depends_on_anyA) ~= nil)) then
dbg.fini("MT:pop_depends_on_any_ck with no doA")
return nil
end
if (not entry.__depends_on_any_ckA) then
entry.__depends_on_any_ckA = deepcopy(entry.depends_on_anyA)
end
local child_sn = table.remove(entry.__depends_on_any_ckA,1)
dbg.print{"child_sn: ",child_sn,"\n"}
dbg.fini("MT:pop_depends_on_any_ck")
return child_sn
end
function M.updateMPathA(self, value)
if (type(value) == "string") then
local clearDblSlash = true
self.mpathA = path2pathA(value,':',clearDblSlash)
elseif (type(value) == "table") then
self.mpathA = value
elseif (type(value) == "nil") then
self.mpathA = {} -- path2pathA("")
end
end
function M.modulePathA(self)
return self.mpathA
end
function M.maxDepthT(self)
return self.depthT
end
--------------------------------------------------------------------------
-- Return the shortTime time.
-- @param self An MT object.
function M.getShortTime(self)
return self.c_shortTime
end
--------------------------------------------------------------------------
-- Return the rebuild time.
-- @param self An MT object.
function M.getRebuildTime(self)
return self.c_rebuildTime
end
--------------------------------------------------------------------------
-- Record the long and short time.
-- @param self An MT object.
-- @param long The long time before rebuilting user cache.
-- @param short The short time before rebuilting user cache.
function M.setRebuildTime(self, long, short)
dbg.start{"MT:setRebuildTime(long: ",long,", short: ",short,")",level=2}
self.c_rebuildTime = long
self.c_shortTime = short
dbg.fini("MT:setRebuildTime")
end
--------------------------------------------------------------------------
-- Mark a module as sticky.
-- @param self An MT object.
-- @param sn the short name
function M.addStickyA(self, sn)
local a = self.__stickyA
local entry = self.mT[sn]
a[#a+1] = {sn = sn, fn = entry.fn, version = extractVersion(entry.fullName, sn),
userName = entry.userName }
end