1
0

AutoInstall.pm 22 KB

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