/
Dependency.pm
664 lines (528 loc) · 19.6 KB
/
Dependency.pm
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
# See bottom of file for license and copyright information
=begin TML
---+ package Foswiki::Configure::Dependency
This module defines a dependency required by a Foswiki module and provides
functions to test if the dependency is installed, and compare versions with
the required version.
It is also used to examine the installed version of a Foswiki module.
=cut
package Foswiki::Configure::Dependency;
use strict;
use warnings;
my @MNAMES = qw(jan feb mar apr may jun jul aug sep oct nov dec);
my $mnamess = join( '|', @MNAMES );
my $MNAME = qr/$mnamess/i;
my %M2N;
foreach ( 0 .. $#MNAMES ) { $M2N{ $MNAMES[$_] } = $_ + 1; }
my %STRINGOPMAP = (
'eq' => 'eq',
'ne' => 'ne',
'lt' => 'lt',
'gt' => 'gt',
'le' => 'le',
'ge' => 'ge',
'=' => 'eq',
'==' => 'eq',
'!=' => 'ne',
'<' => 'lt',
'>' => 'gt',
'<=' => 'le',
'>=' => 'ge'
);
my $MAXINT = 0x7FFFFFFF;
=begin TML
---++ ClassMethod new( %opts )
Create an object instance representing a single dependency, as read from DEPENDENCIES
* %opts
* =name => unqualified name e.g. SafeWikiPlugin=
* =module => qualified module e.g Foswiki::Plugins::SafeWikiPlugin=
* If a qualified =module= is not provided, all possible Foswiki/TWiki module types are searched for =type=perl=
* =type => perl|cpan|external=
* =perl= is a Foswiki or TWiki module. =external= is used for any program other than a perl module. External dependencies are __not__ checked.
* =version => version condition e.g. ">1.2.3"=
* =trigger => ONLYIF condition= (Specifies a version of another module, such as the Foswiki Func API)
* =description => text=
* Instance variables set by calling studyInstallation() or indirectly by calling check()
* =installedVersion => $VERSION string from module=
* =installedRelease => $RELEASE string from module (or $VERSION)=
* =notes => text Notes on condition of module= (ex. fails due to missing dependency)
=cut
sub new {
my ( $class, %opts ) = @_;
my $this = bless( \%opts, $class );
# If {module} is defined but not {name}, we can usually work it out
if ( $this->{module} && !$this->{name} ) {
$this->{name} = $this->{module};
$this->{name} =~ s/^.*:://;
}
# If {name} is defined but {module} is not, we'll have to work that
# out when we try to load the module in studyInstallation.
die "No name or module in dependency" unless $this->{name};
# If no version condition is given, assume we will just test that the
# module is installed (any version)
$this->{version} ||= '>=0';
# Other defaults
$this->{trigger} ||= 1;
$this->{type} ||= 'external'; # assume external module
$this->{description} ||= 'This module has no description.';
$this->{notes} = '';
return $this;
}
=begin TML
---++ ObjectMethod check()
Check whether the dependency is satisfied by a currently-installed module.
* Return: ($ok, $msg)
* $ok is a boolean indicating success/failure
* $msg is a helpful message describing the failure
* $release is the installed release of the module, as determined from the values of $RELEASE and $VERSION in the module.
=cut
sub check {
my $this = shift;
# reject non-Perl dependencies
if ( $this->{type} !~ /^(?:perl|cpan)$/i ) {
return ( 0, <<LALA );
$this->{module} is type $this->{type}, and cannot be automatically checked.
Please check it manually and install if necessary.
LALA
}
# Examine the current install of the module
if ( !$this->studyInstallation() ) {
return ( 0, <<LALA );
$this->{module} version $this->{version} required
-- $this->{type} $this->{notes}
LALA
}
elsif ( $this->{version} =~ /^\s*([<>=]+)?\s*(.+)/ ) {
# the version field is a condition
my $op = $1 || '>=';
my $requiredVersion = $2;
unless ( $this->compare_versions( $op, $requiredVersion ) ) {
# module doesn't meet this condition
return ( 0, <<LALA);
$this->{module} version $op $requiredVersion required
-- installed version is $this->{installedRelease}
LALA
}
}
return ( 1, <<LALA );
$this->{module} version $this->{installedRelease} loaded
LALA
}
=begin TML
---++ ObjectMethod studyInstallation()
Check the current installation, populating the ={installedRelease}= and ={installedVersion}= fields, and returning true if the extension is installed.
={notes}= will also be set when certain conditions are discovered (example: missing dependencies or other compile failures).
* Return: $ok
* $ok is a boolean indicating success/failure. If the module is found and a VERSION and RELEASE are discovered, the method returns true.
=cut
sub studyInstallation {
my $this = shift;
my $load_errors = '';
if ( !$this->{module} ) {
my $lib = ( $this->{name} =~ /Plugin$/ ) ? 'Plugins' : 'Contrib';
foreach my $namespace (qw(Foswiki TWiki)) {
my $path = $namespace . '::' . $lib . '::' . $this->{name};
eval "require $path";
unless ( $@ && $@ =~ m/^Can't locate $path/ ) {
$this->{module} = $path;
$load_errors = $@ if ($@);
last;
}
}
}
return 0 unless $this->{module};
unless ($load_errors) {
eval "require $this->{module}";
$load_errors = $@ if ($@);
}
my $path = $this->{module};
$path =~ s#::#/#g;
$path .= '.pm';
if ( $load_errors =~ m/^Can't locate $path/ ) {
$this->{notes} = "module is not installed";
return;
}
elsif ( $load_errors =~ m/^(Couldn't require|Can't locate|Insecure dep)/ ) {
$this->{notes} = "module missing required dependencies";
print STDERR
"Unexpected errors attempting to determine version of dependency\n$load_errors\n";
return 1 if ( $this->_recover_versions($path) );
return;
}
elsif ( $load_errors =~ m/^Died/ ) {
$this->{notes} = "module died with errors when loaded";
print STDERR
"Unexpected errors attempting to determine version of dependency\n$load_errors\n";
return 1 if ( $this->_recover_versions($path) );
return;
}
elsif ($load_errors) {
$this->{notes} = "Unknown errors loading module";
print STDERR
"Unexpected errors attempting to determine version of dependency\n$load_errors\n";
return 1 if ( $this->_recover_versions($path) );
return;
}
no strict 'refs';
$this->{installedVersion} = ${"$this->{module}::VERSION"} || 0;
$this->{installedRelease} = ${"$this->{module}::RELEASE"}
|| $this->{installedVersion};
use strict 'refs';
# Check if it's pseudo installed. Only works on platforms that
# support soft links (and assumes the pseudo-install was -l)
$path = $this->{module};
$path =~ s#::#/#g;
$path .= '.pm';
foreach my $dir (@INC) {
if ( -e "$dir/$path" ) {
if ( -l "$dir/$path" ) {
# Assume pseudo-installed
$this->{installedVersion} = '9999.99_999';
}
last;
}
}
return 1;
}
sub _recover_versions {
my ( $this, $path ) = @_;
foreach my $dir (@INC) {
if ( -e "$dir/$path" ) {
my $modfh = open my $mod, '<', "$dir/$path";
if ( !$modfh ) {
return 0;
}
my $file_contents = do { local $/; <$mod> };
my $VERSION;
my $RELEASE;
my ($version) = $file_contents =~ m/^\s*(?:our)?\s*(\$VERSION\s*=.*?);/sm;
my ($release) =
$file_contents =~ m/^\s*(?:our)?\s*(\$RELEASE\s*=.*?);/sm;
eval $version if ($version);
eval $release if ($release);
$this->{installedVersion} = $VERSION || 0;
$this->{installedRelease} = $RELEASE || $this->{installedVersion};
return 1;
}
}
}
=begin TML
---++ ObjectMethod compare_versions ($condition, $release)
Compare versions (provided as $RELEASE, $VERSION) with a release specifier
Returns the boolean result of the comparison
=cut
sub compare_versions {
my $this = shift;
if ( $this->{type} eq 'perl' ) {
#print STDERR "Comparing TYPE PERL\n";
return $this->_compare_extension_versions(@_);
}
else {
#print STDERR "Comparing TYPE cpan\n";
return $this->_compare_cpan_versions(@_);
}
}
# Heuristically compare version strings in cpan modules
sub _compare_cpan_versions {
my ( $this, $op, $b ) = @_;
my $a = $this->{installedVersion};
return 0 if not defined $op or not exists $STRINGOPMAP{$op};
my $string_op = $STRINGOPMAP{$op};
# CDot: changed largest char because collation order makes string
# comparison weird in non-iso8859 locales
my $largest_char = 'z';
# remove leading and trailing whitespace
# because ' X' should compare equal to 'X'
$a =~ s/^\s+//;
$a =~ s/\s+$//;
$b =~ s/^\s+//;
$b =~ s/\s+$//;
# $Rev$ without a number should compare higher than anything else
$a =~ s/^\$Rev:?\s*\$$/$largest_char/;
$b =~ s/^\$Rev:?\s*\$$/$largest_char/;
# remove the SVN marker text from the version number, if it is there
$a =~ s/^\$Rev: (\d+) \$$/$1/;
$b =~ s/^\$Rev: (\d+) \$$/$1/;
# swap the day-of-month and year around for ISO dates
my $isoDatePattern = qr/^\d{1,2}-\d{1,2}-\d{4}$/;
if ( $a =~ $isoDatePattern and $b =~ $isoDatePattern ) {
$a =~ s/^(\d+)-(\d+)-(\d+)$/$3-$2-$1/;
$b =~ s/^(\d+)-(\d+)-(\d+)$/$3-$2-$1/;
}
# Change separator characters to be the same,
# because X-Y-Z should compare equal to X.Y.Z
# and combine adjacent separators,
# because '6 jun 2009' should compare equal to '6 jun 2009'
my $separator = '.';
$a =~ s([ ./_-]+)($separator)g;
$b =~ s([ ./_-]+)($separator)g;
# Replace month-names with numbers and swap day-of-month and year
# around to make them sortable as strings
# but only do this if both versions look like a date
my $datePattern = qr(\b\d{1,2}$separator$MNAME$separator\d{4}\b);
if ( $a =~ $datePattern and $b =~ $datePattern ) {
$a =~
s/(\d+)$separator($MNAME)$separator(\d+)/$3.$separator.$M2N{ lc($2) }.$separator.$1/ge;
$b =~
s/(\d+)$separator($MNAME)$separator(\d+)/$3.$separator.$M2N{ lc($2) }.$separator.$1/ge;
}
# convert to lowercase
# because 'cairo' should compare less than 'Dakar'
$a = lc($a);
$b = lc($b);
# remove a leading 'v' if both are of the form X.Y
# because vX.Y should compare equal to X.Y
my $xDotYPattern = qr/^v?\s*\d+(?:$separator\d+)+/;
if ( $a =~ $xDotYPattern and $b =~ $xDotYPattern ) {
$a =~ s/^v\s*//;
$b =~ s/^v\s*//;
}
# work out how many characters there are in the longest sequence
# of digits between the two versions
my ($maxDigits) =
reverse
sort( map { length($_) } ( $a =~ /(\d+)/g ), ( $b =~ /(\d+)/g ), );
# justify digit sequences so that they compare correctly.
# E.g. '063' lt '103'
$a =~ s/(\d+)/sprintf('%0'.$maxDigits.'u', $1)/ge;
$b =~ s/(\d+)/sprintf('%0'.$maxDigits.'u', $1)/ge;
# there is no need to justify non-digit sequences
# because 'alpha' compares less than 'beta'
# X should compare greater than X-beta1
# so append a high-value character to the
# non-beta version if one version looks like
# a beta and the other does not
if ( $a =~ /^$b$separator?beta/ ) {
# $a is beta of $b
# $b should compare greater than $a
$b .= $largest_char;
}
elsif ( $b =~ /^$a$separator?beta/ ) {
# $b is beta of $a
# $a should compare greater than $b
$a .= $largest_char;
}
my $comparison;
if ( $a =~ /^(\d+)(\.\d*)?$/ && $b =~ /^(\d+)(\.\d*)?$/ ) {
$op = '==' if $op eq '=';
$a += 0;
$b += 0;
$comparison = "$a $op $b";
}
else {
$comparison = "'$a' $string_op '$b'";
}
my $result = eval $comparison;
#print STDERR "[$comparison]->$result;\n";
return $result;
}
# Compare foswiki extension versions using more rigorous rules
# Returns true if the condition is true, false if not true, or invalid comparison
sub _compare_extension_versions {
# $aRELEASE, $aVERSION - module release and svn version
# $b - what we are comparing to (from DEPENDENCIES or configure FastReport)
my ( $this, $op, $reqVer ) = @_;
my $aRELEASE = $this->{installedRelease};
my $aVERSION = $this->{installedVersion};
# If the operator is not defined, or invalid, return false
if ( not defined $op or not exists $STRINGOPMAP{$op} ) {
$op = '"undefined"' unless defined $op;
#print STDERR "Unknown Operator $op \n";
return 0;
}
my $string_op = $STRINGOPMAP{$op};
my $e = $b;
# First see what format the RELEASE string is in, and break it
# down into a tuple (most significant first)
my @atuple;
my @btuple;
my $baseType = ''; # Type of version/release string for this module
my $reqType = ''; # Type of version/release string requested
if ( defined $aRELEASE ) {
#print STDERR "Release $aRELEASE defined\n";
( $baseType, @atuple ) = _decodeReleaseString($aRELEASE);
}
elsif ( defined $aVERSION ) {
# for some reason, no Release defined, fall back to the Version.
#print STDERR "Version $aVERSION defined\n";
( $baseType, @atuple ) =
_decodeReleaseString($aVERSION); # if defined $aVERSION;
}
else {
#print STDERR "Neither RELEASE or VERSION defined\n";
return 0;
}
unless ( defined $reqVer ) {
#print STDERR "Comparison not defined\n";
return 0;
}
( $reqType, @btuple ) = _decodeReleaseString($reqVer);
#print STDERR "EXPECT $baseType $string_op BEXPECT $reqType \n";
# Requested version is a svn release, Need to use VERSION instead of RELEASE stirng
if ( $reqType eq 'svn' ) {
#print STDERR "Expecting SVN comparison, but RELEASE was $baseType \n";
( $baseType, @atuple ) = _decodeReleaseString($aVERSION)
if ( defined $aVERSION && $baseType ne 'svn' );
return 0 unless ( $baseType eq 'svn' );
}
# See if request is for anything > 0. If so, return true.
if ( $reqType eq 'tuple'
&& scalar @btuple == 1
&& $btuple[0] == 0
&& $string_op eq 'gt' )
{
#print STDERR "'SPECIAL CASE - zero expected just means present\n";
return 1;
}
# special handling for dates.
if ( $reqType eq 'date' || $baseType eq 'date' ) {
# special case, if requested tuple, and installed date, this is probably
# a migration to a version tuple, so return true to trigger an update
return 1
if ( $reqType eq 'tuple'
&& $baseType eq 'date' );
if ( $reqType ne $baseType ) {
return 0;
}
if ( scalar @btuple != scalar @atuple || scalar @btuple != 3 ) {
#print STDERR "Incorrectly formatted date in $aRELEASE or $b\n";
}
# Simple validations - grossly invalid year, month or day.
return 0 if ( $atuple[0] < 1970 || $btuple[0] < 1970 );
return 0 if ( $atuple[1] > 12 || $btuple[1] > 12 );
return 0 if ( $atuple[1] < 1 || $btuple[1] < 1 );
return 0 if ( $atuple[2] > 31 || $btuple[2] > 31 );
return 0 if ( $atuple[2] < 1 || $btuple[2] < 1 );
}
# We can't figure out the types, so just return false.
return 0 if ( $baseType eq 'unknown' || $reqType eq 'unknown' );
# Do the comparisons
( my $a, $b ) = _digitise_tuples( \@atuple, \@btuple );
my $comparison = "'$a' $string_op '$b'";
my $result = eval $comparison;
#print "[$comparison]->$result\n";
return $result;
}
# Returns the type of the passed string
#
# What format is the release identifier? We support comparison
# of five formats:
# 1. A simple number (subversion revision).
# 2 Encoded SVN $Rev$ formats
# 3. A dd Mmm yyyy format date
# 4. An ISO yyyy-mm-dd format date
# 5. A tuple N(.M)+
# SVN Versions should always be an SVN release number
# coded in 3 formats
# 1. $Rev: <some number> $
# 2. $Rev: <some number> (date)$ (Date is ignored)
# 3. $Rev$ An unassigned Rev indicating a SVN checkout.
sub _decodeReleaseString {
my ($rel) = @_;
my $form;
my @tuple;
$rel =~ s/^\s+//;
$rel =~ s/\s+$//;
if ( $rel =~ /^(\d{4})-(\d{2})-(\d{2}).*$/ ) {
# ISO date
@tuple = ( $1, $2, $3 );
$form = 'date';
}
elsif ( $rel =~ /^(\d+)\s+($MNAME)\s+(\d+).*$/io ) {
# dd Mmm YYY date
@tuple = ( $3, $M2N{ lc $2 }, $1 );
$form = 'date';
}
elsif ( $rel =~ /^([0-9]{4,5})$/ ) {
#print STDERR "matching a svn VERSION\n";
# svn rev, 4-5 digit number
@tuple = ($1);
$form = 'svn';
}
elsif ( $rel =~ /^r([0-9]{1,6})$/ ) {
# svn rev, a 1-6 digit number prefixed by 'r'
@tuple = ($1);
$form = 'svn';
}
elsif ( $rel =~ /^V?(\d+([-_.]\d+)*).*?$/i ) {
# tuple e.g. 1.23.4 Note that a simple tuple could also be a low SVN rev.
@tuple = split( /[-_.]/, $1 );
$form = 'tuple';
}
elsif ( $rel =~ /^\$Rev: (\d+)\s*\(.*\)$/ ) {
# 1234 (7 Aug 2009)
# 1234 (2009-08-07)
@tuple = ($1);
$form = 'svn';
}
elsif ( $rel =~ /^\$Rev: (\d+).*\$$/ ) {
# $Rev: 1234$
@tuple = ($1);
$form = 'svn';
}
elsif ( $rel =~ /^\$Rev:?\s*\$.*$/ ) {
# $Rev$
@tuple = ($MAXINT);
$form = 'svn';
}
elsif ( $rel =~ /^\s?$/ ) {
# Blank or empty version
@tuple = (0);
$form = 'tuple';
}
elsif ( $rel =~ /^Foswiki-(\d+([-_.]\d+)*).*?$/i ) {
@tuple = split( /[-_.]/, $1 );
$form = 'tuple';
}
else {
# Some other format
@tuple = (0);
$form = 'unknown';
}
#print STDERR "RELEASE $rel decodes as $form, @tuple \n";
return ( $form, @tuple );
}
# Given two tuples, convert them both into number strings, padding with
# zeroes as necessary.
sub _digitise_tuples {
my ( $a, $b ) = @_;
my ($maxDigits) = reverse sort ( map { length($_) } ( @$a, @$b ) );
$a = join(
'',
map {
if ( $_ eq 'HEAD' ) { $_ }
else { sprintf( '%0' . $maxDigits . 'u', $_ ); }
} @$a
);
$b = join(
'',
map {
if ( $_ eq 'HEAD' ) { $_ }
else { sprintf( '%0' . $maxDigits . 'u', $_ ); }
} @$b
);
# Pad with zeroes to equal length
if ( length($b) > length($a) ) {
$a .= '0' x ( length($b) - length($a) );
}
elsif ( length($a) > length($b) ) {
$b .= '0' x ( length($a) - length($b) );
}
return ( $a, $b );
}
1;
__END__
Author: Crawford Currie http://wikiring.com
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.