/
Build.pm
1220 lines (1016 loc) · 42.8 KB
/
Build.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
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
# $Id$
#
# BioPerl module for Bio::Root::Build
#
# Please direct questions and support issues to <bioperl-l@bioperl.org>
#
# Cared for by Sendu Bala <bix@sendu.me.uk>
#
# Copyright Sendu Bala
#
# You may distribute this module under the same terms as perl itself
# POD documentation - main docs before the code
=head1 NAME
Bio::Root::Build - A common Module::Build subclass base for BioPerl distributions
=head1 SYNOPSIS
...TO BE ADDED
=head1 DESCRIPTION
This is a subclass of Module::Build so we can override certain methods and do
fancy stuff
It was first written against Module::Build::Base v0.2805. Many of the methods
here are copy/pasted from there in their entirety just to change one or two
minor things, since for the most part Module::Build::Base code is hard to
cleanly override.
=head1 FEEDBACK
=head2 Mailing Lists
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to
the Bioperl mailing list. Your participation is much appreciated.
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
=head2 Support
Please direct usage questions or support issues to the mailing list:
I<bioperl-l@bioperl.org>
rather than to the module maintainer directly. Many experienced and
reponsive experts will be able look at the problem and quickly
address it. Please include a thorough description of the problem
with code and data examples if at all possible.
=head2 Reporting Bugs
Report bugs to the Bioperl bug tracking system to help us keep track
of the bugs and their resolution. Bug reports can be submitted via
the web:
http://bugzilla.open-bio.org/
=head1 AUTHOR - Sendu Bala
Email bix@sendu.me.uk
=head1 APPENDIX
The rest of the documentation details each of the object methods.
Internal methods are usually preceded with a _
=cut
package Bio::Root::Build;
BEGIN {
# we really need Module::Build to be installed
eval "use base Module::Build; 1" or die "This package requires Module::Build v0.2805 or greater to install itself.\n$@";
# ensure we'll be able to reload this module later by adding its path to inc
use Cwd;
use lib Cwd::cwd();
}
use strict;
use warnings;
our $VERSION = '1.006001';
our @extra_types = qw(options excludes_os feature_requires test); # test must always be last in the list!
our $checking_types = "requires|conflicts|".join("|", @extra_types);
# our modules are in Bio, not lib
sub find_pm_files {
my $self = shift;
foreach my $pm (@{$self->rscan_dir('Bio', qr/\.pm$/)}) {
$self->{properties}{pm_files}->{$pm} = File::Spec->catfile('lib', $pm);
}
$self->_find_file_by_type('pm', 'lib');
}
# ask what scripts to install (this method is unique to bioperl)
sub choose_scripts {
my $self = shift;
my $accept = shift;
# we can offer interactive installation by groups only if we have subdirs
# in scripts and no .PLS files there
opendir(my $scripts_dir, 'scripts') or die "Can't open directory 'scripts': $!\n";
my $int_ok = 0;
my @group_dirs;
while (my $thing = readdir($scripts_dir)) {
next if $thing =~ /^\./;
next if $thing eq 'CVS';
if ($thing =~ /PLS$|pl$/) {
$int_ok = 0;
last;
}
$thing = File::Spec->catfile('scripts', $thing);
if (-d $thing) {
$int_ok = 1;
push(@group_dirs, $thing);
}
}
closedir($scripts_dir);
my $question = $int_ok ? "Install [a]ll BioPerl scripts, [n]one, or choose groups [i]nteractively?" : "Install [a]ll BioPerl scripts or [n]one?";
my $prompt = $accept ? 'a' : $self->prompt($question, 'a');
if ($prompt =~ /^[aA]/) {
$self->log_info(" - will install all scripts\n");
$self->notes(chosen_scripts => 'all');
}
elsif ($prompt =~ /^[iI]/) {
$self->log_info(" - will install interactively:\n");
my @chosen_scripts;
foreach my $group_dir (@group_dirs) {
my $group = File::Basename::basename($group_dir);
print " * group '$group' has:\n";
my @script_files = @{$self->rscan_dir($group_dir, qr/\.PLS$|\.pl$/)};
foreach my $script_file (@script_files) {
my $script = File::Basename::basename($script_file);
print " $script\n";
}
my $result = $self->prompt(" Install scripts for group '$group'? [y]es [n]o [q]uit", 'n');
die if $result =~ /^[qQ]/;
if ($result =~ /^[yY]/) {
$self->log_info(" + will install group '$group'\n");
push(@chosen_scripts, @script_files);
}
else {
$self->log_info(" - will not install group '$group'\n");
}
}
my $chosen_scripts = @chosen_scripts ? join("|", @chosen_scripts) : 'none';
$self->notes(chosen_scripts => $chosen_scripts);
}
else {
$self->log_info(" - won't install any scripts\n");
$self->notes(chosen_scripts => 'none');
}
print "\n";
}
# our version of script_files doesn't take args but just installs those scripts
# requested by the user after choose_scripts() is called. If it wasn't called,
# installs all scripts in scripts directory
sub script_files {
my $self = shift;
unless (-d 'scripts') {
return {};
}
my $chosen_scripts = $self->notes('chosen_scripts');
if ($chosen_scripts) {
return if $chosen_scripts eq 'none';
return { map {$_, 1} split(/\|/, $chosen_scripts) } unless $chosen_scripts eq 'all';
}
return $_ = { map {$_,1} @{$self->rscan_dir('scripts', qr/\.PLS$|\.pl$/)} };
}
# process scripts normally, except that we change name from *.PLS to bp_*.pl
sub process_script_files {
my $self = shift;
my $files = $self->find_script_files;
return unless keys %$files;
my $script_dir = File::Spec->catdir($self->blib, 'script');
File::Path::mkpath( $script_dir );
foreach my $file (keys %$files) {
my $result = $self->copy_if_modified($file, $script_dir, 'flatten') or next;
$self->fix_shebang_line($result) unless $self->os_type eq 'VMS';
$self->make_executable($result);
my $final = File::Basename::basename($result);
$final =~ s/\.PLS$/\.pl/; # change from .PLS to .pl
$final =~ s/^/bp_/ unless $final =~ /^bp/; # add the "bp" prefix
$final = File::Spec->catfile($script_dir, $final);
$self->log_info("$result -> $final\n");
if (-e $final) {
unlink $final || warn "[WARNING] Deleting '$final' failed!\n";
}
File::Copy::move($result, $final) or die "Can't rename '$result' to '$final': $!";
}
}
# extended to handle extra checking types
sub features {
my $self = shift;
my $ph = $self->{phash};
if (@_) {
my $key = shift;
if ($ph->{features}->exists($key)) {
return $ph->{features}->access($key, @_);
}
if (my $info = $ph->{auto_features}->access($key)) {
my $failures = $self->prereq_failures($info);
my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
return !$disabled;
}
return $ph->{features}->access($key, @_);
}
# No args - get the auto_features & overlay the regular features
my %features;
my %auto_features = $ph->{auto_features}->access();
while (my ($name, $info) = each %auto_features) {
my $failures = $self->prereq_failures($info);
my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
$features{$name} = $disabled ? 0 : 1;
}
%features = (%features, $ph->{features}->access());
return wantarray ? %features : \%features;
}
*feature = \&features;
# overridden to fix a stupid bug in Module::Build and extended to handle extra
# checking types
sub check_autofeatures {
my ($self) = @_;
my $features = $self->auto_features;
return unless %$features;
$self->log_info("Checking features:\n");
my $max_name_len = 0; # this wasn't set to 0 in Module::Build, causing warning in next line
$max_name_len = ( length($_) > $max_name_len ) ? length($_) : $max_name_len for keys %$features;
while (my ($name, $info) = each %$features) {
$self->log_info(" $name" . '.' x ($max_name_len - length($name) + 4));
if ($name eq 'PL_files') {
print "got $name => $info\n";
print "info has:\n";
while (my ($key, $val) = each %$info) {
print " $key => $val\n";
}
}
if ( my $failures = $self->prereq_failures($info) ) {
my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
$self->log_info( $disabled ? "disabled\n" : "enabled\n" );
my $log_text;
while (my ($type, $prereqs) = each %$failures) {
while (my ($module, $status) = each %$prereqs) {
my $required = ($type =~ /^(?:\w+_)?(?:requires|conflicts)$/) ? 1 : 0;
my $prefix = ($required) ? '-' : '*';
$log_text .= " $prefix $status->{message}\n";
}
}
$self->log_warn($log_text) if $log_text && ! $self->quiet;
}
else {
$self->log_info("enabled\n");
}
}
$self->log_info("\n");
}
# overriden just to hide pointless ugly warnings
sub check_installed_status {
my $self = shift;
open (my $olderr, ">&", \*STDERR);
open(STDERR, "/dev/null");
my $return = $self->SUPER::check_installed_status(@_);
open(STDERR, ">&", $olderr);
return $return;
}
# extend to handle option checking (which takes an array ref) and code test
# checking (which takes a code ref and must return a message only on failure)
# and excludes_os (which takes an array ref of regexps).
# also handles more informative output of recommends section
sub prereq_failures {
my ($self, $info) = @_;
my @types = (@{ $self->prereq_action_types }, @extra_types);
$info ||= {map {$_, $self->$_()} @types};
my $out = {};
foreach my $type (@types) {
my $prereqs = $info->{$type} || next;
my $status = {};
if ($type eq 'test') {
unless (keys %$out) {
if (ref($prereqs) eq 'CODE') {
$status->{message} = &{$prereqs};
# drop the code-ref to avoid Module::Build trying to store
# it with Data::Dumper, generating warnings. (And also, may
# be expensive to run the sub multiple times.)
$info->{$type} = $status->{message};
}
else {
$status->{message} = $prereqs;
}
$out->{$type}{'test'} = $status if $status->{message};
}
}
elsif ($type eq 'options') {
my @not_ok;
foreach my $wanted_option (@{$prereqs}) {
unless ($self->args($wanted_option)) {
push(@not_ok, $wanted_option);
}
}
if (@not_ok > 0) {
$status->{message} = "Command line option(s) '@not_ok' not supplied";
$out->{$type}{'options'} = $status;
}
}
elsif ($type eq 'excludes_os') {
foreach my $os (@{$prereqs}) {
if ($^O =~ /$os/i) {
$status->{message} = "This feature isn't supported under your OS ($os)";
$out->{$type}{'excludes_os'} = $status;
last;
}
}
}
else {
while ( my ($modname, $spec) = each %$prereqs ) {
$status = $self->check_installed_status($modname, $spec);
if ($type =~ /^(?:\w+_)?conflicts$/) {
next if !$status->{ok};
$status->{conflicts} = delete $status->{need};
$status->{message} = "$modname ($status->{have}) conflicts with this distribution";
}
elsif ($type =~ /^(?:\w+_)?recommends$/) {
next if $status->{ok};
my ($preferred_version, $why, $by_what) = split("/", $spec);
$by_what = join(", ", split(",", $by_what));
$by_what =~ s/, (\S+)$/ and $1/;
$status->{message} = (!ref($status->{have}) && $status->{have} eq '<none>'
? "Optional prerequisite $modname is not installed"
: "$modname ($status->{have}) is installed, but we prefer to have $preferred_version");
$status->{message} .= "\n (wanted for $why, used by $by_what)";
if ($by_what =~ /\[circular dependency!\]/) {
$preferred_version = -1;
}
my $installed = $self->install_optional($modname, $preferred_version, $status->{message});
next if $installed eq 'ok';
$status->{message} = $installed unless $installed eq 'skip';
}
elsif ($type =~ /^feature_requires/) {
next if $status->{ok};
# if there is a test code-ref, drop it to avoid
# Module::Build trying to store it with Data::Dumper,
# generating warnings.
delete $info->{test};
}
else {
next if $status->{ok};
my $installed = $self->install_required($modname, $spec, $status->{message});
next if $installed eq 'ok';
$status->{message} = $installed;
}
$out->{$type}{$modname} = $status;
}
}
}
return keys %{$out} ? $out : return;
}
# install an external module using CPAN prior to testing and installation
# should only be called by install_required or install_optional
sub install_prereq {
my ($self, $desired, $version, $required) = @_;
if ($self->under_cpan) {
# Just add to the required hash, which CPAN >= 1.81 will check prior
# to install
$self->{properties}{requires}->{$desired} = $version;
$self->log_info(" I'll get CPAN to prepend the installation of this\n");
return 'ok';
}
else {
my $question = $required ? "$desired is absolutely required prior to installation: shall I install it now using a CPAN shell?" :
"To install $desired I'll need to open a CPAN shell right now; is that OK?";
my $do_install = $self->y_n($question.' y/n', 'y');
if ($do_install) {
# Here we use CPAN to actually install the desired module, the benefit
# being we continue even if installation fails, and that this works
# even when not using CPAN to install.
require Cwd;
require CPAN;
# Save this because CPAN will chdir all over the place.
my $cwd = Cwd::cwd();
CPAN::Shell->install($desired);
my $msg;
my $expanded = CPAN::Shell->expand("Module", $desired);
if ($expanded && $expanded->uptodate) {
$self->log_info("\n\n*** (back in BioPerl Build.PL) ***\n * You chose to install $desired and it installed fine\n");
$msg = 'ok';
}
else {
$self->log_info("\n\n*** (back in BioPerl Build.PL) ***\n");
$msg = "You chose to install $desired but it failed to install";
}
chdir $cwd or die "Cannot chdir() back to $cwd: $!";
return $msg;
}
else {
return $required ? "You chose not to install the REQUIRED module $desired: you'd better install it yourself manually!" :
"Even though you wanted the optional module $desired, you chose not to actually install it: do it yourself manually.";
}
}
}
# install required modules listed in 'requires' or 'build_requires' arg to
# new that weren't already installed. Should only be called by prereq_failures
sub install_required {
my ($self, $desired, $version, $msg) = @_;
$self->log_info(" - ERROR: $msg\n");
return $self->install_prereq($desired, $version, 1);
}
# install optional modules listed in 'recommends' arg to new that weren't
# already installed. Should only be called by prereq_failures
sub install_optional {
my ($self, $desired, $version, $msg) = @_;
unless (defined $self->{ask_optional}) {
$self->{ask_optional} = $self->args->{accept}
? 'n' : $self->prompt("Install [a]ll optional external modules, [n]one, or choose [i]nteractively?", 'n');
}
return 'skip' if $self->{ask_optional} =~ /^n/i;
my $install;
if ($self->{ask_optional} =~ /^a/i) {
$self->log_info(" * $msg\n");
$install = 1;
}
else {
$install = $self->y_n(" * $msg\n Do you want to install it? y/n", 'n');
}
my $orig_version = $version;
$version = 0 if $version == -1;
if ($install && ! ($self->{ask_optional} =~ /^a/i && $orig_version == -1)) {
return $self->install_prereq($desired, $version);
}
else {
my $circular = ($self->{ask_optional} =~ /^a/i && $orig_version == -1) ? " - this is a circular dependency so doesn't get installed when installing 'all' modules. If you really want it, choose modules interactively." : '';
$self->log_info(" * You chose not to install $desired$circular\n");
return 'ok';
}
}
# there's no official way to discover if being run by CPAN, we take an approach
# similar to that of Module::AutoInstall
sub under_cpan {
my $self = shift;
unless (defined $self->{under_cpan}) {
## modified from Module::AutoInstall
my $cpan_env = $ENV{PERl5_CPAN_IS_RUNNING};
if ($ENV{PERL5_CPANPLUS_IS_RUNNING}) {
$self->{under_cpan} = $cpan_env ? 'CPAN' : 'CPANPLUS';
}
require CPAN;
unless (defined $self->{under_cpan}) {
if ($CPAN::VERSION > '1.89') {
if ($cpan_env) {
$self->{under_cpan} = 'CPAN';
}
else {
$self->{under_cpan} = 0;
}
}
}
unless (defined $self->{under_cpan}) {
# load cpan config
if ($CPAN::HandleConfig::VERSION) {
# Newer versions of CPAN have a HandleConfig module
CPAN::HandleConfig->load;
}
else {
# Older versions had the load method in Config directly
CPAN::Config->load;
}
# Find the CPAN lock-file
my $lock = File::Spec->catfile($CPAN::Config->{cpan_home}, '.lock');
if (-f $lock) {
# Module::AutoInstall now goes on to open the lock file and compare
# its pid to ours, but we're not in a situation where we expect
# the pids to match, so we take the windows approach for all OSes:
# find out if we're in cpan_home
my $cwd = File::Spec->canonpath(Cwd::cwd());
my $cpan = File::Spec->canonpath($CPAN::Config->{cpan_home});
$self->{under_cpan} = index($cwd, $cpan) > -1;
}
}
if ($self->{under_cpan}) {
$self->log_info("(I think I'm being run by CPAN/CPANPLUS, so will rely on it to handle prerequisite installation)\n");
}
else {
$self->log_info("(I think you ran Build.PL directly, so will use CPAN to install prerequisites on demand)\n");
$self->{under_cpan} = 0;
}
}
return $self->{under_cpan};
}
# overridden simply to not print the default answer if chosen by hitting return
sub prompt {
my $self = shift;
my $mess = shift or die "prompt() called without a prompt message";
my $def;
if ( $self->_is_unattended && !@_ ) {
die <<EOF;
ERROR: This build seems to be unattended, but there is no default value
for this question. Aborting.
EOF
}
$def = shift if @_;
($def, my $dispdef) = defined $def ? ($def, "[$def] ") : ('', ' ');
local $|=1;
print "$mess $dispdef";
my $ans = $self->_readline();
if ( !defined($ans) # Ctrl-D or unattended
or !length($ans) ) { # User hit return
#print "$def\n"; didn't like this!
$ans = $def;
}
return $ans;
}
# like the Module::Build version, except that we always get version from
# dist_version
sub find_dist_packages {
my $self = shift;
# Only packages in .pm files are candidates for inclusion here.
# Only include things in the MANIFEST, not things in developer's
# private stock.
my $manifest = $self->_read_manifest('MANIFEST') or die "Can't find dist packages without a MANIFEST file - run 'manifest' action first";
# Localize
my %dist_files = map { $self->localize_file_path($_) => $_ } keys %$manifest;
my @pm_files = grep {exists $dist_files{$_}} keys %{ $self->find_pm_files };
my $actual_version = $self->dist_version;
# First, we enumerate all packages & versions,
# seperating into primary & alternative candidates
my( %prime, %alt );
foreach my $file (@pm_files) {
next if $dist_files{$file} =~ m{^t/}; # Skip things in t/
my @path = split( /\//, $dist_files{$file} );
(my $prime_package = join( '::', @path[1..$#path] )) =~ s/\.pm$//;
my $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
foreach my $package ( $pm_info->packages_inside ) {
next if $package eq 'main'; # main can appear numerous times, ignore
next if grep /^_/, split( /::/, $package ); # private package, ignore
my $version = $pm_info->version( $package );
if ($version && $version != $actual_version) {
$self->log_warn("Package $package had version $version!\n");
}
$version = $actual_version;
if ( $package eq $prime_package ) {
if ( exists( $prime{$package} ) ) {
# M::B::ModuleInfo will handle this conflict
die "Unexpected conflict in '$package'; multiple versions found.\n";
}
else {
$prime{$package}{file} = $dist_files{$file};
$prime{$package}{version} = $version if defined( $version );
}
}
else {
push( @{$alt{$package}}, { file => $dist_files{$file}, version => $version } );
}
}
}
# Then we iterate over all the packages found above, identifying conflicts
# and selecting the "best" candidate for recording the file & version
# for each package.
foreach my $package ( keys( %alt ) ) {
my $result = $self->_resolve_module_versions( $alt{$package} );
if ( exists( $prime{$package} ) ) { # primary package selected
if ( $result->{err} ) {
# Use the selected primary package, but there are conflicting
# errors amoung multiple alternative packages that need to be
# reported
$self->log_warn("Found conflicting versions for package '$package'\n" .
" $prime{$package}{file} ($prime{$package}{version})\n" . $result->{err});
}
elsif ( defined( $result->{version} ) ) {
# There is a primary package selected, and exactly one
# alternative package
if ( exists( $prime{$package}{version} ) && defined( $prime{$package}{version} ) ) {
# Unless the version of the primary package agrees with the
# version of the alternative package, report a conflict
if ( $self->compare_versions( $prime{$package}{version}, '!=', $result->{version} ) ) {
$self->log_warn("Found conflicting versions for package '$package'\n" .
" $prime{$package}{file} ($prime{$package}{version})\n" .
" $result->{file} ($result->{version})\n");
}
}
else {
# The prime package selected has no version so, we choose to
# use any alternative package that does have a version
$prime{$package}{file} = $result->{file};
$prime{$package}{version} = $result->{version};
}
}
else {
# no alt package found with a version, but we have a prime
# package so we use it whether it has a version or not
}
}
else { # No primary package was selected, use the best alternative
if ( $result->{err} ) {
$self->log_warn("Found conflicting versions for package '$package'\n" . $result->{err});
}
# Despite possible conflicting versions, we choose to record
# something rather than nothing
$prime{$package}{file} = $result->{file};
$prime{$package}{version} = $result->{version} if defined( $result->{version} );
}
}
# Stringify versions
for (grep exists $_->{version}, values %prime) {
$_->{version} = $_->{version}->stringify if ref($_->{version});
}
return \%prime;
}
# our recommends syntax contains extra info that needs to be ignored at this
# stage
sub _parse_conditions {
my ($self, $spec) = @_;
($spec) = split("/", $spec);
if ($spec =~ /^\s*([\w.]+)\s*$/) { # A plain number, maybe with dots, letters, and underscores
return (">= $spec");
}
else {
return split /\s*,\s*/, $spec;
}
}
# when generating META.yml, we output optional_features syntax (instead of
# recommends syntax). Note that as of CPAN v1.9402 nothing useful is done
# with this information, which is why we implement our own request to install
# the optional modules in install_optional().
# Also note that CPAN PLUS complains with an [ERROR] when it sees this META.yml,
# but it isn't fatal and installation continues fine.
# 'recommends' groups broken up now into separate modules and grouping the
# 'requires' instead of lumping modules together (quotes were choking YAML
# parsing). Now passes Parse::CPAN::Meta w/o errors.
# -cjfields 9-17-09
sub prepare_metadata {
my ($self, $node, $keys) = @_;
my $p = $self->{properties};
# A little helper sub
my $add_node = sub {
my ($name, $val) = @_;
$node->{$name} = $val;
push @$keys, $name if $keys;
};
foreach (qw(dist_name dist_version dist_author dist_abstract license)) {
(my $name = $_) =~ s/^dist_//;
$add_node->($name, $self->$_());
die "ERROR: Missing required field '$_' for META.yml\n" unless defined($node->{$name}) && length($node->{$name});
}
$node->{version} = '' . $node->{version}; # Stringify version objects
if (defined( $self->license ) && defined( my $url = $self->valid_licenses->{ $self->license } )) {
$node->{resources}{license} = $url;
}
foreach ( @{$self->prereq_action_types} ) {
if (exists $p->{$_} and keys %{ $p->{$_} }) {
if ($_ eq 'recommends') {
my $hash;
while (my ($req, $val) = each %{ $p->{$_} }) {
my ($ver, $why, $mods) = split("/", $val);
for my $used_by (split ',',$mods) {
$used_by =~ s{^(\S+)\s.*$}{$1};
if (exists $hash->{$used_by}) {
push @{$hash->{$used_by}->{requires}}, {$req => $ver};
} else {
$hash->{$used_by} = {description => $why,
requires => [{$req => $ver}]};
}
}
}
$add_node->('optional_features', $hash);
}
else {
$add_node->($_, $p->{$_});
}
}
}
if (exists $p->{dynamic_config}) {
$add_node->('dynamic_config', $p->{dynamic_config});
}
my $pkgs = eval { $self->find_dist_packages };
if ($@) {
$self->log_warn("$@\nWARNING: Possible missing or corrupt 'MANIFEST' file.\n" . "Nothing to enter for 'provides' field in META.yml\n");
}
else {
$node->{provides} = $pkgs if %$pkgs;
};
if (exists $p->{no_index}) {
$add_node->('no_index', $p->{no_index});
}
$add_node->('generated_by', "Module::Build version $Module::Build::VERSION");
$add_node->('meta-spec',
{version => '1.2',
url => 'http://module-build.sourceforge.net/META-spec-v1.2.html',
});
while (my($k, $v) = each %{$self->meta_add}) {
$add_node->($k, $v);
}
while (my($k, $v) = each %{$self->meta_merge}) {
$self->_hash_merge($node, $k, $v);
}
return $node;
}
# let us store extra things persistently in _build
sub _construct {
my $self = shift;
# calling SUPER::_construct will dump some of the input to this sub out
# with Data::Dumper, which will complain about code refs. So we replace
# any code refs with dummies first, then put them back afterwards
my %in_hash = @_;
my $auto_features = $in_hash{auto_features} if defined $in_hash{auto_features};
my %code_refs;
if ($auto_features) {
while (my ($key, $hash) = each %{$auto_features}) {
while (my ($sub_key, $val) = each %{$hash}) {
if (ref($val) && ref($val) eq 'CODE') {
$hash->{$sub_key} = 'CODE_ref';
$code_refs{$key}->{$sub_key} = $val;
}
}
}
}
$self = $self->SUPER::_construct(@_);
my ($p, $ph) = ($self->{properties}, $self->{phash});
if (keys %code_refs) {
while (my ($key, $hash) = each %{$auto_features}) {
if (defined $code_refs{$key}) {
while (my ($sub_key, $code_ref) = each %{$code_refs{$key}}) {
$hash->{$sub_key} = $code_ref;
}
$ph->{auto_features}->{$key} = $hash;
}
}
}
foreach (qw(manifest_skip post_install_scripts)) {
my $file = File::Spec->catfile($self->config_dir, $_);
$ph->{$_} = Module::Build::Notes->new(file => $file);
$ph->{$_}->restore if -e $file;
}
return $self;
}
sub write_config {
my $self = shift;
$self->SUPER::write_config;
# write extra things
$self->{phash}{$_}->write() foreach qw(manifest_skip post_install_scripts);
# be even more certain we can reload ourselves during a resume by copying
# ourselves to _build\lib
# this is only possible for the core distribution where we are actually
# present in the distribution
my $self_filename = File::Spec->catfile('Bio', 'Root', 'Build.pm');
-e $self_filename || return;
my $filename = File::Spec->catfile($self->{properties}{config_dir}, 'lib', 'Bio', 'Root', 'Build.pm');
my $filedir = File::Basename::dirname($filename);
File::Path::mkpath($filedir);
warn "Can't create directory $filedir: $!" unless -d $filedir;
File::Copy::copy($self_filename, $filename);
warn "Unable to copy 'Bio/Root/Build.pm' to '$filename'\n" unless -e $filename;
}
# add a file to the default MANIFEST.SKIP
sub add_to_manifest_skip {
my $self = shift;
my %files = map {$self->localize_file_path($_), 1} @_;
$self->{phash}{manifest_skip}->write(\%files);
}
# we always generate a new MANIFEST and MANIFEST.SKIP here, instead of allowing
# existing files to remain
sub ACTION_manifest {
my ($self) = @_;
my $maniskip = 'MANIFEST.SKIP';
if ( -e 'MANIFEST' || -e $maniskip ) {
$self->log_warn("MANIFEST files already exist, will overwrite them\n");
unlink('MANIFEST');
unlink($maniskip);
}
$self->_write_default_maniskip($maniskip);
require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
ExtUtils::Manifest::mkmanifest();
}
# extended to add extra things to the default MANIFEST.SKIP
sub _write_default_maniskip {
my $self = shift;
$self->SUPER::_write_default_maniskip;
my @extra = keys %{$self->{phash}{manifest_skip}->read};
if (@extra) {
open(my $fh, '>>', 'MANIFEST.SKIP') or die "Could not open MANIFEST.SKIP file\n";
print $fh "\n# Avoid additional run-time generated things\n";
foreach my $line (@extra) {
print $fh $line, "\n";
}
close($fh);
}
}
# extended to run scripts post-installation
sub ACTION_install {
my ($self) = @_;
require ExtUtils::Install;
$self->depends_on('build');
ExtUtils::Install::install($self->install_map, !$self->quiet, 0, $self->{args}{uninst}||0);
$self->run_post_install_scripts;
}
sub add_post_install_script {
my $self = shift;
my %files = map {$self->localize_file_path($_), 1} @_;
$self->{phash}{post_install_scripts}->write(\%files);
}
sub run_post_install_scripts {
my $self = shift;
my @scripts = keys %{$self->{phash}{post_install_scripts}->read};
foreach my $script (@scripts) {
$self->run_perl_script($script);
}
}
# for use with auto_features, which should require LWP::UserAgent as one of
# its reqs
sub test_internet {
eval {require LWP::UserAgent;};
if ($@) {
# ideally this won't happen because auto_feature already specified
# LWP::UserAgent, so this sub wouldn't get called if LWP not installed
return "LWP::UserAgent not installed";
}
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get('http://search.cpan.org/');
unless ($response->is_success) {
return "Could not connect to the internet (http://search.cpan.org/)";
}
return;
}
# nice directory names for dist-related actions
sub dist_dir {
my ($self) = @_;
my $version = $self->dist_version;
if ($version =~ /^\d\.\d{6}\d$/) {
# 1.x.x.100 returned as 1.x.x.1
$version .= '00';
}
$version =~ s/00(\d)/$1./g;
$version =~ s/\.$//;
if (my ($minor, $rev) = $version =~ /^\d\.(\d)\.\d\.(\d+)$/) {
my $dev = ! ($minor % 2 == 0);
if ($rev == 100) {
my $replace = $dev ? "_$rev" : '';
$version =~ s/\.\d+$/$replace/;
}
elsif ($rev < 100) {
$rev = sprintf("%03d", $rev);
$version =~ s/\.\d+$/_$rev-RC/;
}
else {
$rev -= 100 unless $dev;
my $replace = $dev ? "_$rev" : ".$rev";
$version =~ s/\.\d+$/$replace/;
}
}
return "$self->{properties}{dist_name}-$version";
}
sub ppm_name {
my $self = shift;
return $self->dist_dir.'-ppm';
}
# generate complete ppd4 version file