bug 2549; fixed small bug in Bio::Taxon which doesn't catch -common_name
[bioperl-live.git] / ModuleBuildBioperl.pm
blob7e85a735eb5dbe73fb52d858793e5c535e83770b
1 #!/usr/bin/perl -w
3 # This is a subclass of Module::Build so we can override certain methods and do
4 # fancy stuff
6 # It was first written against Module::Build::Base v0.2805. Many of the methods
7 # here are copy/pasted from there in their entirety just to change one or two
8 # minor things, since for the most part Module::Build::Base code is hard to
9 # cleanly override.
11 # This was written by Sendu Bala and is released under the same license as
12 # Bioperl itself
14 package ModuleBuildBioperl;
16 BEGIN {
17 # we really need Module::Build to be installed
18 unless (eval "use Module::Build 0.2805; 1") {
19 print "This package requires Module::Build v0.2805 or greater to install itself.\n";
21 require ExtUtils::MakeMaker;
22 my $yn = ExtUtils::MakeMaker::prompt(' Install Module::Build now from CPAN?', 'y');
24 unless ($yn =~ /^y/i) {
25 die " *** Cannot install without Module::Build. Exiting ...\n";
28 require Cwd;
29 require File::Spec;
30 require File::Copy;
31 require CPAN;
33 # Save this because CPAN will chdir all over the place.
34 my $cwd = Cwd::cwd();
36 my $build_pl = File::Spec->catfile($cwd, "Build.PL");
38 File::Copy::move($build_pl, $build_pl."hidden"); # avoid bizarre bug with Module::Build tests using the wrong Build.PL if it happens to be in PERL5LIB
39 CPAN::Shell->install('Module::Build');
40 File::Copy::move($build_pl."hidden", $build_pl);
41 CPAN::Shell->expand("Module", "Module::Build")->uptodate or die "Couldn't install Module::Build, giving up.\n";
43 chdir $cwd or die "Cannot chdir() back to $cwd: $!\n\n***\nInstallation will probably work fine if you now quit CPAN and try again.\n***\n\n";
46 eval "use base Module::Build; 1" or die $@;
48 # ensure we'll be able to reload this module later by adding its path to inc
49 use Cwd;
50 use lib Cwd::cwd();
53 use strict;
54 use warnings;
56 our $VERSION = 1.005002101;
57 our @extra_types = qw(options excludes_os feature_requires test); # test must always be last in the list!
58 our $checking_types = "requires|conflicts|".join("|", @extra_types);
61 # our modules are in Bio, not lib
62 sub find_pm_files {
63 my $self = shift;
64 foreach my $pm (@{$self->rscan_dir('Bio', qr/\.pm$/)}) {
65 $self->{properties}{pm_files}->{$pm} = File::Spec->catfile('lib', $pm);
68 $self->_find_file_by_type('pm', 'lib');
71 # ask what scripts to install (this method is unique to bioperl)
72 sub choose_scripts {
73 my $self = shift;
74 my $accept = shift;
76 # we can offer interactive installation by groups only if we have subdirs
77 # in scripts and no .PLS files there
78 opendir(my $scripts_dir, 'scripts') or die "Can't open directory 'scripts': $!\n";
79 my $int_ok = 0;
80 my @group_dirs;
81 while (my $thing = readdir($scripts_dir)) {
82 next if $thing =~ /^\./;
83 next if $thing eq 'CVS';
84 if ($thing =~ /PLS$|pl$/) {
85 $int_ok = 0;
86 last;
88 $thing = File::Spec->catfile('scripts', $thing);
89 if (-d $thing) {
90 $int_ok = 1;
91 push(@group_dirs, $thing);
94 closedir($scripts_dir);
95 my $question = $int_ok ? "Install [a]ll Bioperl scripts, [n]one, or choose groups [i]nteractively?" : "Install [a]ll Bioperl scripts or [n]one?";
97 my $prompt = $accept
98 ? 'a' : $self->prompt($question, 'a');
100 if ($prompt =~ /^[aA]/) {
101 $self->log_info(" - will install all scripts\n");
102 $self->notes(chosen_scripts => 'all');
104 elsif ($prompt =~ /^[iI]/) {
105 $self->log_info(" - will install interactively:\n");
107 my @chosen_scripts;
108 foreach my $group_dir (@group_dirs) {
109 my $group = File::Basename::basename($group_dir);
110 print " * group '$group' has:\n";
112 my @script_files = @{$self->rscan_dir($group_dir, qr/\.PLS$|\.pl$/)};
113 foreach my $script_file (@script_files) {
114 my $script = File::Basename::basename($script_file);
115 print " $script\n";
118 my $result = $self->prompt(" Install scripts for group '$group'? [y]es [n]o [q]uit", 'n');
119 die if $result =~ /^[qQ]/;
120 if ($result =~ /^[yY]/) {
121 $self->log_info(" + will install group '$group'\n");
122 push(@chosen_scripts, @script_files);
124 else {
125 $self->log_info(" - will not install group '$group'\n");
129 my $chosen_scripts = @chosen_scripts ? join("|", @chosen_scripts) : 'none';
131 $self->notes(chosen_scripts => $chosen_scripts);
133 else {
134 $self->log_info(" - won't install any scripts\n");
135 $self->notes(chosen_scripts => 'none');
138 print "\n";
141 # our version of script_files doesn't take args but just installs those scripts
142 # requested by the user after choose_scripts() is called. If it wasn't called,
143 # installs all scripts in scripts directory
144 sub script_files {
145 my $self = shift;
147 my $chosen_scripts = $self->notes('chosen_scripts');
148 if ($chosen_scripts) {
149 return if $chosen_scripts eq 'none';
150 return { map {$_, 1} split(/\|/, $chosen_scripts) } unless $chosen_scripts eq 'all';
153 return $_ = { map {$_,1} @{$self->rscan_dir('scripts', qr/\.PLS$|\.pl$/)} };
156 # process scripts normally, except that we change name from *.PLS to bp_*.pl
157 sub process_script_files {
158 my $self = shift;
159 my $files = $self->find_script_files;
160 return unless keys %$files;
162 my $script_dir = File::Spec->catdir($self->blib, 'script');
163 File::Path::mkpath( $script_dir );
165 foreach my $file (keys %$files) {
166 my $result = $self->copy_if_modified($file, $script_dir, 'flatten') or next;
167 $self->fix_shebang_line($result) unless $self->os_type eq 'VMS';
168 $self->make_executable($result);
170 my $final = File::Basename::basename($result);
171 $final =~ s/\.PLS$/\.pl/; # change from .PLS to .pl
172 $final =~ s/^/bp_/ unless $final =~ /^bp/; # add the "bp" prefix
173 $final = File::Spec->catfile($script_dir, $final);
174 $self->log_info("$result -> $final\n");
175 File::Copy::move($result, $final) or die "Can't rename '$result' to '$final': $!";
179 # extended to handle extra checking types
180 sub features {
181 my $self = shift;
182 my $ph = $self->{phash};
184 if (@_) {
185 my $key = shift;
186 if ($ph->{features}->exists($key)) {
187 return $ph->{features}->access($key, @_);
190 if (my $info = $ph->{auto_features}->access($key)) {
191 my $failures = $self->prereq_failures($info);
192 my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
193 return !$disabled;
196 return $ph->{features}->access($key, @_);
199 # No args - get the auto_features & overlay the regular features
200 my %features;
201 my %auto_features = $ph->{auto_features}->access();
202 while (my ($name, $info) = each %auto_features) {
203 my $failures = $self->prereq_failures($info);
204 my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
205 $features{$name} = $disabled ? 0 : 1;
207 %features = (%features, $ph->{features}->access());
209 return wantarray ? %features : \%features;
211 *feature = \&features;
213 # overridden to fix a stupid bug in Module::Build and extended to handle extra
214 # checking types
215 sub check_autofeatures {
216 my ($self) = @_;
217 my $features = $self->auto_features;
219 return unless %$features;
221 $self->log_info("Checking features:\n");
223 my $max_name_len = 0; # this wasn't set to 0 in Module::Build, causing warning in next line
224 $max_name_len = ( length($_) > $max_name_len ) ? length($_) : $max_name_len for keys %$features;
226 while (my ($name, $info) = each %$features) {
227 $self->log_info(" $name" . '.' x ($max_name_len - length($name) + 4));
228 if ($name eq 'PL_files') {
229 print "got $name => $info\n";
230 print "info has:\n";
231 while (my ($key, $val) = each %$info) {
232 print " $key => $val\n";
236 if ( my $failures = $self->prereq_failures($info) ) {
237 my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
238 $self->log_info( $disabled ? "disabled\n" : "enabled\n" );
240 my $log_text;
241 while (my ($type, $prereqs) = each %$failures) {
242 while (my ($module, $status) = each %$prereqs) {
243 my $required = ($type =~ /^(?:\w+_)?(?:requires|conflicts)$/) ? 1 : 0;
244 my $prefix = ($required) ? '-' : '*';
245 $log_text .= " $prefix $status->{message}\n";
248 $self->log_warn($log_text) if $log_text && ! $self->quiet;
250 else {
251 $self->log_info("enabled\n");
255 $self->log_info("\n");
258 # overriden just to hide pointless ugly warnings
259 sub check_installed_status {
260 my $self = shift;
261 open (my $olderr, ">&", \*STDERR);
262 open(STDERR, "/dev/null");
263 my $return = $self->SUPER::check_installed_status(@_);
264 open(STDERR, ">&", $olderr);
265 return $return;
268 # extend to handle option checking (which takes an array ref) and code test
269 # checking (which takes a code ref and must return a message only on failure)
270 # and excludes_os (which takes an array ref of regexps).
271 # also handles more informative output of recommends section
272 sub prereq_failures {
273 my ($self, $info) = @_;
275 my @types = (@{ $self->prereq_action_types }, @extra_types);
276 $info ||= {map {$_, $self->$_()} @types};
278 my $out = {};
279 foreach my $type (@types) {
280 my $prereqs = $info->{$type} || next;
282 my $status = {};
283 if ($type eq 'test') {
284 unless (keys %$out) {
285 if (ref($prereqs) eq 'CODE') {
286 $status->{message} = &{$prereqs};
288 # drop the code-ref to avoid Module::Build trying to store
289 # it with Data::Dumper, generating warnings. (And also, may
290 # be expensive to run the sub multiple times.)
291 $info->{$type} = $status->{message};
293 else {
294 $status->{message} = $prereqs;
296 $out->{$type}{'test'} = $status if $status->{message};
299 elsif ($type eq 'options') {
300 my @not_ok;
301 foreach my $wanted_option (@{$prereqs}) {
302 unless ($self->args($wanted_option)) {
303 push(@not_ok, $wanted_option);
307 if (@not_ok > 0) {
308 $status->{message} = "Command line option(s) '@not_ok' not supplied";
309 $out->{$type}{'options'} = $status;
312 elsif ($type eq 'excludes_os') {
313 foreach my $os (@{$prereqs}) {
314 if ($^O =~ /$os/i) {
315 $status->{message} = "This feature isn't supported under your OS ($os)";
316 $out->{$type}{'excludes_os'} = $status;
317 last;
321 else {
322 while ( my ($modname, $spec) = each %$prereqs ) {
323 $status = $self->check_installed_status($modname, $spec);
325 if ($type =~ /^(?:\w+_)?conflicts$/) {
326 next if !$status->{ok};
327 $status->{conflicts} = delete $status->{need};
328 $status->{message} = "$modname ($status->{have}) conflicts with this distribution";
330 elsif ($type =~ /^(?:\w+_)?recommends$/) {
331 next if $status->{ok};
333 my ($preferred_version, $why, $by_what) = split("/", $spec);
334 $by_what = join(", ", split(",", $by_what));
335 $by_what =~ s/, (\S+)$/ and $1/;
337 $status->{message} = (!ref($status->{have}) && $status->{have} eq '<none>'
338 ? "Optional prerequisite $modname is not installed"
339 : "$modname ($status->{have}) is installed, but we prefer to have $preferred_version");
341 $status->{message} .= "\n (wanted for $why, used by $by_what)";
343 my $installed = $self->install_optional($modname, $preferred_version, $status->{message});
344 next if $installed eq 'ok';
345 $status->{message} = $installed unless $installed eq 'skip';
347 elsif ($type =~ /^feature_requires/) {
348 next if $status->{ok};
350 # if there is a test code-ref, drop it to avoid
351 # Module::Build trying to store it with Data::Dumper,
352 # generating warnings.
353 delete $info->{test};
355 else {
356 next if $status->{ok};
358 my $installed = $self->install_required($modname, $spec, $status->{message});
359 next if $installed eq 'ok';
360 $status->{message} = $installed;
363 $out->{$type}{$modname} = $status;
368 return keys %{$out} ? $out : return;
371 # install an external module using CPAN prior to testing and installation
372 # should only be called by install_required or install_optional
373 sub install_prereq {
374 my ($self, $desired, $version) = @_;
376 if ($self->under_cpan) {
377 # Just add to the required hash, which CPAN >= 1.81 will check prior
378 # to install
379 $self->{properties}{requires}->{$desired} = $version;
380 $self->log_info(" I'll get CPAN to prepend the installation of this\n");
381 return 'ok';
383 else {
384 # Here we use CPAN to actually install the desired module, the benefit
385 # being we continue even if installation fails, and that this works
386 # even when not using CPAN to install.
387 require Cwd;
388 require CPAN;
390 # Save this because CPAN will chdir all over the place.
391 my $cwd = Cwd::cwd();
393 CPAN::Shell->install($desired);
394 my $msg;
395 my $expanded = CPAN::Shell->expand("Module", $desired);
396 if ($expanded && $expanded->uptodate) {
397 $self->log_info("\n\n*** (back in Bioperl Build.PL) ***\n * You chose to install $desired and it installed fine\n");
398 $msg = 'ok';
400 else {
401 $self->log_info("\n\n*** (back in Bioperl Build.PL) ***\n");
402 $msg = "You chose to install $desired but it failed to install";
405 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
406 return $msg;
410 # install required modules listed in 'requires' or 'build_requires' arg to
411 # new that weren't already installed. Should only be called by prereq_failures
412 sub install_required {
413 my ($self, $desired, $version, $msg) = @_;
415 $self->log_info(" - ERROR: $msg\n");
417 return $self->install_prereq($desired, $version);
420 # install optional modules listed in 'recommends' arg to new that weren't
421 # already installed. Should only be called by prereq_failures
422 sub install_optional {
423 my ($self, $desired, $version, $msg) = @_;
425 unless (defined $self->{ask_optional}) {
426 $self->{ask_optional} = $self->args->{accept}
427 ? 'n' : $self->prompt("Install [a]ll optional external modules, [n]one, or choose [i]nteractively?", 'n');
429 return 'skip' if $self->{ask_optional} =~ /^n/i;
431 my $install;
432 if ($self->{ask_optional} =~ /^a/i) {
433 $self->log_info(" * $msg\n");
434 $install = 1;
436 else {
437 $install = $self->y_n(" * $msg\n Do you want to install it? y/n", 'n');
440 if ($install) {
441 return $self->install_prereq($desired, $version);
443 else {
444 $self->log_info(" * You chose not to install $desired\n");
445 return 'ok';
449 # there's no official way to discover if being run by CPAN, we take an approach
450 # similar to that of Module::AutoInstall
451 sub under_cpan {
452 my $self = shift;
454 unless (defined $self->{under_cpan}) {
455 ## modified from Module::AutoInstall
457 # load cpan config
458 require CPAN;
459 if ($CPAN::HandleConfig::VERSION) {
460 # Newer versions of CPAN have a HandleConfig module
461 CPAN::HandleConfig->load;
463 else {
464 # Older versions had the load method in Config directly
465 CPAN::Config->load;
468 # Find the CPAN lock-file
469 my $lock = File::Spec->catfile($CPAN::Config->{cpan_home}, '.lock');
470 if (-f $lock) {
471 # Module::AutoInstall now goes on to open the lock file and compare
472 # its pid to ours, but we're not in a situation where we expect
473 # the pids to match, so we take the windows approach for all OSes:
474 # find out if we're in cpan_home
475 my $cwd = File::Spec->canonpath(Cwd::cwd());
476 my $cpan = File::Spec->canonpath($CPAN::Config->{cpan_home});
478 $self->{under_cpan} = index($cwd, $cpan) > -1;
481 if ($self->{under_cpan}) {
482 $self->log_info("(I think I'm being run by CPAN, so will rely on CPAN to handle prerequisite installation)\n");
484 else {
485 $self->log_info("(I think you ran Build.PL directly, so will use CPAN to install prerequisites on demand)\n");
486 $self->{under_cpan} = 0;
490 return $self->{under_cpan};
493 # overridden simply to not print the default answer if chosen by hitting return
494 sub prompt {
495 my $self = shift;
496 my $mess = shift or die "prompt() called without a prompt message";
498 my $def;
499 if ( $self->_is_unattended && !@_ ) {
500 die <<EOF;
501 ERROR: This build seems to be unattended, but there is no default value
502 for this question. Aborting.
505 $def = shift if @_;
506 ($def, my $dispdef) = defined $def ? ($def, "[$def] ") : ('', ' ');
508 local $|=1;
509 print "$mess $dispdef";
511 my $ans = $self->_readline();
513 if ( !defined($ans) # Ctrl-D or unattended
514 or !length($ans) ) { # User hit return
515 #print "$def\n"; didn't like this!
516 $ans = $def;
519 return $ans;
522 # like the Module::Build version, except that we always get version from
523 # dist_version
524 sub find_dist_packages {
525 my $self = shift;
527 # Only packages in .pm files are candidates for inclusion here.
528 # Only include things in the MANIFEST, not things in developer's
529 # private stock.
531 my $manifest = $self->_read_manifest('MANIFEST') or die "Can't find dist packages without a MANIFEST file - run 'manifest' action first";
533 # Localize
534 my %dist_files = map { $self->localize_file_path($_) => $_ } keys %$manifest;
536 my @pm_files = grep {exists $dist_files{$_}} keys %{ $self->find_pm_files };
538 my $actual_version = $self->dist_version;
540 # First, we enumerate all packages & versions,
541 # seperating into primary & alternative candidates
542 my( %prime, %alt );
543 foreach my $file (@pm_files) {
544 next if $dist_files{$file} =~ m{^t/}; # Skip things in t/
546 my @path = split( /\//, $dist_files{$file} );
547 (my $prime_package = join( '::', @path[1..$#path] )) =~ s/\.pm$//;
549 my $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
551 foreach my $package ( $pm_info->packages_inside ) {
552 next if $package eq 'main'; # main can appear numerous times, ignore
553 next if grep /^_/, split( /::/, $package ); # private package, ignore
555 my $version = $pm_info->version( $package );
556 if ($version && $version != $actual_version) {
557 $self->log_warn("Package $package had version $version!\n");
559 $version = $actual_version;
561 if ( $package eq $prime_package ) {
562 if ( exists( $prime{$package} ) ) {
563 # M::B::ModuleInfo will handle this conflict
564 die "Unexpected conflict in '$package'; multiple versions found.\n";
566 else {
567 $prime{$package}{file} = $dist_files{$file};
568 $prime{$package}{version} = $version if defined( $version );
571 else {
572 push( @{$alt{$package}}, { file => $dist_files{$file}, version => $version } );
577 # Then we iterate over all the packages found above, identifying conflicts
578 # and selecting the "best" candidate for recording the file & version
579 # for each package.
580 foreach my $package ( keys( %alt ) ) {
581 my $result = $self->_resolve_module_versions( $alt{$package} );
583 if ( exists( $prime{$package} ) ) { # primary package selected
584 if ( $result->{err} ) {
585 # Use the selected primary package, but there are conflicting
586 # errors amoung multiple alternative packages that need to be
587 # reported
588 $self->log_warn("Found conflicting versions for package '$package'\n" .
589 " $prime{$package}{file} ($prime{$package}{version})\n" . $result->{err});
591 elsif ( defined( $result->{version} ) ) {
592 # There is a primary package selected, and exactly one
593 # alternative package
595 if ( exists( $prime{$package}{version} ) && defined( $prime{$package}{version} ) ) {
596 # Unless the version of the primary package agrees with the
597 # version of the alternative package, report a conflict
598 if ( $self->compare_versions( $prime{$package}{version}, '!=', $result->{version} ) ) {
599 $self->log_warn("Found conflicting versions for package '$package'\n" .
600 " $prime{$package}{file} ($prime{$package}{version})\n" .
601 " $result->{file} ($result->{version})\n");
604 else {
605 # The prime package selected has no version so, we choose to
606 # use any alternative package that does have a version
607 $prime{$package}{file} = $result->{file};
608 $prime{$package}{version} = $result->{version};
611 else {
612 # no alt package found with a version, but we have a prime
613 # package so we use it whether it has a version or not
616 else { # No primary package was selected, use the best alternative
617 if ( $result->{err} ) {
618 $self->log_warn("Found conflicting versions for package '$package'\n" . $result->{err});
621 # Despite possible conflicting versions, we choose to record
622 # something rather than nothing
623 $prime{$package}{file} = $result->{file};
624 $prime{$package}{version} = $result->{version} if defined( $result->{version} );
628 # Stringify versions
629 for (grep exists $_->{version}, values %prime) {
630 $_->{version} = $_->{version}->stringify if ref($_->{version});
633 return \%prime;
636 # our recommends syntax contains extra info that needs to be ignored at this
637 # stage
638 sub _parse_conditions {
639 my ($self, $spec) = @_;
641 ($spec) = split("/", $spec);
643 if ($spec =~ /^\s*([\w.]+)\s*$/) { # A plain number, maybe with dots, letters, and underscores
644 return (">= $spec");
646 else {
647 return split /\s*,\s*/, $spec;
651 # when generating META.yml, we output optional_features syntax (instead of
652 # recommends syntax). Note that as of CPAN v1.8802 nothing useful is done
653 # with this information, which is why we implement our own request to install
654 # the optional modules in install_optional()
655 sub prepare_metadata {
656 my ($self, $node, $keys) = @_;
657 my $p = $self->{properties};
659 # A little helper sub
660 my $add_node = sub {
661 my ($name, $val) = @_;
662 $node->{$name} = $val;
663 push @$keys, $name if $keys;
666 foreach (qw(dist_name dist_version dist_author dist_abstract license)) {
667 (my $name = $_) =~ s/^dist_//;
668 $add_node->($name, $self->$_());
669 die "ERROR: Missing required field '$_' for META.yml\n" unless defined($node->{$name}) && length($node->{$name});
671 $node->{version} = '' . $node->{version}; # Stringify version objects
673 if (defined( $self->license ) && defined( my $url = $self->valid_licenses->{ $self->license } )) {
674 $node->{resources}{license} = $url;
677 foreach ( @{$self->prereq_action_types} ) {
678 if (exists $p->{$_} and keys %{ $p->{$_} }) {
679 if ($_ eq 'recommends') {
680 my $hash;
681 while (my ($req, $val) = each %{ $p->{$_} }) {
682 my ($ver, $why, $used_by) = split("/", $val);
683 my $info = {};
684 $info->{description} = $why;
685 $info->{requires} = { $req => $ver };
686 $hash->{$used_by} = $info;
688 $add_node->('optional_features', $hash);
690 else {
691 $add_node->($_, $p->{$_});
696 if (exists $p->{dynamic_config}) {
697 $add_node->('dynamic_config', $p->{dynamic_config});
699 my $pkgs = eval { $self->find_dist_packages };
700 if ($@) {
701 $self->log_warn("$@\nWARNING: Possible missing or corrupt 'MANIFEST' file.\n" . "Nothing to enter for 'provides' field in META.yml\n");
703 else {
704 $node->{provides} = $pkgs if %$pkgs;
707 if (exists $p->{no_index}) {
708 $add_node->('no_index', $p->{no_index});
711 $add_node->('generated_by', "Module::Build version $Module::Build::VERSION");
713 $add_node->('meta-spec',
714 {version => '1.2',
715 url => 'http://module-build.sourceforge.net/META-spec-v1.2.html',
718 while (my($k, $v) = each %{$self->meta_add}) {
719 $add_node->($k, $v);
722 while (my($k, $v) = each %{$self->meta_merge}) {
723 $self->_hash_merge($node, $k, $v);
726 return $node;
729 # let us store extra things persistently in _build
730 sub _construct {
731 my $self = shift;
732 $self = $self->SUPER::_construct(@_);
734 my ($p, $ph) = ($self->{properties}, $self->{phash});
736 foreach (qw(manifest_skip post_install_scripts)) {
737 my $file = File::Spec->catfile($self->config_dir, $_);
738 $ph->{$_} = Module::Build::Notes->new(file => $file);
739 $ph->{$_}->restore if -e $file;
742 return $self;
744 sub write_config {
745 my $self = shift;
746 $self->SUPER::write_config;
748 # write extra things
749 $self->{phash}{$_}->write() foreach qw(manifest_skip post_install_scripts);
751 # be even more certain we can reload ourselves during a resume by copying
752 # ourselves to _build\lib
753 my $filename = File::Spec->catfile($self->{properties}{config_dir}, 'lib', 'ModuleBuildBioperl.pm');
754 my $filedir = File::Basename::dirname($filename);
756 File::Path::mkpath($filedir);
757 warn "Can't create directory $filedir: $!" unless -d $filedir;
759 File::Copy::copy('ModuleBuildBioperl.pm', $filename);
760 warn "Unable to copy 'ModuleBuildBioperl.pm' to '$filename'\n" unless -e $filename;
763 # add a file to the default MANIFEST.SKIP
764 sub add_to_manifest_skip {
765 my $self = shift;
766 my %files = map {$self->localize_file_path($_), 1} @_;
767 $self->{phash}{manifest_skip}->write(\%files);
770 # we always generate a new MANIFEST and MANIFEST.SKIP here, instead of allowing
771 # existing files to remain
772 sub ACTION_manifest {
773 my ($self) = @_;
775 my $maniskip = 'MANIFEST.SKIP';
776 if ( -e 'MANIFEST' || -e $maniskip ) {
777 $self->log_warn("MANIFEST files already exist, will overwrite them\n");
778 unlink('MANIFEST');
779 unlink($maniskip);
781 $self->_write_default_maniskip($maniskip);
783 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
784 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
785 ExtUtils::Manifest::mkmanifest();
788 # extended to add extra things to the default MANIFEST.SKIP
789 sub _write_default_maniskip {
790 my $self = shift;
791 $self->SUPER::_write_default_maniskip;
793 my @extra = keys %{$self->{phash}{manifest_skip}->read};
794 if (@extra) {
795 open(my $fh, '>>', 'MANIFEST.SKIP') or die "Could not open MANIFEST.SKIP file\n";
796 print $fh "\n# Avoid additional run-time generated things\n";
797 foreach my $line (@extra) {
798 print $fh $line, "\n";
800 close($fh);
804 # extended to run scripts post-installation
805 sub ACTION_install {
806 my ($self) = @_;
807 require ExtUtils::Install;
808 $self->depends_on('build');
809 ExtUtils::Install::install($self->install_map, !$self->quiet, 0, $self->{args}{uninst}||0);
810 $self->run_post_install_scripts;
812 sub add_post_install_script {
813 my $self = shift;
814 my %files = map {$self->localize_file_path($_), 1} @_;
815 $self->{phash}{post_install_scripts}->write(\%files);
817 sub run_post_install_scripts {
818 my $self = shift;
819 my @scripts = keys %{$self->{phash}{post_install_scripts}->read};
820 foreach my $script (@scripts) {
821 $self->run_perl_script($script);
825 # for use with auto_features, which should require LWP::UserAgent as one of
826 # its reqs
827 sub test_internet {
828 eval {require LWP::UserAgent;};
829 if ($@) {
830 # ideally this won't happen because auto_feature already specified
831 # LWP::UserAgent, so this sub wouldn't get called if LWP not installed
832 return "LWP::UserAgent not installed";
834 my $ua = LWP::UserAgent->new;
835 $ua->timeout(10);
836 $ua->env_proxy;
837 my $response = $ua->get('http://search.cpan.org/');
838 unless ($response->is_success) {
839 return "Could not connect to the internet (http://search.cpan.org/)";
841 return;
844 # nice directory names for dist-related actions
845 sub dist_dir {
846 my ($self) = @_;
847 my $version = $self->dist_version;
848 if ($version =~ /^\d\.\d{6}\d$/) {
849 # 1.x.x.100 returned as 1.x.x.1
850 $version .= '00';
852 $version =~ s/00(\d)/$1./g;
853 $version =~ s/\.$//;
855 if (my ($minor, $rev) = $version =~ /^\d\.(\d)\.\d\.(\d+)$/) {
856 my $dev = ! ($minor % 2 == 0);
857 if ($rev == 100) {
858 my $replace = $dev ? "_$rev" : '';
859 $version =~ s/\.\d+$/$replace/;
861 elsif ($rev < 100) {
862 $rev = sprintf("%03d", $rev);
863 $version =~ s/\.\d+$/_$rev-RC/;
865 else {
866 $rev -= 100 unless $dev;
867 my $replace = $dev ? "_$rev" : ".$rev";
868 $version =~ s/\.\d+$/$replace/;
872 return "$self->{properties}{dist_name}-$version";
874 sub ppm_name {
875 my $self = shift;
876 return $self->dist_dir.'-ppm';
879 # generate complete ppd4 version file
880 sub ACTION_ppd {
881 my $self = shift;
883 my $file = $self->make_ppd(%{$self->{args}});
884 $self->add_to_cleanup($file);
885 $self->add_to_manifest_skip($file);
888 # add pod2htm temp files to MANIFEST.SKIP, generated during ppmdist most likely
889 sub htmlify_pods {
890 my $self = shift;
891 $self->SUPER::htmlify_pods(@_);
892 $self->add_to_manifest_skip('pod2htm*');
895 # don't copy across man3 docs since they're of little use under Windows and
896 # have bad filenames
897 sub ACTION_ppmdist {
898 my $self = shift;
899 my @types = $self->install_types(1);
900 $self->SUPER::ACTION_ppmdist(@_);
901 $self->install_types(0);
904 # when supplied a true value, pretends libdoc doesn't exist (preventing man3
905 # installation for ppmdist). when supplied false, they exist again
906 sub install_types {
907 my ($self, $no_libdoc) = @_;
908 $self->{no_libdoc} = $no_libdoc if defined $no_libdoc;
909 my @types = $self->SUPER::install_types;
910 if ($self->{no_libdoc}) {
911 my @altered_types;
912 foreach my $type (@types) {
913 push(@altered_types, $type) unless $type eq 'libdoc';
915 return @altered_types;
917 return @types;
920 # overridden from Module::Build::PPMMaker for ppd4 compatability
921 sub make_ppd {
922 my ($self, %args) = @_;
924 require Module::Build::PPMMaker;
925 my $mbp = Module::Build::PPMMaker->new();
927 my %dist;
928 foreach my $info (qw(name author abstract version)) {
929 my $method = "dist_$info";
930 $dist{$info} = $self->$method() or die "Can't determine distribution's $info\n";
932 $dist{codebase} = $self->ppm_name.'.tar.gz';
933 $mbp->_simple_xml_escape($_) foreach $dist{abstract}, $dist{codebase}, @{$dist{author}};
935 my (undef, undef, undef, $mday, $mon, $year) = localtime();
936 $year += 1900;
937 $mon++;
938 my $date = "$year-$mon-$mday";
940 my $softpkg_version = $self->dist_dir;
941 $softpkg_version =~ s/^$dist{name}-//;
943 # to avoid a ppm bug, instead of including the requires in the softpackage
944 # for the distribution we're making, we'll make a seperate Bundle::
945 # softpackage that contains all the requires, and require only the Bundle in
946 # the real softpackage
947 my ($bundle_name) = $dist{name} =~ /^.+-(.+)/;
948 $bundle_name ||= 'core';
949 $bundle_name =~ s/^(\w)/\U$1/;
950 my $bundle_dir = "Bundle-BioPerl-$bundle_name-$softpkg_version-ppm";
951 my $bundle_file = "$bundle_dir.tar.gz";
952 my $bundle_softpkg_name = "Bundle-BioPerl-$bundle_name";
953 $bundle_name = "Bundle::BioPerl::$bundle_name";
955 # header
956 my $ppd = <<"PPD";
957 <SOFTPKG NAME=\"$dist{name}\" VERSION=\"$softpkg_version\" DATE=\"$date\">
958 <TITLE>$dist{name}</TITLE>
959 <ABSTRACT>$dist{abstract}</ABSTRACT>
960 @{[ join "\n", map " <AUTHOR>$_</AUTHOR>", @{$dist{author}} ]}
961 <PROVIDE NAME=\"$dist{name}::\" VERSION=\"$dist{version}\"/>
964 # provide section
965 foreach my $pm (@{$self->rscan_dir('Bio', qr/\.pm$/)}) {
966 # convert these filepaths to Module names
967 $pm =~ s/\//::/g;
968 $pm =~ s/\.pm//;
970 $ppd .= sprintf(<<'EOF', $pm, $dist{version});
971 <PROVIDE NAME="%s" VERSION="%s"/>
975 # rest of softpkg
976 $ppd .= <<"PPD";
977 <IMPLEMENTATION>
978 <ARCHITECTURE NAME=\"MSWin32-x86-multi-thread-5.8\"/>
979 <CODEBASE HREF=\"$dist{codebase}\"/>
980 <REQUIRE NAME=\"$bundle_name\" VERSION=\"$dist{version}\"/>
981 </IMPLEMENTATION>
982 </SOFTPKG>
985 # now a new softpkg for the bundle
986 $ppd .= <<"PPD";
988 <SOFTPKG NAME=\"$bundle_softpkg_name\" VERSION=\"$softpkg_version\" DATE=\"$date\">
989 <TITLE>$bundle_name</TITLE>
990 <ABSTRACT>Bundle of pre-requisites for $dist{name}</ABSTRACT>
991 @{[ join "\n", map " <AUTHOR>$_</AUTHOR>", @{$dist{author}} ]}
992 <PROVIDE NAME=\"$bundle_name\" VERSION=\"$dist{version}\"/>
993 <IMPLEMENTATION>
994 <ARCHITECTURE NAME=\"MSWin32-x86-multi-thread-5.8\"/>
995 <CODEBASE HREF=\"$bundle_file\"/>
998 # required section
999 # we do both requires and recommends to make installation on Windows as
1000 # easy (mindless) as possible
1001 for my $type ('requires', 'recommends') {
1002 my $prereq = $self->$type;
1003 while (my ($modname, $version) = each %$prereq) {
1004 next if $modname eq 'perl';
1005 ($version) = split("/", $version) if $version =~ /\//;
1007 # Module names must have at least one ::
1008 unless ($modname =~ /::/) {
1009 $modname .= '::';
1012 # Bio::Root::Version number comes out as triplet number like 1.5.2;
1013 # convert to our own version
1014 if ($modname eq 'Bio::Root::Version') {
1015 $version = $dist{version};
1018 $ppd .= sprintf(<<'EOF', $modname, $version || '');
1019 <REQUIRE NAME="%s" VERSION="%s"/>
1024 # footer
1025 $ppd .= <<'EOF';
1026 </IMPLEMENTATION>
1027 </SOFTPKG>
1030 my $ppd_file = "$dist{name}.ppd";
1031 my $fh = IO::File->new(">$ppd_file") or die "Cannot write to $ppd_file: $!";
1032 print $fh $ppd;
1033 close $fh;
1035 $self->delete_filetree($bundle_dir);
1036 mkdir($bundle_dir) or die "Cannot create '$bundle_dir': $!";
1037 $self->make_tarball($bundle_dir);
1038 $self->delete_filetree($bundle_dir);
1039 $self->add_to_cleanup($bundle_file);
1040 $self->add_to_manifest_skip($bundle_file);
1042 return $ppd_file;
1045 # we make all archive formats we want, not just .tar.gz
1046 # we also auto-run manifest action, since we always want to re-create
1047 # MANIFEST and MANIFEST.SKIP just-in-time
1048 sub ACTION_dist {
1049 my ($self) = @_;
1051 $self->depends_on('manifest');
1052 $self->depends_on('distdir');
1054 my $dist_dir = $self->dist_dir;
1056 $self->make_zip($dist_dir);
1057 $self->make_tarball($dist_dir);
1058 $self->delete_filetree($dist_dir);
1061 # makes zip file for windows users and bzip2 files as well
1062 sub make_zip {
1063 my ($self, $dir, $file) = @_;
1064 $file ||= $dir;
1066 $self->log_info("Creating $file.zip\n");
1067 my $zip_flags = $self->verbose ? '-r' : '-rq';
1068 $self->do_system($self->split_like_shell("zip"), $zip_flags, "$file.zip", $dir);
1070 $self->log_info("Creating $file.bz2\n");
1071 require Archive::Tar;
1072 # Archive::Tar versions >= 1.09 use the following to enable a compatibility
1073 # hack so that the resulting archive is compatible with older clients.
1074 $Archive::Tar::DO_NOT_USE_PREFIX = 0;
1075 my $files = $self->rscan_dir($dir);
1076 Archive::Tar->create_archive("$file.tar", 0, @$files);
1077 $self->do_system($self->split_like_shell("bzip2"), "-k", "$file.tar");