-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpseudo-install.pl
executable file
·1589 lines (1378 loc) · 46.7 KB
/
pseudo-install.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
#!/usr/bin/perl -wT
# See bottom of file for license and copyright information
use strict;
use warnings;
use re 'taint';
use File::Path();
use File::Copy();
use File::Spec();
use FindBin();
use Cwd();
my $usagetext = <<'EOM';
pseudo-install extensions into a SVN (or git) checkout
This is done by a link or copy of the files listed in the MANIFEST for the
extension. The installer script is *not* called. It should be almost equivalent
to a tar zx of the packaged extension over the dev tree, except that the use
of links enable a much more useable development environment.
It picks up extensions to be installed from a search path compiled from (1) the
environment variable $FOSWIKI_EXTENSIONS, then (2) the extensions_path array
defined under the key 'pseudo-install' in the config file ($HOME/.buildcontrib
by default). The default path includes current working directory & its parent.
Usage: pseudo-install.pl -[G|C][feA][l|c|u] [-E<cfg> <module>] [all|default|
developer|<module>|git://a.git/url, a@g.it:/url etc.]
-C[onfig] - path to config file (default $HOME/.buildcontrib, or envar
$FOSWIKI_PSEUDOINSTALL_CONFIG)
-G[enerate] - generate default psuedo-install config in $HOME/.buildcontrib
-f[orce] - force an action to complete even if there are warnings
-e[nable] - automatically enable installed plugins in LocalSite.cfg
-E<c> <extn> - include extra configuration values into LocalSite.cfg. <c>.cfg
file must exist in the same dir as <extn>'s Config.spec file
-m[anual] - do not automatically enable installed plugins in LocalSite.cfg
-l[ink] - create links %linkByDefault%
-c[opy] - copy instead of linking %copyByDefault%
-u[ninstall] - self explanatory (doesn't remove dirs)
core - install core (create and link derived objects)
all - install core + all extensions (big job)
default - install core + extensions listed in lib/MANIFEST
developer - core + default + key developer environment
<module>... - one or more extensions to install (by name or git URL)
-[A]utoconf - make a simplistic LocalSite.cfg, using just the defaults in
lib/Foswiki.spec
Examples:
softlink and enable FirstPlugin and SomeContrib
perl pseudo-install.pl -force -enable -link FirstPlugin SomeContrib
Check out a new trunk, create a default LocalSite.cfg, install and enable
all the plugins for the default distribution (and then run the unit tests)
svn co http://svn.foswiki.org/trunk
cd trunk/core
./pseudo-install.pl -A developer
cd test/unit
../bin/TestRunner.pl -clean FoswikiSuite.pm
Create a git-repo-per-extension checkout: start by cloning core,
git clone git://github.com/foswiki/core.git
cd core
Then, install extensions (missing modules automatically cloned & configured
for git-svn against svn.foswiki.org; 'master' branch is svn's trunk, see [1]):
./pseudo-install.pl developer
Install & enable an extension from an abritrary git repo
./pseudo-install.pl -e git@github.com:/me/MyPlugin.git
Install UnitTestContrib and include config values into LocalSite.cfg from
lib/Foswiki/Contrib/UnitTestContrib/AutoBuildSelenium.cfg
./pseudo-install.pl -EAutoBuildSelenium UnitTestContrib
* When using git clean, add the -x modifier to clean ignored files. See [1]
* Each module's root has a .gitignore maintained w/list of derived files [1]
* [1] http://foswiki.org/Development/GitAndPseudoInstall
EOM
my %generated_files;
my $install;
my $basedir;
my $CAN_LINK;
my $force;
my $parentdir;
my $fetchedExtensionsPath;
my @error_log;
my %config;
my $do_genconfig;
my @extensions_path;
my %extensions_extra_config;
my $autoenable = 0;
my $installing = 1;
my $autoconf = 0;
my $config_file = $ENV{FOSWIKI_PSEUDOINSTALL_CONFIG};
my $internal_gzip = eval { require Compress::Zlib; 1; };
my %arg_dispatch = (
'-E' => sub {
my ($cfg) = @_;
my ($extn) = shift(@ARGV);
push( @{ $extensions_extra_config{$extn} }, $cfg );
},
'-f' => sub { $force = 1 },
'-l' => sub { $install = \&just_link },
'-c' => sub {
$install = \©_in;
$CAN_LINK = 0;
},
'-u' => sub {
$install = \&uninstall;
$installing = 0;
},
'-e' => sub {
$autoenable = 1;
},
'-m' => sub {
$autoenable = 0;
},
'-A' => sub {
$autoconf = 1;
},
'-C' => sub {
$config_file = shift(@ARGV);
},
'-G' => sub {
$do_genconfig = 1;
}
);
my %default_config = (
repos => [
{
type => 'svn',
url => 'http://svn.foswiki.org',
branches => {
pharvey => {
WikiDrawPlugin => 'branches/scratch/pharvey/WikiDrawPlugin'
},
ItaloValcy => {
ImageGalleryPlugin =>
'branches/scratch/ItaloValcy/ImageGalleryPlugin_5x10'
},
'Release01x00' => { path => 'branches/Release01x00' },
'Release01x01' => { path => 'branches/Release01x01' },
'trunk' => { path => 'trunk' },
'scratch' => { path => 'branches/scratch' }
}
},
{
type => 'git',
url => 'git://github.com/foswiki',
svn => 'http://svn.foswiki.org',
bare => 1,
note => <<'HERE'
This will automatically configure any cloned git repo with git-svn to track
svn.foswiki.org, because the svn value matches the url of the svn repo.
HERE
}
],
extensions_path => [ '$basedir/twikiplugins', '.', '$parentdir' ],
clone_dir => '$parentdir'
);
sub init {
no re 'taint';
$FindBin::Bin =~ /(.*)/; # core dir
$basedir = $1;
use re 'taint';
$parentdir = File::Spec->catdir( $basedir, '..' );
$fetchedExtensionsPath = $parentdir;
my $n = 0;
$n++ while ( -e "testtgt$n" || -e "testlink$n" );
open( my $testfile, '>', "testtgt$n" )
or die "$basedir is not writable: $!";
print $testfile "";
close $testfile;
$CAN_LINK = eval {
symlink( "testtgt$n", "testlink$n" );
1;
};
if ($CAN_LINK) {
$install = \&just_link;
}
else {
$install = \©_in;
}
unlink( "testtgt$n", "testlink$n" );
return;
}
sub init_config {
if ( !$config_file ) {
if ( $ENV{HOME} ) {
$config_file = File::Spec->catfile( $ENV{HOME}, '.buildcontrib' );
}
}
if ( $config_file && -f $config_file ) {
my $buildconfig;
$config_file = untaint($config_file);
$buildconfig = do "$config_file";
die "Malformed config: '$config_file'"
unless ref($buildconfig) eq 'HASH';
if ( exists $buildconfig->{'pseudo-install'} ) {
die "Malformed config: '$config_file'"
unless ref( $buildconfig->{'pseudo-install'} ) eq 'HASH';
%config = %{ $buildconfig->{'pseudo-install'} };
}
}
if ($do_genconfig) {
genconfig();
}
if ( !scalar( keys %config ) ) {
%config = %default_config;
}
if ( $config{extensions_path} ) {
die "Malformed config: '$config_file'"
unless ref( $config{extensions_path} ) eq 'ARRAY';
@{ $config{extensions_path} } =
map { expandConfigPathTokens($_) } @{ $config{extensions_path} };
}
if ( $config{clone_dir} ) {
$config{clone_dir} = expandConfigPathTokens( $config{clone_dir} );
}
return;
}
sub init_extensions_path {
my %paths;
if ( $ENV{FOSWIKI_EXTENSIONS} ) {
my @filtered = filterpaths( \%paths, $ENV{FOSWIKI_EXTENSIONS} );
# Only put FOSWIKI_EXTENSIONS first in the search path if that dir is
# not already in the search path
if ( scalar(@filtered) ) {
unshift( @extensions_path, @filtered );
}
}
push( @extensions_path,
filterpaths( \%paths, @{ $config{extensions_path} } ) );
return;
}
sub genconfig {
my $buildconfig;
my $needforce;
# Detect if we're about to clobber some stuff in the existsing buildconfig
if ( -f $config_file ) {
$buildconfig = do "$config_file";
if ( exists $buildconfig->{'pseudo-install'} ) {
foreach my $key ( keys %default_config ) {
if ( exists $buildconfig->{'pseudo-install'}{$key} ) {
$needforce = 1;
}
}
}
}
if ( $needforce && !$force ) {
die <<"HERE";
Not writing a default pseudo-install config into '$config_file': already
contains a pseudo-install config, and -f (force) not specified.
HERE
}
elsif ( !-f $config_file || -w $config_file ) {
foreach my $key ( keys %default_config ) {
$buildconfig->{'pseudo-install'}{$key} = $default_config{$key};
}
$config_file = untaint($config_file);
if ( open( my $fh, '>', $config_file ) ) {
require Data::Dumper;
print $fh Data::Dumper->Dump( [$buildconfig] );
if ( close($fh) ) {
print <<"HERE";
Successfully wrote a default 'pseudo-install' config into
'$config_file'
HERE
}
}
else {
die <<"HERE";
Failed to write a default pseudo-install config into
'$config_file': error opening for write
HERE
}
}
else {
die <<"HERE";
Failed to write a default pseudo-install config into
'$config_file': not writeable
HERE
}
return;
}
# Remove duplicates and missing dirs
sub filterpaths {
my ( $map, @paths ) = @_;
my @result;
foreach my $p ( grep { -d $_ } @paths ) {
if ( !exists $map->{$p} ) {
$map->{$p} = 1;
push( @result, $p );
}
}
return @result;
}
sub expandConfigPathTokens {
my ($path) = @_;
$path =~ s/\$parentdir/$parentdir/g;
$path =~ s/\$basedir/$basedir/g;
return $path;
}
sub untaint {
no re 'taint';
$_[0] =~ /^(.*)$/;
use re 'taint';
return $1;
}
sub error {
my @errors = @_;
push @error_log, @errors;
warn 'ERROR: ', @errors;
return;
}
sub trace {
#warn "...",@_,"\n";
return;
}
sub usage {
my $def = '(default behaviour on this platform)';
my $linkByDefault = $CAN_LINK ? $def : "";
my $copyByDefault = $CAN_LINK ? "" : $def;
$usagetext =~ s/%linkByDefault%/$linkByDefault/g;
$usagetext =~ s/%copyByDefault%/$copyByDefault/g;
print $usagetext;
return;
}
sub findRelativeTo {
my ( $startdir, $name ) = @_;
my @path = File::Spec->splitdir($startdir);
while ( scalar(@path) > 0 ) {
my $found = File::Spec->catfile( @path, $name );
return $found if -e $found;
pop(@path);
}
return;
}
sub findModuleDir {
my ($module) = @_;
my $moduleDir;
foreach my $dir (@extensions_path) {
my $testDir = File::Spec->catdir( $dir, $module );
if ( -d $testDir ) {
$moduleDir = $testDir;
last;
}
}
return $moduleDir;
}
sub urlToModuleName {
my ($url) = @_;
$url =~ /^.*\/([^\.\/]+)(\.git)?\/?$/;
return $1;
}
sub installModule {
my ($module) = @_;
# Assume that only URLs will have '.' or '/', never module names
if ( $installing && $module =~ /[\/\.]/ ) {
cloneModuleByURL( $config{clone_dir}, $module );
$module = urlToModuleName($module);
}
return installModuleByName($module);
}
sub isContrib {
my ($module) = @_;
return $module =~ /(Contrib|Skin|AddOn|^core)$/;
}
sub installModuleByName {
my $module = shift;
my $subdir = 'Plugins';
my $libDir = 'Foswiki';
my $moduleDir;
my $manifest;
# If $ignoreBlock is true, will ignore blocking files (not complain
# if a file it is trying to copy in / link already exists)
my $ignoreBlock = 0;
$module =~ s#/+$##; #remove trailing slashes
print "Processing $module\n";
$subdir = 'Contrib' if isContrib($module);
if ( $module eq 'core' ) {
# Special install procedure for core, processes manifest
# and checks for missing files
$moduleDir = '.';
$ignoreBlock = 1;
}
else {
$moduleDir = findModuleDir($module);
}
if ( $installing && !defined $moduleDir ) {
$moduleDir = cloneModuleByName($module);
}
unless ( defined $moduleDir && -d $moduleDir ) {
warn "--> Could not find $module\n";
return;
}
$moduleDir = Cwd::realpath($moduleDir);
$manifest = findRelativeTo(
File::Spec->catdir( $moduleDir, 'lib', 'Foswiki', $subdir, $module ),
'MANIFEST' );
if ( !-e $manifest ) {
$manifest = findRelativeTo(
File::Spec->catdir( $moduleDir, 'lib', 'TWiki', $subdir, $module ),
'MANIFEST'
);
$libDir = 'TWiki';
}
if ( -e $manifest ) {
installFromMANIFEST( $module, $moduleDir, $manifest, $ignoreBlock );
update_gitignore_file($moduleDir);
}
else {
$libDir = undef;
warn "---> No MANIFEST in $module (at $manifest)\n";
}
return $libDir;
}
sub populateSVNRepoListings {
my ($svninfo) = @_;
my $ctx;
if ( !eval { require SVN::Client; 1 } ) {
warn <<'HERE';
SVN::Client not installed, unable discover branch listings from SVN
HERE
return;
}
$ctx = SVN::Client->new();
$svninfo->{extensions} = {};
while ( my ( $branch, $branchdata ) = each( %{ $svninfo->{branches} } ) ) {
# If it contains a path key, then assume we need to populate the list
# of extensions this branch contains via SVN listing
if ( $branchdata->{path} ) {
my @branchextensions;
print "Listing $svninfo->{url}/$branchdata->{path}\n";
@branchextensions =
keys %{
$ctx->ls( $svninfo->{url} . '/' . $branchdata->{path},
'HEAD', 0 )
};
foreach my $ext (@branchextensions) {
if ( $ext ne 't2fos.sh' ) {
$branchdata->{$ext} = $branchdata->{path} . '/' . $ext;
push(
@{ $svninfo->{extensions}->{$ext} },
{ branch => $branch, path => $branchdata->{$ext} }
);
}
}
}
# Else, we have a manual branch+path mapping for a given extension
else {
while ( my ( $ext, $path ) = each %{$branchdata} ) {
push(
@{ $svninfo->{extensions}->{$ext} },
{ branch => $branch, path => $path }
);
}
}
}
return;
}
sub gitClone2GitSVN {
my ( $module, $moduleDir, $svninfo ) = @_;
my $success = 0;
if ( $svninfo->{extensions}->{$module} ) {
foreach my $branchdata ( @{ $svninfo->{extensions}->{$module} } ) {
if ( $branchdata->{branch} ne 'trunk' ) {
my $svnremoteref = "refs/remotes/$branchdata->{path}";
my $gitremoteref = "origin/$branchdata->{branch}";
my $svnurl =
"$svninfo->{url}/$svninfo->{branches}->{$branchdata->{branch}}->{path}/$module";
print "Aliasing $svnremoteref as $gitremoteref\n";
do_commands(<<"HERE");
cd $moduleDir
git update-ref $svnremoteref $gitremoteref
HERE
print "\tclone from SVN url $svnurl :$svnremoteref\n";
do_commands(<<"HERE");
cd $moduleDir
git config svn-remote.$branchdata->{branch}.url $svnurl
git config svn-remote.$branchdata->{branch}.fetch :$svnremoteref
HERE
}
}
print
"Aliasing refs/remotes/$svninfo->{branches}->{trunk}->{path}/$module as refs/remotes/trunk &\n";
print "Aliasing refs/remotes/trunk as origin/master\n";
do_commands(<<"HERE");
cd $moduleDir
git update-ref refs/remotes/trunk origin/master
git svn init $svninfo->{url} -T $svninfo->{branches}->{trunk}->{path}/$module
HERE
$success = 1;
}
else {
print STDERR <<"HERE";
Could not find $module at $svninfo->{url} in any branch paths,
module is not configured for git-svn
HERE
}
return $success;
}
sub do_commands {
my ($commands) = @_;
#print $commands . "\n";
local $ENV{PATH} = untaint( $ENV{PATH} );
return `$commands`;
}
sub connectGitRepoToSVN {
my ( $module, $moduleDir, $svninfo ) = @_;
if ( !$svninfo->{extensions}->{$module} ) {
populateSVNRepoListings($svninfo);
}
return gitClone2GitSVN( $module, $moduleDir, $svninfo );
}
sub connectGitRepoToSVNByRepoURL {
my ( $module, $moduleDir, $svnreponame ) = @_;
my $lookingupname = 1;
my $repoIndex = 0;
while ( $lookingupname && $repoIndex < scalar( @{ $config{repos} } ) ) {
if ( $config{repos}->[$repoIndex]->{url} eq $svnreponame ) {
$lookingupname = 0;
connectGitRepoToSVN( $module, $moduleDir,
$config{repos}->[$repoIndex] );
}
else {
$repoIndex = $repoIndex + 1;
}
}
return;
}
sub cloneModuleByName {
my ($module) = @_;
my $cloned = 0;
my $repoIndex = 0;
my $moduleDir = File::Spec->catdir( $config{clone_dir}, $module );
while ( !$cloned && ( $repoIndex < scalar( @{ $config{repos} } ) ) ) {
if ( $config{repos}->[$repoIndex]->{type} eq 'git' ) {
my $url = $config{repos}->[$repoIndex]->{url} . "/$module";
if ( $config{repos}->[$repoIndex]->{bare} ) {
$url .= '.git';
}
cloneModuleByURL( $config{clone_dir}, $url );
if ( -d $moduleDir ) {
$cloned = 1;
if ( $config{repos}->[$repoIndex]->{svn} ) {
connectGitRepoToSVNByRepoURL( $module, $moduleDir,
$config{repos}->[$repoIndex]->{svn} );
}
print "Cloned $module OK\n";
}
else {
$repoIndex = $repoIndex + 1;
}
}
else {
$repoIndex = $repoIndex + 1;
}
}
if ( !checkModuleByNameHasSVNBranch( 'core', 'Release01x01' ) ) {
my $svnRepo = getSVNRepoByModuleBranchName( 'core', 'Release01x01' );
print "It seems your 'core' checkout isn't connected to a svn repo... ";
if ($svnRepo) {
print "connecting\n";
connectGitRepoToSVNByRepoURL( 'core',
File::Spec->catdir( $config{clone_dir}, 'core' ),
$svnRepo->{url} );
}
else {
print "couldn't find any svn repo containing 'core'\n";
}
}
return $moduleDir;
}
sub getSVNRepoByModuleBranchName {
my ( $module, $branch ) = @_;
my $svnRepo;
my $nRepos = scalar( @{ $config{repos} } );
my $i = 0;
while ( !$svnRepo && $i < $nRepos ) {
my $repo = $config{repos}->[$i];
if ( $repo->{type} eq 'svn' ) {
if ( $repo->{branches}->{$branch} ) {
if ( $repo->{branches}->{$branch}->{$module} ) {
$svnRepo = $repo;
}
}
}
$i += 1;
}
return $svnRepo;
}
sub checkModuleByNameHasSVNBranch {
my ( $module, $branch ) = @_;
my $moduleDir = File::Spec->catdir( $config{clone_dir}, $module );
return do_commands(<<"HERE") ? 1 : 0;
cd $moduleDir
git config --get svn-remote.$branch.url
HERE
}
sub cloneModuleByURL {
my ( $target, $source ) = @_;
#TODO: Make this capable of using svn as an alternative
return gitCloneFromURL( $target, $source );
}
sub gitCloneFromURL {
my ( $target, $source ) = @_;
my $command = "cd $target && git clone $source";
my $moduleDir = File::Spec->catdir( $target, urlToModuleName($source) );
if ( !-d $moduleDir ) {
print "Trying clone from $source...\n";
local $ENV{PATH} = untaint( $ENV{PATH} );
trace `$command`;
}
else {
print STDERR "$moduleDir already exists\n";
}
return;
}
sub installFromMANIFEST {
my ( $module, $moduleDir, $manifest, $ignoreBlock ) = @_;
trace "Using manifest from $manifest";
open( my $df, '<', $manifest )
or die "Cannot open manifest $manifest for reading: $!";
foreach my $file (<$df>) {
chomp($file);
next unless $file =~ /^\w+/;
$file =~ s/\s.*$//;
next if -d File::Spec->catdir( $moduleDir, $file );
$file = untaint($file);
my ( undef, $dir ) = File::Spec->splitpath($file);
$install->( $moduleDir, $dir, $file, $ignoreBlock );
# Unlink zip generated by compression. This is inefficient, but
# the alternative is comparing file dates, which is hard work.
if ( -f File::Spec->catfile( $moduleDir, $file ) && $file =~ /\.gz$/ ) {
unlink _cleanPath( File::Spec->catfile( $moduleDir, $file ) );
}
if ($installing) {
# Special cases for derived objects created by compression and/or
# zipping.
my $found = -f File::Spec->catfile( $moduleDir, $file );
unless ($found) {
$found = generateAlternateVersion( $moduleDir, $dir, $file,
$CAN_LINK );
}
unless ($found) {
warn 'WARNING: Cannot find source file for '
. File::Spec->catfile( $moduleDir, $file ) . "\n";
}
}
}
close $df;
if ( -d File::Spec->catdir( $moduleDir, 'test', 'unit', $module ) ) {
opendir( $df,
File::Spec->catdir( $moduleDir, 'test', 'unit', $module ) );
foreach my $f ( grep { /\.pm$/ } readdir($df) ) {
$f = untaint($f);
$install->(
$moduleDir,
File::Spec->catdir( 'test', 'unit', $module ),
File::Spec->catfile( 'test', 'unit', $module, $f ),
$ignoreBlock
);
}
closedir $df;
}
# process dependencies, if we are installing
if ($installing) {
my $deps = $manifest;
$deps =~ s/MANIFEST/DEPENDENCIES/;
if ( open( $df, '<', $deps ) ) {
trace "read deps from $deps";
my $skipnext = 0;
foreach my $dep (<$df>) {
chomp($dep);
if ($skipnext) {
$skipnext = 0;
next;
}
next unless $dep =~ /^\w+/;
# Evaluate the ONLYIF. It applies to the next dependency line.
if ( $dep =~ /^ONLYIF\s+(.+)$/ ) {
my $cond = untaint($1);
# We may need to require to check if modules are
# already installed.
unshift( @INC, 'lib' );
# Hack to require a referenced Foswiki:: module.
# Required for $Foswiki::Plugins::VERSION type references
if ( $cond =~ /\$(Foswiki[\w:]*)::\w+/ ) {
my $p = $1;
unless (
do { local $SIG{__WARN__}; eval "require $p; 1" }
)
{
print STDERR
"require '$p' for ONLYIF $cond failed: $@\n";
$skipnext = 1;
next;
}
}
unless (
do { local $SIG{__WARN__}; eval $cond }
)
{
$skipnext = 1;
next;
}
next;
}
satisfyDependency( split( /\s*,\s*/, $dep ) );
}
close $df;
}
else {
error "*** Could not open $deps\n";
}
}
if ( $installing && $autoconf ) {
# Read current LocalSite.cfg to see if the current module is enabled
my $localSiteCfg =
File::Spec->catfile( $basedir, 'lib', 'LocalSite.cfg' );
open my $lsc, '<', $localSiteCfg
or die "Cannot open $localSiteCfg for reading: $!";
my $enabled = 0;
my $spec;
my $localConfiguration = '';
while (<$lsc>) {
# Can't $_ eq '1;' because /^1;$/ is less picky about newlines
next if /^1;$/;
$localConfiguration .= $_;
if (m/^\$Foswiki::cfg{Plugins}{$module}{(\S+)}\s+=\s+(\S+);/) {
if ( $1 eq 'Enabled' ) {
$enabled = $2;
}
elsif ( $1 eq 'Module' ) {
my $moduleName = $2;
$moduleName =~ s#'##g;
$spec =
File::Spec->catfile( $basedir, 'lib',
split( '::', $moduleName ),
'Config.spec' );
}
}
}
close $lsc;
if ( !$spec && isContrib($module) ) {
$spec = File::Spec->catfile( $basedir, 'lib', 'Foswiki', 'Contrib',
$module, 'Config.spec' );
}
if ( ( $enabled || isContrib($module) ) && $spec && -f $spec ) {
if ( open( my $pluginSpec, '<', $spec ) ) {
$localConfiguration .= "# $module specific configuration\n";
while (<$pluginSpec>) {
next if /^(?:1;|\s*|#.*)$/;
$localConfiguration .= $_;
}
close $pluginSpec;
$localConfiguration .= "1;\n";
if ( open( my $lsc, '>', $localSiteCfg ) ) {
print $lsc $localConfiguration;
close $lsc;
warn "Added ${module}'s Config.spec to $localSiteCfg\n";
}
else {
warn "Could not write new $localSiteCfg: $!\n";
}
}
else {
warn "Could not open spec file $spec for $module: $!\n";
}
}
}
return;
}
sub package_exists {
my ($mod) = @_;
local @INC = @INC;
my @curdir = File::Spec->splitdir( File::Spec->curdir() );
# Add ./lib to front of INC path
unshift( @INC, File::Spec->catdir( @curdir, 'lib' ) );
# Add ./lib/CPAN/lib to end of INC path
push( @INC, File::Spec->catdir( @curdir, qw(lib CPAN lib) ) );
no re 'taint';
$mod =~ /^([\w:]+)$/;
$mod = $1;
use re 'taint';
{
local $SIG{__WARN__};
return eval "require $mod; 1"
}
}
sub satisfyDependency {
my ( $mod, $cond, $type, $mess ) = @_;
# First see if we can find it in the install or @INC path
if ( package_exists($mod) ) {
trace "$mod is already installed";
}
else {
trace "$mod is not installed";
# Not found, is it required?
if ( $mess !~ /^required/i ) {
warn "$mod is an optional dependency, but is not installed\n";
return;
}
if ( $type eq 'perl' && $mod =~ /^Foswiki/ ) {
error
"**** $mod is a required Foswiki dependency, but it is not installed\n";
}
else {
error
"**** $mod is a required dependency, but it is not installed\n";
}
}
return;
}
sub linkOrCopy {
my ( $moduleDir, $source, $target, $link ) = @_;
my $srcfile = File::Spec->catfile( $moduleDir, $source );
my $dstfile = File::Spec->catfile( $moduleDir, $target );
trace '...' . ( $link ? 'link' : 'copy' ) . " $srcfile to $dstfile";
if ($link) {
$srcfile = _cleanPath($srcfile);
$dstfile = _cleanPath($dstfile);
symlink( $srcfile, $dstfile )
or die "Failed to link $srcfile as $dstfile: $!";
print "Linked $source as $target\n";
$generated_files{$basedir}{$target} = 1;
}
else {
if ( -e $srcfile ) {
File::Copy::copy( $srcfile, $target )
|| die "Couldn't install $target: $!";
}
print "Copied $source as $target\n";
}
$generated_files{$moduleDir}{$target} = 1;
return;
}
# Tries to find out alternate versions of a file
# So that file.js.gz and file.uncompressed.js get created
sub generateAlternateVersion {
my ( $moduleDir, $dir, $file, $link ) = @_;
my $found = 0;
my $compress = 0;
trace( File::Spec->catfile( $moduleDir, $file ) . ' not found' );
if ( !$found && $file =~ /(.*)\.gz$/ ) {
$file = $1;
$found = ( -f File::Spec->catfile( $moduleDir, $1 ) );
$compress = 1;
}
if ( !$found
&& $file =~ /^(.+)(\.(?:un)?compressed|_src)(\..+)$/
&& -f File::Spec->catfile( $moduleDir, $1 . $3 ) )
{
linkOrCopy $moduleDir, $file, $1 . $3, $link;
$found++;
}
elsif ( !$found && $file =~ /^(.+)(\.[^\.]+)$/ ) {
my ( $src, $ext ) = ( $1, $2 );
for my $kind (qw( .uncompressed .compressed _src )) {
my $srcfile = $src . $kind . $ext;
if ( -f File::Spec->catfile( $moduleDir, $srcfile ) ) {
linkOrCopy $moduleDir, $srcfile, $file, $link;
$found++;
last;
}
}
}
if ( $found && $compress ) {
trace "...compressing $file to create $file.gz";
if ($internal_gzip) {
open( my $if, '<', _cleanPath($file) )
or die "Failed to open $file to read: $!";
local $/ = undef;
my $text = <$if>;
close($if);
$text = Compress::Zlib::memGzip($text);
open( my $of, '>', _cleanPath($file) . ".gz" )
or die "Failed to open $file.gz to write: $!";
binmode $of;
print $of $text;
close($of);
$generated_files{$moduleDir}{"$file.gz"} = 1;
}