1 #line 1 "inc/ExtUtils/AutoInstall.pm - /Library/Perl/5.8.1/ExtUtils/AutoInstall.pm"
2 # $File: //member/autrijus/ExtUtils-AutoInstall/lib/ExtUtils/AutoInstall.pm $
3 # $Revision: #9 $ $Change: 9532 $ $DateTime: 2004/01/01 06:47:30 $ vim: expandtab shiftwidth=4
5 package ExtUtils::AutoInstall;
6 $ExtUtils::AutoInstall::VERSION = '0.56';
10 use ExtUtils::MakeMaker ();
14 # special map on pre-defined feature sets
16 '' => 'Core Features', # XXX: deprecated
17 '-core' => 'Core Features',
20 # various lexical flags
21 my (@Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS);
22 my ($Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly);
23 my ($PostambleActions, $PostambleUsed);
25 $AcceptDefault = 1 unless -t STDIN; # non-interactive session
34 [ UNIVERSAL::isa($Config, 'HASH') ? %{$Config} : @{$Config}],
39 # initialize various flags, and/or perform install
41 foreach my $arg (@ARGV, split(/[\s\t]+/, $ENV{PERL_EXTUTILS_AUTOINSTALL} || '')) {
42 if ($arg =~ /^--config=(.*)$/) {
43 $Config = [ split(',', $1) ];
45 elsif ($arg =~ /^--installdeps=(.*)$/) {
46 __PACKAGE__->install($Config, @Missing = split(/,/, $1));
49 elsif ($arg =~ /^--default(?:deps)?$/) {
52 elsif ($arg =~ /^--check(?:deps)?$/) {
55 elsif ($arg =~ /^--skip(?:deps)?$/) {
58 elsif ($arg =~ /^--test(?:only)?$/) {
64 # overrides MakeMaker's prompt() to automatically accept the default choice
66 goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault;
68 my ($prompt, $default) = @_;
69 my $y = ($default =~ /^[Yy]/);
71 print $prompt, ' [', ($y ? 'Y' : 'y'), '/', ($y ? 'n' : 'N'), '] ';
79 my @args = @_ or return;
82 print "*** $class version ".$class->VERSION."\n";
83 print "*** Checking for dependencies...\n";
89 my $maxlen = length((sort { length($b) <=> length($a) }
91 map { ref($_) ? keys %{ref($_) eq 'HASH' ? $_ : +{@{$_}}} : '' }
92 map { +{@args}->{$_} }
93 grep { /^[^\-]/ or /^-core$/i } keys %{+{@args}})[0]);
95 while (my ($feature, $modules) = splice(@args, 0, 2)) {
96 my (@required, @tests, @skiptests);
100 if ($feature =~ m/^-(\w+)$/) {
103 # check for a newer version of myself
104 _update_to($modules, @_) and return if $option eq 'version';
106 # sets CPAN configuration options
107 $Config = $modules if $option eq 'config';
109 # promote every features to core status
110 $core_all = ($modules =~ /^all$/i) and next
111 if $option eq 'core';
113 next unless $option eq 'core';
116 print "[".($FeatureMap{lc($feature)} || $feature)."]\n";
118 $modules = [ %{$modules} ] if UNIVERSAL::isa($modules, 'HASH');
120 unshift @$modules, -default => &{shift(@$modules)}
121 if (ref($modules->[0]) eq 'CODE'); # XXX: bugward combatability
123 while (my ($mod, $arg) = splice(@$modules, 0, 2)) {
124 if ($mod =~ m/^-(\w+)$/) {
127 $default = $arg if ($option eq 'default');
128 $conflict = $arg if ($option eq 'conflict');
129 @tests = @{$arg} if ($option eq 'tests');
130 @skiptests = @{$arg} if ($option eq 'skiptests');
135 printf("- %-${maxlen}s ...", $mod);
137 # XXX: check for conflicts and uninstalls(!) them.
138 if (defined(my $cur = _version_check(_load($mod), $arg ||= 0))) {
139 print "loaded. ($cur".($arg ? " >= $arg" : '').")\n";
140 push @Existing, $mod => $arg;
141 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
144 print "missing." . ($arg ? " (would need $arg)" : '') . "\n";
145 push @required, $mod => $arg;
149 next unless @required;
151 my $mandatory = ($feature eq '-core' or $core_all);
153 if (!$SkipInstall and ($CheckOnly or _prompt(
154 qq{==> Auto-install the }. (@required / 2).
155 ($mandatory ? ' mandatory' : ' optional').
156 qq{ module(s) from CPAN?}, $default ? 'y' : 'n',
158 push (@Missing, @required);
159 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
162 elsif (!$SkipInstall and $default and $mandatory and _prompt(
163 qq{==> The module(s) are mandatory! Really skip?}, 'n',
165 push (@Missing, @required);
166 $DisabledTests{$_} = 1 for map { glob($_) } @skiptests;
170 $DisabledTests{$_} = 1 for map { glob($_) } @tests;
174 _check_lock(); # check for $UnderCPAN
176 if (@Missing and not ($CheckOnly or $UnderCPAN)) {
178 print "*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n";
179 # make an educated guess of whether we'll need root permission.
180 print " (You may need to do that as the 'root' user.)\n" if eval '$>';
182 print "*** $class configuration finished.\n";
188 *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main';
191 # CPAN.pm is non-reentrant, so check if we're under it and have no CPANPLUS
193 return unless @Missing;
194 return if _has_cpanplus();
196 require CPAN; CPAN::Config->load;
197 my $lock = MM->catfile($CPAN::Config->{cpan_home}, ".lock");
199 if (-f $lock and open(LOCK, $lock)
200 and ($^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid())
201 and ($CPAN::Config->{prerequisites_policy} || '') ne 'ignore'
205 *** Since we're running under CPAN, I'll just let it take care
206 of the dependency's installation later.
217 my $i; # used below to strip leading '-' from config keys
218 my @config = (map { s/^-// if ++$i; $_ } @{+shift});
220 my (@modules, @installed);
221 while (my ($pkg, $ver) = splice(@_, 0, 2)) {
222 # grep out those already installed
223 if (defined(_version_check(_load($pkg), $ver))) {
224 push @installed, $pkg;
227 push @modules, $pkg, $ver;
231 return @installed unless @modules; # nothing to do
233 print "*** Installing dependencies...\n";
235 return unless _connected_to('cpan.org');
240 if ($args{do_once} and open(FAILED, '.#autoinstall.failed')) {
241 while (<FAILED>) { chomp; $failed{$_}++ }
245 while (my ($k, $v) = splice(@modules, 0, 2)) {
246 push @newmod, ($k => $v) unless $failed{$k};
251 if (_has_cpanplus()) {
252 _install_cpanplus(\@modules, \@config);
255 _install_cpan(\@modules, \@config);
258 print "*** $class installation finished.\n";
260 # see if we have successfully installed them
261 while (my ($pkg, $ver) = splice(@modules, 0, 2)) {
262 if (defined(_version_check(_load($pkg), $ver))) {
263 push @installed, $pkg;
265 elsif ($args{do_once} and open(FAILED, '>> .#autoinstall.failed')) {
266 print FAILED "$pkg\n";
270 close FAILED if $args{do_once};
275 sub _install_cpanplus {
276 my @modules = @{+shift};
277 my @config = @{+shift};
280 require CPANPLUS::Backend;
281 my $cp = CPANPLUS::Backend->new;
282 my $conf = $cp->configure_object;
284 return unless _can_write($conf->_get_build('base'));
286 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
287 my $makeflags = $conf->get_conf('makeflags') || '';
288 if (UNIVERSAL::isa($makeflags, 'HASH')) {
289 # 0.03+ uses a hashref here
290 $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST};
293 # 0.02 and below uses a scalar
294 $makeflags = join(' ', split(' ', $makeflags), 'UNINST=1')
295 if ($makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' });
297 $conf->set_conf(makeflags => $makeflags);
299 while (my ($key, $val) = splice(@config, 0, 2)) {
300 eval { $conf->set_conf($key, $val) };
303 my $modtree = $cp->module_tree;
304 while (my ($pkg, $ver) = splice(@modules, 0, 2)) {
305 print "*** Installing $pkg...\n";
307 MY::preinstall($pkg, $ver) or next if defined &MY::preinstall;
310 my $obj = $modtree->{$pkg};
312 if ($obj and defined(_version_check($obj->{version}, $ver))) {
313 my $pathname = $pkg; $pathname =~ s/::/\\W/;
315 foreach my $inc (grep { m/$pathname.pm/i } keys(%INC)) {
319 my $rv = $cp->install( modules => [ $obj->{module} ]);
321 if ($rv and ($rv->{$obj->{module}} or $rv->{ok})) {
322 print "*** $pkg successfully installed.\n";
326 print "*** $pkg installation cancelled.\n";
330 $installed += $success;
334 *** Could not find a version $ver or above for $pkg; skipping.
338 MY::postinstall($pkg, $ver, $success) if defined &MY::postinstall;
345 my @modules = @{+shift};
346 my @config = @{+shift};
350 require CPAN; CPAN::Config->load;
352 return unless _can_write(MM->catfile($CPAN::Config->{cpan_home}, 'sources'));
354 # if we're root, set UNINST=1 to avoid trouble unless user asked for it.
355 my $makeflags = $CPAN::Config->{make_install_arg} || '';
356 $CPAN::Config->{make_install_arg} = join(' ', split(' ', $makeflags), 'UNINST=1')
357 if ($makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' });
359 # don't show start-up info
360 $CPAN::Config->{inhibit_startup_message} = 1;
362 # set additional options
363 while (my ($opt, $arg) = splice(@config, 0, 2)) {
364 ($args{$opt} = $arg, next)
365 if $opt =~ /^force$/; # pseudo-option
366 $CPAN::Config->{$opt} = $arg;
369 while (my ($pkg, $ver) = splice(@modules, 0, 2)) {
370 MY::preinstall($pkg, $ver) or next if defined &MY::preinstall;
372 print "*** Installing $pkg...\n";
374 my $obj = CPAN::Shell->expand(Module => $pkg);
377 if ($obj and defined(_version_check($obj->cpan_version, $ver))) {
378 my $pathname = $pkg; $pathname =~ s/::/\\W/;
380 foreach my $inc (grep { m/$pathname.pm/i } keys(%INC)) {
384 $obj->force('install') if $args{force};
386 if ($obj->install eq 'YES') {
387 print "*** $pkg successfully installed.\n";
391 print "*** $pkg installation failed.\n";
395 $installed += $success;
399 *** Could not find a version $ver or above for $pkg; skipping.
403 MY::postinstall($pkg, $ver, $success) if defined &MY::postinstall;
412 $INC{'CPANPLUS/Config.pm'} or
413 _load('CPANPLUS::Shell::Default')
418 # make guesses on whether we're under the CPAN installation directory
423 my $cwd = File::Spec->canonpath(Cwd::cwd());
424 my $cpan = File::Spec->canonpath($CPAN::Config->{cpan_home});
426 return (index($cwd, $cpan) > -1);
430 my $class = __PACKAGE__;
433 return if defined(_version_check(_load($class), $ver)); # no need to upgrade
436 "==> A newer version of $class ($ver) is required. Install?", 'y'
438 die "*** Please install $class $ver manually.\n";
442 *** Trying to fetch it from CPAN...
446 _load($class) and return $class->import(@_)
447 if $class->install([], $class, $ver);
449 print << '.'; exit 1;
451 *** Cannot bootstrap myself. :-( Installation terminated.
455 # check if we're connected to some host, using inet_aton
460 ( _load('Socket') and Socket::inet_aton($site) ) or _prompt(qq(
461 *** Your host cannot resolve the domain name '$site', which
462 probably means the Internet connections are unavailable.
463 ==> Should we try to install the required module(s) anyway?), 'n'
468 # check if a directory is writable; may create it on demand
471 mkdir ($path, 0755) unless -e $path;
474 return 1 if -w $path and -w $Config::Config{sitelib};
477 *** You are not allowed to write to the directory '$path';
478 the installation may fail due to insufficient permissions.
481 if (eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt(qq(
482 ==> Should we try to re-execute the autoinstall process with 'sudo'?), 'y'
484 # try to bootstrap ourselves from sudo
486 *** Trying to re-execute the autoinstall process with 'sudo'...
488 my $missing = join(',', @Missing);
489 my $config = join(',',
490 UNIVERSAL::isa($Config, 'HASH') ? %{$Config} : @{$Config}
493 return unless system('sudo', $^X, $0, "--config=$config", "--installdeps=$missing");
496 *** The 'sudo' command exited with error! Resuming...
501 ==> Should we try to install the required module(s) anyway?), 'n'
505 # load a module and return the version it reports
507 my $mod = pop; # class/instance doesn't matter
514 return eval { require $file; $mod->VERSION } || ($@ ? undef : 0);
517 # compare two versions, either use Sort::Versions or plain comparison
519 my ($cur, $min) = @_;
520 return unless defined $cur;
524 # check for version numbers that are not in decimal format
525 if (ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./) {
526 if ($version::VERSION or defined(_load('version'))) {
527 # use version.pm if it is installed.
528 return ((version->new($cur) >= version->new($min)) ? $cur : undef);
530 elsif ($Sort::Versions::VERSION or defined(_load('Sort::Versions'))) {
531 # use Sort::Versions as the sorting algorithm for a.b.c versions
532 return ((Sort::Versions::versioncmp($cur, $min) != -1) ? $cur : undef);
535 warn "Cannot reliably compare non-decimal formatted versions.\n".
536 "Please install version.pm or Sort::Versions.\n";
540 local $^W = 0; # shuts off 'not numeric' bugs
541 return ($cur >= $min ? $cur : undef);
544 # nothing; this usage is deprecated.
545 sub main::PREREQ_PM { return {}; }
550 $args{PREREQ_PM} = { %{$args{PREREQ_PM} || {} }, @Existing, @Missing }
551 if $UnderCPAN or $TestOnly;
553 if ($args{EXE_FILES}) {
554 require ExtUtils::Manifest;
555 my $manifest = ExtUtils::Manifest::maniread('MANIFEST');
558 grep { exists $manifest->{$_} } @{$args{EXE_FILES}}
562 $args{test}{TESTS} ||= 't/*.t';
563 $args{test}{TESTS} = join(' ', grep {
564 !exists($DisabledTests{$_})
565 } map { glob($_) } split(/\s+/, $args{test}{TESTS}));
567 my $missing = join(',', @Missing);
568 my $config = join(',',
569 UNIVERSAL::isa($Config, 'HASH') ? %{$Config} : @{$Config}
572 $PostambleActions = (
573 $missing ? "\$(PERL) $0 --config=$config --installdeps=$missing"
580 # a wrapper to ExtUtils::MakeMaker::WriteMakefile
583 Carp::croak "WriteMakefile: Need even number of args" if @_ % 2;
587 *** Makefile not written in check-only mode.
592 my %args = _make_args(@_);
597 local *MY::postamble = \&postamble unless defined &MY::postamble;
598 ExtUtils::MakeMaker::WriteMakefile(%args);
600 print << "." unless $PostambleUsed;
601 *** WARNING: Makefile written with customized MY::postamble() without
602 including contents from ExtUtils::AutoInstall::postamble() --
603 auto installation features disabled. Please contact the author.
614 config :: installdeps
618 \t\$(PERL) $0 --checkdeps