bug 1825
[bioperl-live.git] / ModuleBuildBioperl.pm
blob8c983d71ea12eaca885c2a80a0fbabd877f88c90
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;
75 # we can offer interactive installation by groups only if we have subdirs
76 # in scripts and no .PLS files there
77 opendir(my $scripts_dir, 'scripts') or die "Can't open directory 'scripts': $!\n";
78 my $int_ok = 0;
79 my @group_dirs;
80 while (my $thing = readdir($scripts_dir)) {
81 next if $thing =~ /^\./;
82 next if $thing eq 'CVS';
83 if ($thing =~ /PLS$|pl$/) {
84 $int_ok = 0;
85 last;
87 $thing = File::Spec->catfile('scripts', $thing);
88 if (-d $thing) {
89 $int_ok = 1;
90 push(@group_dirs, $thing);
93 closedir($scripts_dir);
94 my $question = $int_ok ? "Install [a]ll Bioperl scripts, [n]one, or choose groups [i]nteractively?" : "Install [a]ll Bioperl scripts or [n]one?";
96 my $prompt = $self->prompt($question, 'a');
98 if ($prompt =~ /^[aA]/) {
99 $self->log_info(" - will install all scripts\n");
100 $self->notes(chosen_scripts => 'all');
102 elsif ($prompt =~ /^[iI]/) {
103 $self->log_info(" - will install interactively:\n");
105 my @chosen_scripts;
106 foreach my $group_dir (@group_dirs) {
107 my $group = File::Basename::basename($group_dir);
108 print " * group '$group' has:\n";
110 my @script_files = @{$self->rscan_dir($group_dir, qr/\.PLS$|\.pl$/)};
111 foreach my $script_file (@script_files) {
112 my $script = File::Basename::basename($script_file);
113 print " $script\n";
116 my $result = $self->prompt(" Install scripts for group '$group'? [y]es [n]o [q]uit", 'n');
117 die if $result =~ /^[qQ]/;
118 if ($result =~ /^[yY]/) {
119 $self->log_info(" + will install group '$group'\n");
120 push(@chosen_scripts, @script_files);
122 else {
123 $self->log_info(" - will not install group '$group'\n");
127 my $chosen_scripts = @chosen_scripts ? join("|", @chosen_scripts) : 'none';
129 $self->notes(chosen_scripts => $chosen_scripts);
131 else {
132 $self->log_info(" - won't install any scripts\n");
133 $self->notes(chosen_scripts => 'none');
136 print "\n";
139 # our version of script_files doesn't take args but just installs those scripts
140 # requested by the user after choose_scripts() is called. If it wasn't called,
141 # installs all scripts in scripts directory
142 sub script_files {
143 my $self = shift;
145 my $chosen_scripts = $self->notes('chosen_scripts');
146 if ($chosen_scripts) {
147 return if $chosen_scripts eq 'none';
148 return { map {$_, 1} split(/\|/, $chosen_scripts) } unless $chosen_scripts eq 'all';
151 return $_ = { map {$_,1} @{$self->rscan_dir('scripts', qr/\.PLS$|\.pl$/)} };
154 # process scripts normally, except that we change name from *.PLS to bp_*.pl
155 sub process_script_files {
156 my $self = shift;
157 my $files = $self->find_script_files;
158 return unless keys %$files;
160 my $script_dir = File::Spec->catdir($self->blib, 'script');
161 File::Path::mkpath( $script_dir );
163 foreach my $file (keys %$files) {
164 my $result = $self->copy_if_modified($file, $script_dir, 'flatten') or next;
165 $self->fix_shebang_line($result) unless $self->os_type eq 'VMS';
166 $self->make_executable($result);
168 my $final = File::Basename::basename($result);
169 $final =~ s/\.PLS$/\.pl/; # change from .PLS to .pl
170 $final =~ s/^/bp_/ unless $final =~ /^bp/; # add the "bp" prefix
171 $final = File::Spec->catfile($script_dir, $final);
172 $self->log_info("$result -> $final\n");
173 File::Copy::move($result, $final) or die "Can't rename '$result' to '$final': $!";
177 # extended to handle extra checking types
178 sub features {
179 my $self = shift;
180 my $ph = $self->{phash};
182 if (@_) {
183 my $key = shift;
184 if ($ph->{features}->exists($key)) {
185 return $ph->{features}->access($key, @_);
188 if (my $info = $ph->{auto_features}->access($key)) {
189 my $failures = $self->prereq_failures($info);
190 my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
191 return !$disabled;
194 return $ph->{features}->access($key, @_);
197 # No args - get the auto_features & overlay the regular features
198 my %features;
199 my %auto_features = $ph->{auto_features}->access();
200 while (my ($name, $info) = each %auto_features) {
201 my $failures = $self->prereq_failures($info);
202 my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
203 $features{$name} = $disabled ? 0 : 1;
205 %features = (%features, $ph->{features}->access());
207 return wantarray ? %features : \%features;
209 *feature = \&features;
211 # overridden to fix a stupid bug in Module::Build and extended to handle extra
212 # checking types
213 sub check_autofeatures {
214 my ($self) = @_;
215 my $features = $self->auto_features;
217 return unless %$features;
219 $self->log_info("Checking features:\n");
221 my $max_name_len = 0; # this wasn't set to 0 in Module::Build, causing warning in next line
222 $max_name_len = ( length($_) > $max_name_len ) ? length($_) : $max_name_len for keys %$features;
224 while (my ($name, $info) = each %$features) {
225 $self->log_info(" $name" . '.' x ($max_name_len - length($name) + 4));
226 if ($name eq 'PL_files') {
227 print "got $name => $info\n";
228 print "info has:\n";
229 while (my ($key, $val) = each %$info) {
230 print " $key => $val\n";
234 if ( my $failures = $self->prereq_failures($info) ) {
235 my $disabled = grep( /^(?:\w+_)?(?:$checking_types)$/, keys %$failures ) ? 1 : 0;
236 $self->log_info( $disabled ? "disabled\n" : "enabled\n" );
238 my $log_text;
239 while (my ($type, $prereqs) = each %$failures) {
240 while (my ($module, $status) = each %$prereqs) {
241 my $required = ($type =~ /^(?:\w+_)?(?:requires|conflicts)$/) ? 1 : 0;
242 my $prefix = ($required) ? '-' : '*';
243 $log_text .= " $prefix $status->{message}\n";
246 $self->log_warn($log_text) if $log_text && ! $self->quiet;
248 else {
249 $self->log_info("enabled\n");
253 $self->log_info("\n");
256 # overriden just to hide pointless ugly warnings
257 sub check_installed_status {
258 my $self = shift;
259 open (my $olderr, ">&", \*STDERR);
260 open(STDERR, "/dev/null");
261 my $return = $self->SUPER::check_installed_status(@_);
262 open(STDERR, ">&", $olderr);
263 return $return;
266 # extend to handle option checking (which takes an array ref) and code test
267 # checking (which takes a code ref and must return a message only on failure)
268 # and excludes_os (which takes an array ref of regexps).
269 # also handles more informative output of recommends section
270 sub prereq_failures {
271 my ($self, $info) = @_;
273 my @types = (@{ $self->prereq_action_types }, @extra_types);
274 $info ||= {map {$_, $self->$_()} @types};
276 my $out = {};
277 foreach my $type (@types) {
278 my $prereqs = $info->{$type} || next;
280 my $status = {};
281 if ($type eq 'test') {
282 unless (keys %$out) {
283 if (ref($prereqs) eq 'CODE') {
284 $status->{message} = &{$prereqs};
286 # drop the code-ref to avoid Module::Build trying to store
287 # it with Data::Dumper, generating warnings. (And also, may
288 # be expensive to run the sub multiple times.)
289 $info->{$type} = $status->{message};
291 else {
292 $status->{message} = $prereqs;
294 $out->{$type}{'test'} = $status if $status->{message};
297 elsif ($type eq 'options') {
298 my @not_ok;
299 foreach my $wanted_option (@{$prereqs}) {
300 unless ($self->args($wanted_option)) {
301 push(@not_ok, $wanted_option);
305 if (@not_ok > 0) {
306 $status->{message} = "Command line option(s) '@not_ok' not supplied";
307 $out->{$type}{'options'} = $status;
310 elsif ($type eq 'excludes_os') {
311 foreach my $os (@{$prereqs}) {
312 if ($^O =~ /$os/i) {
313 $status->{message} = "This feature isn't supported under your OS ($os)";
314 $out->{$type}{'excludes_os'} = $status;
315 last;
319 else {
320 while ( my ($modname, $spec) = each %$prereqs ) {
321 $status = $self->check_installed_status($modname, $spec);
323 if ($type =~ /^(?:\w+_)?conflicts$/) {
324 next if !$status->{ok};
325 $status->{conflicts} = delete $status->{need};
326 $status->{message} = "$modname ($status->{have}) conflicts with this distribution";
328 elsif ($type =~ /^(?:\w+_)?recommends$/) {
329 next if $status->{ok};
331 my ($preferred_version, $why, $by_what) = split("/", $spec);
332 $by_what = join(", ", split(",", $by_what));
333 $by_what =~ s/, (\S+)$/ and $1/;
335 $status->{message} = (!ref($status->{have}) && $status->{have} eq '<none>'
336 ? "Optional prerequisite $modname is not installed"
337 : "$modname ($status->{have}) is installed, but we prefer to have $preferred_version");
339 $status->{message} .= "\n (wanted for $why, used by $by_what)";
341 my $installed = $self->install_optional($modname, $preferred_version, $status->{message});
342 next if $installed eq 'ok';
343 $status->{message} = $installed unless $installed eq 'skip';
345 elsif ($type =~ /^feature_requires/) {
346 next if $status->{ok};
348 # if there is a test code-ref, drop it to avoid
349 # Module::Build trying to store it with Data::Dumper,
350 # generating warnings.
351 delete $info->{test};
353 else {
354 next if $status->{ok};
356 my $installed = $self->install_required($modname, $spec, $status->{message});
357 next if $installed eq 'ok';
358 $status->{message} = $installed;
361 $out->{$type}{$modname} = $status;
366 return keys %{$out} ? $out : return;
369 # install an external module using CPAN prior to testing and installation
370 # should only be called by install_required or install_optional
371 sub install_prereq {
372 my ($self, $desired, $version) = @_;
374 if ($self->under_cpan) {
375 # Just add to the required hash, which CPAN >= 1.81 will check prior
376 # to install
377 $self->{properties}{requires}->{$desired} = $version;
378 $self->log_info(" I'll get CPAN to prepend the installation of this\n");
379 return 'ok';
381 else {
382 # Here we use CPAN to actually install the desired module, the benefit
383 # being we continue even if installation fails, and that this works
384 # even when not using CPAN to install.
385 require Cwd;
386 require CPAN;
388 # Save this because CPAN will chdir all over the place.
389 my $cwd = Cwd::cwd();
391 CPAN::Shell->install($desired);
392 my $msg;
393 my $expanded = CPAN::Shell->expand("Module", $desired);
394 if ($expanded && $expanded->uptodate) {
395 $self->log_info("\n\n*** (back in Bioperl Build.PL) ***\n * You chose to install $desired and it installed fine\n");
396 $msg = 'ok';
398 else {
399 $self->log_info("\n\n*** (back in Bioperl Build.PL) ***\n");
400 $msg = "You chose to install $desired but it failed to install";
403 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
404 return $msg;
408 # install required modules listed in 'requires' or 'build_requires' arg to
409 # new that weren't already installed. Should only be called by prereq_failures
410 sub install_required {
411 my ($self, $desired, $version, $msg) = @_;
413 $self->log_info(" - ERROR: $msg\n");
415 return $self->install_prereq($desired, $version);
418 # install optional modules listed in 'recommends' arg to new that weren't
419 # already installed. Should only be called by prereq_failures
420 sub install_optional {
421 my ($self, $desired, $version, $msg) = @_;
423 unless (defined $self->{ask_optional}) {
424 $self->{ask_optional} = $self->prompt("Install [a]ll optional external modules, [n]one, or choose [i]nteractively?", 'n');
426 return 'skip' if $self->{ask_optional} =~ /^n/i;
428 my $install;
429 if ($self->{ask_optional} =~ /^a/i) {
430 $self->log_info(" * $msg\n");
431 $install = 1;
433 else {
434 $install = $self->y_n(" * $msg\n Do you want to install it? y/n", 'n');
437 if ($install) {
438 return $self->install_prereq($desired, $version);
440 else {
441 $self->log_info(" * You chose not to install $desired\n");
442 return 'ok';
446 # there's no official way to discover if being run by CPAN, we take an approach
447 # similar to that of Module::AutoInstall
448 sub under_cpan {
449 my $self = shift;
451 unless (defined $self->{under_cpan}) {
452 ## modified from Module::AutoInstall
454 # load cpan config
455 require CPAN;
456 if ($CPAN::HandleConfig::VERSION) {
457 # Newer versions of CPAN have a HandleConfig module
458 CPAN::HandleConfig->load;
460 else {
461 # Older versions had the load method in Config directly
462 CPAN::Config->load;
465 # Find the CPAN lock-file
466 my $lock = File::Spec->catfile($CPAN::Config->{cpan_home}, '.lock');
467 if (-f $lock) {
468 # Module::AutoInstall now goes on to open the lock file and compare
469 # its pid to ours, but we're not in a situation where we expect
470 # the pids to match, so we take the windows approach for all OSes:
471 # find out if we're in cpan_home
472 my $cwd = File::Spec->canonpath(Cwd::cwd());
473 my $cpan = File::Spec->canonpath($CPAN::Config->{cpan_home});
475 $self->{under_cpan} = index($cwd, $cpan) > -1;
478 if ($self->{under_cpan}) {
479 $self->log_info("(I think I'm being run by CPAN, so will rely on CPAN to handle prerequisite installation)\n");
481 else {
482 $self->log_info("(I think you ran Build.PL directly, so will use CPAN to install prerequisites on demand)\n");
483 $self->{under_cpan} = 0;
487 return $self->{under_cpan};
490 # overridden simply to not print the default answer if chosen by hitting return
491 sub prompt {
492 my $self = shift;
493 my $mess = shift or die "prompt() called without a prompt message";
495 my $def;
496 if ( $self->_is_unattended && !@_ ) {
497 die <<EOF;
498 ERROR: This build seems to be unattended, but there is no default value
499 for this question. Aborting.
502 $def = shift if @_;
503 ($def, my $dispdef) = defined $def ? ($def, "[$def] ") : ('', ' ');
505 local $|=1;
506 print "$mess $dispdef";
508 my $ans = $self->_readline();
510 if ( !defined($ans) # Ctrl-D or unattended
511 or !length($ans) ) { # User hit return
512 #print "$def\n"; didn't like this!
513 $ans = $def;
516 return $ans;
519 # like the Module::Build version, except that we always get version from
520 # dist_version
521 sub find_dist_packages {
522 my $self = shift;
524 # Only packages in .pm files are candidates for inclusion here.
525 # Only include things in the MANIFEST, not things in developer's
526 # private stock.
528 my $manifest = $self->_read_manifest('MANIFEST') or die "Can't find dist packages without a MANIFEST file - run 'manifest' action first";
530 # Localize
531 my %dist_files = map { $self->localize_file_path($_) => $_ } keys %$manifest;
533 my @pm_files = grep {exists $dist_files{$_}} keys %{ $self->find_pm_files };
535 my $actual_version = $self->dist_version;
537 # First, we enumerate all packages & versions,
538 # seperating into primary & alternative candidates
539 my( %prime, %alt );
540 foreach my $file (@pm_files) {
541 next if $dist_files{$file} =~ m{^t/}; # Skip things in t/
543 my @path = split( /\//, $dist_files{$file} );
544 (my $prime_package = join( '::', @path[1..$#path] )) =~ s/\.pm$//;
546 my $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
548 foreach my $package ( $pm_info->packages_inside ) {
549 next if $package eq 'main'; # main can appear numerous times, ignore
550 next if grep /^_/, split( /::/, $package ); # private package, ignore
552 my $version = $pm_info->version( $package );
553 if ($version && $version != $actual_version) {
554 $self->log_warn("Package $package had version $version!\n");
556 $version = $actual_version;
558 if ( $package eq $prime_package ) {
559 if ( exists( $prime{$package} ) ) {
560 # M::B::ModuleInfo will handle this conflict
561 die "Unexpected conflict in '$package'; multiple versions found.\n";
563 else {
564 $prime{$package}{file} = $dist_files{$file};
565 $prime{$package}{version} = $version if defined( $version );
568 else {
569 push( @{$alt{$package}}, { file => $dist_files{$file}, version => $version } );
574 # Then we iterate over all the packages found above, identifying conflicts
575 # and selecting the "best" candidate for recording the file & version
576 # for each package.
577 foreach my $package ( keys( %alt ) ) {
578 my $result = $self->_resolve_module_versions( $alt{$package} );
580 if ( exists( $prime{$package} ) ) { # primary package selected
581 if ( $result->{err} ) {
582 # Use the selected primary package, but there are conflicting
583 # errors amoung multiple alternative packages that need to be
584 # reported
585 $self->log_warn("Found conflicting versions for package '$package'\n" .
586 " $prime{$package}{file} ($prime{$package}{version})\n" . $result->{err});
588 elsif ( defined( $result->{version} ) ) {
589 # There is a primary package selected, and exactly one
590 # alternative package
592 if ( exists( $prime{$package}{version} ) && defined( $prime{$package}{version} ) ) {
593 # Unless the version of the primary package agrees with the
594 # version of the alternative package, report a conflict
595 if ( $self->compare_versions( $prime{$package}{version}, '!=', $result->{version} ) ) {
596 $self->log_warn("Found conflicting versions for package '$package'\n" .
597 " $prime{$package}{file} ($prime{$package}{version})\n" .
598 " $result->{file} ($result->{version})\n");
601 else {
602 # The prime package selected has no version so, we choose to
603 # use any alternative package that does have a version
604 $prime{$package}{file} = $result->{file};
605 $prime{$package}{version} = $result->{version};
608 else {
609 # no alt package found with a version, but we have a prime
610 # package so we use it whether it has a version or not
613 else { # No primary package was selected, use the best alternative
614 if ( $result->{err} ) {
615 $self->log_warn("Found conflicting versions for package '$package'\n" . $result->{err});
618 # Despite possible conflicting versions, we choose to record
619 # something rather than nothing
620 $prime{$package}{file} = $result->{file};
621 $prime{$package}{version} = $result->{version} if defined( $result->{version} );
625 # Stringify versions
626 for (grep exists $_->{version}, values %prime) {
627 $_->{version} = $_->{version}->stringify if ref($_->{version});
630 return \%prime;
633 # our recommends syntax contains extra info that needs to be ignored at this
634 # stage
635 sub _parse_conditions {
636 my ($self, $spec) = @_;
638 ($spec) = split("/", $spec);
640 if ($spec =~ /^\s*([\w.]+)\s*$/) { # A plain number, maybe with dots, letters, and underscores
641 return (">= $spec");
643 else {
644 return split /\s*,\s*/, $spec;
648 # when generating META.yml, we output optional_features syntax (instead of
649 # recommends syntax). Note that as of CPAN v1.8802 nothing useful is done
650 # with this information, which is why we implement our own request to install
651 # the optional modules in install_optional()
652 sub prepare_metadata {
653 my ($self, $node, $keys) = @_;
654 my $p = $self->{properties};
656 # A little helper sub
657 my $add_node = sub {
658 my ($name, $val) = @_;
659 $node->{$name} = $val;
660 push @$keys, $name if $keys;
663 foreach (qw(dist_name dist_version dist_author dist_abstract license)) {
664 (my $name = $_) =~ s/^dist_//;
665 $add_node->($name, $self->$_());
666 die "ERROR: Missing required field '$_' for META.yml\n" unless defined($node->{$name}) && length($node->{$name});
668 $node->{version} = '' . $node->{version}; # Stringify version objects
670 if (defined( $self->license ) && defined( my $url = $self->valid_licenses->{ $self->license } )) {
671 $node->{resources}{license} = $url;
674 foreach ( @{$self->prereq_action_types} ) {
675 if (exists $p->{$_} and keys %{ $p->{$_} }) {
676 if ($_ eq 'recommends') {
677 my $hash;
678 while (my ($req, $val) = each %{ $p->{$_} }) {
679 my ($ver, $why, $used_by) = split("/", $val);
680 my $info = {};
681 $info->{description} = $why;
682 $info->{requires} = { $req => $ver };
683 $hash->{$used_by} = $info;
685 $add_node->('optional_features', $hash);
687 else {
688 $add_node->($_, $p->{$_});
693 if (exists $p->{dynamic_config}) {
694 $add_node->('dynamic_config', $p->{dynamic_config});
696 my $pkgs = eval { $self->find_dist_packages };
697 if ($@) {
698 $self->log_warn("$@\nWARNING: Possible missing or corrupt 'MANIFEST' file.\n" . "Nothing to enter for 'provides' field in META.yml\n");
700 else {
701 $node->{provides} = $pkgs if %$pkgs;
704 if (exists $p->{no_index}) {
705 $add_node->('no_index', $p->{no_index});
708 $add_node->('generated_by', "Module::Build version $Module::Build::VERSION");
710 $add_node->('meta-spec',
711 {version => '1.2',
712 url => 'http://module-build.sourceforge.net/META-spec-v1.2.html',
715 while (my($k, $v) = each %{$self->meta_add}) {
716 $add_node->($k, $v);
719 while (my($k, $v) = each %{$self->meta_merge}) {
720 $self->_hash_merge($node, $k, $v);
723 return $node;
726 # let us store extra things persistently in _build
727 sub _construct {
728 my $self = shift;
729 $self = $self->SUPER::_construct(@_);
731 my ($p, $ph) = ($self->{properties}, $self->{phash});
733 foreach (qw(manifest_skip post_install_scripts)) {
734 my $file = File::Spec->catfile($self->config_dir, $_);
735 $ph->{$_} = Module::Build::Notes->new(file => $file);
736 $ph->{$_}->restore if -e $file;
739 return $self;
741 sub write_config {
742 my $self = shift;
743 $self->SUPER::write_config;
745 # write extra things
746 $self->{phash}{$_}->write() foreach qw(manifest_skip post_install_scripts);
748 # be even more certain we can reload ourselves during a resume by copying
749 # ourselves to _build\lib
750 my $filename = File::Spec->catfile($self->{properties}{config_dir}, 'lib', 'ModuleBuildBioperl.pm');
751 my $filedir = File::Basename::dirname($filename);
753 File::Path::mkpath($filedir);
754 warn "Can't create directory $filedir: $!" unless -d $filedir;
756 File::Copy::copy('ModuleBuildBioperl.pm', $filename);
757 warn "Unable to copy 'ModuleBuildBioperl.pm' to '$filename'\n" unless -e $filename;
760 # add a file to the default MANIFEST.SKIP
761 sub add_to_manifest_skip {
762 my $self = shift;
763 my %files = map {$self->localize_file_path($_), 1} @_;
764 $self->{phash}{manifest_skip}->write(\%files);
767 # we always generate a new MANIFEST and MANIFEST.SKIP here, instead of allowing
768 # existing files to remain
769 sub ACTION_manifest {
770 my ($self) = @_;
772 my $maniskip = 'MANIFEST.SKIP';
773 if ( -e 'MANIFEST' || -e $maniskip ) {
774 $self->log_warn("MANIFEST files already exist, will overwrite them\n");
775 unlink('MANIFEST');
776 unlink($maniskip);
778 $self->_write_default_maniskip($maniskip);
780 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
781 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
782 ExtUtils::Manifest::mkmanifest();
785 # extended to add extra things to the default MANIFEST.SKIP
786 sub _write_default_maniskip {
787 my $self = shift;
788 $self->SUPER::_write_default_maniskip;
790 my @extra = keys %{$self->{phash}{manifest_skip}->read};
791 if (@extra) {
792 open(my $fh, '>>', 'MANIFEST.SKIP') or die "Could not open MANIFEST.SKIP file\n";
793 print $fh "\n# Avoid additional run-time generated things\n";
794 foreach my $line (@extra) {
795 print $fh $line, "\n";
797 close($fh);
801 # extended to run scripts post-installation
802 sub ACTION_install {
803 my ($self) = @_;
804 require ExtUtils::Install;
805 $self->depends_on('build');
806 ExtUtils::Install::install($self->install_map, !$self->quiet, 0, $self->{args}{uninst}||0);
807 $self->run_post_install_scripts;
809 sub add_post_install_script {
810 my $self = shift;
811 my %files = map {$self->localize_file_path($_), 1} @_;
812 $self->{phash}{post_install_scripts}->write(\%files);
814 sub run_post_install_scripts {
815 my $self = shift;
816 my @scripts = keys %{$self->{phash}{post_install_scripts}->read};
817 foreach my $script (@scripts) {
818 $self->run_perl_script($script);
822 # for use with auto_features, which should require LWP::UserAgent as one of
823 # its reqs
824 sub test_internet {
825 eval {require LWP::UserAgent;};
826 if ($@) {
827 # ideally this won't happen because auto_feature already specified
828 # LWP::UserAgent, so this sub wouldn't get called if LWP not installed
829 return "LWP::UserAgent not installed";
831 my $ua = LWP::UserAgent->new;
832 $ua->timeout(10);
833 $ua->env_proxy;
834 my $response = $ua->get('http://search.cpan.org/');
835 unless ($response->is_success) {
836 return "Could not connect to the internet (http://search.cpan.org/)";
838 return;
841 # nice directory names for dist-related actions
842 sub dist_dir {
843 my ($self) = @_;
844 my $version = $self->dist_version;
845 if ($version =~ /^\d\.\d{6}\d$/) {
846 # 1.x.x.100 returned as 1.x.x.1
847 $version .= '00';
849 $version =~ s/00(\d)/$1./g;
850 $version =~ s/\.$//;
852 if (my ($minor, $rev) = $version =~ /^\d\.(\d)\.\d\.(\d+)$/) {
853 my $dev = ! ($minor % 2 == 0);
854 if ($rev == 100) {
855 my $replace = $dev ? "_$rev" : '';
856 $version =~ s/\.\d+$/$replace/;
858 elsif ($rev < 100) {
859 $rev = sprintf("%03d", $rev);
860 $version =~ s/\.\d+$/_$rev-RC/;
862 else {
863 $rev -= 100 unless $dev;
864 my $replace = $dev ? "_$rev" : ".$rev";
865 $version =~ s/\.\d+$/$replace/;
869 return "$self->{properties}{dist_name}-$version";
871 sub ppm_name {
872 my $self = shift;
873 return $self->dist_dir.'-ppm';
876 # generate complete ppd4 version file
877 sub ACTION_ppd {
878 my $self = shift;
880 my $file = $self->make_ppd(%{$self->{args}});
881 $self->add_to_cleanup($file);
882 $self->add_to_manifest_skip($file);
885 # add pod2htm temp files to MANIFEST.SKIP, generated during ppmdist most likely
886 sub htmlify_pods {
887 my $self = shift;
888 $self->SUPER::htmlify_pods(@_);
889 $self->add_to_manifest_skip('pod2htm*');
892 # don't copy across man3 docs since they're of little use under Windows and
893 # have bad filenames
894 sub ACTION_ppmdist {
895 my $self = shift;
896 my @types = $self->install_types(1);
897 $self->SUPER::ACTION_ppmdist(@_);
898 $self->install_types(0);
901 # when supplied a true value, pretends libdoc doesn't exist (preventing man3
902 # installation for ppmdist). when supplied false, they exist again
903 sub install_types {
904 my ($self, $no_libdoc) = @_;
905 $self->{no_libdoc} = $no_libdoc if defined $no_libdoc;
906 my @types = $self->SUPER::install_types;
907 if ($self->{no_libdoc}) {
908 my @altered_types;
909 foreach my $type (@types) {
910 push(@altered_types, $type) unless $type eq 'libdoc';
912 return @altered_types;
914 return @types;
917 # overridden from Module::Build::PPMMaker for ppd4 compatability
918 sub make_ppd {
919 my ($self, %args) = @_;
921 require Module::Build::PPMMaker;
922 my $mbp = Module::Build::PPMMaker->new();
924 my %dist;
925 foreach my $info (qw(name author abstract version)) {
926 my $method = "dist_$info";
927 $dist{$info} = $self->$method() or die "Can't determine distribution's $info\n";
929 $dist{codebase} = $self->ppm_name.'.tar.gz';
930 $mbp->_simple_xml_escape($_) foreach $dist{abstract}, $dist{codebase}, @{$dist{author}};
932 my (undef, undef, undef, $mday, $mon, $year) = localtime();
933 $year += 1900;
934 $mon++;
935 my $date = "$year-$mon-$mday";
937 my $softpkg_version = $self->dist_dir;
938 $softpkg_version =~ s/^$dist{name}-//;
940 # to avoid a ppm bug, instead of including the requires in the softpackage
941 # for the distribution we're making, we'll make a seperate Bundle::
942 # softpackage that contains all the requires, and require only the Bundle in
943 # the real softpackage
944 my ($bundle_name) = $dist{name} =~ /^.+-(.+)/;
945 $bundle_name ||= 'core';
946 $bundle_name =~ s/^(\w)/\U$1/;
947 my $bundle_dir = "Bundle-BioPerl-$bundle_name-$softpkg_version-ppm";
948 my $bundle_file = "$bundle_dir.tar.gz";
949 my $bundle_softpkg_name = "Bundle-BioPerl-$bundle_name";
950 $bundle_name = "Bundle::BioPerl::$bundle_name";
952 # header
953 my $ppd = <<"PPD";
954 <SOFTPKG NAME=\"$dist{name}\" VERSION=\"$softpkg_version\" DATE=\"$date\">
955 <TITLE>$dist{name}</TITLE>
956 <ABSTRACT>$dist{abstract}</ABSTRACT>
957 @{[ join "\n", map " <AUTHOR>$_</AUTHOR>", @{$dist{author}} ]}
958 <PROVIDE NAME=\"$dist{name}::\" VERSION=\"$dist{version}\"/>
961 # provide section
962 foreach my $pm (@{$self->rscan_dir('Bio', qr/\.pm$/)}) {
963 # convert these filepaths to Module names
964 $pm =~ s/\//::/g;
965 $pm =~ s/\.pm//;
967 $ppd .= sprintf(<<'EOF', $pm, $dist{version});
968 <PROVIDE NAME="%s" VERSION="%s"/>
972 # rest of softpkg
973 $ppd .= <<"PPD";
974 <IMPLEMENTATION>
975 <ARCHITECTURE NAME=\"MSWin32-x86-multi-thread-5.8\"/>
976 <CODEBASE HREF=\"$dist{codebase}\"/>
977 <REQUIRE NAME=\"$bundle_name\" VERSION=\"$dist{version}\"/>
978 </IMPLEMENTATION>
979 </SOFTPKG>
982 # now a new softpkg for the bundle
983 $ppd .= <<"PPD";
985 <SOFTPKG NAME=\"$bundle_softpkg_name\" VERSION=\"$softpkg_version\" DATE=\"$date\">
986 <TITLE>$bundle_name</TITLE>
987 <ABSTRACT>Bundle of pre-requisites for $dist{name}</ABSTRACT>
988 @{[ join "\n", map " <AUTHOR>$_</AUTHOR>", @{$dist{author}} ]}
989 <PROVIDE NAME=\"$bundle_name\" VERSION=\"$dist{version}\"/>
990 <IMPLEMENTATION>
991 <ARCHITECTURE NAME=\"MSWin32-x86-multi-thread-5.8\"/>
992 <CODEBASE HREF=\"$bundle_file\"/>
995 # required section
996 # we do both requires and recommends to make installation on Windows as
997 # easy (mindless) as possible
998 for my $type ('requires', 'recommends') {
999 my $prereq = $self->$type;
1000 while (my ($modname, $version) = each %$prereq) {
1001 next if $modname eq 'perl';
1002 ($version) = split("/", $version) if $version =~ /\//;
1004 # Module names must have at least one ::
1005 unless ($modname =~ /::/) {
1006 $modname .= '::';
1009 # Bio::Root::Version number comes out as triplet number like 1.5.2;
1010 # convert to our own version
1011 if ($modname eq 'Bio::Root::Version') {
1012 $version = $dist{version};
1015 $ppd .= sprintf(<<'EOF', $modname, $version || '');
1016 <REQUIRE NAME="%s" VERSION="%s"/>
1021 # footer
1022 $ppd .= <<'EOF';
1023 </IMPLEMENTATION>
1024 </SOFTPKG>
1027 my $ppd_file = "$dist{name}.ppd";
1028 my $fh = IO::File->new(">$ppd_file") or die "Cannot write to $ppd_file: $!";
1029 print $fh $ppd;
1030 close $fh;
1032 $self->delete_filetree($bundle_dir);
1033 mkdir($bundle_dir) or die "Cannot create '$bundle_dir': $!";
1034 $self->make_tarball($bundle_dir);
1035 $self->delete_filetree($bundle_dir);
1036 $self->add_to_cleanup($bundle_file);
1037 $self->add_to_manifest_skip($bundle_file);
1039 return $ppd_file;
1042 # we make all archive formats we want, not just .tar.gz
1043 # we also auto-run manifest action, since we always want to re-create
1044 # MANIFEST and MANIFEST.SKIP just-in-time
1045 sub ACTION_dist {
1046 my ($self) = @_;
1048 $self->depends_on('manifest');
1049 $self->depends_on('distdir');
1051 my $dist_dir = $self->dist_dir;
1053 $self->make_zip($dist_dir);
1054 $self->make_tarball($dist_dir);
1055 $self->delete_filetree($dist_dir);
1058 # makes zip file for windows users and bzip2 files as well
1059 sub make_zip {
1060 my ($self, $dir, $file) = @_;
1061 $file ||= $dir;
1063 $self->log_info("Creating $file.zip\n");
1064 my $zip_flags = $self->verbose ? '-r' : '-rq';
1065 $self->do_system($self->split_like_shell("zip"), $zip_flags, "$file.zip", $dir);
1067 $self->log_info("Creating $file.bz2\n");
1068 require Archive::Tar;
1069 # Archive::Tar versions >= 1.09 use the following to enable a compatibility
1070 # hack so that the resulting archive is compatible with older clients.
1071 $Archive::Tar::DO_NOT_USE_PREFIX = 0;
1072 my $files = $self->rscan_dir($dir);
1073 Archive::Tar->create_archive("$file.tar", 0, @$files);
1074 $self->do_system($self->split_like_shell("bzip2"), "-k", "$file.tar");