1 #line 1 "inc/Module/AutoInstall.pm - /Library/Perl/5.8.6/Module/AutoInstall.pm"
2 package Module::AutoInstall;
3 $Module::AutoInstall::VERSION = '1.01';
7 use ExtUtils::MakeMaker ();
11 # special map on pre-defined feature sets
13 '' => 'Core Features', # XXX: deprecated
14 '-core' => 'Core Features',
17 # various lexical flags
18 my ( @Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS );
19 my ( $Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly );
20 my ( $PostambleActions, $PostambleUsed );
22 _accept_default( !-t STDIN ); # see if it's a non-interactive session
26 $AcceptDefault = shift;
37 ? ( UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
44 # initialize various flags, and/or perform install
50 $ENV{PERL_AUTOINSTALL} || $ENV{PERL_EXTUTILS_AUTOINSTALL} || ''
54 if ( $arg =~ /^--config=(.*)$/ ) {
55 $Config = [ split( ',', $1 ) ];
57 elsif ( $arg =~ /^--installdeps=(.*)$/ ) {
58 __PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) );
61 elsif ( $arg =~ /^--default(?:deps)?$/ ) {
64 elsif ( $arg =~ /^--check(?:deps)?$/ ) {
67 elsif ( $arg =~ /^--skip(?:deps)?$/ ) {
70 elsif ( $arg =~ /^--test(?:only)?$/ ) {
76 # overrides MakeMaker's prompt() to automatically accept the default choice
78 goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault;
80 my ( $prompt, $default ) = @_;
81 my $y = ( $default =~ /^[Yy]/ );
83 print $prompt, ' [', ( $y ? 'Y' : 'y' ), '/', ( $y ? 'n' : 'N' ), '] ';
91 my @args = @_ or return;
94 print "*** $class version " . $class->VERSION . "\n";
95 print "*** Checking for dependencies...\n";
103 sort { length($b) <=> length($a) }
107 ? ( ( ref($_) eq 'HASH' ) ? keys(%$_) : @{$_} )
110 map { +{@args}->{$_} }
111 grep { /^[^\-]/ or /^-core$/i } keys %{ +{@args} }
115 while ( my ( $feature, $modules ) = splice( @args, 0, 2 ) ) {
116 my ( @required, @tests, @skiptests );
120 if ( $feature =~ m/^-(\w+)$/ ) {
123 # check for a newer version of myself
124 _update_to( $modules, @_ ) and return if $option eq 'version';
126 # sets CPAN configuration options
127 $Config = $modules if $option eq 'config';
129 # promote every features to core status
130 $core_all = ( $modules =~ /^all$/i ) and next
131 if $option eq 'core';
133 next unless $option eq 'core';
136 print "[" . ( $FeatureMap{ lc($feature) } || $feature ) . "]\n";
138 $modules = [ %{$modules} ] if UNIVERSAL::isa( $modules, 'HASH' );
140 unshift @$modules, -default => &{ shift(@$modules) }
141 if ( ref( $modules->[0] ) eq 'CODE' ); # XXX: bugward combatability
143 while ( my ( $mod, $arg ) = splice( @$modules, 0, 2 ) ) {
144 if ( $mod =~ m/^-(\w+)$/ ) {
147 $default = $arg if ( $option eq 'default' );
148 $conflict = $arg if ( $option eq 'conflict' );
149 @tests = @{$arg} if ( $option eq 'tests' );
150 @skiptests = @{$arg} if ( $option eq 'skiptests' );
155 printf( "- %-${maxlen}s ...", $mod );
157 if ( $arg and $arg =~ /^\D/ ) {
158 unshift @$modules, $arg;
162 # XXX: check for conflicts and uninstalls(!) them.
164 defined( my $cur = _version_check( _load($mod), $arg ||= 0 ) ) )
166 print "loaded. ($cur" . ( $arg ? " >= $arg" : '' ) . ")\n";
167 push @Existing, $mod => $arg;
168 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
171 print "missing." . ( $arg ? " (would need $arg)" : '' ) . "\n";
172 push @required, $mod => $arg;
176 next unless @required;
178 my $mandatory = ( $feature eq '-core' or $core_all );
185 qq{==> Auto-install the }
187 . ( $mandatory ? ' mandatory' : ' optional' )
188 . qq{ module(s) from CPAN?},
189 $default ? 'y' : 'n',
194 push( @Missing, @required );
195 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
198 elsif ( !$SkipInstall
202 _prompt( qq{==> The module(s) are mandatory! Really skip?}, 'n', )
205 push( @Missing, @required );
206 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
210 $DisabledTests{$_} = 1 for map { glob($_) } @tests;
214 _check_lock(); # check for $UnderCPAN
216 if ( @Missing and not( $CheckOnly or $UnderCPAN ) ) {
219 "*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n";
221 # make an educated guess of whether we'll need root permission.
222 print " (You may need to do that as the 'root' user.)\n"
225 print "*** $class configuration finished.\n";
231 *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main';
234 # CPAN.pm is non-reentrant, so check if we're under it and have no CPANPLUS
236 return unless @Missing;
239 # Find the CPAN lock-file
240 my $lock = MM->catfile( $CPAN::Config->{cpan_home}, ".lock" );
241 return unless -f $lock;
245 return unless open(LOCK, $lock);
248 ( $^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid() )
249 and ( $CPAN::Config->{prerequisites_policy} || '' ) ne 'ignore'
251 print <<'END_MESSAGE';
253 *** Since we're running under CPAN, I'll just let it take care
254 of the dependency's installation later.
265 my $i; # used below to strip leading '-' from config keys
266 my @config = ( map { s/^-// if ++$i; $_ } @{ +shift } );
268 my ( @modules, @installed );
269 while ( my ( $pkg, $ver ) = splice( @_, 0, 2 ) ) {
271 # grep out those already installed
272 if ( defined( _version_check( _load($pkg), $ver ) ) ) {
273 push @installed, $pkg;
276 push @modules, $pkg, $ver;
280 return @installed unless @modules; # nothing to do
282 print "*** Installing dependencies...\n";
284 return unless _connected_to('cpan.org');
289 if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) {
290 while (<FAILED>) { chomp; $failed{$_}++ }
294 while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) {
295 push @newmod, ( $k => $v ) unless $failed{$k};
300 if ( ! $UnderCPAN and _has_cpanplus() ) {
301 _install_cpanplus( \@modules, \@config );
303 _install_cpan( \@modules, \@config );
306 print "*** $class installation finished.\n";
308 # see if we have successfully installed them
309 while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
310 if ( defined( _version_check( _load($pkg), $ver ) ) ) {
311 push @installed, $pkg;
313 elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) {
314 print FAILED "$pkg\n";
318 close FAILED if $args{do_once};
323 sub _install_cpanplus {
324 my @modules = @{ +shift };
325 my @config = _cpanplus_config( @{ +shift } );
328 require CPANPLUS::Backend;
329 my $cp = CPANPLUS::Backend->new;
330 my $conf = $cp->configure_object;
332 return unless $conf->can('conf') # 0.05x+ with "sudo" support
333 or _can_write($conf->_get_build('base')); # 0.04x
335 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
336 my $makeflags = $conf->get_conf('makeflags') || '';
337 if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) {
338 # 0.03+ uses a hashref here
339 $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST};
342 # 0.02 and below uses a scalar
343 $makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
344 if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
347 $conf->set_conf( makeflags => $makeflags );
348 $conf->set_conf( prereqs => 1 );
352 while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) {
353 $conf->set_conf( $key, $val );
356 my $modtree = $cp->module_tree;
357 while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
358 print "*** Installing $pkg...\n";
360 MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
363 my $obj = $modtree->{$pkg};
365 if ( $obj and defined( _version_check( $obj->{version}, $ver ) ) ) {
367 $pathname =~ s/::/\\W/;
369 foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
373 my $rv = $cp->install( modules => [ $obj->{module} ] );
375 if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) {
376 print "*** $pkg successfully installed.\n";
379 print "*** $pkg installation cancelled.\n";
383 $installed += $success;
386 *** Could not find a version $ver or above for $pkg; skipping.
390 MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
396 sub _cpanplus_config {
399 my ($key, $value) = (shift(), shift());
400 if ( $key eq 'prerequisites_policy' ) {
401 if ( $value eq 'follow' ) {
402 $value = CPANPLUS::Internals::Constants::PREREQ_INSTALL();
403 } elsif ( $value eq 'ask' ) {
404 $value = CPANPLUS::Internals::Constants::PREREQ_ASK();
405 } elsif ( $value eq 'ignore' ) {
406 $value = CPANPLUS::Internals::Constants::PREREQ_IGNORE();
408 die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n";
411 die "*** Cannot convert option $key to CPANPLUS version.\n";
418 my @modules = @{ +shift };
419 my @config = @{ +shift };
426 if (CPAN->VERSION < 1.80) {
427 # no "sudo" support, probe for writableness
428 return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) )
429 and _can_write( $Config::Config{sitelib} );
432 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
433 my $makeflags = $CPAN::Config->{make_install_arg} || '';
434 $CPAN::Config->{make_install_arg} =
435 join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
436 if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
438 # don't show start-up info
439 $CPAN::Config->{inhibit_startup_message} = 1;
441 # set additional options
442 while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) {
443 ( $args{$opt} = $arg, next )
444 if $opt =~ /^force$/; # pseudo-option
445 $CPAN::Config->{$opt} = $arg;
448 local $CPAN::Config->{prerequisites_policy} = 'follow';
450 while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
451 MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
453 print "*** Installing $pkg...\n";
455 my $obj = CPAN::Shell->expand( Module => $pkg );
458 if ( $obj and defined( _version_check( $obj->cpan_version, $ver ) ) ) {
460 $pathname =~ s/::/\\W/;
462 foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
466 $obj->force('install') if $args{force};
468 my $rv = $obj->install || eval {
469 $CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, )
474 if ( $rv eq 'YES' ) {
475 print "*** $pkg successfully installed.\n";
479 print "*** $pkg installation failed.\n";
483 $installed += $success;
487 *** Could not find a version $ver or above for $pkg; skipping.
491 MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
500 $INC{'CPANPLUS/Config.pm'}
501 or _load('CPANPLUS::Shell::Default')
506 # make guesses on whether we're under the CPAN installation directory
511 my $cwd = File::Spec->canonpath( Cwd::cwd() );
512 my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} );
514 return ( index( $cwd, $cpan ) > -1 );
518 my $class = __PACKAGE__;
522 if defined( _version_check( _load($class), $ver ) ); # no need to upgrade
525 _prompt( "==> A newer version of $class ($ver) is required. Install?",
529 die "*** Please install $class $ver manually.\n";
533 *** Trying to fetch it from CPAN...
537 _load($class) and return $class->import(@_)
538 if $class->install( [], $class, $ver );
540 print << '.'; exit 1;
542 *** Cannot bootstrap myself. :-( Installation terminated.
546 # check if we're connected to some host, using inet_aton
551 ( _load('Socket') and Socket::inet_aton($site) ) or _prompt(
553 *** Your host cannot resolve the domain name '$site', which
554 probably means the Internet connections are unavailable.
555 ==> Should we try to install the required module(s) anyway?), 'n'
560 # check if a directory is writable; may create it on demand
563 mkdir( $path, 0755 ) unless -e $path;
565 return 1 if -w $path;
568 *** You are not allowed to write to the directory '$path';
569 the installation may fail due to insufficient permissions.
573 eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt(
575 ==> Should we try to re-execute the autoinstall process with 'sudo'?),
576 ((-t STDIN) ? 'y' : 'n')
581 # try to bootstrap ourselves from sudo
583 *** Trying to re-execute the autoinstall process with 'sudo'...
585 my $missing = join( ',', @Missing );
586 my $config = join( ',',
587 UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
591 unless system( 'sudo', $^X, $0, "--config=$config",
592 "--installdeps=$missing" );
595 *** The 'sudo' command exited with error! Resuming...
601 ==> Should we try to install the required module(s) anyway?), 'n'
605 # load a module and return the version it reports
607 my $mod = pop; # class/instance doesn't matter
614 return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 );
617 # Load CPAN.pm and it's configuration
619 return if $CPAN::VERSION;
621 if ( $CPAN::HandleConfig::VERSION ) {
622 # Newer versions of CPAN have a HandleConfig module
623 CPAN::HandleConfig->load;
625 # Older versions had the load method in Config directly
630 # compare two versions, either use Sort::Versions or plain comparison
632 my ( $cur, $min ) = @_;
633 return unless defined $cur;
637 # check for version numbers that are not in decimal format
638 if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) {
639 if ( $version::VERSION or defined( _load('version') ) ) {
641 # use version.pm if it is installed.
643 ( version->new($cur) >= version->new($min) ) ? $cur : undef );
645 elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) )
648 # use Sort::Versions as the sorting algorithm for a.b.c versions
649 return ( ( Sort::Versions::versioncmp( $cur, $min ) != -1 )
654 warn "Cannot reliably compare non-decimal formatted versions.\n"
655 . "Please install version.pm or Sort::Versions.\n";
659 local $^W = 0; # shuts off 'not numeric' bugs
660 return ( $cur >= $min ? $cur : undef );
663 # nothing; this usage is deprecated.
664 sub main::PREREQ_PM { return {}; }
669 $args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing }
670 if $UnderCPAN or $TestOnly;
672 if ( $args{EXE_FILES} and -e 'MANIFEST' ) {
673 require ExtUtils::Manifest;
674 my $manifest = ExtUtils::Manifest::maniread('MANIFEST');
677 [ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ];
680 $args{test}{TESTS} ||= 't/*.t';
681 $args{test}{TESTS} = join( ' ',
682 grep { !exists( $DisabledTests{$_} ) }
683 map { glob($_) } split( /\s+/, $args{test}{TESTS} ) );
685 my $missing = join( ',', @Missing );
687 join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
690 $PostambleActions = (
692 ? "\$(PERL) $0 --config=$config --installdeps=$missing"
699 # a wrapper to ExtUtils::MakeMaker::WriteMakefile
702 Carp::croak "WriteMakefile: Need even number of args" if @_ % 2;
706 *** Makefile not written in check-only mode.
711 my %args = _make_args(@_);
716 local *MY::postamble = \&postamble unless defined &MY::postamble;
717 ExtUtils::MakeMaker::WriteMakefile(%args);
719 print << "." unless $PostambleUsed;
720 *** WARNING: Makefile written with customized MY::postamble() without
721 including contents from Module::AutoInstall::postamble() --
722 auto installation features disabled. Please contact the author.
733 config :: installdeps
737 \t\$(PERL) $0 --checkdeps