1
0

AutoInstall.pm 25 KB

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