Bring base classes from BioPerl-run back to BioPerl.
[bioperl-live.git] / Bio / Root / Build.pm
blob36dbd47d6871ec95ad12c60b12a5bb8432ae6973
1 package Bio::Root::Build;
2 use Bio::Root::Version;
3 use strict;
4 use warnings;
6 =head1 SYNOPSIS
8 ...TO BE ADDED
10 =head1 DESCRIPTION
12 This is a subclass of Module::Build so we can override certain methods and do
13 fancy stuff
15 It was first written against Module::Build::Base v0.2805. Many of the methods
16 here are copy/pasted from there in their entirety just to change one or two
17 minor things, since for the most part Module::Build::Base code is hard to
18 cleanly override.
20 B<Note>: per bug 3196, the majority of the code in this module has been revised
21 or commented out to bring it in line with the Module::Build API. In particular,
22 'requires/recommends' tags in the Build.PL file were not of the same format as
23 those for Module::Build, and so caused serious issues with newer versions
24 (including giving incorrect meta data). Other problematic methods involving
25 automatic installation of prereq modules via CPAN were also removed as they do
26 not work with more modern perl tools such as perlbrew and cpanm.
28 =head1 AUTHOR Sendu Bala
30 =cut
32 BEGIN {
33 # we really need Module::Build to be installed
34 eval "use base 'Module::Build'; 1" or die "This package requires Module::Build v0.42 or greater to install itself.\n$@";
36 # ensure we'll be able to reload this module later by adding its path to inc
37 use Cwd;
38 use lib Cwd::cwd();
41 our @extra_types = qw(options excludes_os feature_requires test); # test must always be last in the list!
42 our $checking_types = "requires|conflicts|".join("|", @extra_types);
44 our $VERSION = $Bio::Root::Version::VERSION;
46 =head2 find_pm_files
48 Our modules are in Bio, not lib
49 =cut
51 sub find_pm_files {
52 my $self = shift;
53 foreach my $pm (@{$self->rscan_dir('Bio', qr/\.pm$/)}) {
54 $self->{properties}{pm_files}->{$pm} = File::Spec->catfile('lib', $pm);
57 $self->_find_file_by_type('pm', 'lib');
60 =head2 choose_scripts
62 Ask what scripts to install (this method is unique to bioperl)
63 =cut
65 sub choose_scripts {
66 my $self = shift;
67 my $accept = shift;
69 # we can offer interactive installation by groups only if we have subdirs
70 # in scripts and no .PLS files there
71 opendir(my $scripts_dir, 'scripts') or die "Can't open directory 'scripts': $!\n";
72 my $int_ok = 0;
73 my @group_dirs;
75 # only retain top-level script directories (the 'categories')
76 while (my $thing = readdir($scripts_dir)) {
77 next if $thing =~ /^\./;
78 $thing = File::Spec->catfile('scripts', $thing);
79 if (-d $thing) {
80 $int_ok = 1;
81 push(@group_dirs, $thing);
84 closedir($scripts_dir);
85 my $question = $int_ok ? "Install [a]ll BioPerl scripts, [n]one, ".
86 "or choose groups [i]nteractively?" : "Install [a]ll BioPerl scripts ".
87 "or [n]one?";
89 my $prompt = $accept ? 'a' : $self->prompt($question, 'a');
91 if ($prompt =~ /^[aA]/) {
92 $self->log_info(" - will install all scripts\n");
93 $self->notes(chosen_scripts => 'all');
95 elsif ($prompt =~ /^[iI]/) {
96 $self->log_info(" - will install interactively:\n");
98 my @chosen_scripts;
99 foreach my $group_dir (@group_dirs) {
100 my $group = File::Basename::basename($group_dir);
101 print " * group '$group' has:\n";
103 my @script_files = @{$self->rscan_dir($group_dir, qr/\.PLS$|\.pl$/)};
104 foreach my $script_file (@script_files) {
105 my $script = File::Basename::basename($script_file);
106 print " $script\n";
109 my $result = $self->prompt(" Install scripts for group '$group'? [y]es [n]o [q]uit", 'n');
110 die if $result =~ /^[qQ]/;
111 if ($result =~ /^[yY]/) {
112 $self->log_info(" + will install group '$group'\n");
113 push(@chosen_scripts, @script_files);
115 else {
116 $self->log_info(" - will not install group '$group'\n");
120 my $chosen_scripts = @chosen_scripts ? join("|", @chosen_scripts) : 'none';
122 $self->notes(chosen_scripts => $chosen_scripts);
124 else {
125 $self->log_info(" - won't install any scripts\n");
126 $self->notes(chosen_scripts => 'none');
129 print "\n";
132 =head2 script_files
134 Our version of script_files doesn't take args but just installs those scripts
135 requested by the user after choose_scripts() is called. If it wasn't called,
136 installs all scripts in scripts directory
137 =cut
139 sub script_files {
140 my $self = shift;
142 unless (-d 'scripts') {
143 return {};
146 my $chosen_scripts = $self->notes('chosen_scripts');
147 if ($chosen_scripts) {
148 return if $chosen_scripts eq 'none';
149 return { map {$_, 1} split(/\|/, $chosen_scripts) } unless $chosen_scripts eq 'all';
152 return $_ = { map {$_,1} @{$self->rscan_dir('scripts', qr/\.PLS$|\.pl$/)} };
155 =head2 prompt
157 Overridden simply to not print the default answer if chosen by hitting return
158 =cut
160 sub prompt {
161 my $self = shift;
162 my $mess = shift or die "prompt() called without a prompt message";
164 my $def;
165 if ( $self->_is_unattended && !@_ ) {
166 die <<EOF;
167 ERROR: This build seems to be unattended, but there is no default value
168 for this question. Aborting.
171 $def = shift if @_;
172 ($def, my $dispdef) = defined $def ? ($def, "[$def] ") : ('', ' ');
174 local $|=1;
175 print "$mess $dispdef";
177 my $ans = $self->_readline();
179 if ( !defined($ans) # Ctrl-D or unattended
180 or !length($ans) ) { # User hit return
181 #print "$def\n"; didn't like this!
182 $ans = $def;
185 return $ans;
188 =head2 ACTION_manifest
190 We always generate a new MANIFEST instead of allowing existing files to remain
191 MANIFEST.SKIP is left alone
192 =cut
194 sub ACTION_manifest {
195 my ($self) = @_;
196 if ( -e 'MANIFEST' || -e 'MANIFEST.SKIP' ) {
197 $self->log_warn("MANIFEST files already exist, will overwrite them\n");
198 unlink('MANIFEST');
200 require ExtUtils::Manifest; # ExtUtils::Manifest is not warnings clean.
201 local ($^W, $ExtUtils::Manifest::Quiet) = (0,1);
202 ExtUtils::Manifest::mkmanifest();
205 =head2 ACTION_install
207 Extended to run scripts post-installation
208 =cut
210 sub ACTION_install {
211 my ($self) = @_;
212 require ExtUtils::Install;
213 $self->depends_on('build');
214 ExtUtils::Install::install($self->install_map,
215 !$self->quiet,
217 $self->{args}{uninst} || 0);
218 #$self->run_post_install_scripts;
221 =head2 test_internet
223 For use with auto_features, which should require LWP::UserAgent as one of
224 its reqs
226 Note: as of 4-11-11, this is no longer called - if someone wants to run
227 network tests (off by default) w/o a network, then they are hanging themselves
228 by their own shoelaces.
229 =cut
231 sub test_internet {
232 eval {require LWP::UserAgent;};
233 if ($@) {
234 # ideally this won't happen because auto_feature already specified
235 # LWP::UserAgent, so this sub wouldn't get called if LWP not installed
236 return "LWP::UserAgent not installed";
238 my $ua = LWP::UserAgent->new;
239 $ua->timeout(10);
240 $ua->env_proxy;
241 my $response = $ua->get('http://search.cpan.org/');
242 unless ($response->is_success) {
243 return "Could not connect to the internet (http://search.cpan.org/)";
245 return;
248 =head2 ACTION_ppmdist
250 Don't copy across man3 docs since they're of little use under Windows and
251 have bad filenames
252 =cut
254 sub ACTION_ppmdist {
255 my $self = shift;
256 my @types = $self->install_types(1);
257 $self->SUPER::ACTION_ppmdist(@_);
258 $self->install_types(0);
261 =head2 install_types
263 When supplied a true value, pretends libdoc doesn't exist (preventing man3
264 installation for ppmdist). when supplied false, they exist again
265 =cut
267 sub install_types {
268 my ($self, $no_libdoc) = @_;
269 $self->{no_libdoc} = $no_libdoc if defined $no_libdoc;
270 my @types = $self->SUPER::install_types;
271 if ($self->{no_libdoc}) {
272 my @altered_types;
273 foreach my $type (@types) {
274 push(@altered_types, $type) unless $type eq 'libdoc';
276 return @altered_types;
278 return @types;
281 =head2 ACTION_dist
283 We make all archive formats we want, not just .tar.gz
284 we also auto-run manifest action, since we always want to re-create
285 MANIFEST and MANIFEST.SKIP just-in-time
286 =cut
288 sub ACTION_dist {
289 my ($self) = @_;
291 $self->depends_on('manifest');
292 $self->depends_on('distdir');
294 my $dist_dir = $self->dist_dir;
296 $self->make_zip($dist_dir);
297 $self->make_tarball($dist_dir);
298 $self->delete_filetree($dist_dir);
301 =head2 ACTION_clean
303 Define custom clean/realclean actions to rearrange config file cleanup
304 =cut
306 sub ACTION_clean {
307 my ($self) = @_;
308 $self->log_info("Cleaning up build files\n");
309 foreach my $item (map glob($_), $self->cleanup) {
310 $self->delete_filetree($item);
312 $self->log_info("Cleaning up configuration files\n");
313 $self->delete_filetree($self->config_dir);
316 =head2 ACTION_realclean
318 Define custom clean/realclean actions to rearrange config file cleanup
319 =cut
321 sub ACTION_realclean {
322 my ($self) = @_;
323 $self->depends_on('clean');
324 for my $method (qw(mymetafile mymetafile2 build_script)) {
325 if ($self->can($method)) {
326 $self->delete_filetree($self->$method);
327 $self->log_info("Cleaning up $method data\n");
332 =head2 get_metadata
334 This wraps the base metafile method to add in version information from
335 Bio::Root::Version to META.json and META.yml if it isn't already present. Note
336 this should be compliant with meta_add and meta_merge, but occurs after those
337 steps. If a version is already set and dist_version differs from the set one, a
338 warning is printed.
340 =cut
342 sub get_metadata {
343 my ($self, %args) = @_;
344 my $metadata = $self->SUPER::get_metadata(%args);
346 if (exists $metadata->{provides}) {
347 my $ver = $self->dist_version;
348 my $pkgs = $metadata->{provides};
349 for my $p (keys %{$pkgs}) {
350 if (!exists($pkgs->{$p}->{'version'})) {
351 $pkgs->{$p}->{'version'} = $ver;
352 } else {
353 $self->log_warn("Note: Module $p has a set version: ".$pkgs->{$p}->{'version'}."\n")
354 if $pkgs->{$p}->{'version'} ne $ver;
358 return $metadata;
361 =head2 make_zip
363 Makes zip file for windows users and bzip2 files as well
364 =cut
366 sub make_zip {
367 my ($self, $dir, $file) = @_;
368 $file ||= $dir;
370 $self->log_info("Creating $file.zip\n");
371 my $zip_flags = $self->verbose ? '-r' : '-rq';
372 $self->do_system($self->split_like_shell("zip"), $zip_flags, "$file.zip", $dir);
374 $self->log_info("Creating $file.bz2\n");
375 require Archive::Tar;
376 # Archive::Tar versions >= 1.09 use the following to enable a compatibility
377 # hack so that the resulting archive is compatible with older clients.
378 $Archive::Tar::DO_NOT_USE_PREFIX = 0;
379 my $files = $self->rscan_dir($dir);
380 Archive::Tar->create_archive("$file.tar", 0, @$files);
381 $self->do_system($self->split_like_shell("bzip2"), "-k", "$file.tar");
384 =head2 prompt_for_network
386 A method that can be called in a Build.PL script to ask the user if they want
387 internet tests.
388 Should only be called if you have tested for yourself that
389 $build->feature('Network Tests') is true
390 =cut
392 sub prompt_for_network {
393 my ($self, $accept) = @_;
395 my $proceed = $accept ? 0 : $self->y_n( "Do you want to run tests that require connection to servers across the internet\n"
396 . "(likely to cause some failures)? y/n", 'n');
398 if ($proceed) {
399 $self->notes('network' => 1);
400 $self->log_info(" - will run internet-requiring tests\n");
401 my $use_email = $self->y_n("Do you want to run tests requiring a valid email address? y/n",'n');
402 if ($use_email) {
403 my $address = $self->prompt("Enter email address:");
404 $self->notes(email => $address);
407 else {
408 $self->notes(network => 0);
409 $self->log_info(" - will not run internet-requiring tests\n");
413 =head2 print_build_script
415 Override the build script warnings flag
416 =cut
418 sub print_build_script {
419 my ($self, $fh) = @_;
421 my $build_package = $self->build_class;
423 my $closedata="";
425 my $config_requires;
426 if ( -f $self->metafile ) {
427 my $meta = eval { $self->read_metafile( $self->metafile ) };
428 $config_requires = $meta && $meta->{configure_requires}{'Module::Build'};
430 $config_requires ||= 0;
432 my %q = map {$_, $self->$_()} qw(config_dir base_dir);
434 $q{base_dir} = Win32::GetShortPathName($q{base_dir}) if $self->is_windowsish;
436 $q{magic_numfile} = $self->config_file('magicnum');
438 my @myINC = $self->_added_to_INC;
439 @myINC = map { $_ = File::Spec->canonpath( $_ );
440 $_ =~ s/([\\\'])/\\$1/g;
442 } @myINC;
443 # Remove duplicates
444 @myINC = sort {$a cmp $b}
445 keys %{ { map { $_ => 1 } @myINC } };
447 foreach my $key (keys %q) {
448 $q{$key} = File::Spec->canonpath( $q{$key} );
449 $q{$key} =~ s/([\\\'])/\\$1/g;
452 my $quoted_INC = join ",\n", map " '$_'", @myINC;
453 my $shebang = $self->_startperl;
454 my $magic_number = $self->magic_number;
456 # unique to bioperl, shut off overly verbose warnings on windows, bug 3215
457 my $w = $^O =~ /win/i ? '# no warnings (win)' : '$^W = 1; # Use warnings';
459 print $fh <<EOF;
460 $shebang
462 use strict;
463 use Cwd;
464 use File::Basename;
465 use File::Spec;
467 sub magic_number_matches {
468 return 0 unless -e '$q{magic_numfile}';
469 open my \$FH, '<', '$q{magic_numfile}' or return 0;
470 my \$filenum = <\$FH>;
471 close \$FH;
472 return \$filenum == $magic_number;
475 my \$progname;
476 my \$orig_dir;
477 BEGIN {
479 \$progname = basename(\$0);
480 \$orig_dir = Cwd::cwd();
481 my \$base_dir = '$q{base_dir}';
482 if (!magic_number_matches()) {
483 unless (chdir(\$base_dir)) {
484 die ("Could not chdir '\$base_dir', aborting\\n");
486 unless (magic_number_matches()) {
487 die ("Configuration seems to be out of date, please re-run 'perl Build.PL' again.\\n");
490 unshift \@INC,
492 $quoted_INC
496 close(*DATA) unless eof(*DATA); # ensure no open handles to this script
498 use $build_package;
499 Module::Build->VERSION(q{$config_requires});
501 # Some platforms have problems setting \$^X in shebang contexts, fix it up here
502 \$^X = Module::Build->find_perl_interpreter;
504 if (-e 'Build.PL' and not $build_package->up_to_date('Build.PL', \$progname)) {
505 warn "Warning: Build.PL has been altered. You may need to run 'perl Build.PL' again.\\n";
508 # This should have just enough arguments to be able to bootstrap the rest.
509 my \$build =
510 $build_package->resume( properties => { config_dir => '$q{config_dir}',
511 orig_dir => \$orig_dir, },
514 \$build->dispatch;