/
extender.pl
1285 lines (1117 loc) · 37.8 KB
/
extender.pl
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
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 1999-2007 Foswiki Contributors. All Rights Reserved.
# 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.
#
# Author: Crawford Currie http://wikiring.com
#
# This module contains the functions used by the extensions installer.
# It is not treated as a "standard" module because it has radically
# different environment requirements (i.e. as few as possible)
#
# It is invoked from the individual installer scripts shipped with
# extensions, and should not be run directly.
#
package Foswiki::Extender;
use strict;
use warnings;
use Cwd;
use File::Temp;
use File::Copy;
use File::Path;
no warnings 'redefine';
my $noconfirm = 0;
my $downloadOK = 0;
my $alreadyUnpacked = 0;
my $reuseOK = 0;
my $inactive = 0;
my $session;
my %available;
my $lwp;
my @archTypes = ( '.tgz', '.tar.gz', '.zip' );
my $installationRoot;
my $MODULE;
my $PACKAGES_URL;
my $MANIFEST;
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 %N2M;
foreach ( 0 .. $#MNAMES ) { $N2M{ $MNAMES[$_] } = $_; }
my %STRINGOPMAP = (
'eq' => 'eq',
'ne' => 'ne',
'lt' => 'lt',
'gt' => 'gt',
'le' => 'le',
'ge' => 'ge',
'=' => 'eq',
'==' => 'eq',
'!=' => 'ne',
'<' => 'lt',
'>' => 'gt',
'<=' => 'le',
'>=' => 'ge'
);
BEGIN {
$installationRoot = Cwd::getcwd();
# getcwd is often a simple `pwd` thus it's tainted, untaint it
$installationRoot =~ /^(.*)$/;
$installationRoot = $1;
# Check if we were invoked from configure
# by looking at the call stack
sub running_from_configure {
my $i = 0;
while ( my $caller = caller( ++$i ) ) {
if ( $caller =~ /^Foswiki::Configure::UIs::EXTEND$/ ) {
return 1;
}
}
return 0;
}
my $check_perl_module = sub {
my $module = shift;
if ( $module =~ /^CPAN/ ) {
# Check how we were invoked as CPAN shouldn't
# be loaded from the configure
if (running_from_configure) {
print "Running from configure, disabling $module\n";
return $available{$module} = 0;
}
}
if ( eval "use $module; 1;" ) {
$available{$module} = 1;
}
else {
print "Warning: $module is not available,"
. " some installer functions have been disabled\n";
$available{$module} = 0;
}
return $available{$module};
};
unless ( -d 'lib'
&& -d 'bin'
&& -e 'bin/setlib.cfg' )
{
die 'This installer must be run from the root directory'
. ' of a Foswiki installation';
}
# read setlib.cfg
chdir('bin');
require 'setlib.cfg';
# See if we can make a Foswiki. If we can, then we can save topic
# and attachment histories. Key off Foswiki::Merge because it is
# fairly new and fairly unique.
unless ( &$check_perl_module('Foswiki::Merge') ) {
die "Can't find Foswiki: $@";
}
require Foswiki;
# We have to get the admin user, as a guest user may be blocked.
my $user = $Foswiki::cfg{AdminUserLogin};
$session = new Foswiki($user);
chdir($installationRoot);
if ( &$check_perl_module('LWP') ) {
$lwp = new LWP::UserAgent();
$lwp->agent("PluginsInstaller");
$lwp->env_proxy();
}
&$check_perl_module('CPAN');
}
sub remap {
my $file = shift;
if ( $Foswiki::cfg{SystemWebName} ne 'System' ) {
$file =~ s#^data/System/#data/$Foswiki::cfg{SystemWebName}/#;
$file =~ s#^pub/System/#pub/$Foswiki::cfg{SystemWebName}/#;
}
if ( $Foswiki::cfg{TrashWebName} ne 'Trash' ) {
$file =~ s#^data/Trash/#data/$Foswiki::cfg{TrashWebName}/#;
$file =~ s#^pub/Trash/#pub/$Foswiki::cfg{TrashWebName}/#;
}
if ( $Foswiki::cfg{UsersWebName} ne 'Main' ) {
$file =~ s#^data/Main/#data/$Foswiki::cfg{UsersWebName}/#;
$file =~ s#^pub/Main/#pub/$Foswiki::cfg{UsersWebName}/#;
}
if ( $Foswiki::cfg{UsersWebName} ne 'Users' ) {
$file =~ s#^data/Users/#data/$Foswiki::cfg{UsersWebName}/#;
$file =~ s#^pub/Users/#pub/$Foswiki::cfg{UsersWebName}/#;
}
# Canonical symbol mappings
foreach my $w qw( SystemWebName TrashWebName UsersWebName ) {
if ( defined $Foswiki::cfg{$w} ) {
$file =~ s#^data/$w/#data/$Foswiki::cfg{$w}/#;
$file =~ s#^pub/$w/#pub/$Foswiki::cfg{$w}/#;
}
}
foreach my $t qw( NotifyTopicName HomeTopicName WebPrefsTopicName
MimeTypesFileName ) {
if ( defined $Foswiki::cfg{$t} )
{
$file =~
s#^data/(.*)/$t\.txt(,v)?#data/$1/$Foswiki::cfg{$t}.txt$2/#;
$file =~ s#^pub/(.*)/$t/([^/]*)$#pub/$1/$Foswiki::cfg{$t}/$2/#;
}
} return $file;
}
sub compare_versions {
my ( $a, $op, $b ) = @_;
return 0 if not defined $op or not exists $STRINGOPMAP{$op};
my $string_op = $STRINGOPMAP{$op};
#print "|$a$op$b|=>";
my $largest_char = chr(255);
# 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.$N2M{ lc($2) }.$separator.$1/ge;
$b =~
s/(\d+)$separator($MNAME)$separator(\d+)/$3.$separator.$N2M{ 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 = "'$a' $string_op '$b'";
my $result = eval $comparison;
#print "[$comparison]->$result\n";
return $result;
}
sub check_dep {
my ($dep) = @_;
my ( $ok, $msg ) = ( 1, "" );
# reject non-Perl dependencies
if ( $dep->{type} !~ /^(?:perl|cpan)$/i ) {
$ok = 0;
$msg =
"Module is type $dep->{type}, and cannot be automatically checked.\n"
. "Please check it manually and install if necessary.\n";
return ( $ok, $msg );
}
# try to load the module
my $module = $dep->{name};
eval "require $module";
if ($@) {
$ok = 0;
( $msg = $@ ) =~ s/ in .*$/\n/s;
return ( $ok, $msg );
}
my $moduleVersion = 0;
{
no strict 'refs';
$moduleVersion = ${"${module}::VERSION"};
# remove the SVN marker text from the version number, if it is there
$moduleVersion =~ s/^\$Rev: (\d+) \$$/$1/;
}
# check if the version satisfies the prerequisite
if ( defined $dep->{version} and $dep->{version} ne '' ) {
# the version field is in fact a condition
if ( $dep->{version} =~ /^\s*(?:>=?)?\s*([0-9a-z._-]+)/ ) {
# Condition is >0 or >= 1.3
my $requiredVersion = $1;
if ( compare_versions( $moduleVersion, '<', $requiredVersion ) ) {
# But module doesn't meet this condition
$msg = "$module version $requiredVersion required"
. "--this is only version $moduleVersion";
$ok = 0;
return ( $ok, $msg );
}
}
elsif ( $dep->{version} =~ /<\s*([0-9a-z._-]+)/ ) {
# Condition is < 2.7
my $requiredVersion = $1;
if ( compare_versions( $moduleVersion, '>=', $requiredVersion ) ) {
# But module doesn't meet this condition
$ok = 0;
$msg =
"Module $module is version v"
. $moduleVersion
. " and the dependency wants "
. $dep->{version};
return ( $ok, $msg );
}
}
else {
$ok = 0;
$msg =
"Module $module is version v"
. $moduleVersion
. " and the dependency wants "
. $dep->{version};
return ( $ok, $msg );
}
}
$msg = "$module v$moduleVersion loaded\n";
return ( $ok, $msg );
}
# Satisfy dependencies on modules, by checking:
# 1. If the module is a perl module, then:
# 1. If the module is loadable in the current environment
# 2. If the dependency has specified a version constraint, then
# the module must have a top-level variable VERSION which satisfies
# the constraint.
# Note that all Foswiki modules are perl modules - even non-perl
# distributions have a perl 'stub' module that carries the version info.
# 2. If the module is _not_ perl, then we can't check it.
sub satisfy {
my $dep = shift;
my $trig = eval $dep->{trigger};
return 1 unless ($trig);
print <<DONE;
##########################################################
Checking dependency on $dep->{name}....
DONE
my ( $ok, $msg ) = check_dep($dep);
if ($ok) {
print $msg;
return 1;
}
print <<DONE;
*** $MODULE depends on $dep->{type} package $dep->{name} $dep->{version}
which is described as "$dep->{description}"
But when I tried to find it I got this error:
$msg
DONE
if ( $dep->{name} =~ m/^(Foswiki|TWiki)::(Contrib|Plugins)::(\w*)/ ) {
my $type = $1;
my $pack = $2;
my $packname = $3;
$packname .= $pack if ( $pack eq 'Contrib' && $packname !~ /Contrib$/ );
if ( !$noconfirm || ( $noconfirm && $downloadOK ) ) {
my $reply =
ask( 'Would you like me to try to download '
. 'and install the latest version of '
. $packname
. ' from foswiki.org?' );
if ($reply) {
return installPackage($packname);
}
}
return 0;
}
if ( $dep->{type} eq 'cpan' && $available{CPAN} ) {
print <<'DONE';
This module is available from the CPAN archive (http://www.cpan.org). You
can download and install it from here. The module will be installed
to wherever you configured CPAN to install to.
DONE
my $reply =
ask( 'Would you like me to try to download '
. 'and install the latest version of '
. $dep->{name}
. ' from cpan.org?' );
return 0 unless $reply;
my $mod = CPAN::Shell->expand( 'Module', $dep->{name} );
my $info = $mod->dslip_status();
if ( $info->{D} eq 'S' ) {
# Standard perl module!
print STDERR <<DONE;
#########################################################################
# WARNING: $dep->{name} is a standard perl module
#
# I cannot install it without upgrading your version of perl, something
# I'm not willing to do. Please either install the module manually (from
# a package downloaded from cpan.org) or upgrade your perl to a version
# that includes this module.
#########################################################################
DONE
return 0;
}
if ($noconfirm) {
$CPAN::Config->{prerequisites_policy} = 'follow';
}
else {
$CPAN::Config->{prerequisites_policy} = 'ask';
}
CPAN::install( $dep->{name} );
( $ok, $msg ) = check_dep($dep);
return 1 if $ok;
my $e = 'it';
if ( $CPAN::Config->{makepl_arg} =~ /PREFIX=(\S+)/ ) {
$e = $1;
}
print STDERR <<DONE;
#########################################################################
# WARNING: I still can't find the module $dep->{name}
#
# If you installed the module in a non-standard directory, make sure you
# have followed the instructions in bin/setlib.cfg and added $e
# to your \@INC path.
#########################################################################
DONE
}
return 0;
}
=pod
---++ StaticMethod ask( $question ) -> $boolean
Ask a question.
Example: =if( ask( "Proceed?" )) { ... }=
=cut
sub ask {
my $q = shift;
my $reply;
return 1 if $noconfirm;
local $/ = "\n";
$q .= '?' unless $q =~ /\?\s*$/;
print $q. ' [y/n] ';
while ( ( $reply = <STDIN> ) !~ /^[yn]/i ) {
print "Please answer yes or no\n";
}
return ( $reply =~ /^y/i ) ? 1 : 0;
}
=pod
---++ StaticMethod prompt( $question, $default ) -> $string
Prompt for a string, using a default if return is pressed.
Example: =$dir = prompt("Directory")=;
=cut
sub prompt {
my ( $q, $default ) = @_;
my $reply = '';
local $/ = "\n";
while ( !$reply ) {
print $q;
print " ($default)" if defined $default;
print ': ';
$reply = <STDIN>;
chomp($reply);
$reply ||= $default;
}
return $reply;
}
# Try and find an installer or archive.
# Look in (1) the current directory (2) on the $TWIKI_PACKAGES path and
# (3) in the twikiplugins subdirectory (if there, to support developers)
# and finally (4) download from $PACKAGES_URL
sub getComponent {
my ( $module, $types, $what ) = @_;
my $f;
# Look for the archive.
require Config;
if ( !$noconfirm || ( $noconfirm && $reuseOK ) ) {
foreach my $dir (
$installationRoot,
$installationRoot . '/twikiplugins/' . $module,
split( $Config::Config{path_sep}, $ENV{TWIKI_PACKAGES} || '' )
)
{
foreach my $type (@$types) { # .tgz preferred
$f = $dir . '/' . $module . $type;
if ( -e $f ) {
my @st = stat($f);
my $credate = localtime( $st[9] );
print <<HERE;
$f exists on this machine; would you like me to use it?
It was created on $credate.
If not, I will try to download a new one.
HERE
if ( ask("Use existing $f?") ) {
print "Got a local $what from $f\n";
return $f;
}
}
}
}
}
unless ($lwp) {
print STDERR <<HERE;
Cannot find a local $what for $module, and LWP is not installed
so I can't download it. Please download it manually and re-run
this script.
HERE
return;
}
my $url = "$PACKAGES_URL/$module/$module";
my $downloadDir = $installationRoot;
if ( $ENV{TWIKI_PACKAGES} && -d $ENV{TWIKI_PACKAGES} ) {
# see if we can write in $TWIKI_PACKAGES
my $test = $ENV{TWIKI_PACKAGES} . '/' . $$;
if ( open( F, '>', $test ) ) {
close(F);
unlink($test);
$downloadDir = $ENV{TWIKI_PACKAGES};
}
}
my $response;
foreach my $type (@$types) {
$response = $lwp->get( $url . $type );
if ( $response->is_success() ) {
$f = $downloadDir . '/' . $module . $type;
open( F, '>', $f ) || die "Failed to open $f for write: $!";
binmode F;
print F $response->content();
close(F);
last;
}
}
unless ( $f && -e $f ) {
print STDERR "Failed to download $module $what\n",
$response->status_line(), "\n";
return;
}
else {
print "Downloaded $what from $PACKAGES_URL to $f\n";
}
return $f;
}
# Try and find an archive for the named module.
sub getArchive {
my $module = shift;
return getComponent( $module, \@archTypes, 'archive' );
}
# Try and find an installer for the named module.
sub getInstaller {
my $module = shift;
return getComponent( $module, ['_installer'], 'installer' );
}
# install a package by running the installer
sub installPackage {
my ($module) = @_;
my $script = getInstaller($module);
if ( $script && -e $script ) {
my $cmd = "perl $script";
$cmd .= ' -a' if $noconfirm;
$cmd .= ' -d' if $downloadOK;
$cmd .= ' -r' if $reuseOK;
$cmd .= ' -n' if $inactive;
$cmd .= ' install';
local $| = 0;
# Fork the installation of the downloaded package.
my $pid = fork();
if ($pid) {
wait();
if ($?) {
print STDERR "Installation of $module failed: $?\n";
return 0;
}
}
else {
exec($cmd);
}
}
else {
print STDERR <<HERE;
I cannot locate an installer for $module.
$module may not have been designed to be installed with this installer.
I might be able to download and unpack a simple archive, but you will
have to satisfy the dependencies and finish the install of it yourself,
as per the instructions for $module.
HERE
my $ans = ask("Would you like me to try to get an archive of $module?");
return 0 unless ($ans);
my $arch = getArchive($module);
unless ($arch) {
print STDERR <<HERE;
Cannot locate an archive for $module; installation failed.
HERE
return 0;
}
# Unpack the archive in place. Don't bother trying to
# look for a MANIFEST or run the installer script - it
# was probably packaged by an amateur.
unpackArchive( $arch, $installationRoot );
}
return 1;
}
=pod
---++ StaticMethod unpackArchive($archive [,$dir] )
Unpack an archive. The unpacking method is determined from the file
extension e.g. .zip, .tgz. .tar, etc. If $dir is not given, unpack
to a temporary directory, the name of which is returned.
=cut
sub unpackArchive {
my ( $name, $dir ) = @_;
$dir ||= File::Temp::tempdir( CLEANUP => 1 );
chdir($dir);
unless ( $name =~ /\.zip/i && unzip($name)
|| $name =~ /(\.tar\.gz|\.tgz|\.tar)/ && untar($name) )
{
$dir = undef;
print STDERR "Failed to unpack archive $name\n";
}
chdir($installationRoot);
return $dir;
}
sub unzip {
my $archive = shift;
eval 'require Archive::Zip';
if ($@) {
my $zip = Archive::Zip->new();
my $err = $zip->read($archive);
if ($err) {
print STDERR "Could not openzip file $archive (" . $err . "\n";
return 0;
}
my @members = $zip->members();
foreach my $member (@members) {
my $file = $member->fileName();
my $target = $file;
my $err = $zip->extractMember( $file, $target );
if ($err) {
print STDERR "Failed to extract '$file' from zip file ",
$zip, ". Archive may be corrupt.\n";
return 0;
}
else {
print " $target\n";
}
}
}
else {
print STDERR
"Archive::Zip is not installed; trying unzip on the command line\n";
print `unzip $archive`;
if ($?) {
print STDERR "unzip failed: $?\n";
return 0;
}
}
return 1;
}
sub untar {
my $archive = shift;
my $compressed = ( $archive =~ /z$/i ) ? 'z' : '';
eval 'require Archive::Tar';
if ($@) {
my $tar = Archive::Tar->new();
my $numberOfFiles = $tar->read( $archive, $compressed );
unless ( $numberOfFiles > 0 ) {
print STDERR "Could not open tar file $archive ("
. $tar->error() . "\n";
return 0;
}
my @members = $tar->list_files();
foreach my $file (@members) {
my $target = $file;
my $ok = $tar->extract_file( $file, $target );
unless ($ok) {
print STDERR 'Failed to extract ', $file, ' from tar file ',
$tar, ". Archive may be corrupt.\n";
return 0;
}
else {
print " $target\n";
}
}
}
else {
print STDERR
"Archive::Tar is not installed; trying tar on the command-line\n";
print `tar xvf$compressed $archive`;
if ($?) {
print STDERR "tar failed: $?\n";
return 0;
}
}
return 1;
}
# Check in.
sub checkin {
my ( $web, $topic, $file ) = @_;
return 0 unless ($session);
my $err = 1;
if ($file) {
my $origfile =
$Foswiki::cfg{PubDir} . '/' . $web . '/' . $topic . '/' . $file;
print "Add attachment $origfile\n";
return 1 if ($inactive);
print <<DONE;
##########################################################
Adding file: $file to installation ....
(attaching it to $web.$topic)
DONE
# Need copy of file to upload it, use temporary location
# Use non object version of File::Temp for Perl 5.6.1 compatibility
my @stats = stat $origfile;
my $fileSize = $stats[7];
my $fileDate = $stats[9];
# make sure it's readable and writable by the current user
chmod( ( $stats[2] & 07777 ) | 0600, $origfile );
my ( $tmp, $tmpfilename ) = File::Temp::tempfile( unlink => 1 );
File::Copy::copy( $origfile, $tmpfilename )
|| die "$origfile could not be copied to tmp dir ($tmpfilename): $!";
eval {
Foswiki::Func::saveAttachment(
$web, $topic, $file,
{
comment => 'Saved by install script',
file => $tmpfilename,
filesize => $fileSize,
filedate => $fileDate
}
);
};
$err = $@;
}
else {
print "Add topic $web.$topic\n";
return 1 if ($inactive);
print <<DONE;
##########################################################
Adding topic: $web.$topic to installation ....
DONE
# read the topic to recover meta-data
eval {
my ( $meta, $text ) = Foswiki::Func::readTopic( $web, $topic );
Foswiki::Func::saveTopic( $web, $topic, $meta, $text,
{ comment => 'Saved by install script' } );
};
$err = $@;
}
return ( !$err );
}
sub _uninstall {
my $file;
my @dead;
foreach $file ( keys %$MANIFEST ) {
if ( -e $file ) {
push( @dead, remap($file) );
}
}
unless ( $#dead > 1 ) {
print STDERR "No part of $MODULE is installed\n";
return 0;
}
print "To uninstall $MODULE, the following files will be deleted:\n";
print "\t" . join( "\n\t", @dead ) . "\n";
return 1 if $inactive;
my $reply = ask("Are you SURE you want to uninstall $MODULE?");
if ($reply) {
if ( defined &Foswiki::preuninstall ) {
Foswiki::preuninstall();
}
elsif ( defined &TWiki::preuninstall ) {
TWiki::preuninstall();
}
foreach $file ( keys %$MANIFEST ) {
if ( -e $file ) {
unlink($file);
}
}
if ( defined &Foswiki::postuninstall ) {
Foswiki::postuninstall();
}
elsif ( defined &TWiki::postuninstall ) {
TWiki::postuninstall();
}
print "### $MODULE uninstalled ###\n";
}
return 1;
}
# 1 Check dependencies
# 2 Transfer files from temporary unpack area to the target installation
# 3 Check in any files with existing ,vs on disc
# 4 Perform post-install
sub _emplace {
my $source = shift;
# For each file in the MANIFEST, move the file into the installation,
# set the permissions, and check if it is a data or pub file. If it is,
# then check it in.
my @ci_topic; # topics to checkin
my @ci_attachment; # topics to checkin
my $file;
foreach $file ( keys %$MANIFEST ) {
my $source = "$source/$file";
my $target = remap($file);
print "Install $target, permissions $MANIFEST->{$file}->{perms}\n";
unless ($inactive) {
if ( -e $target ) {
# Save current permissions, remove write protect for Windows sake,
# Back up the file and then restore the original permissions
my $mode = ( stat($file) )[2];
chmod( oct(600), "$target" );
chmod( oct(600), "$target.bak" ) if ( -e "$target.bak" );
if ( File::Copy::move( $target, "$target.bak" ) ) {
chmod( $mode, "$target.bak" );
}
else {
print STDERR "Could not create $target.bak: $!\n";
}
}
my @path = split( /[\/\\]+/, $target );
pop(@path);
if ( scalar(@path) ) {
File::Path::mkpath( join( '/', @path ) );
}
File::Copy::move( $source, $target )
|| die "Failed to move $source to $target: $!\n";
}
unless ($inactive) {
chmod( oct( $MANIFEST->{$file}->{perms} ), $target )
|| print STDERR
"WARNING: cannot set permissions on $target: $!\n";
}
if ( $MANIFEST->{$file}->{ci} ) {
if ( $target =~ /^data\/(\w+)\/(\w+).txt$/ ) {
push( @ci_topic, $target );
}
elsif ( $target =~ /^pub\/(\w+)\/(\w+)\/([^\/]+)$/ ) {
push( @ci_attachment, $target );
}
}
}
my @bads;
foreach $file (@ci_topic) {
$file =~ /^data\/(.*)\/(\w+).txt$/;
unless ( checkin( $1, $2, undef ) ) {
push( @bads, $file );
}
}
foreach $file (@ci_attachment) {
$file =~ /^pub\/(.*)\/(\w+)\/([^\/]+)$/;
unless ( checkin( $1, $2, $3 ) ) {
push( @bads, $file );
}
}
if ( scalar(@bads) ) {
print STDERR '
WARNING: I cannot automatically update the local revision history for:', "\n\t";
print STDERR join( "\n\t", @bads );
print STDERR <<DONE;
You can update the revision histories of these files by:
1. Editing any topics and saving them without changing them,
2. Uploading attachments to the same topics again.
Ignore this warning unless you have modified the files locally.
DONE
}
}
sub usage {
print STDERR <<DONE;
Usage: ${MODULE}_installer -a -n -d -r -u install
${MODULE}_installer -a -n uninstall
${MODULE}_installer manifest
${MODULE}_installer dependencies
Operates on the directory tree below where it is run from,
so should be run from the top level of your Foswiki installation.
install will check dependencies and perform any required
post-install steps.
uninstall will remove all files that were installed for
$MODULE even if they have been locally modified.
-a means don't prompt for confirmation before resolving
dependencies
-d means auto-download if -a (no effect if not -a)
-r means reuse packages on disc if -a (no effect if not -a)