-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfml_fun.sh
More file actions
1176 lines (1032 loc) · 42.1 KB
/
fml_fun.sh
File metadata and controls
1176 lines (1032 loc) · 42.1 KB
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
#####################
# Bash function definitions for FastModLoad
# These functions (and lua 'pseudofunctions' encoded by bash variables) are visible only to fml.sh
######################
##################
# lua script for extracting the list of module files, in the correct build order.
# This obtains a list of lua modulefiles from an lmod-style module table (lua code)
# Used to get 'ordered_module_list'
##################
process_collection_lua_script='
for key, subTable in pairs(_ModuleTable_.mT) do
if type(subTable) == "table" and subTable.fn then
print(subTable.loadOrder, subTable.fn)
end
end '
##################
# lua script for getting the ordered list of module *names* from
# the lmod-style module table (lua code)
##################
module_names_from_mt_lua_script='
for key, subTable in pairs(_ModuleTable_.mT) do
if type(subTable) == "table" and subTable.fullName then
print(subTable.loadOrder, subTable.fullName, subTable.stackDepth)
end
end '
##################
# Bash command to save file info, including the full contents, size, and date,
# for the set of lua modulefiles that defines a shortcut; this is used
# to test if any of them changed, meaning the shortcut should be rebuilt.
# Bash code is saved in string form to be executed later.
# When executed, it will require that the list of module files, $ordered_module_list, be set already.
##################
build_lua_record="stat -c '%y'"' ${ordered_module_list[@]}; cat ${ordered_module_list[@]}'
######################
# __fml_module() : the main fast module loading function
# Output of consists of printed commands that will be executed
# by the calling shell (bash) process.
######################
function __fml_module() {
local fml_source_modfile_local
local fmlglobal
local fmldebug
local load_arguments
local old_fml_info
local old_fml_name
local old_fml_modfile
local status
local fml_skip
local ordered_module_list
local fml_info
local fml_name
local fml_modfile
local fml_filename
local requested_fml_name
local update_needed
local first_mod_spec
######################
# Read function inputs
######################
fml_source_modfile_local="$1"
shift
######################
# Input flags that change the behavior
######################
fmlglobal=''
fmldebug=''
while [[ "${1:-}" == '--fmldebug' ]] ; do
case "$1" in
--fmldebug)
shift
;;
esac
done
######################
# Do a modified module listing if a fast module is loaded
######################
if [[ " $@ " == *" list "* ]] ; then
terselist=$(module --terse list 2>&1 )
fml_name=$( echo "${terselist}" | awk '$0 ~ "^fml[-]" {print(substr($1, 5, length($1)-4))}' )
if [[ -n ${fml_name} ]] ; then
list_file=
tmp1=${fml_global_prebuilds_dir}/${fml_name}/fml-${fml_name}.list
tmp2=${fml_prebuilds_dir}/${fml_name}/fml-${fml_name}.list
if [[ -f "${tmp1}" ]] ; then
list_file="${tmp1}"
elif [[ -f "${tmp2}" ]] ; then
list_file="${tmp2}"
fi
if [[ -n "${list_file}" ]] ; then
cat <<EOF
cat "${list_file%.list}.fancy_list"
# | awk '/Currently Loaded Modules:/ {getline; printing=1} printing == 1'
echo "#####################################################"
echo "FastModLoad Emulated Environment:"
echo " fml-${fml_name}"
echo "#####################################################"
echo ''
echo "Type 'fml' to disable this Fast Module and restore the original Lmod environment"
echo "Type 'fml --off' or 'ml --force -fml' to turn off Fast Module Loading altogether"
echo "Type 'module --lmod list' to peek at the current (true) Lmod environment"
echo ''
EOF
else
# New bash signaling approach:
# return 64
module list
fi
return
fi
fi
######################
# If "reset" is requested, reload fml after
######################
if [[ " $@ " == *" reset "* ]] ; then
__fml_reset --quiet "${fml_source_modfile_local}" reset
return
fi
######################
# If no load requested, or if fast module loading is turned off in the config file,
# pass the command through to Lmod
######################
first_load_arg=$(echo "$@" | awk '$1=="load" || $1=="unload" {gsub("/", "_", $2); if($2!="") print $2; exit}')
fml_active=$( awk '
$1 == "active" {active=$2}
END {
if(active == "on")
print("on")
else
print("off")
}' ~/.config/fml/config )
if [[ -z ${first_load_arg} || "${fml_active}" == "off" ]] ; then
__lmod_module_execute "$@"
# New bash signaling approach:
# return 64
return
fi
######################
# Otherwise, potentially provide a snappy message
######################
if [[ -z $( module --terse list |& grep -v '^StdEnv$' |& grep -v '^fml[/]' ) \
&& -n $( ls ${fml_global_prebuilds_dir}/${first_load_arg}* \
${fml_prebuilds_dir}/${first_load_arg}* 2> /dev/null ) \
]] ; then
:
# echo "Fast Module Check: ${@:2} (use 'ml fml' to unpack the full environment)" >&2
fi
######################
# Set up fml load variables & check for errors
######################
echo '__fml_start=0 ; '
echo '__fml_end=0 ; '
load_arguments=( $(__fml_get_load_arguments "$@") )
# Check if we should just pass through to the Lmod module function (if not building a fast module)
if [[ -z "${load_arguments[@]}" ]] ; then
# If no load requested, pass the command through to Lmod
# New bash signaling approach:
# return 64
__lmod_module_execute "$@"
return
fi
# Get first requested module minus any version spec
first_mod_spec=$(echo ${load_arguments[0]} | awk '{sub("[/].*", "", $0) ; print($1)}')
# Skip to the Lmod module function if both the below conditions are true::
# (1) There are no loaded fast modules
# (2) There is no stored fast module corresponding to the module request
if [[ -z $( module --terse list |& grep '^fml[-]' ) \
&& -z $( ls ${fml_global_prebuilds_dir}/${first_mod_spec}_* \
${fml_prebuilds_dir}/${first_mod_spec}_* 2> /dev/null ) ]] ; then
# New bash signaling approach:
# return 64
__lmod_module_execute "$@"
return
fi
status=0
old_fml_info=( $(__fml_get_loaded_fml) ) || status=$?
# If old_fml_name returned an error code (nonzero) something went wrong (oooops)
if [[ "${status}" -ne 0 ]] ; then
echo 'ERROR: Corrupted fml environment :' >&2
module list
# New bash signaling approach:
# return 65
return
fi
old_fml_name="${old_fml_info[0]:-}"
old_fml_modfile="${old_fml_info[2]:-}"
# Check to be sure we are starting from a fresh environment (no other loaded modules or fast modules);
# otherwise there can be pathologies.
fml_skip=0
# If a fast module is loaded, we need to unpack it as ordinary Lmod modules
# and then load the requested modules the ordinary way (fml_skip=1)
if [[ -n "${old_fml_name}" && "${old_fml_name}" != '0' ]] ; then
fml_skip=1
# If the requested modules actually exist, unpack the existing fast module
# to get ready to load the new ones
status=0
fml_info=( $(__fml_get_load_info "${load_arguments[@]}" ) ) || status=$?
if [[ "$status" -eq '0' && "${#fml_info[@]}" -gt 0 ]] ; then
# For message printing, get the original list of user-requested modules
# -> note the last 2 lines in the awk script below (END clause) handle the edge
# case where for some reason the last listed module is not a user-requested one
# (we print it anyway)
ordered_module_list=( $( (cat ${old_fml_modfile%.lua}.mt ; \
echo "${module_names_from_mt_lua_script}" ) \
|& lua - | sort -n -k 1 \
| awk '{if($3 + 0 == 0) {
lastln=NR ;
if($2 != "StdEnv" && $2 !~ "^fml[/]" )
print $2 ;
}}
{arg2=$2}
END {if(NR != lastln && arg2 != "StdEnv" && arg2 !~ "^fml[/]")
print arg2}' ) )
__fml_unpack "${fml_source_modfile_local}" || status=$?
if [[ $? -ne 0 ]] ; then
echo 'echo "Warning: unable to restore the full lmod environment"'
# New bash signaling approach?
# echo "Warning: unable to restore the full lmod environment" >&2
# ???return 65???
echo 'return 1'
fi
else
# fall back to the original Lmod module code
__lmod_module_execute "$@"
# New bash signaling approach:
# return 64
return
fi
fi
# If ordinary Lmod modules are loaded, turn on fml_skip to skip fast module loading
if [[ ( "${old_fml_name}" == '0' || -n "${ordered_module_list[@]}" ) ]] ; then
fml_skip=1
# Get module list for printed output:
# -> note the hack in the awk script below (END clause) as above to handle
# the faulty YCRC R module
if [[ "${old_fml_name}" == '0' ]] ; then
# For message printing, get the original list of user-requested modules
# -> note the last 2 lines in the awk script below (END clause) handle the edge
# case where for some reason the last listed module is not a user-requested one
# (we print it anyway)
ordered_module_list=( $( ( module --mt ; \
echo "${module_names_from_mt_lua_script}" ) \
|& lua - | sort -n -k 1 \
| awk '{if($3 + 0 == 0) {
lastln=NR ;
if($2 != "StdEnv" && $2 !~ "^fml[/]" )
print $2 ;
}}
{arg2=$2}
END {if(NR != lastln && arg2 != "StdEnv" && arg2 !~ "^fml[/]")
print arg2}' ) )
fi
fi
status=0
fml_info=( $(__fml_get_load_info "${load_arguments[@]}" ) ) || status=$?
if [[ "$status" -ne 0 || "${fml_skip}" -ne 0 || "${#fml_info[@]}" -eq 0 ]] ; then
# Revert to lmod functions
# New bash signaling approach:
# return 64
__lmod_module_execute "$@"
return
fi
fml_filename_info=( $( __get_fml_filename ${fmlglobal} ${fml_info[@]} ) ) || status=$?
if [[ "$status" -ne 0 || ${#fml_filename_info[@]} -eq 3 ]] ; then
fml_filename="${fml_filename_info[0]}"
requested_fml_name="${fml_filename_info[1]}"
update_needed="${fml_filename_info[2]}"
else
fml_filename=''
requested_fml_name=''
update_needed=''
fi
# if xxxx.d exists, the fast module is disabled and we skip loading it
if [[ -d ${fml_prebuilds_dir}/${requested_fml_name}.d ]] ; then
# Revert to lmod functions
# New bash signaling approach:
# return 64
__lmod_module_execute "$@"
return
fi
######################
# Perform specialized load/unloading actions
######################
# Load the fast module if it exists.
if [[ -f "${fml_filename}" && "${update_needed}" -eq '0' ]] ; then
echo "Fast Module Load: fml-${requested_fml_name}" >&2
__lmod_module_execute "use $(dirname ${fml_filename})"
# Load the fast module but suppress the output; no need to reprint the
# Fast Module Loading activated message every time
__lmod_module_execute "load fml-${requested_fml_name} > /dev/null 2>&1 "
echo 'if [[ $? -eq 0 ]] ; then '
echo " if [[ -f ${fml_filename%.lua}.out ]] ; then cat ${fml_filename%.lua}.out ; fi ; "
echo 'else '
echo echo "Fast Module failed to load: fml-${requested_fml_name}"
echo echo "Falling back to Lmod:"
echo echo "module $@"
__lmod_module_execute "$@"
echo 'fi ; '
else
# Request a module load, also recording the output, load time and exit status
if [[ "${update_needed}" -eq '0' ]] ; then
:
# echo 'Fast module check:'" ${load_arguments[@]}" >&2
fi
# Ensure the directory exists? Should not be necessary for a fast module load or rebuild
echo "mkdir -p $(dirname ${fml_filename} ) ; "
if [[ "${update_needed}" -eq '1' ]] ; then
echo echo 'Fast Module Update: '"fml-${requested_fml_name}"
fi
echo '__fml_start=$(date +%s) ; '
__lmod_module_execute "$@ >& ${fml_filename%.lua}.out_tmp"
echo "awk '/Lmod Warning/ {printing=1} printing == 1' ${fml_filename%.lua}.out_tmp > ${fml_filename%.lua}.out"
echo "/bin/rm ${fml_filename%.lua}.out_tmp"
echo '__fml_end=$(date +%s) ; '
echo "cat ${fml_filename%.lua}.out ; "
# Rebuild the module, if needed
if [[ "${update_needed}" -eq '1' ]] ; then
echo '__fml_start=0 ; '
echo '__fml_end=0 ; '
__fml_build "${fml_source_modfile_local}" "${requested_fml_name}" "${fml_filename}"
# cat <<EOF
# eval "\$(bash ${fml_base_dir}/fml.sh ${fml_source_modfile_local} build
# EOF
fi
fi
}
######################
# __fml() : generate fast modules
# As with __fml_module(), output of consists of printed commands that will be executed
# by the calling shell (bash) process.
######################
function __fml() {
local fml_source_modfile_local
local fmlglobal
local fmldebug
local load_arguments
local old_fml_info
local old_fml_name
local old_fml_modfile
local status
local fml_skip
local ordered_module_list
local fml_info
local fml_name
local fml_modfile
local fml_filename
local requested_fml_name
local update_needed
local first_mod_spec
fml_source_modfile_local="$1"
shift
######################
# Input flags that change the behavior
######################
fmlglobal=''
fmldebug=''
while [[ "${1:-}" == '--global' || "${1:-}" == '--fmldebug' ]] ; do
case "$1" in
--global)
fmlglobal='--global'
shift
;;
--fmldebug)
shift
;;
esac
done
######################
# Set up fml load variables & check for errors
######################
load_arguments=( $(__fml_get_load_arguments "$@") )
status=0
old_fml_info=( $(__fml_get_loaded_fml) ) || status=$?
# If old_fml_name returned an error code (integer less than 0) something went wrong (oooops)
if [[ "${status}" -ne 0 ]] ; then
echo 'ERROR: Corrupted fml environment :' >&2
module list
echo "Please do 'module reset' to recover" >&2
return
fi
old_fml_name="${old_fml_info[0]:-}"
old_fml_modfile="${old_fml_info[2]:-}"
# Check to be sure we are starting from a fresh environment (no other loaded modules or fast modules);
# otherwise there can be pathologies.
local autofml
autofml=0
if [[ "${old_fml_name}" == '0' ]] ; then
# Above is true if a Fast Module is not loaded AND ordinary modules are present/loaded
if [[ "${#load_arguments[@]}" -eq 0 ]] ; then
# If the user types fml with no arguments, trigger 'autofml'
# to build a Fast Module from the current environment
autofml=1
else
# User requested another module load on top of existing loaded modules
# (not allowed for now)
echo echo "Modules are already loaded. Please use 'fml' by itself or do 'module reset' first."
return
fi
else
if [[ "${#load_arguments[@]}" -eq 0 && -z "${old_fml_name}" ]] ; then
# No arguments to fml, and no Fast Module or other modules are loaded:
echo 'fml --help'
return
fi
if [[ -n "${old_fml_name}" ]] ; then
if [[ "${#load_arguments[@]}" -eq 0 ]] ; then
# If a Fast Module is loaded, unpack it and disable the fast module by
# creating xxxx.d in the user's .config/fml/fml_prebuilds folder
# echo "echo 'Unpacking the module environment for fml-'${old_fml_name}"
status=0
__fml_unpack "${fml_source_modfile_local}" || status=$?
if [[ $status -eq 0 ]] ; then
echo "mkdir -p ${fml_prebuilds_dir}/${old_fml_name}.d ; "
# cat <<'EOF'
# echo ' (__)' ;
# echo ' (@@)' ;
# echo ' /##--##-\#)' ;
# echo ' / ### # | ' ;
# echo " * ||ww--|| " ;
# echo ' ^^ ^^ ' ;
# echo '' ;
# EOF
echo "echo Fast Module disabled: fml-${old_fml_name}"
echo "echo Type \'fml\' again to re-enable"
fi
return $status
else
echo echo "Modules are already loaded. Please use 'fml' by itself or do 'module reset' first."
echo 'fml --help'
return
fi
fi
fi
status=0
if [[ ${autofml} -eq 0 ]] ; then
fml_info=( $(__fml_get_load_info "${load_arguments[@]}" ) ) || status=$?
else
# Get the original list of user-requested modules
# -> note the last 2 lines in the awk script below (END clause) handle the edge
# case where for some reason the last listed module is not a user-requested one
# (we will use it anyway)
load_arguments=( $( ( module --mt ; \
echo "${module_names_from_mt_lua_script}" ) \
|& lua - | sort -n -k 1 \
| awk '{if($3 + 0 == 0) {
lastln=NR ;
if($2 != "StdEnv" && $2 !~ "^fml[/]" )
print $2 ;
}}
{arg2=$2}
END {if(NR != lastln && arg2 != "StdEnv" && arg2 !~ "^fml[/]")
print arg2}' ) )
fml_info=( $(__fml_get_load_info --slow ${load_arguments[@]} ) ) || status=$?
fi
if [[ "${#fml_info[@]}" -eq 0 || "$status" -ne '0' ]] ; then
echo 'fml --help'
return
fi
status=0
fml_filename_info=( $( __get_fml_filename ${fmlglobal} ${fml_info[@]} ) ) || status=$?
if [[ "$status" -ne 0 || ${#fml_filename_info[@]} -eq 3 ]] ; then
fml_filename="${fml_filename_info[0]}"
requested_fml_name="${fml_filename_info[1]}"
update_needed="${fml_filename_info[2]}"
else
fml_filename=''
requested_fml_name=''
update_needed=''
fi
######################
# Perform specialized load/unloading actions
######################
# Load the fast module if it exists.
if [[ -f "${fml_filename}" && "${update_needed}" -eq '0' ]] ; then
if [[ ${autofml} -eq 1 ]] ; then
# echo echo "Fast module already exists and is up to date: fml-${requested_fml_name}"
__fml_reset --quiet "${fml_source_modfile_local}" reset
fi
echo "Fast Module Load: fml-${requested_fml_name}" >&2
__lmod_module_execute "use $(dirname ${fml_filename})"
__lmod_module_execute "load fml-${requested_fml_name} > /dev/null 2>&1"
# If the load was successful, ensure the fast module is now active by
# removing any xxxx.d in the user's .config/fml/fml_prebuilds folder ;
# and print the saved output message (if any)
echo 'if [[ $? -eq 0 ]] ; then '
echo " if [[ -d ${fml_prebuilds_dir}/${requested_fml_name}.d ]] ; then "
echo " rmdir ${fml_prebuilds_dir}/${requested_fml_name}.d ; "
echo ' fi'
echo " if [[ -f ${fml_filename%.lua}.out ]] ; then cat ${fml_filename%.lua}.out ; fi ; "
echo 'else '
echo " echo Fast Module failed to load: fml-${requested_fml_name}"
echo " echo Falling back to Lmod:"
if [[ ${autofml} -eq 1 ]] ; then
echo " echo module load ${load_arguments[@]}"
__lmod_module_execute load "${load_arguments[@]}"
else
echo " echo module $@"
__lmod_module_execute "$@"
fi
echo 'fi ; '
else
# Request a module load, also recording the output, load time and exit status
# Make the fast module directory, if it doesn't already exist, to capture the output
# of the below 'module load' command:
mkdir -p $(dirname ${fml_filename} )
echo echo 'Fast Module Build: '"fml-${requested_fml_name}"
if [[ ${autofml} -ne 1 ]] ; then
echo '__fml_start=$(date +%s)'
__lmod_module_execute "$@ >& ${fml_filename%.lua}.out_tmp"
echo "awk '/Lmod Warning/ {printing=1} printing == 1' ${fml_filename%.lua}.out_tmp > ${fml_filename%.lua}.out"
echo "/bin/rm ${fml_filename%.lua}.out_tmp"
echo '__fml_end=$(date +%s)'
echo "cat ${fml_filename%.lua}.out ; "
fi
# Build the module
__fml_build "${fml_source_modfile_local}" "${requested_fml_name}" "${fml_filename}"
cat <<EOF
# eval "\$(bash ${fml_base_dir}/fml.sh ${fml_source_modfile_local} build ${requested_fml_name} ${fml_filename})"
echo "Complete. To rapidly load this environment in the future, do:"
echo " module reset ; module load ${load_arguments[@]}"
EOF
# Ensure the fast module will be active going forwards, by
# removing any xxxx.d in the user's .config/fml/fml_prebuilds folder
echo "if [[ -d ${fml_prebuilds_dir}/${requested_fml_name}.d ]] ; then "
echo " rmdir ${fml_prebuilds_dir}/${requested_fml_name}.d ; "
echo 'fi'
fi
}
######################
# Execute the original lmod 'module' function ; use --lmod if it has been
# embedded in the 'hijacked' module function
######################
function __lmod_module_execute() {
[[ -z "$@" ]] && return
cat <<EOF
if [[ -z \$( declare -f module | grep fml ) ]] ; then
module $@ ;
else
module --lmod $@ ;
fi ;
EOF
}
######################
# Perform a 'module purge' or 'module reset', then reload fml after
######################
function __fml_reset() {
local fml_source_modfile_local
local fml_path
local fml_version
local func
local quiet
quiet=0
if [[ "$1" == "--quiet" ]] ; then
quiet=1
shift
fi
fml_source_modfile_local="$1"
fml_source_modfile_local="${fml_source_modfile_local%.lua}"
fml_path=$(dirname $(dirname "${fml_source_modfile_local}"))
fml_name=$(basename $(dirname "${fml_source_modfile_local}"))
fml_version=$(basename "${fml_source_modfile_local}")
shift
if [[ $# -lt 1 ]] ; then
func=reset
else
func="$@"
fi
# echo module --lmod reset
# Perform the reset or purge command:
if [[ ${quiet} -eq 1 ]] ; then
__lmod_module_execute "${func} > /dev/null 2>&1"
else
__lmod_module_execute "${func} 2> /dev/null"
fi
# After fml is unloaded, we need to use the original 'module' commands to reload fml:
echo 'if [[ ":\$MODULEPATH:" != *":'${fml_path}'":* ]] ; then '
__lmod_module_execute "use ${fml_path}"
echo 'fi ; '
if [[ ${quiet} -eq 1 ]] ; then
__lmod_module_execute "load ${fml_name}/${fml_version} > /dev/null 2>&1"
else
__lmod_module_execute "load ${fml_name}/${fml_version}"
fi
}
######################
# Get the filename associated with a 'fml module' ; it does not necessarily need to exist yet
# '--global' option maps to within the fml_prebuilds directory where fml.sh is located ; otherwise
# it maps to within ~/.config/fml/fml_prebuilds
######################
function __get_fml_filename() {
local fml_info
local requested_fml_name
local requested_modfiles
local fml1_global
local fml2_user
local update_needed
local suffix
local fml_filename
local ordered_module_list
local fmlglobal
fmlglobal=
if [[ "${1:-}" == "--global" ]] ; then
fmlglobal=1
shift
fi
fml_info=( "$@" )
requested_fml_name="${fml_info[0]}"
requested_modfiles="${fml_info[@]:1}"
fml1_global="${requested_fml_name}/fml-${requested_fml_name}.lua"
fml2_user="${fml_prebuilds_dir}/${requested_fml_name}/fml-${requested_fml_name}.lua"
fml_filename=''
update_needed='0'
for fml_dir in ${fml_global_prebuilds_dir} ${fml_prebuilds_dir} ; do
suffix=''
# Initialize fml_filename for the while loop:
fml_filename="${fml_dir}/${requested_fml_name}${suffix}/fml-${requested_fml_name}.lua"
# Loop through all alternative fast module versions with suffixes ___1, ___2, ...
while [[ -f "${fml_filename}" ]] ; do
# Double check if the module is up to date
if [[ -f ${fml_filename%.lua}.mt && -f ${fml_filename%.lua}.lua_record ]] ; then
ordered_module_list=( $( (cat ${fml_filename%.lua}.mt ; \
echo "${process_collection_lua_script}" ) \
|& lua - | sort -n -k 1 \
| awk '{n=split($2, a, "/") ;
if(a[n] != "StdEnv.lua" && a[n-1] != "fml") {
print $2
}}' ) )
stat "${ordered_module_list[@]}" &>/dev/null \
&& eval ${build_lua_record} | cmp ${fml_filename%.lua}.lua_record >& /dev/null
update_needed=$?
else
update_needed=1
fi
if [[ "${update_needed}" -eq '0' ]] ; then
# If we found a good file, we're done
break
else
if [[ -z ${suffix} ]] ; then
suffix='___1'
else
suffix=$(echo ${suffix} | awk '{print("___" substr($1, 4, length($1)-3) + 1) }')
fi
# echo 'Fast module seems to be out of date: ' "${fml_filename}" >&2
# echo ' -> next up: ' "${requested_fml_name}${suffix}" >&2
fi
# Update fml_filename for the while loop:
fml_filename="${fml_dir}/${requested_fml_name}${suffix}/fml-${requested_fml_name}.lua"
done
# If we found a good file, break out of the outer loop
if [[ -f "${fml_filename}" && "${update_needed}" -eq '0' ]] ; then
break
fi
# Skip the second pass of this loop ($fml_basename == $fml2_user) if we are doing a global install.
# This means we are going to rebuild the global fastmodule
if [[ -n "${fmlglobal}" ]] ; then
printf 'Global ' >&2
break
fi
done
echo "${fml_filename}"
echo "${requested_fml_name}"
echo "${update_needed}"
}
######################
# Build the Fast Modules: this function is optionally called at the end of every
# 'module load' request, capturing the current module environment if
# needed. No 'module' commands are invoked or requested by this function.
######################
function __fml_build() {
local fml_source_modfile_local
local mod_name
local mod_filename
local tmpfile1
local tmpfile2
local tmpfile3
local ordered_module_list
fml_source_modfile_local="$1"
shift
if [[ $# -lt 2 ]] ; then
return
fi
mod_name="$1"
mod_filename="$2"
# Save the module listing
module --terse --redirect list > ${mod_filename%.lua}.list
# Fanciful way to preserve colors (use 'less -R' to reproduce)
LMOD_PAGER=none script -q -c "ml list" ${mod_filename%.lua}.list_tmp > /dev/null
if [[ $? -eq 0 ]] ; then
sed '1d; $d; s/\r//g' ${mod_filename%.lua}.list_tmp > ${mod_filename%.lua}.fancy_list
else
LMOD_PAGER=none ml list 2> ${mod_filename%.lua}.fancy_list
fi
/bin/rm ${mod_filename%.lua}.list_tmp >& /dev/null
mkdir -p $(dirname "${mod_filename}")
# tmpfile1=$( mktemp -p $(dirname "${mod_filename}") )
mkdir -p ~/.config/lmod
tmpfile1=$( mktemp ~/.config/lmod/fmltmpXXXXXXXXXX)
tmpfile2=$( mktemp -p $(dirname "${mod_filename}") )
tmpfile3=$( mktemp -p $(dirname "${mod_filename}") )
module --mt >& "${tmpfile1}"
ordered_module_list=( $( (module --mt ; echo "${process_collection_lua_script}" ) |&lua - | sort -n -k 1 | awk '{n=split($2, a, "/") ; if(a[n] != "StdEnv.lua" && a[n-1] != "fml") {print $2}}' ) )
stat "${ordered_module_list[@]}" &>/dev/null \
&& eval "$build_lua_record" > "${tmpfile3}"
##################
# Concatenate all the .lua files required by this collection,
# but strip out the 'depends_on' statements.
#
# This is predicated on 'module save' having generated a complete, self-consistent
# list of modules, with a defined build order that we will use when loading.
# (ordered_module_list is sorted on the build order).
#
# Each .lua code is wrapped by a 'do...end' statement to preserve
# independence of the local variables
##################
printf '' > "${tmpfile2}"
for m in ${ordered_module_list[@]}; do
echo "do -- Scope for $m"
# Skip all valid lua depends_on() statements, including with comments appended
grep -E -v '^[[:space:]]*depends_on\([[:space:]]*"[^"]*"[[:space:]]*\)[[:space:]]*(--.*)?$' "$m"
echo "end -- End scope for $m"
done >> "${tmpfile2}"
# In case there were previous versions present, possibly being written by someone else:
# make the updates atomic using the tmpfiles:
/bin/mv "${tmpfile1}" "${mod_filename%.lua}.mt"
/bin/mv "${tmpfile2}" "${mod_filename}"
/bin/mv "${tmpfile3}" "${mod_filename%.lua}".lua_record
# Loosen the highly restrictive permissions inherited from the original tmpfiles
# Files and folder are group writeable so all group members can use the --global option
# for Fast Module building
chmod -R a+r $(dirname "${mod_filename}")
chmod -R ug+rw $(dirname "${mod_filename}")
# Now replace the slow-loading environment with the fast module
if [[ "${fml_source_modfile_local}" != '-' ]] ; then
__fml_reset --quiet "${fml_source_modfile_local}" purge
else
__lmod_module_execute purge
fi
# Restore fml
__lmod_module_execute "use $(dirname ${mod_filename})"
__lmod_module_execute "load $(basename ${mod_filename%.lua})"
echo 'if [[ $? -ne 0 ]] ; then '
echo " echo Fast Module failed to load: fml-${mod_name} ; "
echo " echo Falling back to Lmod ; "
__lmod_module_execute "$@"
echo 'else '
echo ' : '
# cat<<"EOF"
# echo '' ;
# echo ' (~~) ' ;
# echo ' <(@@) ' ;
# echo ' *---##--##-\#) ' ;
# echo ' |## # |_ ' ;
# echo ' ;_//ww---- \\ ' ;
# echo ' ^^' ;
# echo '' ;
# EOF
echo 'fi ; '
# cat "${mod_filename%.lua}.out"
}
######################
# Replace a loaded fml module with the corresponding original lmod environment
######################
function __fml_unpack() {
local fml_source_modfile_local
local fml_path
local fml_name
local fml_version
local nofml
local status
local fml_info
local mt_file
local tmpfile
nofml=0
if [[ $# -gt 0 && "$1" == "--nofml" ]] ; then
nofml=1
shift
fi
fml_source_modfile_local="$1"
fml_source_modfile_local="${fml_source_modfile_local%.lua}"
shift
status=0
if [[ $# -gt 0 ]] ; then
fml_file="$1"
else
# with no arguments given, get the loaded fast module if present
fml_info=( $(__fml_get_loaded_fml) ) || status=$?
# If no fast module present, there is nothing to do
if [[ $status -ne 0 || ${#fml_info[@]} -lt 3 ]] ; then
return
fi
fml_modname=${fml_info[0]}
fml_modfile=${fml_info[2]}
# if [[ -n "${fml_name}" && "${fml_info[0]}" != '0' ]] ; then # fast modules present
# module unload fml-${fml_name}
# fi
fi
# echo "echo 'Unpacking the module environment for fml-${fml_modname} (control-C to exit)' ; "
mt_file="${fml_modfile%.lua}.mt"
# Create a unique temporary file
mkdir -p ~/.config/lmod
tmpfile=$( mktemp -p ~/.config/lmod fmlXXXXXXXXXX )
/bin/cp "${mt_file}" "${tmpfile}"
__lmod_module_execute "restore '$(basename "${tmpfile}")' >& /dev/null"
echo "/bin/rm ${tmpfile} ; "
if [[ ${nofml} -eq 1 ]] ; then
fml_name=$(basename $(dirname "${fml_source_modfile_local}"))
fml_version=$(basename "${fml_source_modfile_local}")
__lmod_module_execute "--force unload ${fml_name}/${fml_version}"
fi
if [[ "${status}" -ne 0 ]] ; then
return "${status}"
fi
}
######################
# Parses arguments for the 'load'/'unload' keywords and prints everything afterwards.
# note, if 'unload' is used, results are prefixed with '-'
######################
function __fml_get_load_arguments() {
local load_arguments
load_arguments=( $(echo "$@" | awk '{
for(i=1; i<=NF; ++i) {
if(printargs)
args = args " " load_prefix $i;
if($i != "load" && $i != "unload" && $i !~ /^-/ && !load_cmd) {
exit
}
if( ($i == "load" || $i == "unload") && !load_cmd) {
load_cmd=1;
printargs=1;
if($i == "unload" && !load_cmd)
load_prefix="-" ;
}
if($i ~ "^-fml(|[/])")
printargs=0;
}
}
END {if(printargs) print(args)}') )
echo "${load_arguments[@]}"
}
######################
# Return info on whether a fast module if present.
######################
function __fml_get_loaded_fml() {
# Below: check if a fast module fml-xxx is loaded.
# Returns info on the fast module if present.
# Returns '0' if 'slow modules' are present instead.
# Returns '-1' if there was a problem
# Detected problems include:
# - multiple fast fml-xxx modules
local loaded_fml_name
status=0
loaded_fml_name=( $( (module --mt ; echo "${process_collection_lua_script}") \
|& lua - | sort -n -k 1 | awk '
{
n=split($2, a, "/");
if($2 ~ "[/]fml-.+lua$") {
if(fml)
{print(-2, $2, fml); finished=1 ; exit}
else
fml=substr(a[n],length("fml-")+1, length(a[n])-length("fml-")-length(".lua"));
fmldir=a[n-1];
fmlfile=$2;
} else {
if(a[n] != "StdEnv.lua" && a[n-1] != "fml") {
if(fml)
{print(-1, $2); finished=1 ; exit}
else
slowmod=1;
}
}
}