blib/lib/Module/AutoInstall.pm untested

filter: Statementstmt 0.0% 0 / 459 Branchbran 0.0% 0 / 250 Conditioncond 0.0% 0 / 180 MC/DCmcdc 0.0% 0 / 106 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 1067 CC 126 SCAR 54.5
CC
126
Cov
0%
import
930.0
install
210.0
_install_cpan
210.0
CRAP 231.6
1 package Module::AutoInstall;
2  
3 use strict;
4 use Cwd ();
5 use File::Spec ();
6 use ExtUtils::MakeMaker ();
7  
8 use vars qw{$VERSION};
9 BEGIN {
10 $VERSION = '1.21';
11 }
12  
13 # special map on pre-defined feature sets
14 my %FeatureMap = (
15 '' => 'Core Features', # XXX: deprecated
16 '-core' => 'Core Features',
17 );
18  
19 # various lexical flags
20 my ( @Missing, @Existing, %DisabledTests, $UnderCPAN, $InstallDepsTarget, $HasCPANPLUS );
21 my (
22 $Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly, $AllDeps,
23 $UpgradeDeps
24 );
25 my ( $PostambleActions, $PostambleActionsNoTest, $PostambleActionsUpgradeDeps,
26 $PostambleActionsUpgradeDepsNoTest, $PostambleActionsListDeps,
27 $PostambleActionsListAllDeps, $PostambleUsed, $NoTest);
28  
29 # See if it's a testing or non-interactive session
30 _accept_default( $ENV{AUTOMATED_TESTING} or ! -t STDIN );
31 _init();
32  
33 sub _accept_default {
34 $AcceptDefault = shift;
35 }
36  
37 sub _installdeps_target {
38 $InstallDepsTarget = shift;
39 }
40  
41 sub missing_modules {
42 return @Missing;
43 }
44  
45 sub do_install {
46 __PACKAGE__->install(
47 [
48 $Config
49 ? ( UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
50 : ()
51 ],
52 @Missing,
53 );
54 }
55  
56 # initialize various flags, and/or perform install
57 sub _init {
58 foreach my $arg (
59 @ARGV,
60 split(
61 /[\s\t]+/,
62 $ENV{PERL_AUTOINSTALL} || $ENV{PERL_EXTUTILS_AUTOINSTALL} || ''
63 )
64 )
65 {
66 if ( $arg =~ /^--config=(.*)$/ ) {
67 $Config = [ split( ',', $1 ) ];
68 }
69 elsif ( $arg =~ /^--installdeps=(.*)$/ ) {
70 __PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) );
71 exit 0;
72 }
73 elsif ( $arg =~ /^--upgradedeps=(.*)$/ ) {
74 $UpgradeDeps = 1;
75 __PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) );
76 exit 0;
77 }
78 elsif ( $arg =~ /^--default(?:deps)?$/ ) {
79 $AcceptDefault = 1;
80 }
81 elsif ( $arg =~ /^--check(?:deps)?$/ ) {
82 $CheckOnly = 1;
83 }
84 elsif ( $arg =~ /^--skip(?:deps)?$/ ) {
85 $SkipInstall = 1;
86 }
87 elsif ( $arg =~ /^--test(?:only)?$/ ) {
88 $TestOnly = 1;
89 }
90 elsif ( $arg =~ /^--all(?:deps)?$/ ) {
91 $AllDeps = 1;
92 }
93 }
94 }
95  
96 # overrides MakeMaker's prompt() to automatically accept the default choice
97 sub _prompt {
98 goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault;
99  
100 my ( $prompt, $default ) = @_;
101 my $y = ( $default =~ /^[Yy]/ );
102  
103 print $prompt, ' [', ( $y ? 'Y' : 'y' ), '/', ( $y ? 'n' : 'N' ), '] ';
104 print "$default\n";
105 return $default;
106 }
107  
108 # the workhorse
109 sub import {
110 my $class = shift;
111 my @args = @_ or return;
112 my $core_all;
113  
114 print "*** $class version " . $class->VERSION . "\n";
115 print "*** Checking for Perl dependencies...\n";
116  
117 my $cwd = Cwd::getcwd();
118  
119 $Config = [];
120  
121 my $maxlen = length(
122 (
123 sort { length($b) <=> length($a) }
124 grep { /^[^\-]/ }
125 map {
126 ref($_)
127 ? ( ( ref($_) eq 'HASH' ) ? keys(%$_) : @{$_} )
128 : ''
129 }
130 map { +{@args}->{$_} }
131 grep { /^[^\-]/ or /^-core$/i } keys %{ +{@args} }
132 )[0]
133 );
134  
135 # We want to know if we're under CPAN early to avoid prompting, but
136 # if we aren't going to try and install anything anyway then skip the
137 # check entirely since we don't want to have to load (and configure)
138 # an old CPAN just for a cosmetic message
139  
140 $UnderCPAN = _check_lock(1) unless $SkipInstall || $InstallDepsTarget;
141  
142 while ( my ( $feature, $modules ) = splice( @args, 0, 2 ) ) {
143 my ( @required, @tests, @skiptests );
144 my $default = 1;
145 my $conflict = 0;
146  
147 if ( $feature =~ m/^-(\w+)$/ ) {
148 my $option = lc($1);
149  
150 # check for a newer version of myself
151 _update_to( $modules, @_ ) and return if $option eq 'version';
152  
153 # sets CPAN configuration options
154 $Config = $modules if $option eq 'config';
155  
156 # promote every features to core status
157 $core_all = ( $modules =~ /^all$/i ) and next
158 if $option eq 'core';
159  
160 next unless $option eq 'core';
161 }
162  
163 print "[" . ( $FeatureMap{ lc($feature) } || $feature ) . "]\n";
164  
165 $modules = [ %{$modules} ] if UNIVERSAL::isa( $modules, 'HASH' );
166  
167 unshift @$modules, -default => &{ shift(@$modules) }
168 if ( ref( $modules->[0] ) eq 'CODE' ); # XXX: bugward compatibility
169  
170 while ( my ( $mod, $arg ) = splice( @$modules, 0, 2 ) ) {
171 if ( $mod =~ m/^-(\w+)$/ ) {
172 my $option = lc($1);
173  
174 $default = $arg if ( $option eq 'default' );
175 $conflict = $arg if ( $option eq 'conflict' );
176 @tests = @{$arg} if ( $option eq 'tests' );
177 @skiptests = @{$arg} if ( $option eq 'skiptests' );
178  
179 next;
180 }
181  
182 printf( "- %-${maxlen}s ...", $mod );
183  
184 if ( $arg and $arg =~ /^\D/ ) {
185 unshift @$modules, $arg;
186 $arg = 0;
187 }
188  
189 # XXX: check for conflicts and uninstalls(!) them.
190 my $cur = _version_of($mod);
191 if (_version_cmp ($cur, $arg) >= 0)
192 {
193 print "loaded. ($cur" . ( $arg ? " >= $arg" : '' ) . ")\n";
194 push @Existing, $mod => $arg;
195 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
196 }
197 else {
198 if (not defined $cur) # indeed missing
199 {
200 print "missing." . ( $arg ? " (would need $arg)" : '' ) . "\n";
201 }
202 else
203 {
204 # no need to check $arg as _version_cmp ($cur, undef) would satisfy >= above
205 print "too old. ($cur < $arg)\n";
206 }
207  
208 push @required, $mod => $arg;
209 }
210 }
211  
212 next unless @required;
213  
214 my $mandatory = ( $feature eq '-core' or $core_all );
215  
216 if (
217 !$SkipInstall
218 and (
219 $CheckOnly
220 or ($mandatory and $UnderCPAN)
221 or $AllDeps
222 or $InstallDepsTarget
223 or _prompt(
224 qq{==> Auto-install the }
225 . ( @required / 2 )
226 . ( $mandatory ? ' mandatory' : ' optional' )
227 . qq{ module(s) from CPAN?},
228 $default ? 'y' : 'n',
229 ) =~ /^[Yy]/
230 )
231 )
232 {
233 push( @Missing, @required );
234 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
235 }
236  
237 elsif ( !$SkipInstall
238 and $default
239 and $mandatory
240 and
241 _prompt( qq{==> The module(s) are mandatory! Really skip?}, 'n', )
242 =~ /^[Nn]/ )
243 {
244 push( @Missing, @required );
245 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
246 }
247  
248 else {
249 $DisabledTests{$_} = 1 for map { glob($_) } @tests;
250 }
251 }
252  
253 if ( @Missing and not( $CheckOnly or $UnderCPAN) ) {
254 require Config;
255 my $make = $Config::Config{make};
256 if ($InstallDepsTarget) {
257 print
258 "*** To install dependencies type '$make installdeps' or '$make installdeps_notest'.\n";
259 }
260 else {
261 print
262 "*** Dependencies will be installed the next time you type '$make'.\n";
263 }
264  
265 # make an educated guess of whether we'll need root permission.
266 print " (You may need to do that as the 'root' user.)\n"
267 if eval '$>';
268 }
269 print "*** $class configuration finished.\n";
270  
271 chdir $cwd;
272  
273 # import to main::
274 no strict 'refs';
275 *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main';
276  
277 return (@Existing, @Missing);
278 }
279  
280 sub _running_under {
281 my $thing = shift;
282 print <<"END_MESSAGE";
283 *** Since we're running under ${thing}, I'll just let it take care
284 of the dependency's installation later.
285 END_MESSAGE
286 return 1;
287 }
288  
289 # Check to see if we are currently running under CPAN.pm and/or CPANPLUS;
290 # if we are, then we simply let it taking care of our dependencies
291 sub _check_lock {
292 return unless @Missing or @_;
293  
294 if ($ENV{PERL5_CPANM_IS_RUNNING}) {
295 return _running_under('cpanminus');
296 }
297  
298 my $cpan_env = $ENV{PERL5_CPAN_IS_RUNNING};
299  
300 if ($ENV{PERL5_CPANPLUS_IS_RUNNING}) {
301 return _running_under($cpan_env ? 'CPAN' : 'CPANPLUS');
302 }
303  
304 require CPAN;
305  
306 if ($CPAN::VERSION > '1.89') {
307 if ($cpan_env) {
308 return _running_under('CPAN');
309 }
310 return; # CPAN.pm new enough, don't need to check further
311 }
312  
313 # last ditch attempt, this -will- configure CPAN, very sorry
314  
315 _load_cpan(1); # force initialize even though it's already loaded
316  
317 # Find the CPAN lock-file
318 my $lock = MM->catfile( $CPAN::Config->{cpan_home}, ".lock" );
319 return unless -f $lock;
320  
321 # Check the lock
322 local *LOCK;
323 return unless open(LOCK, $lock);
324  
325 if (
326 ( $^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid() )
327 and ( $CPAN::Config->{prerequisites_policy} || '' ) ne 'ignore'
328 ) {
329 print <<'END_MESSAGE';
330  
331 *** Since we're running under CPAN, I'll just let it take care
332 of the dependency's installation later.
333 END_MESSAGE
334 return 1;
335 }
336  
337 close LOCK;
338 return;
339 }
340  
341 sub install {
342 my $class = shift;
343  
344 my $i; # used below to strip leading '-' from config keys
345 my @config = ( map { s/^-// if ++$i; $_ } @{ +shift } );
346  
347 my ( @modules, @installed, @modules_to_upgrade );
348 while (my ($pkg, $ver) = splice(@_, 0, 2)) {
349  
350 # grep out those already installed
351 if (_version_cmp(_version_of($pkg), $ver) >= 0) {
352 push @installed, $pkg;
353 if ($UpgradeDeps) {
354 push @modules_to_upgrade, $pkg, $ver;
355 }
356 }
357 else {
358 push @modules, $pkg, $ver;
359 }
360 }
361  
362 if ($UpgradeDeps) {
363 push @modules, @modules_to_upgrade;
364 @installed = ();
365 @modules_to_upgrade = ();
366 }
367  
368 return @installed unless @modules; # nothing to do
369 return @installed if _check_lock(); # defer to the CPAN shell
370  
371 print "*** Installing dependencies...\n";
372  
373 return unless _connected_to('cpan.org');
374  
375 my %args = @config;
376 my %failed;
377 local *FAILED;
378 if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) {
379 while (<FAILED>) { chomp; $failed{$_}++ }
380 close FAILED;
381  
382 my @newmod;
383 while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) {
384 push @newmod, ( $k => $v ) unless $failed{$k};
385 }
386 @modules = @newmod;
387 }
388  
389 if ( _has_cpanplus() and not $ENV{PERL_AUTOINSTALL_PREFER_CPAN} ) {
390 _install_cpanplus( \@modules, \@config );
391 } else {
392 _install_cpan( \@modules, \@config );
393 }
394  
395 print "*** $class installation finished.\n";
396  
397 # see if we have successfully installed them
398 while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
399 if ( _version_cmp( _version_of($pkg), $ver ) >= 0 ) {
400 push @installed, $pkg;
401 }
402 elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) {
403 print FAILED "$pkg\n";
404 }
405 }
406  
407 close FAILED if $args{do_once};
408  
409 return @installed;
410 }
411  
412 sub _install_cpanplus {
413 my @modules = @{ +shift };
414 my @config = _cpanplus_config( @{ +shift } );
415 my $installed = 0;
416  
417 require CPANPLUS::Backend;
418 my $cp = CPANPLUS::Backend->new;
419 my $conf = $cp->configure_object;
420  
421 return unless $conf->can('conf') # 0.05x+ with "sudo" support
422 or _can_write($conf->_get_build('base')); # 0.04x
423  
424 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
425 my $makeflags = $conf->get_conf('makeflags') || '';
426 if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) {
427 # 0.03+ uses a hashref here
428 $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST};
429  
430 } else {
431 # 0.02 and below uses a scalar
432 $makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
433 if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
434  
435 }
436 $conf->set_conf( makeflags => $makeflags );
437 $conf->set_conf( prereqs => 1 );
438  
439
440  
441 while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) {
442 $conf->set_conf( $key, $val );
443 }
444  
445 my $modtree = $cp->module_tree;
446 while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
447 print "*** Installing $pkg...\n";
448  
449 MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
450  
451 my $success;
452 my $obj = $modtree->{$pkg};
453  
454 if ( $obj and _version_cmp( $obj->{version}, $ver ) >= 0 ) {
455 my $pathname = $pkg;
456 $pathname =~ s/::/\\W/;
457  
458 foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
459 delete $INC{$inc};
460 }
461  
462 my $rv = $cp->install( modules => [ $obj->{module} ] );
463  
464 if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) {
465 print "*** $pkg successfully installed.\n";
466 $success = 1;
467 } else {
468 print "*** $pkg installation cancelled.\n";
469 $success = 0;
470 }
471  
472 $installed += $success;
473 } else {
474 print << ".";
475 *** Could not find a version $ver or above for $pkg; skipping.
476 .
477 }
478  
479 MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
480 }
481  
482 return $installed;
483 }
484  
485 sub _cpanplus_config {
486 my @config = ();
487 while ( @_ ) {
488 my ($key, $value) = (shift(), shift());
489 if ( $key eq 'prerequisites_policy' ) {
490 if ( $value eq 'follow' ) {
491 $value = CPANPLUS::Internals::Constants::PREREQ_INSTALL();
492 } elsif ( $value eq 'ask' ) {
493 $value = CPANPLUS::Internals::Constants::PREREQ_ASK();
494 } elsif ( $value eq 'ignore' ) {
495 $value = CPANPLUS::Internals::Constants::PREREQ_IGNORE();
496 } else {
497 die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n";
498 }
499 push @config, 'prereqs', $value;
500 } elsif ( $key eq 'force' ) {
501 push @config, $key, $value;
502 } elsif ( $key eq 'notest' ) {
503 push @config, 'skiptest', $value;
504 } else {
505 die "*** Cannot convert option $key to CPANPLUS version.\n";
506 }
507 }
508 return @config;
509 }
510  
511 sub _install_cpan {
512 my @modules = @{ +shift };
513 my @config = @{ +shift };
514 my $installed = 0;
515 my %args;
516  
517 _load_cpan();
518 require Config;
519  
520 if (CPAN->VERSION < 1.80) {
521 # no "sudo" support, probe for writableness
522 return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) )
523 and _can_write( $Config::Config{sitelib} );
524 }
525  
526 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
527 my $makeflags = $CPAN::Config->{make_install_arg} || '';
528 $CPAN::Config->{make_install_arg} =
529 join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
530 if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
531  
532 # don't show start-up info
533 $CPAN::Config->{inhibit_startup_message} = 1;
534  
535 # set additional options
536 while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) {
537 ( $args{$opt} = $arg, next )
538 if $opt =~ /^(?:force|notest)$/; # pseudo-option
539 $CPAN::Config->{$opt} = $opt eq 'urllist' ? [$arg] : $arg;
540 }
541  
542 if ($args{notest} && (not CPAN::Shell->can('notest'))) {
543 die "Your version of CPAN is too old to support the 'notest' pragma";
544 }
545  
546 local $CPAN::Config->{prerequisites_policy} = 'follow';
547  
548 while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
549 MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
550  
551 print "*** Installing $pkg...\n";
552  
553 my $obj = CPAN::Shell->expand( Module => $pkg );
554 my $success = 0;
555  
556 if ( $obj and _version_cmp( $obj->cpan_version, $ver ) >= 0 ) {
557 my $pathname = $pkg;
558 $pathname =~ s/::/\\W/;
559  
560 foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
561 delete $INC{$inc};
562 }
563  
564 my $rv = do {
565 if ($args{force}) {
566 CPAN::Shell->force( install => $pkg )
567 } elsif ($args{notest}) {
568 CPAN::Shell->notest( install => $pkg )
569 } else {
570 CPAN::Shell->install($pkg)
571 }
572 };
573  
574 $rv ||= eval {
575 $CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, )
576 ->{install}
577 if $CPAN::META;
578 };
579  
580 if ( $rv eq 'YES' ) {
581 print "*** $pkg successfully installed.\n";
582 $success = 1;
583 }
584 else {
585 print "*** $pkg installation failed.\n";
586 $success = 0;
587 }
588  
589 $installed += $success;
590 }
591 else {
592 print << ".";
593 *** Could not find a version $ver or above for $pkg; skipping.
594 .
595 }
596  
597 MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
598 }
599  
600 return $installed;
601 }
602  
603 sub _has_cpanplus {
604 return (
605 $HasCPANPLUS = (
606 $INC{'CPANPLUS/Config.pm'}
607 or _load('CPANPLUS::Shell::Default')
608 )
609 );
610 }
611  
612 # make guesses on whether we're under the CPAN installation directory
613 sub _under_cpan {
614 require Cwd;
615 require File::Spec;
616  
617 my $cwd = File::Spec->canonpath( Cwd::getcwd() );
618 my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} );
619  
620 return ( index( $cwd, $cpan ) > -1 );
621 }
622  
623 sub _update_to {
624 my $class = __PACKAGE__;
625 my $ver = shift;
626  
627 return
628 if _version_cmp( _version_of($class), $ver ) >= 0; # no need to upgrade
629  
630 if (
631 _prompt( "==> A newer version of $class ($ver) is required. Install?",
632 'y' ) =~ /^[Nn]/
633 )
634 {
635 die "*** Please install $class $ver manually.\n";
636 }
637  
638 print << ".";
639 *** Trying to fetch it from CPAN...
640 .
641  
642 # install ourselves
643 _load($class) and return $class->import(@_)
644 if $class->install( [], $class, $ver );
645  
646 print << '.'; exit 1;
647  
648 *** Cannot bootstrap myself. :-( Installation terminated.
649 .
650 }
651  
652 # check if we're connected to some host, using inet_aton
653 sub _connected_to {
654 my $site = shift;
655  
656 return (
657 ( _load('Socket') and Socket::inet_aton($site) ) or _prompt(
658 qq(
659 *** Your host cannot resolve the domain name '$site', which
660 probably means the Internet connections are unavailable.
661 ==> Should we try to install the required module(s) anyway?), 'n'
662 ) =~ /^[Yy]/
663 );
664 }
665  
666 # check if a directory is writable; may create it on demand
667 sub _can_write {
668 my $path = shift;
669 mkdir( $path, 0755 ) unless -e $path;
670  
671 return 1 if -w $path;
672  
673 print << ".";
674 *** You are not allowed to write to the directory '$path';
675 the installation may fail due to insufficient permissions.
676 .
677  
678 if (
679 eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt(
680 qq(
681 ==> Should we try to re-execute the autoinstall process with 'sudo'?),
682 ((-t STDIN) ? 'y' : 'n')
683 ) =~ /^[Yy]/
684 )
685 {
686  
687 # try to bootstrap ourselves from sudo
688 print << ".";
689 *** Trying to re-execute the autoinstall process with 'sudo'...
690 .
691 my $missing = join( ',', @Missing );
692 my $config = join( ',',
693 UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
694 if $Config;
695  
696 return
697 unless system( 'sudo', $^X, $0, "--config=$config",
698 "--installdeps=$missing" );
699  
700 print << ".";
701 *** The 'sudo' command exited with error! Resuming...
702 .
703 }
704  
705 return _prompt(
706 qq(
707 ==> Should we try to install the required module(s) anyway?), 'n'
708 ) =~ /^[Yy]/;
709 }
710  
711 # load a module and return the version it reports
712 sub _load {
713 my $mod = pop; # method/function doesn't matter
714 my $file = $mod;
715 $file =~ s|::|/|g;
716 $file .= '.pm';
717 local $@;
718 return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 );
719 }
720  
721 # report version without loading a module
722 sub _version_of {
723 my $mod = pop; # method/function doesn't matter
724 my $file = $mod;
725 $file =~ s|::|/|g;
726 $file .= '.pm';
727 foreach my $dir ( @INC ) {
728 next if ref $dir;
729 my $path = File::Spec->catfile($dir, $file);
730 next unless -e $path;
731 require ExtUtils::MM_Unix;
732 return ExtUtils::MM_Unix->parse_version($path);
733 }
734 return undef;
735 }
736  
737 # Load CPAN.pm and it's configuration
738 sub _load_cpan {
739 return if $CPAN::VERSION and $CPAN::Config and not @_;
740 require CPAN;
741  
742 # CPAN-1.82+ adds CPAN::Config::AUTOLOAD to redirect to
743 # CPAN::HandleConfig->load. CPAN reports that the redirection
744 # is deprecated in a warning printed at the user.
745  
746 # CPAN-1.81 expects CPAN::HandleConfig->load, does not have
747 # $CPAN::HandleConfig::VERSION but cannot handle
748 # CPAN::Config->load
749  
750 # Which "versions expect CPAN::Config->load?
751  
752 if ( $CPAN::HandleConfig::VERSION
753 || CPAN::HandleConfig->can('load')
754 ) {
755 # Newer versions of CPAN have a HandleConfig module
756 CPAN::HandleConfig->load;
757 } else {
758 # Older versions had the load method in Config directly
759 CPAN::Config->load;
760 }
761 }
762  
763 # compare two versions, either use Sort::Versions or plain comparison
764 # return values same as <=>
765 sub _version_cmp {
766 my ( $cur, $min ) = @_;
767 return -1 unless defined $cur; # if 0 keep comparing
768 return 1 unless $min;
769  
770 $cur =~ s/\s+$//;
771  
772 # check for version numbers that are not in decimal format
773 if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) {
774 if ( ( $version::VERSION or defined( _load('version') )) and
775 version->can('new')
776 ) {
777  
778 # use version.pm if it is installed.
779 return version->new($cur) <=> version->new($min);
780 }
781 elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) )
782 {
783  
784 # use Sort::Versions as the sorting algorithm for a.b.c versions
785 return Sort::Versions::versioncmp( $cur, $min );
786 }
787  
788 warn "Cannot reliably compare non-decimal formatted versions.\n"
789 . "Please install version.pm or Sort::Versions.\n";
790 }
791  
792 # plain comparison
793 local $^W = 0; # shuts off 'not numeric' bugs
794 return $cur <=> $min;
795 }
796  
797 # nothing; this usage is deprecated.
798 sub main::PREREQ_PM { return {}; }
799  
800 sub _make_args {
801 my %args = @_;
802  
803 $args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing }
804 if $UnderCPAN or $TestOnly;
805  
806 if ( $args{EXE_FILES} and -e 'MANIFEST' ) {
807 require ExtUtils::Manifest;
808 my $manifest = ExtUtils::Manifest::maniread('MANIFEST');
809  
810 $args{EXE_FILES} =
811 [ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ];
812 }
813  
814 $args{test}{TESTS} ||= 't/*.t';
815 $args{test}{TESTS} = join( ' ',
816 grep { !exists( $DisabledTests{$_} ) }
817 map { glob($_) } split( /\s+/, $args{test}{TESTS} ) );
818  
819 my $missing = join( ',', @Missing );
820 my $config =
821 join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
822 if $Config;
823  
824 $PostambleActions = (
825 ($missing and not $UnderCPAN)
826 ? "\$(PERL) $0 --config=$config --installdeps=$missing"
827 : "\$(NOECHO) \$(NOOP)"
828 );
829  
830 my $deps_list = join( ',', @Missing, @Existing );
831  
832 $PostambleActionsUpgradeDeps =
833 "\$(PERL) $0 --config=$config --upgradedeps=$deps_list";
834  
835 my $config_notest =
836 join( ',', (UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config}),
837 'notest', 1 )
838 if $Config;
839  
840 $PostambleActionsNoTest = (
841 ($missing and not $UnderCPAN)
842 ? "\$(PERL) $0 --config=$config_notest --installdeps=$missing"
843 : "\$(NOECHO) \$(NOOP)"
844 );
845  
846 $PostambleActionsUpgradeDepsNoTest =
847 "\$(PERL) $0 --config=$config_notest --upgradedeps=$deps_list";
848  
849 $PostambleActionsListDeps =
850 '@$(PERL) -le "print for @ARGV" '
851 . join(' ', map $Missing[$_], grep $_ % 2 == 0, 0..$#Missing);
852  
853 my @all = (@Missing, @Existing);
854  
855 $PostambleActionsListAllDeps =
856 '@$(PERL) -le "print for @ARGV" '
857 . join(' ', map $all[$_], grep $_ % 2 == 0, 0..$#all);
858  
859 return %args;
860 }
861  
862 # a wrapper to ExtUtils::MakeMaker::WriteMakefile
863 sub Write {
864 require Carp;
865 Carp::croak "WriteMakefile: Need even number of args" if @_ % 2;
866  
867 if ($CheckOnly) {
868 print << ".";
869 *** Makefile not written in check-only mode.
870 .
871 return;
872 }
873  
874 my %args = _make_args(@_);
875  
876 no strict 'refs';
877  
878 $PostambleUsed = 0;
879 local *MY::postamble = \&postamble unless defined &MY::postamble;
880 ExtUtils::MakeMaker::WriteMakefile(%args);
881  
882 print << "." unless $PostambleUsed;
883 *** WARNING: Makefile written with customized MY::postamble() without
884 including contents from Module::AutoInstall::postamble() --
885 auto installation features disabled. Please contact the author.
886 .
887  
888 return 1;
889 }
890  
891 sub postamble {
892 $PostambleUsed = 1;
893 my $fragment;
894  
895 $fragment .= <<"AUTO_INSTALL" if !$InstallDepsTarget;
896  
897 config :: installdeps
898 \t\$(NOECHO) \$(NOOP)
899 AUTO_INSTALL
900  
901 $fragment .= <<"END_MAKE";
902  
903 checkdeps ::
904 \t\$(PERL) $0 --checkdeps
905  
906 installdeps ::
907 \t$PostambleActions
908  
909 installdeps_notest ::
910 \t$PostambleActionsNoTest
911  
912 upgradedeps ::
913 \t$PostambleActionsUpgradeDeps
914  
915 upgradedeps_notest ::
916 \t$PostambleActionsUpgradeDepsNoTest
917  
918 listdeps ::
919 \t$PostambleActionsListDeps
920  
921 listalldeps ::
922 \t$PostambleActionsListAllDeps
923  
924 END_MAKE
925  
926 return $fragment;
927 }
928  
929 1;
930  
931 __END__