| 1 |
#line 1 |
| 2 |
package Module::AutoInstall; |
| 3 |
|
| 4 |
use strict; |
| 5 |
use Cwd (); |
| 6 |
use ExtUtils::MakeMaker (); |
| 7 |
|
| 8 |
use vars qw{$VERSION}; |
| 9 |
BEGIN { |
| 10 |
$VERSION = '1.03'; |
| 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::cwd(); |
| 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 combatability |
| 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 = _load($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 ); |
| 348 |
while ( my ( $pkg, $ver ) = splice( @_, 0, 2 ) ) { |
| 349 |
|
| 350 |
# grep out those already installed |
| 351 |
if ( _version_cmp( _load($pkg), $ver ) >= 0 ) { |
| 352 |
push @installed, $pkg; |
| 353 |
} |
| 354 |
else { |
| 355 |
push @modules, $pkg, $ver; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if ($UpgradeDeps) { |
| 360 |
push @modules, @installed; |
| 361 |
@installed = (); |
| 362 |
} |
| 363 |
|
| 364 |
return @installed unless @modules; # nothing to do |
| 365 |
return @installed if _check_lock(); # defer to the CPAN shell |
| 366 |
|
| 367 |
print "*** Installing dependencies...\n"; |
| 368 |
|
| 369 |
return unless _connected_to('cpan.org'); |
| 370 |
|
| 371 |
my %args = @config; |
| 372 |
my %failed; |
| 373 |
local *FAILED; |
| 374 |
if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) { |
| 375 |
while (<FAILED>) { chomp; $failed{$_}++ } |
| 376 |
close FAILED; |
| 377 |
|
| 378 |
my @newmod; |
| 379 |
while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) { |
| 380 |
push @newmod, ( $k => $v ) unless $failed{$k}; |
| 381 |
} |
| 382 |
@modules = @newmod; |
| 383 |
} |
| 384 |
|
| 385 |
if ( _has_cpanplus() and not $ENV{PERL_AUTOINSTALL_PREFER_CPAN} ) { |
| 386 |
_install_cpanplus( \@modules, \@config ); |
| 387 |
} else { |
| 388 |
_install_cpan( \@modules, \@config ); |
| 389 |
} |
| 390 |
|
| 391 |
print "*** $class installation finished.\n"; |
| 392 |
|
| 393 |
# see if we have successfully installed them |
| 394 |
while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { |
| 395 |
if ( _version_cmp( _load($pkg), $ver ) >= 0 ) { |
| 396 |
push @installed, $pkg; |
| 397 |
} |
| 398 |
elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) { |
| 399 |
print FAILED "$pkg\n"; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
close FAILED if $args{do_once}; |
| 404 |
|
| 405 |
return @installed; |
| 406 |
} |
| 407 |
|
| 408 |
sub _install_cpanplus { |
| 409 |
my @modules = @{ +shift }; |
| 410 |
my @config = _cpanplus_config( @{ +shift } ); |
| 411 |
my $installed = 0; |
| 412 |
|
| 413 |
require CPANPLUS::Backend; |
| 414 |
my $cp = CPANPLUS::Backend->new; |
| 415 |
my $conf = $cp->configure_object; |
| 416 |
|
| 417 |
return unless $conf->can('conf') # 0.05x+ with "sudo" support |
| 418 |
or _can_write($conf->_get_build('base')); # 0.04x |
| 419 |
|
| 420 |
# if we're root, set UNINST=1 to avoid trouble unless user asked for it. |
| 421 |
my $makeflags = $conf->get_conf('makeflags') || ''; |
| 422 |
if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) { |
| 423 |
# 0.03+ uses a hashref here |
| 424 |
$makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST}; |
| 425 |
|
| 426 |
} else { |
| 427 |
# 0.02 and below uses a scalar |
| 428 |
$makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' ) |
| 429 |
if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } ); |
| 430 |
|
| 431 |
} |
| 432 |
$conf->set_conf( makeflags => $makeflags ); |
| 433 |
$conf->set_conf( prereqs => 1 ); |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) { |
| 438 |
$conf->set_conf( $key, $val ); |
| 439 |
} |
| 440 |
|
| 441 |
my $modtree = $cp->module_tree; |
| 442 |
while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { |
| 443 |
print "*** Installing $pkg...\n"; |
| 444 |
|
| 445 |
MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall; |
| 446 |
|
| 447 |
my $success; |
| 448 |
my $obj = $modtree->{$pkg}; |
| 449 |
|
| 450 |
if ( $obj and _version_cmp( $obj->{version}, $ver ) >= 0 ) { |
| 451 |
my $pathname = $pkg; |
| 452 |
$pathname =~ s/::/\\W/; |
| 453 |
|
| 454 |
foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) { |
| 455 |
delete $INC{$inc}; |
| 456 |
} |
| 457 |
|
| 458 |
my $rv = $cp->install( modules => [ $obj->{module} ] ); |
| 459 |
|
| 460 |
if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) { |
| 461 |
print "*** $pkg successfully installed.\n"; |
| 462 |
$success = 1; |
| 463 |
} else { |
| 464 |
print "*** $pkg installation cancelled.\n"; |
| 465 |
$success = 0; |
| 466 |
} |
| 467 |
|
| 468 |
$installed += $success; |
| 469 |
} else { |
| 470 |
print << "."; |
| 471 |
*** Could not find a version $ver or above for $pkg; skipping. |
| 472 |
. |
| 473 |
} |
| 474 |
|
| 475 |
MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall; |
| 476 |
} |
| 477 |
|
| 478 |
return $installed; |
| 479 |
} |
| 480 |
|
| 481 |
sub _cpanplus_config { |
| 482 |
my @config = (); |
| 483 |
while ( @_ ) { |
| 484 |
my ($key, $value) = (shift(), shift()); |
| 485 |
if ( $key eq 'prerequisites_policy' ) { |
| 486 |
if ( $value eq 'follow' ) { |
| 487 |
$value = CPANPLUS::Internals::Constants::PREREQ_INSTALL(); |
| 488 |
} elsif ( $value eq 'ask' ) { |
| 489 |
$value = CPANPLUS::Internals::Constants::PREREQ_ASK(); |
| 490 |
} elsif ( $value eq 'ignore' ) { |
| 491 |
$value = CPANPLUS::Internals::Constants::PREREQ_IGNORE(); |
| 492 |
} else { |
| 493 |
die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n"; |
| 494 |
} |
| 495 |
push @config, 'prereqs', $value; |
| 496 |
} elsif ( $key eq 'force' ) { |
| 497 |
push @config, $key, $value; |
| 498 |
} elsif ( $key eq 'notest' ) { |
| 499 |
push @config, 'skiptest', $value; |
| 500 |
} else { |
| 501 |
die "*** Cannot convert option $key to CPANPLUS version.\n"; |
| 502 |
} |
| 503 |
} |
| 504 |
return @config; |
| 505 |
} |
| 506 |
|
| 507 |
sub _install_cpan { |
| 508 |
my @modules = @{ +shift }; |
| 509 |
my @config = @{ +shift }; |
| 510 |
my $installed = 0; |
| 511 |
my %args; |
| 512 |
|
| 513 |
_load_cpan(); |
| 514 |
require Config; |
| 515 |
|
| 516 |
if (CPAN->VERSION < 1.80) { |
| 517 |
# no "sudo" support, probe for writableness |
| 518 |
return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) ) |
| 519 |
and _can_write( $Config::Config{sitelib} ); |
| 520 |
} |
| 521 |
|
| 522 |
# if we're root, set UNINST=1 to avoid trouble unless user asked for it. |
| 523 |
my $makeflags = $CPAN::Config->{make_install_arg} || ''; |
| 524 |
$CPAN::Config->{make_install_arg} = |
| 525 |
join( ' ', split( ' ', $makeflags ), 'UNINST=1' ) |
| 526 |
if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } ); |
| 527 |
|
| 528 |
# don't show start-up info |
| 529 |
$CPAN::Config->{inhibit_startup_message} = 1; |
| 530 |
|
| 531 |
# set additional options |
| 532 |
while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) { |
| 533 |
( $args{$opt} = $arg, next ) |
| 534 |
if $opt =~ /^(?:force|notest)$/; # pseudo-option |
| 535 |
$CPAN::Config->{$opt} = $arg; |
| 536 |
} |
| 537 |
|
| 538 |
if ($args{notest} && (not CPAN::Shell->can('notest'))) { |
| 539 |
die "Your version of CPAN is too old to support the 'notest' pragma"; |
| 540 |
} |
| 541 |
|
| 542 |
local $CPAN::Config->{prerequisites_policy} = 'follow'; |
| 543 |
|
| 544 |
while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { |
| 545 |
MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall; |
| 546 |
|
| 547 |
print "*** Installing $pkg...\n"; |
| 548 |
|
| 549 |
my $obj = CPAN::Shell->expand( Module => $pkg ); |
| 550 |
my $success = 0; |
| 551 |
|
| 552 |
if ( $obj and _version_cmp( $obj->cpan_version, $ver ) >= 0 ) { |
| 553 |
my $pathname = $pkg; |
| 554 |
$pathname =~ s/::/\\W/; |
| 555 |
|
| 556 |
foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) { |
| 557 |
delete $INC{$inc}; |
| 558 |
} |
| 559 |
|
| 560 |
my $rv = do { |
| 561 |
if ($args{force}) { |
| 562 |
CPAN::Shell->force( install => $pkg ) |
| 563 |
} elsif ($args{notest}) { |
| 564 |
CPAN::Shell->notest( install => $pkg ) |
| 565 |
} else { |
| 566 |
CPAN::Shell->install($pkg) |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
$rv ||= eval { |
| 571 |
$CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, ) |
| 572 |
->{install} |
| 573 |
if $CPAN::META; |
| 574 |
}; |
| 575 |
|
| 576 |
if ( $rv eq 'YES' ) { |
| 577 |
print "*** $pkg successfully installed.\n"; |
| 578 |
$success = 1; |
| 579 |
} |
| 580 |
else { |
| 581 |
print "*** $pkg installation failed.\n"; |
| 582 |
$success = 0; |
| 583 |
} |
| 584 |
|
| 585 |
$installed += $success; |
| 586 |
} |
| 587 |
else { |
| 588 |
print << "."; |
| 589 |
*** Could not find a version $ver or above for $pkg; skipping. |
| 590 |
. |
| 591 |
} |
| 592 |
|
| 593 |
MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall; |
| 594 |
} |
| 595 |
|
| 596 |
return $installed; |
| 597 |
} |
| 598 |
|
| 599 |
sub _has_cpanplus { |
| 600 |
return ( |
| 601 |
$HasCPANPLUS = ( |
| 602 |
$INC{'CPANPLUS/Config.pm'} |
| 603 |
or _load('CPANPLUS::Shell::Default') |
| 604 |
) |
| 605 |
); |
| 606 |
} |
| 607 |
|
| 608 |
# make guesses on whether we're under the CPAN installation directory |
| 609 |
sub _under_cpan { |
| 610 |
require Cwd; |
| 611 |
require File::Spec; |
| 612 |
|
| 613 |
my $cwd = File::Spec->canonpath( Cwd::cwd() ); |
| 614 |
my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} ); |
| 615 |
|
| 616 |
return ( index( $cwd, $cpan ) > -1 ); |
| 617 |
} |
| 618 |
|
| 619 |
sub _update_to { |
| 620 |
my $class = __PACKAGE__; |
| 621 |
my $ver = shift; |
| 622 |
|
| 623 |
return |
| 624 |
if _version_cmp( _load($class), $ver ) >= 0; # no need to upgrade |
| 625 |
|
| 626 |
if ( |
| 627 |
_prompt( "==> A newer version of $class ($ver) is required. Install?", |
| 628 |
'y' ) =~ /^[Nn]/ |
| 629 |
) |
| 630 |
{ |
| 631 |
die "*** Please install $class $ver manually.\n"; |
| 632 |
} |
| 633 |
|
| 634 |
print << "."; |
| 635 |
*** Trying to fetch it from CPAN... |
| 636 |
. |
| 637 |
|
| 638 |
# install ourselves |
| 639 |
_load($class) and return $class->import(@_) |
| 640 |
if $class->install( [], $class, $ver ); |
| 641 |
|
| 642 |
print << '.'; exit 1; |
| 643 |
|
| 644 |
*** Cannot bootstrap myself. :-( Installation terminated. |
| 645 |
. |
| 646 |
} |
| 647 |
|
| 648 |
# check if we're connected to some host, using inet_aton |
| 649 |
sub _connected_to { |
| 650 |
my $site = shift; |
| 651 |
|
| 652 |
return ( |
| 653 |
( _load('Socket') and Socket::inet_aton($site) ) or _prompt( |
| 654 |
qq( |
| 655 |
*** Your host cannot resolve the domain name '$site', which |
| 656 |
probably means the Internet connections are unavailable. |
| 657 |
==> Should we try to install the required module(s) anyway?), 'n' |
| 658 |
) =~ /^[Yy]/ |
| 659 |
); |
| 660 |
} |
| 661 |
|
| 662 |
# check if a directory is writable; may create it on demand |
| 663 |
sub _can_write { |
| 664 |
my $path = shift; |
| 665 |
mkdir( $path, 0755 ) unless -e $path; |
| 666 |
|
| 667 |
return 1 if -w $path; |
| 668 |
|
| 669 |
print << "."; |
| 670 |
*** You are not allowed to write to the directory '$path'; |
| 671 |
the installation may fail due to insufficient permissions. |
| 672 |
. |
| 673 |
|
| 674 |
if ( |
| 675 |
eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt( |
| 676 |
qq( |
| 677 |
==> Should we try to re-execute the autoinstall process with 'sudo'?), |
| 678 |
((-t STDIN) ? 'y' : 'n') |
| 679 |
) =~ /^[Yy]/ |
| 680 |
) |
| 681 |
{ |
| 682 |
|
| 683 |
# try to bootstrap ourselves from sudo |
| 684 |
print << "."; |
| 685 |
*** Trying to re-execute the autoinstall process with 'sudo'... |
| 686 |
. |
| 687 |
my $missing = join( ',', @Missing ); |
| 688 |
my $config = join( ',', |
| 689 |
UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) |
| 690 |
if $Config; |
| 691 |
|
| 692 |
return |
| 693 |
unless system( 'sudo', $^X, $0, "--config=$config", |
| 694 |
"--installdeps=$missing" ); |
| 695 |
|
| 696 |
print << "."; |
| 697 |
*** The 'sudo' command exited with error! Resuming... |
| 698 |
. |
| 699 |
} |
| 700 |
|
| 701 |
return _prompt( |
| 702 |
qq( |
| 703 |
==> Should we try to install the required module(s) anyway?), 'n' |
| 704 |
) =~ /^[Yy]/; |
| 705 |
} |
| 706 |
|
| 707 |
# load a module and return the version it reports |
| 708 |
sub _load { |
| 709 |
my $mod = pop; # class/instance doesn't matter |
| 710 |
my $file = $mod; |
| 711 |
|
| 712 |
$file =~ s|::|/|g; |
| 713 |
$file .= '.pm'; |
| 714 |
|
| 715 |
local $@; |
| 716 |
return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 ); |
| 717 |
} |
| 718 |
|
| 719 |
# Load CPAN.pm and it's configuration |
| 720 |
sub _load_cpan { |
| 721 |
return if $CPAN::VERSION and $CPAN::Config and not @_; |
| 722 |
require CPAN; |
| 723 |
|
| 724 |
# CPAN-1.82+ adds CPAN::Config::AUTOLOAD to redirect to |
| 725 |
# CPAN::HandleConfig->load. CPAN reports that the redirection |
| 726 |
# is deprecated in a warning printed at the user. |
| 727 |
|
| 728 |
# CPAN-1.81 expects CPAN::HandleConfig->load, does not have |
| 729 |
# $CPAN::HandleConfig::VERSION but cannot handle |
| 730 |
# CPAN::Config->load |
| 731 |
|
| 732 |
# Which "versions expect CPAN::Config->load? |
| 733 |
|
| 734 |
if ( $CPAN::HandleConfig::VERSION |
| 735 |
|| CPAN::HandleConfig->can('load') |
| 736 |
) { |
| 737 |
# Newer versions of CPAN have a HandleConfig module |
| 738 |
CPAN::HandleConfig->load; |
| 739 |
} else { |
| 740 |
# Older versions had the load method in Config directly |
| 741 |
CPAN::Config->load; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
# compare two versions, either use Sort::Versions or plain comparison |
| 746 |
# return values same as <=> |
| 747 |
sub _version_cmp { |
| 748 |
my ( $cur, $min ) = @_; |
| 749 |
return -1 unless defined $cur; # if 0 keep comparing |
| 750 |
return 1 unless $min; |
| 751 |
|
| 752 |
$cur =~ s/\s+$//; |
| 753 |
|
| 754 |
# check for version numbers that are not in decimal format |
| 755 |
if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) { |
| 756 |
if ( ( $version::VERSION or defined( _load('version') )) and |
| 757 |
version->can('new') |
| 758 |
) { |
| 759 |
|
| 760 |
# use version.pm if it is installed. |
| 761 |
return version->new($cur) <=> version->new($min); |
| 762 |
} |
| 763 |
elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) ) |
| 764 |
{ |
| 765 |
|
| 766 |
# use Sort::Versions as the sorting algorithm for a.b.c versions |
| 767 |
return Sort::Versions::versioncmp( $cur, $min ); |
| 768 |
} |
| 769 |
|
| 770 |
warn "Cannot reliably compare non-decimal formatted versions.\n" |
| 771 |
. "Please install version.pm or Sort::Versions.\n"; |
| 772 |
} |
| 773 |
|
| 774 |
# plain comparison |
| 775 |
local $^W = 0; # shuts off 'not numeric' bugs |
| 776 |
return $cur <=> $min; |
| 777 |
} |
| 778 |
|
| 779 |
# nothing; this usage is deprecated. |
| 780 |
sub main::PREREQ_PM { return {}; } |
| 781 |
|
| 782 |
sub _make_args { |
| 783 |
my %args = @_; |
| 784 |
|
| 785 |
$args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing } |
| 786 |
if $UnderCPAN or $TestOnly; |
| 787 |
|
| 788 |
if ( $args{EXE_FILES} and -e 'MANIFEST' ) { |
| 789 |
require ExtUtils::Manifest; |
| 790 |
my $manifest = ExtUtils::Manifest::maniread('MANIFEST'); |
| 791 |
|
| 792 |
$args{EXE_FILES} = |
| 793 |
[ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ]; |
| 794 |
} |
| 795 |
|
| 796 |
$args{test}{TESTS} ||= 't/*.t'; |
| 797 |
$args{test}{TESTS} = join( ' ', |
| 798 |
grep { !exists( $DisabledTests{$_} ) } |
| 799 |
map { glob($_) } split( /\s+/, $args{test}{TESTS} ) ); |
| 800 |
|
| 801 |
my $missing = join( ',', @Missing ); |
| 802 |
my $config = |
| 803 |
join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) |
| 804 |
if $Config; |
| 805 |
|
| 806 |
$PostambleActions = ( |
| 807 |
($missing and not $UnderCPAN) |
| 808 |
? "\$(PERL) $0 --config=$config --installdeps=$missing" |
| 809 |
: "\$(NOECHO) \$(NOOP)" |
| 810 |
); |
| 811 |
|
| 812 |
my $deps_list = join( ',', @Missing, @Existing ); |
| 813 |
|
| 814 |
$PostambleActionsUpgradeDeps = |
| 815 |
"\$(PERL) $0 --config=$config --upgradedeps=$deps_list"; |
| 816 |
|
| 817 |
my $config_notest = |
| 818 |
join( ',', (UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config}), |
| 819 |
'notest', 1 ) |
| 820 |
if $Config; |
| 821 |
|
| 822 |
$PostambleActionsNoTest = ( |
| 823 |
($missing and not $UnderCPAN) |
| 824 |
? "\$(PERL) $0 --config=$config_notest --installdeps=$missing" |
| 825 |
: "\$(NOECHO) \$(NOOP)" |
| 826 |
); |
| 827 |
|
| 828 |
$PostambleActionsUpgradeDepsNoTest = |
| 829 |
"\$(PERL) $0 --config=$config_notest --upgradedeps=$deps_list"; |
| 830 |
|
| 831 |
$PostambleActionsListDeps = |
| 832 |
'@$(PERL) -le "print for @ARGV" ' |
| 833 |
. join(' ', map $Missing[$_], grep $_ % 2 == 0, 0..$#Missing); |
| 834 |
|
| 835 |
my @all = (@Missing, @Existing); |
| 836 |
|
| 837 |
$PostambleActionsListAllDeps = |
| 838 |
'@$(PERL) -le "print for @ARGV" ' |
| 839 |
. join(' ', map $all[$_], grep $_ % 2 == 0, 0..$#all); |
| 840 |
|
| 841 |
return %args; |
| 842 |
} |
| 843 |
|
| 844 |
# a wrapper to ExtUtils::MakeMaker::WriteMakefile |
| 845 |
sub Write { |
| 846 |
require Carp; |
| 847 |
Carp::croak "WriteMakefile: Need even number of args" if @_ % 2; |
| 848 |
|
| 849 |
if ($CheckOnly) { |
| 850 |
print << "."; |
| 851 |
*** Makefile not written in check-only mode. |
| 852 |
. |
| 853 |
return; |
| 854 |
} |
| 855 |
|
| 856 |
my %args = _make_args(@_); |
| 857 |
|
| 858 |
no strict 'refs'; |
| 859 |
|
| 860 |
$PostambleUsed = 0; |
| 861 |
local *MY::postamble = \&postamble unless defined &MY::postamble; |
| 862 |
ExtUtils::MakeMaker::WriteMakefile(%args); |
| 863 |
|
| 864 |
print << "." unless $PostambleUsed; |
| 865 |
*** WARNING: Makefile written with customized MY::postamble() without |
| 866 |
including contents from Module::AutoInstall::postamble() -- |
| 867 |
auto installation features disabled. Please contact the author. |
| 868 |
. |
| 869 |
|
| 870 |
return 1; |
| 871 |
} |
| 872 |
|
| 873 |
sub postamble { |
| 874 |
$PostambleUsed = 1; |
| 875 |
my $fragment; |
| 876 |
|
| 877 |
$fragment .= <<"AUTO_INSTALL" if !$InstallDepsTarget; |
| 878 |
|
| 879 |
config :: installdeps |
| 880 |
\t\$(NOECHO) \$(NOOP) |
| 881 |
AUTO_INSTALL |
| 882 |
|
| 883 |
$fragment .= <<"END_MAKE"; |
| 884 |
|
| 885 |
checkdeps :: |
| 886 |
\t\$(PERL) $0 --checkdeps |
| 887 |
|
| 888 |
installdeps :: |
| 889 |
\t$PostambleActions |
| 890 |
|
| 891 |
installdeps_notest :: |
| 892 |
\t$PostambleActionsNoTest |
| 893 |
|
| 894 |
upgradedeps :: |
| 895 |
\t$PostambleActionsUpgradeDeps |
| 896 |
|
| 897 |
upgradedeps_notest :: |
| 898 |
\t$PostambleActionsUpgradeDepsNoTest |
| 899 |
|
| 900 |
listdeps :: |
| 901 |
\t$PostambleActionsListDeps |
| 902 |
|
| 903 |
listalldeps :: |
| 904 |
\t$PostambleActionsListAllDeps |
| 905 |
|
| 906 |
END_MAKE |
| 907 |
|
| 908 |
return $fragment; |
| 909 |
} |
| 910 |
|
| 911 |
1; |
| 912 |
|
| 913 |
__END__ |
| 914 |
|
| 915 |
#line 1178 |