Debian Codegraph @

tcl/modeval.tcl

raw
1 ##########################################################################
2
3 # MODEVAL.TCL, module evaluation procedures
4 # Copyright (C) 2002-2004 Mark Lakata
5 # Copyright (C) 2004-2017 Kent Mein
6 # Copyright (C) 2016-2025 Xavier Delaruelle
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ##########################################################################
22
23 # gather for the current top evaluation the information on all evaluations
24 # happening under its umbrella
25 proc registerModuleEval {context msgrecid {failedmod {}} {failedcontext {}}} {
26 set evalid [topState evalid]
27 set unset [expr {$failedmod eq {} ? 0 : 1}]
28 set contextset 0
29 # unload dependent reload evaluations are mixed with dependent unload ones
30 if {$context eq {depre_un}} {
31 set context depun
32 }
33
34 # add msgrecid to existing evaluation context list
35 if {[info exists ::g_moduleEval($evalid)]} {
36 for {set i 0} {$i < [llength $::g_moduleEval($evalid)]} {incr i 1} {
37 set contextevallist [lindex $::g_moduleEval($evalid) $i]
38 if {[lindex $contextevallist 0] eq $context} {
39 if {$unset} {
40 set contextevallist [replaceFromList $contextevallist\
41 $msgrecid]
42 } else {
43 lappend contextevallist $msgrecid
44 }
45 set ::g_moduleEval($evalid) [expr {[llength $contextevallist] > 1\
46 ? [lreplace $::g_moduleEval($evalid) $i $i $contextevallist]\
47 : [lreplace $::g_moduleEval($evalid) $i $i]}]
48 set contextset 1
49 break
50 }
51 }
52 }
53
54 # add msgrecid to new evaluation context list
55 if {!$unset && !$contextset} {
56 lappend ::g_moduleEval($evalid) [list $context $msgrecid]
57 }
58
59 # add mod to failed evaluation list
60 if {$unset} {
61 lappend ::g_moduleFailedEval($evalid) $failedcontext $failedmod
62 if {[depthState reportholdid]} {
63 lappend ::g_holdModuleFailedEval([currentState reportholdid])\
64 $evalid $failedcontext $failedmod
65 }
66 }
67 }
68
69 # record that module evaluation is set hidden
70 proc registerModuleEvalHidden {context msgrecid} {
71 set evalid [topState evalid]
72 # unload dependent reload evaluations are mixed with dependent unload ones
73 if {$context eq {depre_un}} {
74 set context depun
75 }
76 lappend ::g_moduleHiddenEval($evalid:$context) $msgrecid
77 }
78
79 proc changeContextOfModuleEval {mod old_context new_context} {
80 set evalid [topState evalid]
81 # find old and new context evaluations
82 for {set i 0} {$i < [llength $::g_moduleEval($evalid)]} {incr i 1} {
83 set context_eval_list [lindex $::g_moduleEval($evalid) $i]
84 if {[lindex $context_eval_list 0] eq $old_context} {
85 set old_context_idx $i
86 set old_context_eval_list $context_eval_list
87 } elseif {[lindex $context_eval_list 0] eq $new_context} {
88 set new_context_idx $i
89 set new_context_eval_list $context_eval_list
90 }
91 if {[info exists old_context_idx] && [info exists new_context_idx]} {
92 break
93 }
94 }
95
96 # module evaluation not found
97 if {![info exists old_context_idx]} {
98 return
99 }
100
101 # new context has no evaluation yet
102 if {![info exists new_context_idx]} {
103 set new_context_idx end+1
104 set new_context_eval_list [list $new_context]
105 }
106
107 # find evaluation id of module
108 foreach msgrecid [lrange $old_context_eval_list 1 end] {
109 if {$mod eq [getModuleFromEvalId $msgrecid]} {
110 set mod_evalid $msgrecid
111 break
112 }
113 }
114 if {![info exists mod_evalid]} {
115 return
116 }
117
118 # remove evaluation from old context
119 set old_context_eval_list [replaceFromList $old_context_eval_list\
120 $mod_evalid]
121 lset ::g_moduleEval($evalid) $old_context_idx $old_context_eval_list
122
123 # insert evaluation at start of new context
124 set new_context_eval_list [linsert $new_context_eval_list 1 $mod_evalid]
125 # on Tcl 8.5, lset command cannot append a list
126 if {$new_context_idx eq {end+1}} {
127 lappend ::g_moduleEval($evalid) $new_context_eval_list
128 } else {
129 lset ::g_moduleEval($evalid) $new_context_idx $new_context_eval_list
130 }
131
132 # change context of hidden evaluation
133 set old_hidden_evalid $evalid:$old_context
134 if {[info exists ::g_moduleHiddenEval($old_hidden_evalid)]} {
135 if {$mod_evalid in $::g_moduleHiddenEval($old_hidden_evalid)} {
136 set ::g_moduleHiddenEval($old_hidden_evalid) [replaceFromList\
137 $::g_moduleHiddenEval($old_hidden_evalid) $mod_evalid]
138 set new_hidden_evalid $evalid:$new_context
139 lappend ::g_moduleHiddenEval($new_hidden_evalid) $mod_evalid
140 }
141 }
142 }
143
144 # get context of currently evaluated module
145 proc currentModuleEvalContext {} {
146 return [lindex $::g_moduleEvalAttempt([currentState modulenamevr]) end]
147 }
148
149 # record module evaluation attempt and corresponding context
150 proc registerModuleEvalAttempt {context mod mod_file} {
151 appendNoDupToList ::g_moduleEvalAttempt($mod) $context
152 appendNoDupToList ::g_moduleFileEvalAttempt($mod) $mod_file
153 }
154
155 proc unregisterModuleEvalAttempt {context mod mod_file} {
156 set ::g_moduleEvalAttempt($mod) [replaceFromList\
157 $::g_moduleEvalAttempt($mod) $context]
158 set ::g_moduleFileEvalAttempt($mod) [replaceFromList\
159 $::g_moduleFileEvalAttempt($mod) $mod_file]
160 }
161
162 # is at least one module passed as argument evaluated in passed context
163 proc isModuleEvaluated {context exclmod modulepath_list args} {
164 set ret 0
165 # look at all evaluated mod except excluded one (currently evaluated mod)
166 foreach evalmod [lsearch -all -inline -not [array names\
167 ::g_moduleEvalAttempt] $exclmod] {
168 # test evaluated module matches specified modulepaths
169 if {[llength $modulepath_list]} {
170 set eval_mod_file_match 0
171 foreach eval_mod_file $::g_moduleFileEvalAttempt($evalmod) {
172 if {[isModulefileInModulepathList $eval_mod_file\
173 $modulepath_list]} {
174 set eval_mod_file_match 1
175 break
176 }
177 }
178 if {!$eval_mod_file_match} {
179 continue
180 }
181 }
182 set evalmatch 0
183 # test arguments against all names of evaluated module (translate
184 # eventual modspec in evalmod into module names, in case module
185 # evaluation stopped prior module name setup)
186 # retrieve variants to pass them directly to modEq
187 set modvrlist [getVariantList $evalmod 0 0 1]
188 foreach mod [getAllModulesFromVersSpec $evalmod] {
189 foreach name $args {
190 # indicate module is loading to also compare against all alt names
191 if {[modEq $name $mod eqstart 1 1 1 $modvrlist]} {
192 set evalmatch 1
193 if {$context eq {any} || $context in\
194 $::g_moduleEvalAttempt($evalmod)} {
195 set ret 1
196 }
197 break
198 }
199 }
200 if {$evalmatch} {
201 break
202 }
203 }
204 if {$ret} {
205 break
206 }
207 }
208 return $ret
209 }
210
211 # was passed mod already evaluated for context and failed
212 proc isModuleEvalFailed {context mod} {
213 set ret 0
214 set evalid [topState evalid]
215 if {[info exists ::g_moduleFailedEval($evalid)]} {
216 foreach {curcon curmod} $::g_moduleFailedEval($evalid) {
217 if {$context eq $curcon && $mod eq $curmod} {
218 set ret 1
219 break
220 }
221 }
222 }
223 return $ret
224 }
225
226 # return list of currently loading modules in stack
227 proc getLoadingModuleList {} {
228 set modlist [list]
229 for {set i 0} {$i < [depthState modulename]} {incr i 1} {
230 if {[lindex [getState mode] $i] eq {load}} {
231 lappend modlist [lindex [getState modulename] $i]
232 }
233 }
234 return $modlist
235 }
236
237 # return list of currently loading modulefiles in stack
238 proc getLoadingModuleFileList {} {
239 set modlist [list]
240 for {set i 0} {$i < [depthState modulefile]} {incr i 1} {
241 if {[lindex [getState mode] $i] eq {load}} {
242 lappend modlist [lindex [getState modulefile] $i]
243 }
244 }
245 return $modlist
246 }
247
248 # return list of currently unloading modules in stack
249 proc getUnloadingModuleList {} {
250 set modlist [list]
251 for {set i 0} {$i < [depthState modulename]} {incr i 1} {
252 if {[lindex [getState mode] $i] eq {unload}} {
253 lappend modlist [lindex [getState modulename] $i]
254 }
255 }
256 return $modlist
257 }
258
259 # sort passed module list following both loaded and dependency orders
260 proc sortModulePerLoadedAndDepOrder {modlist {nporeq 0} {loading 0}} {
261 # sort per loaded order
262 set sortlist {}
263 if {[llength $modlist]} {
264 foreach lmmod [getEnvLoadedModulePropertyParsedList name] {
265 if {$lmmod in $modlist} {
266 lappend sortlist $lmmod
267 }
268 }
269 # also sort eventual loading modules if asked
270 if {$loading} {
271 foreach loadingmod [lreverse [getLoadingModuleList]] {
272 if {$loadingmod in $modlist} {
273 lappend sortlist $loadingmod
274 }
275 }
276 }
277 }
278
279 # then refine sort with dependencies between loaded modules: a dependent
280 # module should be placed prior the loaded module requiring it
281 set reqListVar [expr {$nporeq ? {::g_moduleNPODepend} :\
282 {::g_moduleDepend}}]
283 set i 0
284 set imax [llength $sortlist]
285 while {$i < $imax} {
286 set mod [lindex $sortlist $i]
287 set jmin $imax
288
289 ##nagelfar ignore #4 Suspicious variable name
290 if {[info exists ${reqListVar}($mod)]} {
291 # goes over all dependent modules to find the first one in the loaded
292 # order list located after requiring mod
293 foreach lmmodlist [set ${reqListVar}($mod)] {
294 foreach lmmod $lmmodlist {
295 set j [lsearch -exact $sortlist $lmmod]
296 if {$j > $i && $j < $jmin} {
297 set jmin $j
298 set jminmod $lmmod
299 }
300 }
301 }
302 }
303
304 # move first dependent module found after currently inspected mod right
305 # before it
306 if {$jmin != $imax} {
307 set sortlist [linsert [lreplace $sortlist $jmin $jmin] $i $jminmod]
308 # or go to next element in list if current element has not been changed
309 } else {
310 incr i
311 }
312 }
313
314 return $sortlist
315 }
316
317 # return list of loaded modules having an unmet requirement on passed mod
318 # and their recursive dependent
319 proc getUnmetDependentLoadedModuleList {modnamevr mod_file} {
320 set unmetdeplist {}
321 set depmodlist {}
322 defineModEqProc [isIcase] [getConf extended_default] 1
323 set mod [getModuleNameAndVersFromVersSpec $modnamevr]
324 set vrlist [getVariantList $modnamevr 0 0 1]
325
326 # skip dependent analysis if mod has a conflict with a loaded module
327 set modconlist [getModuleLoadedConflict $mod]
328 if {![llength $modconlist]} {
329 foreach ummod [array names ::g_unmetDepHash] {
330 if {[modEq $ummod $mod eqstart 1 2 1 $vrlist]} {
331 foreach {depmod prereq_path_list} $::g_unmetDepHash($ummod) {
332 if {![isModulefileMatchSpecificPath $mod_file\
333 $prereq_path_list]} {
334 continue
335 }
336
337 lappend depmodlist $depmod
338 # temporarily remove prereq violation of depmod if mod
339 # load solves it (no other prereq is missing)
340 if {[info exists ::g_prereqViolation($depmod)]} {
341 foreach prereq $::g_prereqViolation($depmod) {
342 foreach modpre $prereq {
343 # also temporarily remove prereq violation for
344 # requirements loaded after dependent module
345 if {[modEq $modpre $mod eqstart 1 2 1 $vrlist] ||\
346 [is-loaded $modpre]} {
347 # backup original violation to restore it later
348 if {![info exists preunvioarr($depmod)]} {
349 set preunvioarr($depmod)\
350 $::g_prereqViolation($depmod)
351 }
352 # temporarily remove matching violation
353 set ::g_prereqViolation($depmod) [replaceFromList\
354 $::g_prereqViolation($depmod) $prereq]
355 if {![llength $::g_prereqViolation($depmod)]} {
356 unset ::g_prereqViolation($depmod)
357 }
358 break
359 }
360 }
361 }
362 }
363 }
364 }
365 }
366 }
367
368 # select dependent if all its constraint are now satisfied (after removing
369 # eventual prereq violation toward mod)
370 foreach depmod $depmodlist {
371 if {[areModuleConstraintsSatisfied $depmod]} {
372 appendNoDupToList unmetdeplist $depmod
373 }
374 }
375
376 # get dependent of dependent
377 set deplist [getDependentLoadedModuleList $unmetdeplist 0 0 0 0 1]
378
379 # restore temporarily lift prereq violation
380 if {[array exists preunvioarr]} {
381 foreach depmod [array names preunvioarr] {
382 set ::g_prereqViolation($depmod) $preunvioarr($depmod)
383 }
384 }
385
386 set sortlist [sortModulePerLoadedAndDepOrder [list {*}$unmetdeplist\
387 {*}$deplist]]
388 reportDebug "got '$sortlist'"
389 return $sortlist
390 }
391
392 # return list of loaded modules declaring a prereq on passed mod with
393 # distinction made with strong prereqs (no alternative loaded) or weak and
394 # also with prereq loaded after their dependent module
395 proc getDirectDependentList {mod {strong 0} {nporeq 0} {loading 0}\
396 {othmodlist {}}} {
397 set deplist {}
398
399 # include or not requirements loaded after their dependent
400 if {$nporeq} {
401 set depListVar ::g_dependNPOHash
402 set reqListVar ::g_moduleNPODepend
403 } else {
404 set depListVar ::g_dependHash
405 set reqListVar ::g_moduleDepend
406 }
407
408 ##nagelfar ignore #2 Suspicious variable name
409 if {[info exists ${depListVar}($mod)]} {
410 foreach depmod [set ${depListVar}($mod)] {
411 set add 1
412 # skip optional dependency if only looking for strong ones
413 # look at an additionally processed mod list to determine if all
414 # mods from a dependent list (composed of optional parts) are part
415 # of the search, which means mod is not optional but strong dependent
416 if {$strong && [llength $depmod] > 1} {
417 ##nagelfar ignore Suspicious variable name
418 foreach lmmodlist [set ${reqListVar}([lindex $depmod 0])] {
419 if {$mod in $lmmodlist} {
420 foreach lmmod $lmmodlist {
421 # other mod part of the opt list is not there so mod
422 # is considered optional
423 if {$lmmod ni $othmodlist} {
424 set add 0
425 break
426 }
427 }
428 break
429 }
430 }
431 }
432
433 if {$add} {
434 lappend deplist [lindex $depmod 0]
435 }
436 }
437 }
438
439 # take currently loading modules into account if asked
440 if {$loading} {
441 foreach loading_mod [getLoadingModuleList] {
442 foreach prereq_match [getLoadedModulePrereqListAndLoadedMatch\
443 $loading_mod] {
444 set lmprelist {}
445 set item_match_list [lassign $prereq_match prereq\
446 prereq_path_list]
447 foreach {prereq_mod_item loaded_mod_match ign} $item_match_list {
448 if {[string length $loaded_mod_match]} {
449 lappend lmprelist $loaded_mod_match
450 }
451 }
452 if {$mod in $lmprelist && (!$strong || [llength $lmprelist]\
453 == 1)} {
454 lappend deplist $loading_mod
455 break
456 }
457 }
458 }
459 }
460
461 return $deplist
462 }
463
464 # gets the list of all loaded modules which are dependent of passed modlist
465 # ordered by load position. strong argument controls whether only the active
466 # dependent modules should be returned or also those that are optional. direct
467 # argument controls if only dependent module directly requiring passed mods
468 # should be returned or its full dependent tree. nporeq argument tells if
469 # requirement loaded after their dependent should be returned. sat_constraint
470 # argument controls whether only the loaded module satisfying their constraint
471 # should be part or not of the resulting list. being_unload argument controls
472 # whether loaded modules in conflict with one or multiple modules from modlist
473 # should be added to the dependent list as these modules are currently being
474 # unloaded and these conflicting loaded modules should be refreshed.
475 proc getDependentLoadedModuleList {modlist {strong 1} {direct 1} {nporeq 0}\
476 {loading 1} {sat_constraint 0} {being_unload 0}} {
477 reportDebug "get loaded mod dependent of '$modlist' (strong=$strong,\
478 direct=$direct, nporeq=$nporeq, loading=$loading,\
479 sat_constraint=$sat_constraint, being_unload=$being_unload)"
480
481 set deplist {}
482 set fulllist $modlist
483 # look at consistent requirements for unloading modules
484 set unlonporeq [expr {$being_unload ? 0 : $nporeq}]
485 foreach mod $modlist {
486 # no duplicates or modules from query list
487 appendNoDupToList fulllist {*}[getDirectDependentList $mod $strong\
488 $unlonporeq $loading $fulllist]
489 }
490
491 if {$being_unload} {
492 # invite modules in violation with mods to be part of the dependent list
493 # with their own dependent modules as mod is being unloaded. Achieve so
494 # by faking that conflict violation is gone
495 foreach mod $modlist {
496 set modconlist [getModuleLoadedConflict $mod]
497 if {[llength $modconlist]} {
498 unsetModuleConflictViolation $mod
499 set conunvioarr($mod) $modconlist
500 appendNoDupToList fulllist {*}$modconlist
501 }
502 }
503 }
504 set unloadingmodlist [getUnloadingModuleList]
505 for {set i [llength $modlist]} {$i < [llength $fulllist]} {incr i 1} {
506 set depmod [lindex $fulllist $i]
507
508 # skip already added mod or mod violating constraints if asked
509 if {!$sat_constraint || [areModuleConstraintsSatisfied $depmod\
510 $nporeq]} {
511 # get dependent mod of dep mod when looking at full dep tree
512 if {!$direct} {
513 appendNoDupToList fulllist {*}[getDirectDependentList $depmod\
514 $strong $nporeq 0 $fulllist]
515 }
516 # avoid module currently unloading from result list
517 if {$depmod ni $unloadingmodlist} {
518 lappend deplist $depmod
519 }
520 }
521 }
522
523 # restore conflict violation if any
524 if {[array exists conunvioarr]} {
525 foreach conunvio [array names conunvioarr] {
526 setModuleConflictViolation $conunvio $conunvioarr($conunvio)
527 }
528 }
529
530 # sort complete result list to match both loaded and dependency orders
531 set sortlist [sortModulePerLoadedAndDepOrder $deplist $nporeq $loading]
532 reportDebug "got '$sortlist'"
533 return $sortlist
534 }
535
536 # may given module be automatically unloaded
537 # unmodlist: pass a list of modules that are going to be unloaded
538 proc isModuleUnloadable {mod {unmodlist {}}} {
539 set ret 1
540
541 # module may be unloaded if it has been automatically loaded, it is not
542 # tagged keep-loaded and it is not sticky.
543 if {[isModuleTagged $mod loaded 1] || [isModuleTagged $mod keep-loaded\
544 1] || [isModuleSticky $mod]} {
545 set ret 0
546 # there should no one requiring module or these dependent should be part of
547 # the unloaded/unloading list
548 } else {
549 foreach depmod [getDirectDependentList $mod] {
550 if {$depmod ni $unmodlist} {
551 set ret 0
552 break
553 }
554 }
555 }
556
557 return $ret
558 }
559
560 # gets the list of all loaded modules which are required by passed mod_list
561 # ordered by load position. a list of modules to exclude may be provided
562 # (usually to skip requirements that will be unloaded)
563 proc getRequiredLoadedModuleList {mod_list {excluded_mod_list {}}} {
564 # search over all list of loaded modules, starting with passed module
565 # list, then adding in turns their requirements
566 set full_list $mod_list
567 for {set i 0} {$i < [llength $full_list]} {incr i 1} {
568 # gets the list of loaded modules which are required by depmod
569 foreach req_mod_list $::g_moduleDepend([lindex $full_list $i]) {
570 foreach req_mod $req_mod_list {
571 if {$req_mod ni $excluded_mod_list} {
572 appendNoDupToList full_list $req_mod
573 }
574 }
575 }
576 }
577
578 # sort complete result list to match both loaded and dependency orders
579 set sort_list [sortModulePerLoadedAndDepOrder [lrange $full_list [llength\
580 $mod_list] end]]
581 reportDebug "got '$sort_list'"
582 return $sort_list
583 }
584
585 # how many settings bundle are currently saved
586 proc getSavedSettingsStackDepth {} {
587 return [llength $::g_SAVE_g_loadedModules]
588 }
589
590 # manage settings to save as a stack to have a separate set of settings
591 # for each module loaded or unloaded in order to be able to restore the
592 # correct set in case of failure
593 proc pushSettings {} {
594 foreach var {env g_clearedEnvVars g_Aliases g_stateEnvVars g_stateAliases\
595 g_stateFunctions g_Functions g_stateCompletes g_Completes\
596 g_newXResources g_delXResources g_loadedModules g_loadedModuleFiles\
597 g_loadedModuleVariant g_loadedModuleConflict g_loadedModulePrereq\
598 g_loadedModulesRefresh g_loadedModuleAltname g_loadedModuleAutoAltname\
599 g_loadedModuleAliasAltname g_moduleDepend g_dependHash\
600 g_moduleNPODepend g_dependNPOHash g_prereqViolation\
601 g_prereqNPOViolation g_conflictViolation g_moduleUnmetDep\
602 g_unmetDepHash g_moduleEval g_moduleHiddenEval g_scanModuleVariant\
603 g_savedLoReqOfReloadMod g_savedLoReqOfUnloadMod\
604 g_loadedModulePrereqPath g_tagHash} {
605 ##nagelfar ignore Suspicious variable name
606 lappend ::g_SAVE_$var [array get ::$var]
607 }
608 # save non-array variable and indication if it was set
609 foreach var {g_changeDir g_stdoutPuts g_prestdoutPuts g_return_text\
610 g_uReqUnFromDepReList} {
611 ##nagelfar ignore #4 Suspicious variable name
612 if {[info exists ::$var]} {
613 lappend ::g_SAVE_$var [list 1 [set ::$var]]
614 } else {
615 lappend ::g_SAVE_$var [list 0 {}]
616 }
617 }
618 reportDebug "settings saved (#[getSavedSettingsStackDepth])"
619 }
620
621 proc popSettings {} {
622 set flushedid [getSavedSettingsStackDepth]
623 foreach var {env g_clearedEnvVars g_Aliases g_stateEnvVars g_stateAliases\
624 g_stateFunctions g_Functions g_stateCompletes g_Completes\
625 g_newXResources g_delXResources g_changeDir g_stdoutPuts\
626 g_prestdoutPuts g_return_text g_loadedModules g_loadedModuleFiles\
627 g_loadedModuleVariant g_loadedModuleConflict g_loadedModulePrereq\
628 g_loadedModulesRefresh g_loadedModuleAltname g_loadedModuleAutoAltname\
629 g_loadedModuleAliasAltname g_moduleDepend g_dependHash\
630 g_moduleNPODepend g_dependNPOHash g_prereqViolation\
631 g_prereqNPOViolation g_conflictViolation g_moduleUnmetDep\
632 g_unmetDepHash g_moduleEval g_moduleHiddenEval g_scanModuleVariant\
633 g_savedLoReqOfReloadMod g_savedLoReqOfUnloadMod g_uReqUnFromDepReList\
634 g_loadedModulePrereqPath g_tagHash} {
635 ##nagelfar ignore Suspicious variable name
636 set ::g_SAVE_$var [lrange [set ::g_SAVE_$var] 0 end-1]
637 }
638 reportDebug "previously saved settings flushed (#$flushedid)"
639 }
640
641 proc restoreSettings {} {
642 foreach var {g_clearedEnvVars g_Aliases g_stateEnvVars g_stateAliases\
643 g_stateFunctions g_Functions g_stateCompletes g_Completes\
644 g_newXResources g_delXResources g_loadedModules g_loadedModuleFiles\
645 g_loadedModuleVariant g_loadedModuleConflict g_loadedModulePrereq\
646 g_loadedModulesRefresh g_loadedModuleAltname g_loadedModuleAutoAltname\
647 g_loadedModuleAliasAltname g_moduleDepend g_dependHash\
648 g_moduleNPODepend g_dependNPOHash g_prereqViolation\
649 g_prereqNPOViolation g_conflictViolation g_moduleUnmetDep\
650 g_unmetDepHash g_moduleEval g_moduleHiddenEval g_scanModuleVariant\
651 g_savedLoReqOfReloadMod g_savedLoReqOfUnloadMod\
652 g_loadedModulePrereqPath g_tagHash} {
653 # clear current $var arrays
654 ##nagelfar ignore #5 Suspicious variable name
655 if {[info exists ::$var]} {
656 unset ::$var
657 array set ::$var {}
658 }
659 array set ::$var [lindex [set ::g_SAVE_$var] end]
660 }
661 # specific restore mechanism for ::env as unsetting this array will make
662 # Tcl stop monitoring env accesses and not update env variables anymore
663 set envvarlist [list]
664 foreach {var val} [lindex $::g_SAVE_env end] {
665 lappend envvarlist $var
666 interp-sync-env set $var $val
667 }
668 foreach var [array names ::env] {
669 if {$var ni $envvarlist} {
670 interp-sync-env unset $var
671 }
672 }
673 # restore non-array variable if it was set
674 foreach var {g_changeDir g_stdoutPuts g_prestdoutPuts g_return_text\
675 g_uReqUnFromDepReList} {
676 ##nagelfar ignore #6 Suspicious variable name
677 if {[info exists ::$var]} {
678 unset ::$var
679 }
680 lassign [lindex [set ::g_SAVE_$var] end] isdefined val
681 if {$isdefined} {
682 set ::$var $val
683 }
684 }
685 reportDebug "previously saved settings restored\
686 (#[getSavedSettingsStackDepth])"
687 }
688
689 # clear environment change related variables to undo modifications produced by
690 # evaluated modulefile(s)
691 proc flushEnvSettings {} {
692 foreach var {g_Aliases g_stateEnvVars g_stateAliases g_stateFunctions\
693 g_Functions g_stateCompletes g_Completes g_newXResources\
694 g_delXResources g_changeDir g_stdoutPuts g_prestdoutPuts\
695 g_return_text} {
696 ##nagelfar ignore #2 Suspicious variable name
697 if {[info exists ::$var]} {
698 unset ::$var
699 }
700 }
701 }
702
703 # load modules passed as args designated as requirement
704 proc loadRequirementModuleList {tryload optional tag_list modulepath_list\
705 args} {
706 set ret 0
707 set prereqloaded 0
708
709 # calling procedure must have already parsed module specification in args
710 set loadedmod_list {}
711 foreach mod $args {
712 # get all loaded or loading mod in args list
713 if {[set loadedmod [getLoadedMatchingName $mod returnfirst 0 {}\
714 $modulepath_list]] ne {} || [set loadedmod [getLoadedMatchingName\
715 $mod returnfirst 1 {} $modulepath_list]] ne {}} {
716 lappend loadedmod_list $loadedmod
717 }
718 }
719
720 if {![llength $loadedmod_list]} {
721 set imax [llength $args]
722 # if prereq list specified, try to load first then
723 # try next if load of first module not successful
724 for {set i 0} {$i<$imax && !$prereqloaded} {incr i 1} {
725 set arg [lindex $args $i]
726
727 # hold output from current evaluation to catch 'module not found'
728 # message that occurs outside of sub evaluation
729 lappendState reportholdrecid [currentState msgrecordid]
730 # hold output of each sub evaluation until they are all done to drop
731 # those that failed if one succeed or if optional
732 set curholdid load-$i-$arg
733 lappendState reportholdid $curholdid
734 if {[catch {set retlo [cmdModuleLoad reqlo 0 $tryload 0 $tag_list\
735 $modulepath_list $arg]} errorMsg]} {
736 # if an error is raised, release output and rethrow the error
737 # (could be raised if no modulepath defined for instance)
738 lpopState reportholdid
739 lpopState reportholdrecid
740 lappend holdidlist $curholdid report
741 releaseHeldReport {*}$holdidlist
742 knerror $errorMsg
743 }
744 # update return value if an issue occurred in cmdModuleLoad
745 if {$retlo != 0} {
746 set ret $retlo
747 }
748 lpopState reportholdid
749 lpopState reportholdrecid
750
751 if {[string length [getLoadedMatchingName $arg returnfirst 0 {}\
752 $modulepath_list]]} {
753 set prereqloaded 1
754 # set previous reports to be dropped as this one succeed
755 if {[info exists holdidlist]} {
756 foreach {holdid action} $holdidlist {
757 lappend newholdidlist $holdid drop
758 }
759 set holdidlist $newholdidlist
760 }
761 }
762 # drop report if not loaded and optional
763 set action [expr {$prereqloaded || !$optional ? {report} : {drop}}]
764 lappend holdidlist $curholdid $action
765 }
766 # output held messages
767 releaseHeldReport {*}$holdidlist
768 } else {
769 set prereqloaded 1
770 # apply missing tag to all loaded module found
771 cmdModuleTag 0 0 $tag_list {*}$loadedmod_list
772 }
773
774 return [list $ret $prereqloaded]
775 }
776
777 # save reloading module properties before they vanish with unload phase
778 proc savePropsOfReloadingModule {mod} {
779 set is_user_asked [isModuleTagged $mod loaded 1]
780 set vr_list [getVariantList $mod 1 2]
781 set tag_list [getTagList $mod]
782 set extra_tag_list [getExtraTagList $mod]
783 set conflict_list [getLoadedConflict $mod]
784 set prereq_list [getLoadedPrereq $mod]
785 set prereq_path_list [getLoadedPrereqPath $mod]
786 set modpath [getModulepathFromLoadedOrLoadingModule $mod]
787 set ::g_savedPropsOfReloadMod($mod) [list $is_user_asked $vr_list\
788 $tag_list $extra_tag_list $conflict_list $prereq_list $prereq_path_list\
789 $modpath]
790 }
791
792 proc getSavedPropsOfReloadingModule {mod} {
793 return $::g_savedPropsOfReloadMod($mod)
794 }
795
796 # unload phase of a list of modules reload process
797 proc reloadModuleListUnloadPhase {mod_list {err_msg_tpl {}} {context\
798 unload}} {
799 # unload one by one to ensure same behavior whatever auto_handling state
800 foreach mod [lreverse $mod_list] {
801 if {[reloadModuleUnloadPhase $mod $err_msg_tpl $context]} {
802 set mod_list [replaceFromList $mod_list $mod]
803 }
804 }
805 return $mod_list
806 }
807
808 proc reloadModuleUnloadPhase {mod {err_msg_tpl {}} {context unload}} {
809 # record hint that mod will be reloaded (useful in case mod is sticky)
810 lappendState reloading_sticky $mod
811 lappendState reloading_supersticky $mod
812 savePropsOfReloadingModule $mod
813 if {[set ret [cmdModuleUnload $context match 0 s 0 $mod]]} {
814 # avoid failing module on load phase
815 # if force state is enabled, cmdModuleUnload returns 0
816 set err_msg [string map [list _MOD_ [getModuleDesignation loaded $mod]]\
817 $err_msg_tpl]
818 lpopState reloading_sticky
819 lpopState reloading_supersticky
820 # no process stop if ongoing reload command in continue behavior
821 if {![isStateEqual commandname reload] || [commandAbortOnError]} {
822 knerror $err_msg
823 }
824 }
825 lpopState reloading_sticky
826 lpopState reloading_supersticky
827 return $ret
828 }
829
830 # load phase of a list of modules reload process
831 proc reloadModuleListLoadPhase {mod_list {errmsgtpl {}} {context load}} {
832 if {![llength $mod_list]} {
833 return
834 }
835 # loads are made with auto handling mode disabled to avoid disturbances
836 # from a missing prereq automatically reloaded, so these module loads may
837 # fail as prereq may not be satisfied anymore
838 setConf auto_handling 0
839 foreach mod $mod_list {
840 lassign [getSavedPropsOfReloadingModule $mod] is_user_asked vr_list\
841 tag_list extra_tag_list conflict_list prereq_list prereq_path_list\
842 modpath
843 # if an auto set default was excluded, module spec need parsing
844 lassign [parseModuleSpecification 0 0 0 0 $mod {*}$vr_list] modnamevr
845
846 set is_sticky [isModuleStickyFromTagList {*}$tag_list\
847 {*}$extra_tag_list]
848
849 # do not try to reload DepRe module if requirements are not satisfied
850 # unless if sticky
851 if {$context eq {depre} && ![isModuleLoadable $mod $modnamevr\
852 $conflict_list $prereq_list $prereq_path_list $modpath] &&\
853 !$is_sticky} {
854 continue
855 }
856
857 # reload module with user asked property and extra tags preserved
858 if {[cmdModuleLoad $context $is_user_asked 0 0 $extra_tag_list {}\
859 $modnamevr]} {
860 set errMsg [string map [list _MOD_ [getModuleDesignation spec\
861 $modnamevr]] $errmsgtpl]
862 if {$is_sticky} {
863 set errMsg [string map {dependent {sticky dependent}} $errMsg]
864 }
865 # unless sticky no process stop if forced, or ongoing reload or
866 # switch cmd in continue behavior
867 if {!$is_sticky && ([getState force] || (([isStateEqual commandname\
868 reload] || [isStateEqual commandname switch]) &&\
869 ![commandAbortOnError]))} {
870 # no msg for reload sub-cmd which provides an empty msg template
871 reportWarning $errMsg
872 # stop if one load fails unless force mode enabled
873 } else {
874 knerror $errMsg
875 }
876 }
877 }
878 setConf auto_handling 1
879 }
880
881 proc isModuleLoadable {mod mod_vr conflict_list prereq_list\
882 prereq_path_list modpath} {
883 setLoadedConflict $mod {*}$conflict_list
884 set is_conflicting [llength [getModuleLoadedConflict $mod]]
885 unsetLoadedConflict $mod
886
887 if {$is_conflicting} {
888 return 0
889 }
890
891 array set prereq_path_arr $prereq_path_list
892 foreach prereq_arg $prereq_list {
893 if {[info exists prereq_path_arr($prereq_arg)]} {
894 set prereq_arg_path $prereq_path_arr($prereq_arg)
895 } else {
896 set prereq_arg_path {}
897 }
898 set is_requirement_loaded 0
899 foreach req_mod $prereq_arg {
900 # is requirement loaded, loading or optional
901 if {[string length [getLoadedMatchingName $req_mod returnfirst 0 {}\
902 $prereq_arg_path]] || [string length [getLoadedMatchingName\
903 $req_mod returnfirst 1 {} $prereq_arg_path]] || $req_mod eq\
904 $mod} {
905 set is_requirement_loaded 1
906 break
907 }
908 }
909 if {!$is_requirement_loaded} {
910 return 0
911 }
912 }
913
914 # module is loadable if its modulepath is still enabled or if it can be
915 # found in another modulepath (variant availability is currently not
916 # checked by is-avail procedure)
917 if {![is-used $modpath] && ![is-avail $mod_vr]} {
918 return 0
919 }
920
921 return 1
922 }
923
924 proc isModuleStickyFromTagList {args} {
925 return [expr {{super-sticky} in $args || ({sticky} in $args && ![getState\
926 force])}]
927 }
928
929 # test if loaded module 'mod' is sticky and if stickiness definition applies
930 # to one of the reloading module
931 proc isStickinessReloading {mod reloading_mod_list {tag sticky}} {
932 set res 0
933 set mod_name_vers [getModuleNameAndVersFromVersSpec $mod]
934 if {[isModuleTagged $mod_name_vers $tag 1]} {
935 # sticky rules (module-tag definitions) applying to loaded module have
936 # been evaluated when charging loaded environment
937 set full_path_mod [getModulefileFromLoadedModule $mod_name_vers]
938 set tag_rule_list [getModuleTagRuleList $mod $full_path_mod $tag]
939
940 # no rule found (in env), means sticky applies to exact same module
941 if {![llength $tag_rule_list]} {
942 set mod_should_reload 1
943 } else {
944 set mod_should_reload 0
945 # if tag specifically applies to fully qualified module or module
946 # name and version, exact same module should be found in reload list
947 foreach tag_rule $tag_rule_list {
948 if {[modEq $tag_rule $mod equal 1 0 1] || [modEq $tag_rule\
949 $full_path_mod equal 1 0 1]} {
950 set mod_should_reload 1
951 break
952 }
953 }
954 }
955
956 if {$mod_should_reload} {
957 set res [expr {$mod in $reloading_mod_list}]
958 } else {
959 # check if a reloading module satisfies each sticky rules
960 foreach tag_rule $tag_rule_list {
961 set res 0
962 foreach reloading_mod $reloading_mod_list {
963 if {[modEq $tag_rule $reloading_mod eqstart 1 0 1]} {
964 set res 1
965 break
966 }
967 }
968 if {!$res} {
969 break
970 }
971 }
972 }
973 reportDebug "stickiness ($tag), applying to $tag_rule_list, is\
974 reloading=$res"
975 }
976
977 return $res
978 }
979
980 proc isModuleSticky {mod} {
981 return [expr {[isModuleTagged $mod super-sticky 1] || ([isModuleTagged\
982 $mod sticky 1] && ![getState force])}]
983 }
984
985 proc saveLoadedReqOfUnloadingModule {unload_mod} {
986 # fetch requirements of unloading module
987 set ::g_savedLoReqOfUnloadMod($unload_mod)\
988 [getRequiredLoadedModuleList [list $unload_mod]]
989 }
990
991 proc getLoadedReqOfUnloadingModuleList {} {
992 set unloading_req_mod_list {}
993 foreach unloading_mod [array names ::g_savedLoReqOfUnloadMod] {
994 appendNoDupToList unloading_req_mod_list\
995 {*}$::g_savedLoReqOfUnloadMod($unloading_mod)
996 }
997 return [sortModulePerLoadedAndDepOrder $unloading_req_mod_list]
998 }
999
1000 proc clearLoadedReqOfUnloadingModuleList {} {
1001 array unset ::g_savedLoReqOfUnloadMod
1002 }
1003
1004 proc saveLoadedReqOfReloadingModuleList {reload_mod_list unload_mod_list} {
1005 # fetch requirements of reloading modules skipping unloading ones
1006 foreach reload_mod $reload_mod_list {
1007 set ::g_savedLoReqOfReloadMod($reload_mod) [getRequiredLoadedModuleList\
1008 [list $reload_mod] $unload_mod_list]
1009 }
1010 }
1011
1012 proc getLoadedReqOfReloadingModuleList {} {
1013 set reloading_req_mod_list {}
1014 foreach reloading_mod [array names ::g_savedLoReqOfReloadMod] {
1015 appendNoDupToList reloading_req_mod_list\
1016 {*}$::g_savedLoReqOfReloadMod($reloading_mod)
1017 }
1018 return [sortModulePerLoadedAndDepOrder $reloading_req_mod_list]
1019 }
1020
1021 proc unsetLoadedReqOfReloadingModule {mod} {
1022 unset -nocomplain ::g_savedLoReqOfReloadMod($mod)
1023 }
1024
1025 proc clearLoadedReqOfReloadingModuleList {} {
1026 array unset ::g_savedLoReqOfReloadMod
1027 }
1028
1029 proc identityUReqUnFromDepRe {depre_list unload_mod_list} {
1030 foreach depre [lreverse $depre_list] {
1031 if {[isModuleUnloadable $depre $unload_mod_list]} {
1032 lappend unload_mod_list $depre
1033 lappend ::g_uReqUnFromDepReList $depre
1034 }
1035 }
1036 }
1037
1038 proc clearUReqUnFromDepReList {} {
1039 unset -nocomplain ::g_uReqUnFromDepReList
1040 }
1041
1042 proc getIdentifiedUReqUnFromDepRe {} {
1043 if {[info exists ::g_uReqUnFromDepReList]} {
1044 return $::g_uReqUnFromDepReList
1045 }
1046 }
1047
1048 proc removeUReqUnFromDepReAndConvertEval {} {
1049 set depre_list [getDepReList]
1050 set urequn_from_depre_list [getIdentifiedUReqUnFromDepRe]
1051 lassign [getDiffBetweenList $depre_list $urequn_from_depre_list] depre_list
1052 setDepReList $depre_list
1053 foreach urequn_from_depre [lreverse $urequn_from_depre_list] {
1054 changeContextOfModuleEval $urequn_from_depre depun urequn
1055 }
1056 }
1057
1058 proc getUReqUnModuleList {} {
1059 set unloadable_mod_list {}
1060
1061 # add DepRe modules that are UReqUn modules and unset their requirements
1062 # from reloading list (to check them for potential UReqUn modules)
1063 foreach urequn_from_depre [getIdentifiedUReqUnFromDepRe] {
1064 lappend unloadable_mod_list $urequn_from_depre
1065 unsetLoadedReqOfReloadingModule $urequn_from_depre
1066 }
1067
1068 set reloading_req_mod_list [getLoadedReqOfReloadingModuleList]
1069 # useless requirement unload modules are unloadable req of unloaded mods
1070 # treat lastly loaded module first to build unloadable module list
1071 foreach unloading_req_mod [lreverse [getLoadedReqOfUnloadingModuleList]] {
1072 if {$unloading_req_mod ni $reloading_req_mod_list &&\
1073 [isModuleUnloadable $unloading_req_mod $unloadable_mod_list]} {
1074 lappend unloadable_mod_list $unloading_req_mod
1075 }
1076 }
1077 # return result in loaded order
1078 return [lreverse $unloadable_mod_list]
1079 }
1080
1081 proc unloadUReqUnModules {} {
1082 set urequn_list [getUReqUnModuleList]
1083 reportDebug "urequn mod list is '$urequn_list'"
1084
1085 if {[llength $urequn_list]} {
1086 # DepRe: Dependent to Reload (modules optionally dependent or in
1087 # conflict with modname, DepUn or UReqUn modules + modules dependent of
1088 # a module part of this DepRe batch)
1089 set urequn_depre_list [getDependentLoadedModuleList $urequn_list 0 0 1\
1090 0 1 1]
1091 set urequn_depre_list [reloadModuleListUnloadPhase $urequn_depre_list\
1092 {Unload of dependent _MOD_ failed} depre_un]
1093 lprependDepReList $urequn_depre_list
1094
1095 set urequn_list [lreverse $urequn_list]
1096 for {set i 0} {$i < [llength $urequn_list]} {incr i 1} {
1097 set unmod [lindex $urequn_list $i]
1098 if {[cmdModuleUnload urequn match 0 s 0 $unmod]} {
1099 # main unload process continues, but the UReqUn modules that are
1100 # required by unmod (whose unload failed) are withdrawn from
1101 # UReqUn module list
1102 lassign [getDiffBetweenList $urequn_list\
1103 [getRequiredLoadedModuleList [list $unmod]]] urequn_list
1104 }
1105 }
1106 }
1107 }
1108
1109 proc getDepUnModuleList {mod} {
1110 set depun_npo_list [getDependentLoadedModuleList [list $mod] 1 0 1 0]
1111 set depun_list [getDependentLoadedModuleList [list $mod] 1 0 0 0]
1112 # look at both regular dependencies or No Particular Order dependencies:
1113 # use NPO result if situation can be healed with NPO dependencies, which
1114 # will be part of DepRe list to restore the correct loading order for them
1115 if {[llength $depun_npo_list] <= [llength $depun_list]} {
1116 set depun_list $depun_npo_list
1117 }
1118 return $depun_list
1119 }
1120
1121 proc clearDepReList {} {
1122 unset -nocomplain ::g_depReList
1123 }
1124
1125 proc getDepReList {} {
1126 if {[info exists ::g_depReList]} {
1127 return $::g_depReList
1128 }
1129 }
1130
1131 proc setDepReList {mod_list} {
1132 set ::g_depReList $mod_list
1133 }
1134
1135 proc lprependDepReList {mod_list} {
1136 lprepend ::g_depReList {*}$mod_list
1137 }
1138
1139 proc unloadDepUnDepReModules {unload_mod_list reload_mod_list} {
1140 set err_msg_tpl {Unload of dependent _MOD_ failed}
1141 set unload_mod_list [sortModulePerLoadedAndDepOrder [list\
1142 {*}$unload_mod_list {*}$reload_mod_list]]
1143 lprependDepReList $reload_mod_list
1144
1145 foreach unload_mod [lreverse $unload_mod_list] {
1146 if {$unload_mod in $reload_mod_list} {
1147 # an error is raised if an unload eval fails
1148 reloadModuleUnloadPhase $unload_mod $err_msg_tpl depre_un
1149 } else {
1150 if {[cmdModuleUnload depun match 0 s 0 $unload_mod]} {
1151 # stop if one unload fails unless force mode enabled
1152 set err_msg [string map [list _MOD_ [getModuleDesignation loaded\
1153 $unload_mod]] $err_msg_tpl]
1154 knerrorOrWarningIfForced $err_msg
1155 }
1156 }
1157 }
1158 }
1159
1160 proc reloadDepReModules {} {
1161 set err_msg_tpl {Reload of dependent _MOD_ failed}
1162 set depre_list [getDepReList]
1163 reloadModuleListLoadPhase $depre_list $err_msg_tpl depre
1164 }
1165
1166 # Fail unload attempt if module is sticky, unless if forced or reloading
1167 # Also fail unload if mod is super-sticky even if forced, unless reloading
1168 proc failOrSkipUnloadIfSticky {modname modfile} {
1169 # when loaded, tags applies to mod name and version (not with variant)
1170 set is_supersticky_not_reloading [expr {[isModuleTagged $modname\
1171 super-sticky 1 $modfile] && [currentState reloading_supersticky] ne\
1172 $modname}]
1173 set is_sticky_not_reloading [expr {[isModuleTagged $modname sticky 1\
1174 $modfile] && [currentState reloading_sticky] ne $modname &&\
1175 [currentState unloading_sticky] ne $modname}]
1176 set sticky_purge [expr {[getState commandname] eq {purge} ? [getConf\
1177 sticky_purge] : {}}]
1178
1179 if {!$is_supersticky_not_reloading && $is_sticky_not_reloading &&\
1180 [getState force]} {
1181 reportWarning [getStickyForcedUnloadMsg]
1182 } elseif {$is_supersticky_not_reloading || $is_sticky_not_reloading} {
1183 set msg [getStickyUnloadMsg [expr {$is_supersticky_not_reloading ?\
1184 {super-sticky} : {sticky}}]]
1185 # no message if sticky_purge is set to silent
1186 switch -- $sticky_purge {
1187 error - {} {knerror $msg}
1188 warning {reportWarning $msg}
1189 }
1190
1191 # skip unload without raising error
1192 return 1
1193 }
1194
1195 return 0
1196 }
1197
1198 # ;;; Local Variables: ***
1199 # ;;; mode:tcl ***
1200 # ;;; End: ***
1201 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: