| 1 |
########################################################################## |
| 2 |
|
| 3 |
# COLL.TCL, collection management procedures |
| 4 |
# Copyright (C) 2016-2025 Xavier Delaruelle |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
########################################################################## |
| 20 |
|
| 21 |
# build list of what to undo then do to move from an initial list to a target |
| 22 |
# list, eventually checking element presence in extra from/to lists |
| 23 |
proc getMovementBetweenList {from to {extfrom {}} {extto {}} {cmp eq}} { |
| 24 |
reportDebug "from($from) to($to) with extfrom($extfrom) extto($extto)" |
| 25 |
|
| 26 |
set undo {} |
| 27 |
set do {} |
| 28 |
|
| 29 |
# determine what element to undo then do |
| 30 |
# to restore a target list from a current list |
| 31 |
# with preservation of the element order |
| 32 |
##nagelfar ignore #2 Badly formed if statement |
| 33 |
set imax [if {[llength $to] > [llength $from]} {llength $to} {llength\ |
| 34 |
$from}] |
| 35 |
set list_equal 1 |
| 36 |
for {set i 0} {$i < $imax} {incr i} { |
| 37 |
set to_obj [lindex $to $i] |
| 38 |
set from_obj [lindex $from $i] |
| 39 |
# check from/to element presence in extra from/to list |
| 40 |
set in_extfrom [expr {$from_obj in $extfrom}] |
| 41 |
set in_extto [expr {$to_obj in $extto}] |
| 42 |
# are elts the sames and are both part of or missing from extra lists |
| 43 |
# when comparing modules, ask comparison against loaded module |
| 44 |
# alternative and simplified names (modEq will also compare variants) |
| 45 |
if {($cmp eq {modeq} && ![modEq $to_obj $from_obj equal 1 3 1]) ||\ |
| 46 |
($cmp eq {eq} && $to_obj ne $from_obj) || $in_extfrom != $in_extto} { |
| 47 |
set list_equal 0 |
| 48 |
} |
| 49 |
if {!$list_equal} { |
| 50 |
if {$to_obj ne {}} { |
| 51 |
lappend do $to_obj |
| 52 |
} |
| 53 |
if {$from_obj ne {}} { |
| 54 |
lappend undo $from_obj |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return [list $undo $do] |
| 60 |
} |
| 61 |
|
| 62 |
# build list of currently loaded modules where modulename is registered minus |
| 63 |
# module version if loaded version is the default one |
| 64 |
proc getSimplifiedLoadedModuleList {} { |
| 65 |
set curr_mod_list {} |
| 66 |
array set curr_tag_arr {} |
| 67 |
set modpathlist [getModulePathList] |
| 68 |
foreach mod [getEnvLoadedModulePropertyParsedList name] { |
| 69 |
set altandsimplist [getLoadedAltAndSimplifiedName $mod] |
| 70 |
|
| 71 |
set parentmod [file dirname $mod] |
| 72 |
set simplemod $mod |
| 73 |
# simplify to parent name as long as it is found in simplified name list |
| 74 |
while {$parentmod ne {.}} { |
| 75 |
if {$parentmod in $altandsimplist} { |
| 76 |
set simplemod $parentmod |
| 77 |
set parentmod [file dirname $parentmod] |
| 78 |
} else { |
| 79 |
set parentmod . |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
# add each module specification as list to correctly enclose spaces in |
| 84 |
# module name or variant name or value |
| 85 |
set simplemodvr [list $simplemod {*}[getVariantList $mod 5 1]] |
| 86 |
lappend curr_mod_list $simplemodvr |
| 87 |
# record tags applying to module in simplified version form |
| 88 |
set tag_list [getSaveTagList $mod] |
| 89 |
if {[llength $tag_list]} { |
| 90 |
set curr_tag_arr($simplemodvr) $tag_list |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return [list $curr_mod_list [array get curr_tag_arr]] |
| 95 |
} |
| 96 |
|
| 97 |
# return saved collections found in user directory which corresponds to |
| 98 |
# enabled collection target if any set. extract one collection specifically |
| 99 |
# when search mode is set to exact. only compute collection name if mode is |
| 100 |
# set to name. translate collection name to __init__ if not found and |
| 101 |
# swap_by_init enabled. if no_other_target enabled, ensure no result from |
| 102 |
# other target are returned from glob search |
| 103 |
proc findCollections {{coll *} {search glob} {swap_by_init 0} {errnomatch 0}\ |
| 104 |
{checkvalid 1} {no_other_target 0}} { |
| 105 |
# initialize description with collection name |
| 106 |
set colldesc $coll |
| 107 |
|
| 108 |
if {$coll eq {}} { |
| 109 |
reportErrorAndExit [getEmptyNameMsg collection] |
| 110 |
} elseif {$coll eq {__init__}} { |
| 111 |
set collfile $coll |
| 112 |
set colldesc {} |
| 113 |
# is collection a filepath |
| 114 |
} elseif {[string first / $coll] > -1} { |
| 115 |
# collection target has no influence when |
| 116 |
# collection is specified as a filepath |
| 117 |
set collfile $coll |
| 118 |
# elsewhere collection is a name |
| 119 |
} elseif {[isEnvVarDefined HOME]} { |
| 120 |
set coll_dir [file join $::env(HOME) .module] |
| 121 |
set coll_glob $coll |
| 122 |
# find saved collections (matching target suffix). a target is a domain |
| 123 |
# on which a collection is only valid. when a target is set, only the |
| 124 |
# collections made for that target will be available to list and |
| 125 |
# restore, and saving will register the target footprint. current target |
| 126 |
# is ignored if --all option is set on savelist command |
| 127 |
set colltarget [getConf collection_target] |
| 128 |
if {$colltarget ne {} && ([getState hiding_threshold] < 2 ||\ |
| 129 |
[currentState commandname] ne {savelist})} { |
| 130 |
append coll_glob .$colltarget |
| 131 |
# add knowledge of collection target on description |
| 132 |
append colldesc " (for target \"$colltarget\")" |
| 133 |
} |
| 134 |
set collfile [file join $coll_dir $coll_glob] |
| 135 |
} else { |
| 136 |
reportErrorAndExit {HOME not defined} |
| 137 |
} |
| 138 |
|
| 139 |
switch -- $search { |
| 140 |
glob { |
| 141 |
# glob excludes by default files starting with "." |
| 142 |
if {[catch {set clist [glob -nocomplain -directory $coll_dir\ |
| 143 |
$coll_glob]} errMsg]} { |
| 144 |
reportErrorAndExit "Cannot access collection directory.\n$errMsg" |
| 145 |
} else { |
| 146 |
set res {} |
| 147 |
foreach cfile $clist { |
| 148 |
# test collection is from correct target or no target if |
| 149 |
# no_other_target is enabled |
| 150 |
set cfile_ext [string range [file extension $cfile] 1 end] |
| 151 |
if {(!$no_other_target || $cfile_ext eq [getConf\ |
| 152 |
collection_target]) && [checkValidColl $cfile]} { |
| 153 |
lappend res $cfile |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
exact { |
| 159 |
if {$coll ne {__init__}} { |
| 160 |
# verify that file exists |
| 161 |
if {![file exists $collfile]} { |
| 162 |
if {$errnomatch} { |
| 163 |
reportErrorAndExit "Collection $colldesc cannot be found" |
| 164 |
} else { |
| 165 |
set collfile {} |
| 166 |
} |
| 167 |
# error will be raised if collection not valid |
| 168 |
} elseif {$checkvalid && ![checkValidColl $collfile\ |
| 169 |
$errnomatch]} { |
| 170 |
set collfile {} |
| 171 |
} |
| 172 |
} |
| 173 |
if {$collfile eq {} && $swap_by_init} { |
| 174 |
set collfile __init__ |
| 175 |
set colldesc {} |
| 176 |
} |
| 177 |
# return coll filename and its description for exact and name modes |
| 178 |
set res [list $collfile $colldesc] |
| 179 |
} |
| 180 |
name { |
| 181 |
set res [list $collfile $colldesc] |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return $res |
| 186 |
} |
| 187 |
|
| 188 |
proc checkValidColl {collfile {report_issue 0}} { |
| 189 |
set res 0 |
| 190 |
if {[catch { |
| 191 |
set fdata [readFile $collfile 1] |
| 192 |
# extract magic cookie (first word) |
| 193 |
set fh [string trimright [lindex [split [string range $fdata 0 32]]\ |
| 194 |
0] #] |
| 195 |
} errMsg ]} { |
| 196 |
if {$report_issue} { |
| 197 |
reportErrorAndExit [parseAccessIssue $collfile] |
| 198 |
} |
| 199 |
} else { |
| 200 |
# collection without magic cookie are valid |
| 201 |
# check if min version requirement is met |
| 202 |
if {[string equal -length 8 $fh {#%Module}] && [string length $fh] \ |
| 203 |
> 8 && [versioncmp [getState modules_release] [string range $fh 8\ |
| 204 |
end]] < 0} { |
| 205 |
if {$report_issue} { |
| 206 |
reportErrorAndExit "Collection $collfile requires at least\ |
| 207 |
Modules version [string range $fh 8 end]" |
| 208 |
} |
| 209 |
} else { |
| 210 |
set res 1 |
| 211 |
} |
| 212 |
} |
| 213 |
return $res |
| 214 |
} |
| 215 |
|
| 216 |
# generate collection content based on provided path and module lists |
| 217 |
proc formatCollectionContent {path_list mod_list tag_arrser header {sgr 0}} { |
| 218 |
set content {} |
| 219 |
array set tag_arr $tag_arrser |
| 220 |
|
| 221 |
# graphically enhance module command if asked |
| 222 |
set modcmd [expr {$sgr ? [sgr cm module] : {module}}] |
| 223 |
|
| 224 |
# start collection content with modulepaths |
| 225 |
foreach path $path_list { |
| 226 |
# enclose path if space character found in it |
| 227 |
if {[string first { } $path] != -1} { |
| 228 |
set path "{$path}" |
| 229 |
} |
| 230 |
# 'module use' prepends paths by default so we clarify |
| 231 |
# path order here with --append flag |
| 232 |
append content "$modcmd use --append $path" \n |
| 233 |
} |
| 234 |
|
| 235 |
# then add modules |
| 236 |
foreach mod $mod_list { |
| 237 |
# save tags associated to module (like auto-loaded tag) |
| 238 |
if {[info exists tag_arr($mod)] && [llength $tag_arr($mod)]} { |
| 239 |
set opt "--tag=[join $tag_arr($mod) :] " |
| 240 |
} else { |
| 241 |
set opt {} |
| 242 |
} |
| 243 |
# no need to specifically enclose module specification if space char |
| 244 |
# used in it as $mod is a list so elements including space will be |
| 245 |
# automatically enclosed |
| 246 |
append content "$modcmd load $opt$mod" \n |
| 247 |
} |
| 248 |
|
| 249 |
# prepend header if defined and some content has been generated |
| 250 |
if {[string length $header] && [string length $content]} { |
| 251 |
set content "$header\n$content" |
| 252 |
} |
| 253 |
|
| 254 |
return $content |
| 255 |
} |
| 256 |
|
| 257 |
# read given collection file and return the path and module lists it defines |
| 258 |
proc readCollectionContent {collfile colldesc} { |
| 259 |
# read file |
| 260 |
if {[catch { |
| 261 |
set fdata [split [readFile $collfile] \n] |
| 262 |
} errMsg ]} { |
| 263 |
reportErrorAndExit "Collection $colldesc cannot be read.\n$errMsg" |
| 264 |
} |
| 265 |
|
| 266 |
return [parseCollectionContent $fdata] |
| 267 |
} |
| 268 |
|
| 269 |
proc parseCollectionContent {fdata} { |
| 270 |
# init lists (maybe coll does not set mod to load) |
| 271 |
set path_list {} |
| 272 |
set mod_list {} |
| 273 |
set nuasked_list {} |
| 274 |
array set tag_arr {} |
| 275 |
|
| 276 |
# analyze collection content |
| 277 |
foreach fline $fdata { |
| 278 |
if {[regexp {module use (.*)$} $fline match patharg]} { |
| 279 |
# paths are appended by default |
| 280 |
set stuff_path append |
| 281 |
# manage multiple paths and path options specified on single line, |
| 282 |
# for instance "module use --append path1 path2 path3", with list |
| 283 |
# representation of patharg (which handles quoted elements containing |
| 284 |
# space in their name) |
| 285 |
foreach path $patharg { |
| 286 |
# following path is asked to be appended |
| 287 |
if {($path eq {--append}) || ($path eq {-a})\ |
| 288 |
|| ($path eq {-append})} { |
| 289 |
set stuff_path append |
| 290 |
# following path is asked to be prepended |
| 291 |
# collection generated with 'save' does not prepend |
| 292 |
} elseif {($path eq {--prepend}) || ($path eq {-p})\ |
| 293 |
|| ($path eq {-prepend})} { |
| 294 |
set stuff_path prepend |
| 295 |
} else { |
| 296 |
# ensure given path is absolute to be able to correctly |
| 297 |
# compare with paths registered in MODULEPATH |
| 298 |
set path [getAbsolutePath $path] |
| 299 |
# add path to end of list |
| 300 |
if {$stuff_path eq {append}} { |
| 301 |
lappend path_list $path |
| 302 |
# insert path to first position |
| 303 |
} else { |
| 304 |
lprepend path_list $path |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} elseif {[regexp {module load (.*)$} $fline match modarg]} { |
| 309 |
# extract collection-specific flags from module specification |
| 310 |
switch -glob -- [lindex $modarg 0] { |
| 311 |
--notuasked { |
| 312 |
set tag_list [list auto-loaded] |
| 313 |
set cleanlist [lrange $modarg 1 end] |
| 314 |
} |
| 315 |
--tag=* { |
| 316 |
set tag_list [split [string range [lindex $modarg 0] 6 end] :] |
| 317 |
set cleanlist [lrange $modarg 1 end] |
| 318 |
} |
| 319 |
default { |
| 320 |
set tag_list {} |
| 321 |
set cleanlist $modarg |
| 322 |
} |
| 323 |
} |
| 324 |
# parse module specification to distinguish between module + variant |
| 325 |
# specified and multiple modules specified on a single line |
| 326 |
set parsedlist [parseModuleSpecification 0 0 0 0 {*}$cleanlist] |
| 327 |
foreach parsed $parsedlist { |
| 328 |
set tag_arr($parsed) $tag_list |
| 329 |
} |
| 330 |
lappend mod_list {*}$parsedlist |
| 331 |
} |
| 332 |
} |
| 333 |
return [list $path_list $mod_list [array get tag_arr]] |
| 334 |
} |
| 335 |
|
| 336 |
# return specified collection content and differences compared to currently |
| 337 |
# defined environment |
| 338 |
proc getDiffBetweenCurEnvAndColl {collfile colldesc} { |
| 339 |
# read specific __init__ collection from __MODULES_LMINIT env var |
| 340 |
if {$collfile eq {__init__}} { |
| 341 |
lassign [parseCollectionContent [getEnvLoadedModulePropertyParsedList\ |
| 342 |
init]] coll_path_list coll_mod_list coll_tag_arrser |
| 343 |
} else { |
| 344 |
lassign [readCollectionContent $collfile $colldesc] coll_path_list\ |
| 345 |
coll_mod_list coll_tag_arrser |
| 346 |
} |
| 347 |
|
| 348 |
# build list of module tagged auto-loaded in collection |
| 349 |
array set coll_tag_arr $coll_tag_arrser |
| 350 |
set coll_nuasked_list {} |
| 351 |
foreach mod [array names coll_tag_arr] { |
| 352 |
if {{auto-loaded} in $coll_tag_arr($mod)} { |
| 353 |
lappend coll_nuasked_list $mod |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
# collection should at least define a path or a mod, but initial env may be |
| 358 |
# totally empty |
| 359 |
if {$collfile ne {__init__} && ![llength $coll_path_list] && ![llength\ |
| 360 |
$coll_mod_list]} { |
| 361 |
reportErrorAndExit "$colldesc is not a valid collection" |
| 362 |
} |
| 363 |
|
| 364 |
# load tags from loaded modules |
| 365 |
cacheCurrentModules |
| 366 |
|
| 367 |
defineModEqProc [isIcase] [getConf extended_default] |
| 368 |
|
| 369 |
# fetch what is currently loaded |
| 370 |
set curr_path_list [getModulePathList returnempty 0] |
| 371 |
# get current loaded module list |
| 372 |
set curr_mod_list [getEnvLoadedModulePropertyParsedList name] |
| 373 |
set curr_nuasked_list [getTaggedLoadedModuleList auto-loaded] |
| 374 |
# get current save tags of loaded modules |
| 375 |
array set curr_tag_arr [getLoadedModuleWithVariantSaveTagArrayList] |
| 376 |
|
| 377 |
# determine what module to unload to restore collection from current |
| 378 |
# situation with preservation of the load order (asking for a modeq |
| 379 |
# comparison will help to check against simplified mod name and variants) |
| 380 |
lassign [getMovementBetweenList $curr_mod_list $coll_mod_list\ |
| 381 |
$curr_nuasked_list $coll_nuasked_list modeq] mod_to_unload mod_to_load |
| 382 |
|
| 383 |
# proceed as well for modulepath |
| 384 |
lassign [getMovementBetweenList $curr_path_list $coll_path_list] \ |
| 385 |
path_to_unuse path_to_use |
| 386 |
|
| 387 |
# indicate if loaded modules that matches modules in collection have |
| 388 |
# different tags set |
| 389 |
if {![llength $mod_to_load]} { |
| 390 |
# consider a not-set entry as an empty element when comparing collection |
| 391 |
# and current environment tags. compare tags as unordered lists |
| 392 |
lassign [getDiffBetweenArray curr_tag_arr coll_tag_arr 1 1] notincoll\ |
| 393 |
diff notincurr |
| 394 |
set is_tags_diff [llength $diff] |
| 395 |
# if some module from collection are not yet loaded, consider there is a |
| 396 |
# difference |
| 397 |
} else { |
| 398 |
set is_tags_diff 1 |
| 399 |
} |
| 400 |
|
| 401 |
return [list $coll_path_list $coll_mod_list $coll_tag_arrser\ |
| 402 |
$coll_nuasked_list $mod_to_unload $mod_to_load $path_to_unuse\ |
| 403 |
$path_to_use $is_tags_diff] |
| 404 |
} |
| 405 |
|
| 406 |
proc getCollectionFromStash {stash} { |
| 407 |
if {[string match stash-* $stash]} { |
| 408 |
set coll $stash |
| 409 |
} elseif {[string is integer -strict $stash]} { |
| 410 |
# filter collection from other target (especially if no target set) |
| 411 |
set collfile [lindex [lsort -decreasing [findCollections stash-* glob\ |
| 412 |
0 0 1 1]] $stash] |
| 413 |
if {$collfile eq {}} { |
| 414 |
knerror "Invalid stash index '$stash'" |
| 415 |
} |
| 416 |
# extract collection name (without path and target extension) |
| 417 |
set coll [file rootname [file tail $collfile]] |
| 418 |
} else { |
| 419 |
knerror "Invalid stash collection name '$stash'" |
| 420 |
} |
| 421 |
|
| 422 |
return $coll |
| 423 |
} |
| 424 |
|
| 425 |
# ;;; Local Variables: *** |
| 426 |
# ;;; mode:tcl *** |
| 427 |
# ;;; End: *** |
| 428 |
# vim:set tabstop=3 shiftwidth=3 expandtab autoindent: |