2
package Module::AutoInstall;
6
use ExtUtils::MakeMaker ();
13
# special map on pre-defined feature sets
15
'' => 'Core Features', # XXX: deprecated
16
'-core' => 'Core Features',
19
# various lexical flags
20
my ( @Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS );
21
my ( $Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly );
22
my ( $PostambleActions, $PostambleUsed );
24
# See if it's a testing or non-interactive session
25
_accept_default( $ENV{AUTOMATED_TESTING} or ! -t STDIN );
29
$AcceptDefault = shift;
40
? ( UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
47
# initialize various flags, and/or perform install
53
$ENV{PERL_AUTOINSTALL} || $ENV{PERL_EXTUTILS_AUTOINSTALL} || ''
57
if ( $arg =~ /^--config=(.*)$/ ) {
58
$Config = [ split( ',', $1 ) ];
60
elsif ( $arg =~ /^--installdeps=(.*)$/ ) {
61
__PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) );
64
elsif ( $arg =~ /^--default(?:deps)?$/ ) {
67
elsif ( $arg =~ /^--check(?:deps)?$/ ) {
70
elsif ( $arg =~ /^--skip(?:deps)?$/ ) {
73
elsif ( $arg =~ /^--test(?:only)?$/ ) {
79
# overrides MakeMaker's prompt() to automatically accept the default choice
81
goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault;
83
my ( $prompt, $default ) = @_;
84
my $y = ( $default =~ /^[Yy]/ );
86
print $prompt, ' [', ( $y ? 'Y' : 'y' ), '/', ( $y ? 'n' : 'N' ), '] ';
94
my @args = @_ or return;
97
print "*** $class version " . $class->VERSION . "\n";
98
print "*** Checking for Perl dependencies...\n";
100
my $cwd = Cwd::cwd();
106
sort { length($b) <=> length($a) }
110
? ( ( ref($_) eq 'HASH' ) ? keys(%$_) : @{$_} )
113
map { +{@args}->{$_} }
114
grep { /^[^\-]/ or /^-core$/i } keys %{ +{@args} }
118
while ( my ( $feature, $modules ) = splice( @args, 0, 2 ) ) {
119
my ( @required, @tests, @skiptests );
123
if ( $feature =~ m/^-(\w+)$/ ) {
126
# check for a newer version of myself
127
_update_to( $modules, @_ ) and return if $option eq 'version';
129
# sets CPAN configuration options
130
$Config = $modules if $option eq 'config';
132
# promote every features to core status
133
$core_all = ( $modules =~ /^all$/i ) and next
134
if $option eq 'core';
136
next unless $option eq 'core';
139
print "[" . ( $FeatureMap{ lc($feature) } || $feature ) . "]\n";
141
$modules = [ %{$modules} ] if UNIVERSAL::isa( $modules, 'HASH' );
143
unshift @$modules, -default => &{ shift(@$modules) }
144
if ( ref( $modules->[0] ) eq 'CODE' ); # XXX: bugward combatability
146
while ( my ( $mod, $arg ) = splice( @$modules, 0, 2 ) ) {
147
if ( $mod =~ m/^-(\w+)$/ ) {
150
$default = $arg if ( $option eq 'default' );
151
$conflict = $arg if ( $option eq 'conflict' );
152
@tests = @{$arg} if ( $option eq 'tests' );
153
@skiptests = @{$arg} if ( $option eq 'skiptests' );
158
printf( "- %-${maxlen}s ...", $mod );
160
if ( $arg and $arg =~ /^\D/ ) {
161
unshift @$modules, $arg;
165
# XXX: check for conflicts and uninstalls(!) them.
167
defined( my $cur = _version_check( _load($mod), $arg ||= 0 ) ) )
169
print "loaded. ($cur" . ( $arg ? " >= $arg" : '' ) . ")\n";
170
push @Existing, $mod => $arg;
171
$DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
174
print "missing." . ( $arg ? " (would need $arg)" : '' ) . "\n";
175
push @required, $mod => $arg;
179
next unless @required;
181
my $mandatory = ( $feature eq '-core' or $core_all );
188
qq{==> Auto-install the }
190
. ( $mandatory ? ' mandatory' : ' optional' )
191
. qq{ module(s) from CPAN?},
192
$default ? 'y' : 'n',
197
push( @Missing, @required );
198
$DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
201
elsif ( !$SkipInstall
205
_prompt( qq{==> The module(s) are mandatory! Really skip?}, 'n', )
208
push( @Missing, @required );
209
$DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
213
$DisabledTests{$_} = 1 for map { glob($_) } @tests;
217
_check_lock(); # check for $UnderCPAN
219
if ( @Missing and not( $CheckOnly or $UnderCPAN ) ) {
222
"*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n";
224
# make an educated guess of whether we'll need root permission.
225
print " (You may need to do that as the 'root' user.)\n"
228
print "*** $class configuration finished.\n";
234
*{'main::WriteMakefile'} = \&Write if caller(0) eq 'main';
237
# CPAN.pm is non-reentrant, so check if we're under it and have no CPANPLUS
239
return unless @Missing;
242
# Find the CPAN lock-file
243
my $lock = MM->catfile( $CPAN::Config->{cpan_home}, ".lock" );
244
return unless -f $lock;
248
return unless open(LOCK, $lock);
251
( $^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid() )
252
and ( $CPAN::Config->{prerequisites_policy} || '' ) ne 'ignore'
254
print <<'END_MESSAGE';
256
*** Since we're running under CPAN, I'll just let it take care
257
of the dependency's installation later.
268
my $i; # used below to strip leading '-' from config keys
269
my @config = ( map { s/^-// if ++$i; $_ } @{ +shift } );
271
my ( @modules, @installed );
272
while ( my ( $pkg, $ver ) = splice( @_, 0, 2 ) ) {
274
# grep out those already installed
275
if ( defined( _version_check( _load($pkg), $ver ) ) ) {
276
push @installed, $pkg;
279
push @modules, $pkg, $ver;
283
return @installed unless @modules; # nothing to do
285
print "*** Installing dependencies...\n";
287
return unless _connected_to('cpan.org');
292
if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) {
293
while (<FAILED>) { chomp; $failed{$_}++ }
297
while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) {
298
push @newmod, ( $k => $v ) unless $failed{$k};
303
if ( ! $UnderCPAN and _has_cpanplus() ) {
304
_install_cpanplus( \@modules, \@config );
306
_install_cpan( \@modules, \@config );
309
print "*** $class installation finished.\n";
311
# see if we have successfully installed them
312
while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
313
if ( defined( _version_check( _load($pkg), $ver ) ) ) {
314
push @installed, $pkg;
316
elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) {
317
print FAILED "$pkg\n";
321
close FAILED if $args{do_once};
326
sub _install_cpanplus {
327
my @modules = @{ +shift };
328
my @config = _cpanplus_config( @{ +shift } );
331
require CPANPLUS::Backend;
332
my $cp = CPANPLUS::Backend->new;
333
my $conf = $cp->configure_object;
335
return unless $conf->can('conf') # 0.05x+ with "sudo" support
336
or _can_write($conf->_get_build('base')); # 0.04x
338
# if we're root, set UNINST=1 to avoid trouble unless user asked for it.
339
my $makeflags = $conf->get_conf('makeflags') || '';
340
if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) {
341
# 0.03+ uses a hashref here
342
$makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST};
345
# 0.02 and below uses a scalar
346
$makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
347
if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
350
$conf->set_conf( makeflags => $makeflags );
351
$conf->set_conf( prereqs => 1 );
355
while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) {
356
$conf->set_conf( $key, $val );
359
my $modtree = $cp->module_tree;
360
while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
361
print "*** Installing $pkg...\n";
363
MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
366
my $obj = $modtree->{$pkg};
368
if ( $obj and defined( _version_check( $obj->{version}, $ver ) ) ) {
370
$pathname =~ s/::/\\W/;
372
foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
376
my $rv = $cp->install( modules => [ $obj->{module} ] );
378
if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) {
379
print "*** $pkg successfully installed.\n";
382
print "*** $pkg installation cancelled.\n";
386
$installed += $success;
389
*** Could not find a version $ver or above for $pkg; skipping.
393
MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
399
sub _cpanplus_config {
402
my ($key, $value) = (shift(), shift());
403
if ( $key eq 'prerequisites_policy' ) {
404
if ( $value eq 'follow' ) {
405
$value = CPANPLUS::Internals::Constants::PREREQ_INSTALL();
406
} elsif ( $value eq 'ask' ) {
407
$value = CPANPLUS::Internals::Constants::PREREQ_ASK();
408
} elsif ( $value eq 'ignore' ) {
409
$value = CPANPLUS::Internals::Constants::PREREQ_IGNORE();
411
die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n";
414
die "*** Cannot convert option $key to CPANPLUS version.\n";
421
my @modules = @{ +shift };
422
my @config = @{ +shift };
429
if (CPAN->VERSION < 1.80) {
430
# no "sudo" support, probe for writableness
431
return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) )
432
and _can_write( $Config::Config{sitelib} );
435
# if we're root, set UNINST=1 to avoid trouble unless user asked for it.
436
my $makeflags = $CPAN::Config->{make_install_arg} || '';
437
$CPAN::Config->{make_install_arg} =
438
join( ' ', split( ' ', $makeflags ), 'UNINST=1' )
439
if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } );
441
# don't show start-up info
442
$CPAN::Config->{inhibit_startup_message} = 1;
444
# set additional options
445
while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) {
446
( $args{$opt} = $arg, next )
447
if $opt =~ /^force$/; # pseudo-option
448
$CPAN::Config->{$opt} = $arg;
451
local $CPAN::Config->{prerequisites_policy} = 'follow';
453
while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) {
454
MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall;
456
print "*** Installing $pkg...\n";
458
my $obj = CPAN::Shell->expand( Module => $pkg );
461
if ( $obj and defined( _version_check( $obj->cpan_version, $ver ) ) ) {
463
$pathname =~ s/::/\\W/;
465
foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) {
469
$obj->force('install') if $args{force};
471
my $rv = $obj->install || eval {
472
$CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, )
477
if ( $rv eq 'YES' ) {
478
print "*** $pkg successfully installed.\n";
482
print "*** $pkg installation failed.\n";
486
$installed += $success;
490
*** Could not find a version $ver or above for $pkg; skipping.
494
MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall;
503
$INC{'CPANPLUS/Config.pm'}
504
or _load('CPANPLUS::Shell::Default')
509
# make guesses on whether we're under the CPAN installation directory
514
my $cwd = File::Spec->canonpath( Cwd::cwd() );
515
my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} );
517
return ( index( $cwd, $cpan ) > -1 );
521
my $class = __PACKAGE__;
525
if defined( _version_check( _load($class), $ver ) ); # no need to upgrade
528
_prompt( "==> A newer version of $class ($ver) is required. Install?",
532
die "*** Please install $class $ver manually.\n";
536
*** Trying to fetch it from CPAN...
540
_load($class) and return $class->import(@_)
541
if $class->install( [], $class, $ver );
543
print << '.'; exit 1;
545
*** Cannot bootstrap myself. :-( Installation terminated.
549
# check if we're connected to some host, using inet_aton
554
( _load('Socket') and Socket::inet_aton($site) ) or _prompt(
556
*** Your host cannot resolve the domain name '$site', which
557
probably means the Internet connections are unavailable.
558
==> Should we try to install the required module(s) anyway?), 'n'
563
# check if a directory is writable; may create it on demand
566
mkdir( $path, 0755 ) unless -e $path;
568
return 1 if -w $path;
571
*** You are not allowed to write to the directory '$path';
572
the installation may fail due to insufficient permissions.
576
eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt(
578
==> Should we try to re-execute the autoinstall process with 'sudo'?),
579
((-t STDIN) ? 'y' : 'n')
584
# try to bootstrap ourselves from sudo
586
*** Trying to re-execute the autoinstall process with 'sudo'...
588
my $missing = join( ',', @Missing );
589
my $config = join( ',',
590
UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
594
unless system( 'sudo', $^X, $0, "--config=$config",
595
"--installdeps=$missing" );
598
*** The 'sudo' command exited with error! Resuming...
604
==> Should we try to install the required module(s) anyway?), 'n'
608
# load a module and return the version it reports
610
my $mod = pop; # class/instance doesn't matter
617
return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 );
620
# Load CPAN.pm and it's configuration
622
return if $CPAN::VERSION;
624
if ( $CPAN::HandleConfig::VERSION ) {
625
# Newer versions of CPAN have a HandleConfig module
626
CPAN::HandleConfig->load;
628
# Older versions had the load method in Config directly
633
# compare two versions, either use Sort::Versions or plain comparison
635
my ( $cur, $min ) = @_;
636
return unless defined $cur;
640
# check for version numbers that are not in decimal format
641
if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) {
642
if ( $version::VERSION or defined( _load('version') ) ) {
644
# use version.pm if it is installed.
646
( version->new($cur) >= version->new($min) ) ? $cur : undef );
648
elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) )
651
# use Sort::Versions as the sorting algorithm for a.b.c versions
652
return ( ( Sort::Versions::versioncmp( $cur, $min ) != -1 )
657
warn "Cannot reliably compare non-decimal formatted versions.\n"
658
. "Please install version.pm or Sort::Versions.\n";
662
local $^W = 0; # shuts off 'not numeric' bugs
663
return ( $cur >= $min ? $cur : undef );
666
# nothing; this usage is deprecated.
667
sub main::PREREQ_PM { return {}; }
672
$args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing }
673
if $UnderCPAN or $TestOnly;
675
if ( $args{EXE_FILES} and -e 'MANIFEST' ) {
676
require ExtUtils::Manifest;
677
my $manifest = ExtUtils::Manifest::maniread('MANIFEST');
680
[ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ];
683
$args{test}{TESTS} ||= 't/*.t';
684
$args{test}{TESTS} = join( ' ',
685
grep { !exists( $DisabledTests{$_} ) }
686
map { glob($_) } split( /\s+/, $args{test}{TESTS} ) );
688
my $missing = join( ',', @Missing );
690
join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} )
693
$PostambleActions = (
695
? "\$(PERL) $0 --config=$config --installdeps=$missing"
696
: "\$(NOECHO) \$(NOOP)"
702
# a wrapper to ExtUtils::MakeMaker::WriteMakefile
705
Carp::croak "WriteMakefile: Need even number of args" if @_ % 2;
709
*** Makefile not written in check-only mode.
714
my %args = _make_args(@_);
719
local *MY::postamble = \&postamble unless defined &MY::postamble;
720
ExtUtils::MakeMaker::WriteMakefile(%args);
722
print << "." unless $PostambleUsed;
723
*** WARNING: Makefile written with customized MY::postamble() without
724
including contents from Module::AutoInstall::postamble() --
725
auto installation features disabled. Please contact the author.
736
config :: installdeps
737
\t\$(NOECHO) \$(NOOP)
740
\t\$(PERL) $0 --checkdeps