-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathpackage.gi
3552 lines (3223 loc) · 127 KB
/
package.gi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#############################################################################
##
## This file is part of GAP, a system for computational discrete algebra.
## This file's authors include Frank Celler, Alexander Hulpke.
##
## Copyright of GAP belongs to its developers, whose names are too numerous
## to list here. Please refer to the COPYRIGHT file for details.
##
## SPDX-License-Identifier: GPL-2.0-or-later
##
## This file contains support for ⪆ packages.
##
# recode string to GAPInfo.TermEncoding, assuming input is UTF-8 or latin1
# (if useful this may become documented for general use)
BindGlobal( "RecodeForCurrentTerminal", function( str )
local fun, u;
if IsBoundGlobal( "Unicode" ) and IsBoundGlobal( "Encode" ) then
# The GAPDoc package is completely loaded.
fun:= ValueGlobal( "Unicode" );
u:= fun( str, "UTF-8" );
if u = fail then
u:= fun( str, "ISO-8859-1");
fi;
if GAPInfo.TermEncoding <> "UTF-8" then
fun:= ValueGlobal( "SimplifiedUnicodeString" );
u:= fun( u, GAPInfo.TermEncoding );
fi;
fun:= ValueGlobal( "Encode" );
u:= fun( u, GAPInfo.TermEncoding );
return u;
else
# GAPDoc is not yet available, do nothing in this case.
return str;
fi;
end );
#############################################################################
##
#F CompareVersionNumbers( <supplied>, <required>[, "equal"] )
##
InstallGlobalFunction( CompareVersionNumbers, function( arg )
local s, r, inequal, i, j, a, b;
s:= arg[1];
r:= arg[2];
inequal:= not ( Length( arg ) = 3 and arg[3] = "equal" );
# Deal with the case of a `dev' version.
if 2 < Length( s )
and s{ [ Length( s ) - 2 .. Length( s ) ] } = "dev" then
return inequal or ( Length(r)>2 and r{[Length(r)-2..Length(r)]}="dev" );
elif 2 < Length( r )
and r{ [ Length( r ) - 2 .. Length( r ) ] } = "dev" then
return false;
fi;
while 0 < Length( s ) or 0 < Length( r ) do
# Remove leading non-digit characters.
i:= 1;
while i <= Length( s ) and not IsDigitChar( s[i] ) do
i:= i+1;
od;
s:= s{ [ i .. Length( s ) ] };
j:= 1;
while j <= Length( r ) and not IsDigitChar( r[j] ) do
j:= j+1;
od;
r:= r{ [ j .. Length( r ) ] };
# If one of the two strings is empty then we are done.
if Length( s ) = 0 then
return Length( r ) = 0;
elif Length( r ) = 0 then
return inequal;
fi;
# Compare the next portion of digit characters.
i:= 1;
while i <= Length( s ) and IsDigitChar( s[i] ) do
i:= i+1;
od;
a:= Int( s{ [ 1 .. i-1 ] } );
j:= 1;
while j <= Length( r ) and IsDigitChar( r[j] ) do
j:= j+1;
od;
b:= Int( r{ [ 1 .. j-1 ] } );
if a < b then
return false;
elif b < a then
return inequal;
fi;
s:= s{ [ i .. Length( s ) ] };
r:= r{ [ j .. Length( r ) ] };
od;
# The two remaining strings are empty.
return true;
end );
#############################################################################
##
#F PackageInfo( <pkgname> )
##
InstallGlobalFunction( PackageInfo, function( pkgname )
pkgname:= LowercaseString( pkgname );
if not IsBound( GAPInfo.PackagesInfo.( pkgname ) ) then
return [];
else
return GAPInfo.PackagesInfo.( pkgname );
fi;
end );
#############################################################################
##
#F RECORDS_FILE( <name> )
##
InstallGlobalFunction( RECORDS_FILE, function( name )
local str, rows, recs, pos, r;
str:= StringFile( name );
if str = fail then
return [];
fi;
rows:= SplitString( str, "", "\n" );
recs:= [];
for r in rows do
# remove comments starting with `#'
pos:= Position( r, '#' );
if pos <> fail then
r:= r{ [ 1 .. pos-1 ] };
fi;
Append( recs, SplitString( r, "", " \n\t\r" ) );
od;
return List( recs, LowercaseString );
end );
#############################################################################
##
#F SetPackageInfo( <record> )
##
InstallGlobalFunction( SetPackageInfo, function( record )
local rnam, info;
if IsHPCGAP then
info := rec();
for rnam in REC_NAMES(record) do
info.(rnam) := Immutable(record.(rnam));
od;
record := info;
fi;
GAPInfo.PackageInfoCurrent:= record;
end );
#############################################################################
##
#F FindPackageInfosInSubdirectories( pkgdir, name )
##
## Finds all PackageInfos in subdirectories of directory name in
## directory pkgdir, return a list of their paths.
##
BindGlobal( "FindPackageInfosInSubdirectories", function( pkgdir, name )
local pkgpath, file, files, subdir;
pkgpath:= Filename( [ pkgdir ], name );
# This can be 'fail' if 'name' is a void link.
if pkgpath = fail then
return [];
fi;
if not IsDirectoryPath( pkgpath ) then
return [];
fi;
if name in [ ".", ".." ] then
return [];
fi;
file:= Filename( [ pkgdir ],
Concatenation( name, "/PackageInfo.g" ) );
if file = fail then
files := [];
# Perhaps some subdirectories contain `PackageInfo.g' files.
for subdir in Set( DirectoryContents( pkgpath ) ) do
if not subdir in [ ".", ".." ] then
pkgpath:= Filename( [ pkgdir ],
Concatenation( name, "/", subdir ) );
if pkgpath <> fail and IsDirectoryPath( pkgpath )
and not subdir in [ ".", ".." ] then
file:= Filename( [ pkgdir ],
Concatenation( name, "/", subdir, "/PackageInfo.g" ) );
if file <> fail then
Add( files,
[ file, Concatenation( name, "/", subdir ) ] );
fi;
fi;
fi;
od;
else
files:= [ [ file, name ] ];
fi;
return files;
end );
#############################################################################
##
#F AddPackageInfo( files )
##
BindGlobal( "AddPackageInfos", function( files, pkgdir, ignore )
local file, record, pkgname, date, dd, mm;
for file in files do
# Read the `PackageInfo.g' file.
Unbind( GAPInfo.PackageInfoCurrent );
Read( file[1] );
if IsBound( GAPInfo.PackageInfoCurrent ) then
record:= GAPInfo.PackageInfoCurrent;
Unbind( GAPInfo.PackageInfoCurrent );
pkgname:= LowercaseString( record.PackageName );
NormalizeWhitespace( pkgname );
# Check whether GAP wants to reset loadability.
if IsBound( GAPInfo.PackagesRestrictions.( pkgname ) )
and GAPInfo.PackagesRestrictions.( pkgname ).OnInitialization(
record ) = false then
Add( GAPInfo.PackagesInfoRefuseLoad, record );
elif pkgname in ignore then
LogPackageLoadingMessage( PACKAGE_DEBUG,
Concatenation( "ignore package ", record.PackageName,
" (user preference PackagesToIgnore)" ), "GAP" );
else
record.InstallationPath:= Filename( [ pkgdir ], file[2] );
# normalize to include trailing "/"
record.InstallationPath:= Filename( [ Directory( record.InstallationPath ) ], "" );
if not IsBound( record.PackageDoc ) then
record.PackageDoc:= [];
elif IsRecord( record.PackageDoc ) then
record.PackageDoc:= [ record.PackageDoc ];
fi;
# Normalize the format of 'Date', i.e. if it is the format yyyy-mm-dd
# then we change it to dd/mm/yyyy. When other tools have adapted to
# the yyyy-mm-dd format we can normalize to that format and at some
# point in the future get rid of this code.
if Length(record.Date) = 10 and record.Date{[5,8]} = "--" then
date := List( SplitString( record.Date, "-" ), Int);
date := Permuted(date, (1,3)); # date = [dd,mm,yyyy]
# generate the day and month strings
# if the day has only one digit we have to add a 0
if date[1] < 10 then
dd := Concatenation("0", String(date[1]));
else
dd := String(date[1]);
fi;
# if the month has only one digit we have to add a 0
if date[2] < 10 then
mm := Concatenation("0", String(date[2]));
else
mm := String(date[2]);
fi;
record.Date := Concatenation(dd, "/", mm, "/", String(date[3]));
fi;
if IsHPCGAP then
# FIXME: we make the package info record immutable, to
# allow access from multiple threads; but that in turn
# can break packages, which rely on their package info
# record being readable (see issue #2568)
MakeImmutable(record);
fi;
Add( GAPInfo.PackagesInfo, record );
fi;
fi;
od;
end );
#############################################################################
##
#F InitializePackagesInfoRecords()
##
## In earlier versions, this function had an argument; now we ignore it.
##
InstallGlobalFunction( InitializePackagesInfoRecords, function( arg )
local pkgdirs, pkgdir, pkgdirstrs, ignore, name, file, files, record, r;
if IsBound( GAPInfo.PackagesInfoInitialized ) and
GAPInfo.PackagesInfoInitialized = true then
# This function has already been executed in this session.
return;
fi;
GAPInfo.LoadPackageLevel:= 0;
GAPInfo.PackagesInfo:= [];
GAPInfo.PackagesInfoRefuseLoad:= [];
LogPackageLoadingMessage( PACKAGE_DEBUG,
"entering InitializePackagesInfoRecords", "GAP" );
# the first time this is called, add the cmd line args to the list
if IsEmpty(GAPInfo.PackageDirectories) then
for pkgdirstrs in GAPInfo.CommandLineOptions.packagedirs do
pkgdirs:= List( SplitString( pkgdirstrs, ";" ), Directory );
for pkgdir in pkgdirs do
if not pkgdir in GAPInfo.PackageDirectories then
Add( GAPInfo.PackageDirectories, pkgdir );
fi;
od;
od;
fi;
# add any new pkg directories to the list
pkgdirs:= DirectoriesLibrary( "pkg" );
if pkgdirs <> fail then
pkgdirs:= Filtered( pkgdirs, dir -> not dir in GAPInfo.PackageDirectories );
if not IsEmpty(pkgdirs) then
Append( GAPInfo.PackageDirectories, pkgdirs );
fi;
fi;
if IsEmpty(GAPInfo.PackageDirectories) then
LogPackageLoadingMessage( PACKAGE_DEBUG,
"exit InitializePackagesInfoRecords (no pkg directories found)",
"GAP" );
GAPInfo.PackagesInfo:= AtomicRecord();
return;
fi;
if IsBound( GAPInfo.ExcludeFromAutoload ) then
# The function was called from `AutoloadPackages'.
# Collect the `NOAUTO' information in a global list,
# which will be used in `AutoloadPackages' for both packages to be
# loaded according to the user preference "PackagesToLoad"
# and suggested packages of needed packages.
# The component `GAPInfo.ExcludeFromAutoload' will be unbound after the
# call of `AutoloadPackages'.
GAPInfo.ExcludeFromAutoload:= Set(
UserPreference( "ExcludeFromAutoload" ), LowercaseString );
fi;
# Do not store information about packages in "PackagesToIgnore".
ignore:= List( UserPreference( "PackagesToIgnore" ), LowercaseString );
# Loop over the package directories,
# remove the packages listed in `NOAUTO' files from GAP's suggested
# packages, and unite the information for the directories.
for pkgdir in GAPInfo.PackageDirectories do
if IsBound( GAPInfo.ExcludeFromAutoload ) then
UniteSet( GAPInfo.ExcludeFromAutoload,
List( RECORDS_FILE( Filename( pkgdir, "NOAUTO" ) ),
LowercaseString ) );
fi;
# pkgdir may be a package instead of a package directory
file:= Filename( [ pkgdir ], "PackageInfo.g" );
if file <> fail then
AddPackageInfos( [ [ file, "" ] ], pkgdir, ignore );
else
# Loop over subdirectories of this package directory.
for name in Set( DirectoryContents( pkgdir ) ) do
## Get all package dirs
files := FindPackageInfosInSubdirectories( pkgdir, name );
AddPackageInfos( files, pkgdir, ignore );
od;
fi;
od;
# Sort the available info records by their version numbers.
# (Sort stably in order to make sure that an instance from the first
# possible root path gets chosen if the same version of a package
# is available in several root paths.
# Note that 'CompareVersionNumbers' returns 'true'
# if the two arguments are equal.)
StableSortParallel( List( GAPInfo.PackagesInfo, r -> r.Version ),
GAPInfo.PackagesInfo,
{ a, b } -> not CompareVersionNumbers( a, b, "equal" ) and CompareVersionNumbers( a, b ) );
# Turn the lists into records.
record:= rec();
for r in GAPInfo.PackagesInfo do
name:= LowercaseString( r.PackageName );
if IsBound( record.( name ) ) then
record.( name ) := Concatenation( record.( name ), [ r ] );
else
record.( name ):= [ r ];
fi;
if IsHPCGAP then
# FIXME: we make the package info record immutable, to
# allow access from multiple threads; but that in turn
# can break packages, which rely on their package info
# record being readable (see issue #2568)
MakeImmutable( record.( name ) );
fi;
od;
GAPInfo.PackagesInfo:= AtomicRecord(record);
GAPInfo.PackagesInfoInitialized:= true;
LogPackageLoadingMessage( PACKAGE_DEBUG,
"return from InitializePackagesInfoRecords", "GAP" );
end );
#############################################################################
##
#F LinearOrderByPartialWeakOrder( <pairs>, <weights> )
##
## The algorithm works with a directed graph
## whose vertices are subsets of the <M>c_i</M>
## and whose edges represent the given partial order.
## We start with one vertex for each <M>x_i</M> and each <M>y_i</M>
## from the input list, and draw an edge from <M>x_i</M> to <M>y_i</M>.
## Furthermore,
## we need a queue <M>Q</M> of the smallest vertices found up to now,
## and a stack <M>S</M> of the largest vertices found up to now;
## both <M>Q</M> and <M>S</M> are empty at the beginning.
## Now we add the vertices without predecessors to <M>Q</M> and remove the
## edges from these vertices until no more such vertex is found.
## Then we put the vertices without successors on <M>S</M> and remove the
## edges to these vertices until no more such vertex is found.
## If edges are left then each of them leads eventually into a cycle in the
## graph; we find a cycle and amalgamate it into a new vertex.
## Now we repeat the process until all edges have disappeared.
## Finally, the concatenation of <M>Q</M> and <M>S</M> gives us the sets
## <M>c_i</M>.
##
InstallGlobalFunction( LinearOrderByPartialWeakOrder,
function( pairs, weights )
local Q, S, Qw, Sw, F, pair, vx, vy, v, pos, candidates, minwght,
smallest, s, maxwght, largest, p, cycle, next, new;
# Initialize the queue and the stack.
Q:= [];
S:= [];
Qw:= [];
Sw:= [];
# Create a list of vertices according to the input.
F:= [];
for pair in Set( pairs ) do
if pair[1] <> pair[2] then
vx:= First( F, r -> r.keys[1] = pair[1] );
if vx = fail then
vx:= rec( keys:= [ pair[1] ], succ:= [], pred:= [] );
Add( F, vx );
fi;
vy:= First( F, r -> r.keys[1] = pair[2] );
if vy = fail then
vy:= rec( keys:= [ pair[2] ], succ:= [], pred:= [] );
Add( F, vy );
fi;
Add( vx.succ, vy );
Add( vy.pred, vx );
fi;
od;
# Assign the weights.
weights:= SortedList( weights );
for v in F do
pos:= PositionSorted( weights, v.keys );
if pos <= Length( weights ) and weights[ pos ][1] = v.keys[1] then
v.wght:= weights[ pos ][2];
else
v.wght:= 0;
fi;
od;
# While F contains a vertex, ...
while not IsEmpty( F ) do
# ... find the vertices in F without predecessors and add them to Q,
# remove the edges from these vertices,
# and remove these vertices from F.
candidates:= Filtered( F, v -> IsEmpty( v.pred ) );
if not IsEmpty( candidates ) then
minwght:= infinity; # larger than all admissible weights
for v in candidates do
if v.wght < minwght then
minwght:= v.wght;
smallest:= [ v ];
elif v.wght = minwght then
Add( smallest, v );
fi;
od;
for v in smallest do
Add( Q, v.keys );
Add( Qw, v.wght );
for s in v.succ do
s.pred:= Filtered( s.pred, x -> not IsIdenticalObj( v, x ) );
if IsEmpty( s.pred )
and ForAll( smallest, x -> not IsIdenticalObj( s, x ) ) then
Add( smallest, s );
fi;
od;
pos:= PositionProperty( F, x -> IsIdenticalObj( v, x ) );
Unbind( F[ pos ] );
F:= Compacted( F );
od;
fi;
# Then find the vertices in F without successors and put them on S,
# remove the edges to these vertices,
# and remove these vertices from F.
candidates:= Filtered( F, v -> IsEmpty( v.succ ) );
if not IsEmpty( candidates ) then
maxwght:= -1; # smaller than all admissible weights
for v in candidates do
if v.wght > maxwght then
maxwght:= v.wght;
largest:= [ v ];
elif v.wght = maxwght then
Add( largest, v );
fi;
od;
for v in largest do
Add( S, v.keys );
Add( Sw, v.wght );
for p in v.pred do
p.succ:= Filtered( p.succ, x -> not IsIdenticalObj( v, x ) );
if IsEmpty( p.succ )
and ForAll( largest, x -> not IsIdenticalObj( p, x ) ) then
Add( largest, p );
fi;
od;
pos:= PositionProperty( F, x -> IsIdenticalObj( v, x ) );
Unbind( F[ pos ] );
F:= Compacted( F );
od;
fi;
if not IsEmpty( F ) then
# Find a cycle in F.
# (Note that now any vertex has a successor,
# so we may start anywhere, and eventually get into a cycle.)
cycle:= [];
next:= F[1];
repeat
Add( cycle, next );
next:= next.succ[1];
pos:= PositionProperty( cycle, x -> IsIdenticalObj( x, next ) );
until pos <> fail;
cycle:= cycle{ [ pos .. Length( cycle ) ] };
# Replace the set of vertices in the cycle by a new vertex,
# replace all edges from/to a vertex outside the cycle
# to/from a vertex in the cycle by edges to/from the new vertex.
new:= rec( keys:= [], succ:= [], pred:= [],
wght:= Maximum( List( cycle, v -> v.wght ) ) );
for v in cycle do
UniteSet( new.keys, v.keys );
for s in v.succ do
if ForAll( cycle, w -> not IsIdenticalObj( s, w ) ) then
if ForAll( new.succ, w -> not IsIdenticalObj( s, w ) ) then
Add( new.succ, s );
fi;
pos:= PositionProperty( s.pred, w -> IsIdenticalObj( v, w ) );
if ForAll( s.pred, w -> not IsIdenticalObj( new, w ) ) then
s.pred[ pos ]:= new;
else
Unbind( s.pred[ pos ] );
s.pred:= Compacted( s.pred );
fi;
fi;
od;
for p in v.pred do
if ForAll( cycle, w -> not IsIdenticalObj( p, w ) ) then
if ForAll( new.pred, w -> not IsIdenticalObj( p, w ) ) then
Add( new.pred, p );
fi;
pos:= PositionProperty( p.succ, w -> IsIdenticalObj( v, w ) );
if ForAll( p.succ, w -> not IsIdenticalObj( new, w ) ) then
p.succ[ pos ]:= new;
else
Unbind( p.succ[ pos ] );
p.succ:= Compacted( p.succ );
fi;
fi;
od;
pos:= PositionProperty( F, x -> IsIdenticalObj( v, x ) );
Unbind( F[ pos ] );
F:= Compacted( F );
od;
Add( F, new );
fi;
od;
# Now the whole input is distributed to Q and S.
return rec( cycles:= Concatenation( Q, Reversed( S ) ),
weights:= Concatenation( Qw, Reversed( Sw ) ) );
end );
#############################################################################
##
#I InfoPackageLoading
##
## (We cannot do this in `package.gd'.)
##
DeclareInfoClass( "InfoPackageLoading" );
#############################################################################
##
#F LogPackageLoadingMessage( <severity>, <message>[, <name>] )
##
if not IsBound( TextAttr ) then
TextAttr:= "dummy";
fi;
#T needed? (decl. of GAPDoc is loaded before)
InstallGlobalFunction( LogPackageLoadingMessage, function( arg )
local severity, message, currpkg, i;
severity:= arg[1];
message:= arg[2];
if Length( arg ) = 3 then
currpkg:= arg[3];
elif IsBound( GAPInfo.PackageCurrent ) then
# This happens inside availability tests.
currpkg:= GAPInfo.PackageCurrent.PackageName;
else
currpkg:= "(unknown package)";
fi;
if IsString( message ) then
message:= [ message ];
fi;
if severity <= PACKAGE_WARNING
and UserPreference("UseColorsInTerminal") = true
and IsBound( TextAttr )
and IsRecord( TextAttr ) then
if severity = PACKAGE_ERROR then
message:= List( message,
msg -> Concatenation( TextAttr.1, msg, TextAttr.reset ) );
else
message:= List( message,
msg -> Concatenation( TextAttr.4, msg, TextAttr.reset ) );
fi;
fi;
Add( GAPInfo.PackageLoadingMessages, [ currpkg, severity, message ] );
Info( InfoPackageLoading, severity, currpkg, ": ", message[1] );
for i in [ 2 .. Length( message ) ] do
Info( InfoPackageLoading, severity, List( currpkg, x -> ' ' ),
" ", message[i] );
od;
end );
if not IsReadOnlyGlobal( "TextAttr" ) then
Unbind( TextAttr );
fi;
#############################################################################
##
#F DisplayPackageLoadingLog( [<severity>] )
##
InstallGlobalFunction( DisplayPackageLoadingLog, function( arg )
local severity, entry, message, i;
if Length( arg ) = 0 then
severity:= PACKAGE_WARNING;
else
severity:= arg[1];
fi;
for entry in GAPInfo.PackageLoadingMessages do
if severity >= entry[2] then
message:= entry[3];
Info( InfoPackageLoading, 1, entry[1], ": ", message[1] );
for i in [ 2 .. Length( message ) ] do
Info( InfoPackageLoading, 1, List( entry[1], x -> ' ' ),
" ", message[i] );
od;
fi;
od;
end );
#############################################################################
##
#F PackageAvailabilityInfo( <name>, <version>, <record>, <suggested>,
#F <checkall> )
##
InstallGlobalFunction( PackageAvailabilityInfo,
function( name, version, record, suggested, checkall )
local comp, loadinfo, Name, equal, pair,
currversion, inforec, skip, msg, dep, currloadinfo, GAPequal,
record_local, wght, pos, needed, test, name2, testpair;
# Initialize the dependency info.
for comp in [ "AlreadyHandled", "Dependencies",
"InstallationPaths", "Weights" ] do
if not IsBound( record.( comp ) ) then
record.( comp ):= [];
fi;
od;
if not IsBound( record.LoadInfo ) then
record.LoadInfo:= rec();
fi;
loadinfo:= record.LoadInfo;
if loadinfo = fail then
# happens if one fetches the global option but it is not set
loadinfo:= rec();
fi;
loadinfo.name:= name;
loadinfo.versions:= [];
loadinfo.comment:= "";
Name:= name;
name:= LowercaseString( name );
equal:= "";
if StartsWith( version, "=" ) then
equal:= "equal";
fi;
if name = "gap" then
# This case occurs if a package requires a particular GAP version.
if CompareVersionNumbers( GAPInfo.Version, version, equal ) then
return true;
else
Append( loadinfo.comment,
Concatenation( "required GAP version ", version,
" is not compatible with the actual version" ) );
return false;
fi;
fi;
# If the package `name' is already loaded then compare the version
# number of the loaded package with the required one.
# (Note that at most one version of a package can be available.)
if IsBound( GAPInfo.PackagesLoaded.( name ) ) then
if CompareVersionNumbers( GAPInfo.PackagesLoaded.( name )[2],
version, equal ) then
return true;
else
Append( loadinfo.comment,
Concatenation( "package '", name,
"' is already loaded, required version ", version,
" is not compatible with the actual version" ) );
return false;
fi;
fi;
# If the function was called from `AutoloadPackages'
# and if the package is listed among the packages to be excluded
# from autoload then exit.
if IsBound( GAPInfo.ExcludeFromAutoload )
and name in GAPInfo.ExcludeFromAutoload then
LogPackageLoadingMessage( PACKAGE_DEBUG,
"package to be excluded from autoload, return 'false'", Name );
Append( loadinfo.comment,
Concatenation( "package '", name,
"' shall be excluded from autoload, return 'false'" ) );
return false;
fi;
# Deal with the case that `name' is among the packages
# from whose tests the current check for `name' arose.
for pair in record.AlreadyHandled do
if name = pair[1] then
if CompareVersionNumbers( pair[2], version, equal ) then
# The availability of the package will be decided on an outer level.
return fail;
else
# The version assumed on an outer level does not fit.
Append( loadinfo.comment,
Concatenation( "for package '", name,
"', version ", pair[2],
" is assumed on an outer level, ",
"but version ", version, " is required here" ) );
return false;
fi;
fi;
od;
# In recursive calls, regard the current package as handled,
# of course in the version in question.
currversion:= [ name ];
Add( record.AlreadyHandled, currversion );
# Get the info records for the package `name',
# and take the first record that satisfies the conditions.
# (Note that they are ordered w.r.t. descending version numbers.)
for inforec in PackageInfo( name ) do
Name:= inforec.PackageName;
skip:= false;
currversion[2]:= inforec.Version;
if IsBound( inforec.Dependencies ) then
dep:= inforec.Dependencies;
else
dep:= rec();
fi;
currloadinfo:= rec( version:= inforec.Version,
comment:= "",
dependencies:= [] );
Add( loadinfo.versions, currloadinfo );
# Test whether this package version fits.
msg:= [ Concatenation( "PackageAvailabilityInfo for version ",
inforec.Version ) ];
if version <> "" then
if not CompareVersionNumbers( inforec.Version, version, equal ) then
# The severity of the log message must be less than `PACKAGE_INFO',
# since we do not want to see the message when looking for reasons
# why the current package cannot be loaded.
LogPackageLoadingMessage( PACKAGE_DEBUG,
[ Concatenation( "PackageAvailabilityInfo: version ",
inforec.Version, " does not fit" ),
Concatenation( "(required: ", version, ")" ) ],
inforec.PackageName );
Append( currloadinfo.comment,
Concatenation( "version ", version, "is required, " ) );
if not checkall then
continue;
fi;
skip:= true;
else
Add( msg, Concatenation( "(required: ", version, ")" ) );
LogPackageLoadingMessage( PACKAGE_INFO, msg,
inforec.PackageName );
fi;
else
LogPackageLoadingMessage( PACKAGE_INFO, msg,
inforec.PackageName );
fi;
# Test whether the required GAP version fits.
if IsBound( dep.GAP ) then
GAPequal:= "";
if StartsWith( dep.GAP, "=" ) then
GAPequal:= "equal";
fi;
if not CompareVersionNumbers( GAPInfo.Version, dep.GAP, GAPequal ) then
LogPackageLoadingMessage( PACKAGE_INFO,
Concatenation( "PackageAvailabilityInfo: required GAP version (",
dep.GAP, ") does not fit",
inforec.PackageName ) );
Append( currloadinfo.comment,
Concatenation( "GAP version ", dep.GAP, " is required, " ) );
if not checkall then
continue;
fi;
skip:= true;
fi;
fi;
# Test whether the availability test function fits.
GAPInfo.PackageCurrent:= inforec;
test:= (not IsBound(inforec.AvailabilityTest)) or (inforec.AvailabilityTest() = true);
Unbind( GAPInfo.PackageCurrent );
if test = true then
LogPackageLoadingMessage( PACKAGE_DEBUG,
Concatenation( "PackageAvailabilityInfo: the AvailabilityTest",
" function returned 'true'" ),
inforec.PackageName );
else
LogPackageLoadingMessage( PACKAGE_INFO,
Concatenation( "PackageAvailabilityInfo: the AvailabilityTest",
" function returned ", String( test ) ),
inforec.PackageName );
Append( currloadinfo.comment,
Concatenation( "the AvailabilityTest function returned ",
String( test ), ", " ) );
if not checkall then
continue;
fi;
skip:= true;
fi;
# Locate the `init.g' file of the package.
if Filename( [ Directory( inforec.InstallationPath ) ], "init.g" )
= fail then
LogPackageLoadingMessage( PACKAGE_WARNING,
Concatenation( "PackageAvailabilityInfo: cannot locate `",
inforec.InstallationPath,
"/init.g', please check the installation" ),
inforec.PackageName );
Append( currloadinfo.comment,
Concatenation( "cannot locate '",
inforec.InstallationPath, "/init.g', " ) );
if not checkall then
continue;
fi;
skip:= true;
fi;
record_local:= StructuralCopy( record );
wght:= 1;
pos:= PositionProperty( record_local.Weights, pair -> pair[1] = name );
if pos = fail then
Add( record_local.Weights, [ name, wght ] );
else
record_local.Weights[ pos ][2]:= wght;
fi;
# Check the dependencies of this package version.
needed:= [];
if IsBound( dep.NeededOtherPackages ) then
Append( needed, dep.NeededOtherPackages );
fi;
test:= true;
if IsEmpty( needed ) then
LogPackageLoadingMessage( PACKAGE_DEBUG,
"PackageAvailabilityInfo: no needed packages",
inforec.PackageName );
else
LogPackageLoadingMessage( PACKAGE_DEBUG, Concatenation(
[ "PackageAvailabilityInfo: check needed packages" ],
List( needed,
pair -> Concatenation( pair[1], " (", pair[2], ")" ) ) ),
inforec.PackageName );
for pair in needed do
name2:= LowercaseString( pair[1] );
Add( currloadinfo.dependencies,
rec( name:= name2,
versions:= [],
comment:= "" ) );
record_local.LoadInfo:= Last( currloadinfo.dependencies );
testpair:= PackageAvailabilityInfo( name2, pair[2], record_local,
suggested, checkall );
if testpair = false then
# This dependency is not satisfied.
test:= false;
LogPackageLoadingMessage( PACKAGE_INFO,
Concatenation( "PackageAvailabilityInfo: dependency '",
name2, "' is not satisfied" ), inforec.PackageName );
if not checkall then
# Skip the check of other dependencies.
break;
fi;
elif testpair <> true then
# The package `name2' is available but not yet loaded.
Add( record_local.Dependencies, [ name2, name ] );
fi;
od;
LogPackageLoadingMessage( PACKAGE_DEBUG,
"PackageAvailabilityInfo: check of needed packages done",
inforec.PackageName );
fi;
if test = false then
# At least one package needed by this version is not available,
if not checkall then
continue;
fi;
skip:= true;
fi;
# All checks for this version have been performed.
# Go to the next installed version if some check failed.
if skip then
continue;
fi;
# The version given by `inforec' will be taken.
# Copy the information back to the argument record.
record.InstallationPaths:= record_local.InstallationPaths;
Add( record.InstallationPaths,
[ name, [ inforec.InstallationPath, inforec.Version,
inforec.PackageName, false ] ] );
record.Dependencies:= record_local.Dependencies;
record.AlreadyHandled:= record_local.AlreadyHandled;
record.Weights:= record_local.Weights;
if suggested and IsBound( dep.SuggestedOtherPackages ) then
# Collect info about suggested packages and their dependencies,
# but without extending 'LoadInfo'.
LogPackageLoadingMessage( PACKAGE_DEBUG, Concatenation(
[ "PackageAvailabilityInfo: check suggested packages" ],
List( dep.SuggestedOtherPackages,
pair -> Concatenation( pair[1], " (", pair[2], ")" ) ) ),
inforec.PackageName );
for pair in dep.SuggestedOtherPackages do
name2:= LowercaseString( pair[1] );
# Do not change the information collected up to now
# until we are sure that we will really use the suggested package.
record_local:= StructuralCopy( record );
Unbind( record_local.LoadInfo );
test:= PackageAvailabilityInfo( name2, pair[2], record_local,
suggested, checkall );
if test <> true then
Add( record_local.Dependencies, [ name2, name ] );
if test <> false then
record.InstallationPaths:= record_local.InstallationPaths;
record.Dependencies:= record_local.Dependencies;
record.AlreadyHandled:= record_local.AlreadyHandled;
record.Weights:= record_local.Weights;
fi;
fi;