Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4885 lines
139 KiB

  1. package CPAN;
  2. use vars qw{$Try_autoload
  3. $Revision
  4. $META $Signal $Cwd $End
  5. $Suppress_readline %Dontload
  6. $Frontend $Defaultsite
  7. }; #};
  8. $VERSION = '1.48';
  9. # $Id: CPAN.pm,v 1.260 1999/03/06 19:31:02 k Exp $
  10. # only used during development:
  11. $Revision = "";
  12. # $Revision = "[".substr(q$Revision: 1.260 $, 10)."]";
  13. use Carp ();
  14. use Config ();
  15. use Cwd ();
  16. use DirHandle;
  17. use Exporter ();
  18. use ExtUtils::MakeMaker (); # $SelfLoader::DEBUG=1;
  19. use File::Basename ();
  20. use File::Copy ();
  21. use File::Find;
  22. use File::Path ();
  23. use FileHandle ();
  24. use Safe ();
  25. use Text::ParseWords ();
  26. use Text::Wrap;
  27. use File::Spec;
  28. END { $End++; &cleanup; }
  29. %CPAN::DEBUG = qw[
  30. CPAN 1
  31. Index 2
  32. InfoObj 4
  33. Author 8
  34. Distribution 16
  35. Bundle 32
  36. Module 64
  37. CacheMgr 128
  38. Complete 256
  39. FTP 512
  40. Shell 1024
  41. Eval 2048
  42. Config 4096
  43. Tarzip 8192
  44. ];
  45. $CPAN::DEBUG ||= 0;
  46. $CPAN::Signal ||= 0;
  47. $CPAN::Frontend ||= "CPAN::Shell";
  48. $CPAN::Defaultsite ||= "ftp://ftp.perl.org/pub/CPAN";
  49. package CPAN;
  50. use vars qw($VERSION @EXPORT $AUTOLOAD $DEBUG $META $term);
  51. use strict qw(vars);
  52. @CPAN::ISA = qw(CPAN::Debug Exporter);
  53. @EXPORT = qw(
  54. autobundle bundle expand force get
  55. install make readme recompile shell test clean
  56. );
  57. #-> sub CPAN::AUTOLOAD ;
  58. sub AUTOLOAD {
  59. my($l) = $AUTOLOAD;
  60. $l =~ s/.*:://;
  61. my(%EXPORT);
  62. @EXPORT{@EXPORT} = '';
  63. CPAN::Config->load unless $CPAN::Config_loaded++;
  64. if (exists $EXPORT{$l}){
  65. CPAN::Shell->$l(@_);
  66. } else {
  67. my $ok = CPAN::Shell->try_dot_al($AUTOLOAD);
  68. if ($ok) {
  69. goto &$AUTOLOAD;
  70. # } else {
  71. # $CPAN::Frontend->mywarn("Could not autoload $AUTOLOAD");
  72. }
  73. $CPAN::Frontend->mywarn(qq{Unknown command "$AUTOLOAD". }.
  74. qq{Type ? for help.
  75. });
  76. }
  77. }
  78. #-> sub CPAN::shell ;
  79. sub shell {
  80. my($self) = @_;
  81. $Suppress_readline ||= ! -t STDIN;
  82. CPAN::Config->load unless $CPAN::Config_loaded++;
  83. my $prompt = "cpan> ";
  84. local($^W) = 1;
  85. unless ($Suppress_readline) {
  86. require Term::ReadLine;
  87. # import Term::ReadLine;
  88. $term = Term::ReadLine->new('CPAN Monitor');
  89. if ($term->ReadLine eq "Term::ReadLine::Gnu") {
  90. my $attribs = $term->Attribs;
  91. # $attribs->{completion_entry_function} =
  92. # $attribs->{'list_completion_function'};
  93. $attribs->{attempted_completion_function} = sub {
  94. &CPAN::Complete::gnu_cpl;
  95. }
  96. # $attribs->{completion_word} =
  97. # [qw(help me somebody to find out how
  98. # to use completion with GNU)];
  99. } else {
  100. $readline::rl_completion_function =
  101. $readline::rl_completion_function = 'CPAN::Complete::cpl';
  102. }
  103. }
  104. no strict;
  105. $META->checklock();
  106. my $getcwd;
  107. $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  108. my $cwd = CPAN->$getcwd();
  109. my $try_detect_readline = $term->ReadLine eq "Term::ReadLine::Stub";
  110. my $rl_avail = $Suppress_readline ? "suppressed" :
  111. ($term->ReadLine ne "Term::ReadLine::Stub") ? "enabled" :
  112. "available (try ``install Bundle::CPAN'')";
  113. $CPAN::Frontend->myprint(
  114. qq{
  115. cpan shell -- CPAN exploration and modules installation (v$CPAN::VERSION$CPAN::Revision)
  116. ReadLine support $rl_avail
  117. }) unless $CPAN::Config->{'inhibit_startup_message'} ;
  118. my($continuation) = "";
  119. while () {
  120. if ($Suppress_readline) {
  121. print $prompt;
  122. last unless defined ($_ = <> );
  123. chomp;
  124. } else {
  125. last unless defined ($_ = $term->readline($prompt));
  126. }
  127. $_ = "$continuation$_" if $continuation;
  128. s/^\s+//;
  129. next if /^$/;
  130. $_ = 'h' if /^\s*\?/;
  131. if (/^(?:q(?:uit)?|bye|exit)$/i) {
  132. last;
  133. } elsif (s/\\$//s) {
  134. chomp;
  135. $continuation = $_;
  136. $prompt = " > ";
  137. } elsif (/^\!/) {
  138. s/^\!//;
  139. my($eval) = $_;
  140. package CPAN::Eval;
  141. use vars qw($import_done);
  142. CPAN->import(':DEFAULT') unless $import_done++;
  143. CPAN->debug("eval[$eval]") if $CPAN::DEBUG;
  144. eval($eval);
  145. warn $@ if $@;
  146. $continuation = "";
  147. $prompt = "cpan> ";
  148. } elsif (/./) {
  149. my(@line);
  150. if ($] < 5.00322) { # parsewords had a bug until recently
  151. @line = split;
  152. } else {
  153. eval { @line = Text::ParseWords::shellwords($_) };
  154. warn($@), next if $@;
  155. }
  156. $CPAN::META->debug("line[".join("|",@line)."]") if $CPAN::DEBUG;
  157. my $command = shift @line;
  158. eval { CPAN::Shell->$command(@line) };
  159. warn $@ if $@;
  160. chdir $cwd;
  161. $CPAN::Frontend->myprint("\n");
  162. $continuation = "";
  163. $prompt = "cpan> ";
  164. }
  165. } continue {
  166. $Signal=0;
  167. CPAN::Queue->nullify_queue;
  168. if ($try_detect_readline) {
  169. if ($CPAN::META->has_inst("Term::ReadLine::Gnu")
  170. ||
  171. $CPAN::META->has_inst("Term::ReadLine::Perl")
  172. ) {
  173. delete $INC{"Term/ReadLine.pm"};
  174. my $redef;
  175. local($SIG{__WARN__}) = CPAN::Shell::dotdot_onreload(\$redef);
  176. require Term::ReadLine;
  177. $CPAN::Frontend->myprint("\n$redef subroutines in Term::ReadLine redefined\n");
  178. goto &shell;
  179. }
  180. }
  181. }
  182. }
  183. package CPAN::CacheMgr;
  184. @CPAN::CacheMgr::ISA = qw(CPAN::InfoObj CPAN);
  185. use File::Find;
  186. package CPAN::Config;
  187. import ExtUtils::MakeMaker 'neatvalue';
  188. use vars qw(%can $dot_cpan);
  189. %can = (
  190. 'commit' => "Commit changes to disk",
  191. 'defaults' => "Reload defaults from disk",
  192. 'init' => "Interactive setting of all options",
  193. );
  194. package CPAN::FTP;
  195. use vars qw($Ua $Thesite $Themethod);
  196. @CPAN::FTP::ISA = qw(CPAN::Debug);
  197. package CPAN::Complete;
  198. @CPAN::Complete::ISA = qw(CPAN::Debug);
  199. package CPAN::Index;
  200. use vars qw($last_time $date_of_03);
  201. @CPAN::Index::ISA = qw(CPAN::Debug);
  202. $last_time ||= 0;
  203. $date_of_03 ||= 0;
  204. package CPAN::InfoObj;
  205. @CPAN::InfoObj::ISA = qw(CPAN::Debug);
  206. package CPAN::Author;
  207. @CPAN::Author::ISA = qw(CPAN::InfoObj);
  208. package CPAN::Distribution;
  209. @CPAN::Distribution::ISA = qw(CPAN::InfoObj);
  210. package CPAN::Bundle;
  211. @CPAN::Bundle::ISA = qw(CPAN::Module);
  212. package CPAN::Module;
  213. @CPAN::Module::ISA = qw(CPAN::InfoObj);
  214. package CPAN::Shell;
  215. use vars qw($AUTOLOAD $redef @ISA);
  216. @CPAN::Shell::ISA = qw(CPAN::Debug);
  217. #-> sub CPAN::Shell::AUTOLOAD ;
  218. sub AUTOLOAD {
  219. my($autoload) = $AUTOLOAD;
  220. my $class = shift(@_);
  221. # warn "autoload[$autoload] class[$class]";
  222. $autoload =~ s/.*:://;
  223. if ($autoload =~ /^w/) {
  224. if ($CPAN::META->has_inst('CPAN::WAIT')) {
  225. CPAN::WAIT->$autoload(@_);
  226. } else {
  227. $CPAN::Frontend->mywarn(qq{
  228. Commands starting with "w" require CPAN::WAIT to be installed.
  229. Please consider installing CPAN::WAIT to use the fulltext index.
  230. For this you just need to type
  231. install CPAN::WAIT
  232. });
  233. }
  234. } else {
  235. my $ok = CPAN::Shell->try_dot_al($AUTOLOAD);
  236. if ($ok) {
  237. goto &$AUTOLOAD;
  238. # } else {
  239. # $CPAN::Frontend->mywarn("Could not autoload $autoload");
  240. }
  241. $CPAN::Frontend->mywarn(qq{Unknown command '$autoload'. }.
  242. qq{Type ? for help.
  243. });
  244. }
  245. }
  246. #-> CPAN::Shell::try_dot_al
  247. sub try_dot_al {
  248. my($class,$autoload) = @_;
  249. return unless $CPAN::Try_autoload;
  250. # I don't see how to re-use that from the AutoLoader...
  251. my($name,$ok);
  252. # Braces used to preserve $1 et al.
  253. {
  254. my ($pkg,$func) = $autoload =~ /(.*)::([^:]+)$/;
  255. $pkg =~ s|::|/|g;
  256. if (defined($name=$INC{"$pkg.pm"}))
  257. {
  258. $name =~ s|^(.*)$pkg\.pm$|$1auto/$pkg/$func.al|;
  259. $name = undef unless (-r $name);
  260. }
  261. unless (defined $name)
  262. {
  263. $name = "auto/$autoload.al";
  264. $name =~ s|::|/|g;
  265. }
  266. }
  267. my $save = $@;
  268. eval {local $SIG{__DIE__};require $name};
  269. if ($@) {
  270. if (substr($autoload,-9) eq '::DESTROY') {
  271. *$autoload = sub {};
  272. $ok = 1;
  273. } else {
  274. if ($name =~ s{(\w{12,})\.al$}{substr($1,0,11).".al"}e){
  275. eval {local $SIG{__DIE__};require $name};
  276. }
  277. if ($@){
  278. $@ =~ s/ at .*\n//;
  279. Carp::croak $@;
  280. } else {
  281. $ok = 1;
  282. }
  283. }
  284. } else {
  285. $ok = 1;
  286. }
  287. $@ = $save;
  288. # my $lm = Carp::longmess();
  289. # warn "ok[$ok] autoload[$autoload] longmess[$lm]"; # debug
  290. return $ok;
  291. }
  292. #### autoloader is experimental
  293. #### to try it we have to set $Try_autoload and uncomment
  294. #### the use statement and uncomment the __END__ below
  295. #### You also need AutoSplit 1.01 available. MakeMaker will
  296. #### then build CPAN with all the AutoLoad stuff.
  297. # use AutoLoader;
  298. # $Try_autoload = 1;
  299. if ($CPAN::Try_autoload) {
  300. my $p;
  301. for $p (qw(
  302. CPAN::Author CPAN::Bundle CPAN::CacheMgr CPAN::Complete
  303. CPAN::Config CPAN::Debug CPAN::Distribution CPAN::FTP
  304. CPAN::FTP::netrc CPAN::Index CPAN::InfoObj CPAN::Module
  305. )) {
  306. *{"$p\::AUTOLOAD"} = \&AutoLoader::AUTOLOAD;
  307. }
  308. }
  309. package CPAN::Tarzip;
  310. use vars qw($AUTOLOAD @ISA);
  311. @CPAN::Tarzip::ISA = qw(CPAN::Debug);
  312. package CPAN::Queue;
  313. # One use of the queue is to determine if we should or shouldn't
  314. # announce the availability of a new CPAN module
  315. # Now we try to use it for dependency tracking. For that to happen
  316. # we need to draw a dependency tree and do the leaves first. This can
  317. # easily be reached by running CPAN.pm recursively, but we don't want
  318. # to waste memory and run into deep recursion. So what we can do is
  319. # this:
  320. # CPAN::Queue is the package where the queue is maintained. Dependencies
  321. # often have high priority and must be brought to the head of the queue,
  322. # possibly by jumping the queue if they are already there. My first code
  323. # attempt tried to be extremely correct. Whenever a module needed
  324. # immediate treatment, I either unshifted it to the front of the queue,
  325. # or, if it was already in the queue, I spliced and let it bypass the
  326. # others. This became a too correct model that made it impossible to put
  327. # an item more than once into the queue. Why would you need that? Well,
  328. # you need temporary duplicates as the manager of the queue is a loop
  329. # that
  330. #
  331. # (1) looks at the first item in the queue without shifting it off
  332. #
  333. # (2) cares for the item
  334. #
  335. # (3) removes the item from the queue, *even if its agenda failed and
  336. # even if the item isn't the first in the queue anymore* (that way
  337. # protecting against never ending queues)
  338. #
  339. # So if an item has prerequisites, the installation fails now, but we
  340. # want to retry later. That's easy if we have it twice in the queue.
  341. #
  342. # I also expect insane dependency situations where an item gets more
  343. # than two lives in the queue. Simplest example is triggered by 'install
  344. # Foo Foo Foo'. People make this kind of mistakes and I don't want to
  345. # get in the way. I wanted the queue manager to be a dumb servant, not
  346. # one that knows everything.
  347. #
  348. # Who would I tell in this model that the user wants to be asked before
  349. # processing? I can't attach that information to the module object,
  350. # because not modules are installed but distributions. So I'd have to
  351. # tell the distribution object that it should ask the user before
  352. # processing. Where would the question be triggered then? Most probably
  353. # in CPAN::Distribution::rematein.
  354. # Hope that makes sense, my head is a bit off:-) -- AK
  355. use vars qw{ @All };
  356. sub new {
  357. my($class,$mod) = @_;
  358. my $self = bless {mod => $mod}, $class;
  359. push @All, $self;
  360. # my @all = map { $_->{mod} } @All;
  361. # warn "Adding Queue object for mod[$mod] all[@all]";
  362. return $self;
  363. }
  364. sub first {
  365. my $obj = $All[0];
  366. $obj->{mod};
  367. }
  368. sub delete_first {
  369. my($class,$what) = @_;
  370. my $i;
  371. for my $i (0..$#All) {
  372. if ( $All[$i]->{mod} eq $what ) {
  373. splice @All, $i, 1;
  374. return;
  375. }
  376. }
  377. }
  378. sub jumpqueue {
  379. my $class = shift;
  380. my @what = @_;
  381. my $obj;
  382. WHAT: for my $what (reverse @what) {
  383. my $jumped = 0;
  384. for (my $i=0; $i<$#All;$i++) { #prevent deep recursion
  385. if ($All[$i]->{mod} eq $what){
  386. $jumped++;
  387. if ($jumped > 100) { # one's OK if e.g. just processing now;
  388. # more are OK if user typed it several
  389. # times
  390. $CPAN::Frontend->mywarn(
  391. qq{Object [$what] queued more than 100 times, ignoring}
  392. );
  393. next WHAT;
  394. }
  395. }
  396. }
  397. my $obj = bless { mod => $what }, $class;
  398. unshift @All, $obj;
  399. }
  400. }
  401. sub exists {
  402. my($self,$what) = @_;
  403. my @all = map { $_->{mod} } @All;
  404. my $exists = grep { $_->{mod} eq $what } @All;
  405. # warn "Checking exists in Queue object for mod[$what] all[@all] exists[$exists]";
  406. $exists;
  407. }
  408. sub delete {
  409. my($self,$mod) = @_;
  410. @All = grep { $_->{mod} ne $mod } @All;
  411. # my @all = map { $_->{mod} } @All;
  412. # warn "Deleting Queue object for mod[$mod] all[@all]";
  413. }
  414. sub nullify_queue {
  415. @All = ();
  416. }
  417. package CPAN;
  418. $META ||= CPAN->new; # In case we re-eval ourselves we need the ||
  419. 1;
  420. # __END__ # uncomment this and AutoSplit version 1.01 will split it
  421. #-> sub CPAN::autobundle ;
  422. sub autobundle;
  423. #-> sub CPAN::bundle ;
  424. sub bundle;
  425. #-> sub CPAN::expand ;
  426. sub expand;
  427. #-> sub CPAN::force ;
  428. sub force;
  429. #-> sub CPAN::install ;
  430. sub install;
  431. #-> sub CPAN::make ;
  432. sub make;
  433. #-> sub CPAN::clean ;
  434. sub clean;
  435. #-> sub CPAN::test ;
  436. sub test;
  437. #-> sub CPAN::all ;
  438. sub all_objects {
  439. my($mgr,$class) = @_;
  440. CPAN::Config->load unless $CPAN::Config_loaded++;
  441. CPAN->debug("mgr[$mgr] class[$class]") if $CPAN::DEBUG;
  442. CPAN::Index->reload;
  443. values %{ $META->{$class} };
  444. }
  445. *all = \&all_objects;
  446. # Called by shell, not in batch mode. Not clean XXX
  447. #-> sub CPAN::checklock ;
  448. sub checklock {
  449. my($self) = @_;
  450. my $lockfile = MM->catfile($CPAN::Config->{cpan_home},".lock");
  451. if (-f $lockfile && -M _ > 0) {
  452. my $fh = FileHandle->new($lockfile);
  453. my $other = <$fh>;
  454. $fh->close;
  455. if (defined $other && $other) {
  456. chomp $other;
  457. return if $$==$other; # should never happen
  458. $CPAN::Frontend->mywarn(
  459. qq{
  460. There seems to be running another CPAN process ($other). Contacting...
  461. });
  462. if (kill 0, $other) {
  463. $CPAN::Frontend->mydie(qq{Other job is running.
  464. You may want to kill it and delete the lockfile, maybe. On UNIX try:
  465. kill $other
  466. rm $lockfile
  467. });
  468. } elsif (-w $lockfile) {
  469. my($ans) =
  470. ExtUtils::MakeMaker::prompt
  471. (qq{Other job not responding. Shall I overwrite }.
  472. qq{the lockfile? (Y/N)},"y");
  473. $CPAN::Frontend->myexit("Ok, bye\n")
  474. unless $ans =~ /^y/i;
  475. } else {
  476. Carp::croak(
  477. qq{Lockfile $lockfile not writeable by you. }.
  478. qq{Cannot proceed.\n}.
  479. qq{ On UNIX try:\n}.
  480. qq{ rm $lockfile\n}.
  481. qq{ and then rerun us.\n}
  482. );
  483. }
  484. }
  485. }
  486. File::Path::mkpath($CPAN::Config->{cpan_home});
  487. my $fh;
  488. unless ($fh = FileHandle->new(">$lockfile")) {
  489. if ($! =~ /Permission/) {
  490. my $incc = $INC{'CPAN/Config.pm'};
  491. my $myincc = MM->catfile($ENV{HOME},'.cpan','CPAN','MyConfig.pm');
  492. $CPAN::Frontend->myprint(qq{
  493. Your configuration suggests that CPAN.pm should use a working
  494. directory of
  495. $CPAN::Config->{cpan_home}
  496. Unfortunately we could not create the lock file
  497. $lockfile
  498. due to permission problems.
  499. Please make sure that the configuration variable
  500. \$CPAN::Config->{cpan_home}
  501. points to a directory where you can write a .lock file. You can set
  502. this variable in either
  503. $incc
  504. or
  505. $myincc
  506. });
  507. }
  508. $CPAN::Frontend->mydie("Could not open >$lockfile: $!");
  509. }
  510. $fh->print($$, "\n");
  511. $self->{LOCK} = $lockfile;
  512. $fh->close;
  513. $SIG{'TERM'} = sub {
  514. &cleanup;
  515. $CPAN::Frontend->mydie("Got SIGTERM, leaving");
  516. };
  517. $SIG{'INT'} = sub {
  518. # no blocks!!!
  519. &cleanup if $Signal;
  520. $CPAN::Frontend->mydie("Got another SIGINT") if $Signal;
  521. print "Caught SIGINT\n";
  522. $Signal++;
  523. };
  524. $SIG{'__DIE__'} = \&cleanup;
  525. $self->debug("Signal handler set.") if $CPAN::DEBUG;
  526. }
  527. #-> sub CPAN::DESTROY ;
  528. sub DESTROY {
  529. &cleanup; # need an eval?
  530. }
  531. #-> sub CPAN::cwd ;
  532. sub cwd {Cwd::cwd();}
  533. #-> sub CPAN::getcwd ;
  534. sub getcwd {Cwd::getcwd();}
  535. #-> sub CPAN::exists ;
  536. sub exists {
  537. my($mgr,$class,$id) = @_;
  538. CPAN::Index->reload;
  539. ### Carp::croak "exists called without class argument" unless $class;
  540. $id ||= "";
  541. exists $META->{$class}{$id};
  542. }
  543. #-> sub CPAN::delete ;
  544. sub delete {
  545. my($mgr,$class,$id) = @_;
  546. delete $META->{$class}{$id};
  547. }
  548. #-> sub CPAN::has_inst
  549. sub has_inst {
  550. my($self,$mod,$message) = @_;
  551. Carp::croak("CPAN->has_inst() called without an argument")
  552. unless defined $mod;
  553. if (defined $message && $message eq "no") {
  554. $Dontload{$mod}||=1;
  555. return 0;
  556. } elsif (exists $Dontload{$mod}) {
  557. return 0;
  558. }
  559. my $file = $mod;
  560. my $obj;
  561. $file =~ s|::|/|g;
  562. $file =~ s|/|\\|g if $^O eq 'MSWin32';
  563. $file .= ".pm";
  564. if ($INC{$file}) {
  565. # checking %INC is wrong, because $INC{LWP} may be true
  566. # although $INC{"URI/URL.pm"} may have failed. But as
  567. # I really want to say "bla loaded OK", I have to somehow
  568. # cache results.
  569. ### warn "$file in %INC"; #debug
  570. return 1;
  571. } elsif (eval { require $file }) {
  572. # eval is good: if we haven't yet read the database it's
  573. # perfect and if we have installed the module in the meantime,
  574. # it tries again. The second require is only a NOOP returning
  575. # 1 if we had success, otherwise it's retrying
  576. $CPAN::Frontend->myprint("CPAN: $mod loaded ok\n");
  577. if ($mod eq "CPAN::WAIT") {
  578. push @CPAN::Shell::ISA, CPAN::WAIT;
  579. }
  580. return 1;
  581. } elsif ($mod eq "Net::FTP") {
  582. warn qq{
  583. Please, install Net::FTP as soon as possible. CPAN.pm installs it for you
  584. if you just type
  585. install Bundle::libnet
  586. };
  587. sleep 2;
  588. } elsif ($mod eq "MD5"){
  589. $CPAN::Frontend->myprint(qq{
  590. CPAN: MD5 security checks disabled because MD5 not installed.
  591. Please consider installing the MD5 module.
  592. });
  593. sleep 2;
  594. } else {
  595. delete $INC{$file}; # if it inc'd LWP but failed during, say, URI
  596. }
  597. return 0;
  598. }
  599. #-> sub CPAN::instance ;
  600. sub instance {
  601. my($mgr,$class,$id) = @_;
  602. CPAN::Index->reload;
  603. $id ||= "";
  604. $META->{$class}{$id} ||= $class->new(ID => $id );
  605. }
  606. #-> sub CPAN::new ;
  607. sub new {
  608. bless {}, shift;
  609. }
  610. #-> sub CPAN::cleanup ;
  611. sub cleanup {
  612. # warn "cleanup called with arg[@_] End[$End] Signal[$Signal]";
  613. local $SIG{__DIE__} = '';
  614. my($message) = @_;
  615. my $i = 0;
  616. my $ineval = 0;
  617. if (
  618. 0 && # disabled, try reload cpan with it
  619. $] > 5.004_60 # thereabouts
  620. ) {
  621. $ineval = $^S;
  622. } else {
  623. my($subroutine);
  624. while ((undef,undef,undef,$subroutine) = caller(++$i)) {
  625. $ineval = 1, last if
  626. $subroutine eq '(eval)';
  627. }
  628. }
  629. return if $ineval && !$End;
  630. return unless defined $META->{'LOCK'};
  631. return unless -f $META->{'LOCK'};
  632. unlink $META->{'LOCK'};
  633. # require Carp;
  634. # Carp::cluck("DEBUGGING");
  635. $CPAN::Frontend->mywarn("Lockfile removed.\n");
  636. }
  637. package CPAN::CacheMgr;
  638. #-> sub CPAN::CacheMgr::as_string ;
  639. sub as_string {
  640. eval { require Data::Dumper };
  641. if ($@) {
  642. return shift->SUPER::as_string;
  643. } else {
  644. return Data::Dumper::Dumper(shift);
  645. }
  646. }
  647. #-> sub CPAN::CacheMgr::cachesize ;
  648. sub cachesize {
  649. shift->{DU};
  650. }
  651. sub tidyup {
  652. my($self) = @_;
  653. return unless -d $self->{ID};
  654. while ($self->{DU} > $self->{'MAX'} ) {
  655. my($toremove) = shift @{$self->{FIFO}};
  656. $CPAN::Frontend->myprint(sprintf(
  657. "Deleting from cache".
  658. ": $toremove (%.1f>%.1f MB)\n",
  659. $self->{DU}, $self->{'MAX'})
  660. );
  661. return if $CPAN::Signal;
  662. $self->force_clean_cache($toremove);
  663. return if $CPAN::Signal;
  664. }
  665. }
  666. #-> sub CPAN::CacheMgr::dir ;
  667. sub dir {
  668. shift->{ID};
  669. }
  670. #-> sub CPAN::CacheMgr::entries ;
  671. sub entries {
  672. my($self,$dir) = @_;
  673. return unless defined $dir;
  674. $self->debug("reading dir[$dir]") if $CPAN::DEBUG;
  675. $dir ||= $self->{ID};
  676. my $getcwd;
  677. $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  678. my($cwd) = CPAN->$getcwd();
  679. chdir $dir or Carp::croak("Can't chdir to $dir: $!");
  680. my $dh = DirHandle->new(File::Spec->curdir)
  681. or Carp::croak("Couldn't opendir $dir: $!");
  682. my(@entries);
  683. for ($dh->read) {
  684. next if $_ eq "." || $_ eq "..";
  685. if (-f $_) {
  686. push @entries, MM->catfile($dir,$_);
  687. } elsif (-d _) {
  688. push @entries, MM->catdir($dir,$_);
  689. } else {
  690. $CPAN::Frontend->mywarn("Warning: weird direntry in $dir: $_\n");
  691. }
  692. }
  693. chdir $cwd or Carp::croak("Can't chdir to $cwd: $!");
  694. sort { -M $b <=> -M $a} @entries;
  695. }
  696. #-> sub CPAN::CacheMgr::disk_usage ;
  697. sub disk_usage {
  698. my($self,$dir) = @_;
  699. return if exists $self->{SIZE}{$dir};
  700. return if $CPAN::Signal;
  701. my($Du) = 0;
  702. find(
  703. sub {
  704. $File::Find::prune++ if $CPAN::Signal;
  705. return if -l $_;
  706. if ($^O eq 'MacOS') {
  707. require Mac::Files;
  708. my $cat = Mac::Files::FSpGetCatInfo($_);
  709. $Du += $cat->ioFlLgLen() + $cat->ioFlRLgLen();
  710. } else {
  711. $Du += (-s _);
  712. }
  713. },
  714. $dir
  715. );
  716. return if $CPAN::Signal;
  717. $self->{SIZE}{$dir} = $Du/1024/1024;
  718. push @{$self->{FIFO}}, $dir;
  719. $self->debug("measured $dir is $Du") if $CPAN::DEBUG;
  720. $self->{DU} += $Du/1024/1024;
  721. $self->{DU};
  722. }
  723. #-> sub CPAN::CacheMgr::force_clean_cache ;
  724. sub force_clean_cache {
  725. my($self,$dir) = @_;
  726. return unless -e $dir;
  727. $self->debug("have to rmtree $dir, will free $self->{SIZE}{$dir}")
  728. if $CPAN::DEBUG;
  729. File::Path::rmtree($dir);
  730. $self->{DU} -= $self->{SIZE}{$dir};
  731. delete $self->{SIZE}{$dir};
  732. }
  733. #-> sub CPAN::CacheMgr::new ;
  734. sub new {
  735. my $class = shift;
  736. my $time = time;
  737. my($debug,$t2);
  738. $debug = "";
  739. my $self = {
  740. ID => $CPAN::Config->{'build_dir'},
  741. MAX => $CPAN::Config->{'build_cache'},
  742. SCAN => $CPAN::Config->{'scan_cache'} || 'atstart',
  743. DU => 0
  744. };
  745. File::Path::mkpath($self->{ID});
  746. my $dh = DirHandle->new($self->{ID});
  747. bless $self, $class;
  748. $self->scan_cache;
  749. $t2 = time;
  750. $debug .= "timing of CacheMgr->new: ".($t2 - $time);
  751. $time = $t2;
  752. CPAN->debug($debug) if $CPAN::DEBUG;
  753. $self;
  754. }
  755. #-> sub CPAN::CacheMgr::scan_cache ;
  756. sub scan_cache {
  757. my $self = shift;
  758. return if $self->{SCAN} eq 'never';
  759. $CPAN::Frontend->mydie("Unknown scan_cache argument: $self->{SCAN}")
  760. unless $self->{SCAN} eq 'atstart';
  761. $CPAN::Frontend->myprint(
  762. sprintf("Scanning cache %s for sizes\n",
  763. $self->{ID}));
  764. my $e;
  765. for $e ($self->entries($self->{ID})) {
  766. next if $e eq ".." || $e eq ".";
  767. $self->disk_usage($e);
  768. return if $CPAN::Signal;
  769. }
  770. $self->tidyup;
  771. }
  772. package CPAN::Debug;
  773. #-> sub CPAN::Debug::debug ;
  774. sub debug {
  775. my($self,$arg) = @_;
  776. my($caller,$func,$line,@rest) = caller(1); # caller(0) eg
  777. # Complete, caller(1)
  778. # eg readline
  779. ($caller) = caller(0);
  780. $caller =~ s/.*:://;
  781. $arg = "" unless defined $arg;
  782. my $rest = join "|", map { defined $_ ? $_ : "UNDEF" } @rest;
  783. if ($CPAN::DEBUG{$caller} & $CPAN::DEBUG){
  784. if ($arg and ref $arg) {
  785. eval { require Data::Dumper };
  786. if ($@) {
  787. $CPAN::Frontend->myprint($arg->as_string);
  788. } else {
  789. $CPAN::Frontend->myprint(Data::Dumper::Dumper($arg));
  790. }
  791. } else {
  792. $CPAN::Frontend->myprint("Debug($caller:$func,$line,[$rest]): $arg\n");
  793. }
  794. }
  795. }
  796. package CPAN::Config;
  797. #-> sub CPAN::Config::edit ;
  798. sub edit {
  799. my($class,@args) = @_;
  800. return unless @args;
  801. CPAN->debug("class[$class]args[".join(" | ",@args)."]");
  802. my($o,$str,$func,$args,$key_exists);
  803. $o = shift @args;
  804. if($can{$o}) {
  805. $class->$o(@args);
  806. return 1;
  807. } else {
  808. if (ref($CPAN::Config->{$o}) eq ARRAY) {
  809. $func = shift @args;
  810. $func ||= "";
  811. # Let's avoid eval, it's easier to comprehend without.
  812. if ($func eq "push") {
  813. push @{$CPAN::Config->{$o}}, @args;
  814. } elsif ($func eq "pop") {
  815. pop @{$CPAN::Config->{$o}};
  816. } elsif ($func eq "shift") {
  817. shift @{$CPAN::Config->{$o}};
  818. } elsif ($func eq "unshift") {
  819. unshift @{$CPAN::Config->{$o}}, @args;
  820. } elsif ($func eq "splice") {
  821. splice @{$CPAN::Config->{$o}}, @args;
  822. } elsif (@args) {
  823. $CPAN::Config->{$o} = [@args];
  824. } else {
  825. $CPAN::Frontend->myprint(
  826. join "",
  827. " $o ",
  828. ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$o}),
  829. "\n"
  830. );
  831. }
  832. } else {
  833. $CPAN::Config->{$o} = $args[0] if defined $args[0];
  834. $CPAN::Frontend->myprint(" $o " .
  835. (defined $CPAN::Config->{$o} ?
  836. $CPAN::Config->{$o} : "UNDEFINED"));
  837. }
  838. }
  839. }
  840. #-> sub CPAN::Config::commit ;
  841. sub commit {
  842. my($self,$configpm) = @_;
  843. unless (defined $configpm){
  844. $configpm ||= $INC{"CPAN/MyConfig.pm"};
  845. $configpm ||= $INC{"CPAN/Config.pm"};
  846. $configpm || Carp::confess(q{
  847. CPAN::Config::commit called without an argument.
  848. Please specify a filename where to save the configuration or try
  849. "o conf init" to have an interactive course through configing.
  850. });
  851. }
  852. my($mode);
  853. if (-f $configpm) {
  854. $mode = (stat $configpm)[2];
  855. if ($mode && ! -w _) {
  856. Carp::confess("$configpm is not writable");
  857. }
  858. }
  859. my $msg = <<EOF unless $configpm =~ /MyConfig/;
  860. # This is CPAN.pm's systemwide configuration file. This file provides
  861. # defaults for users, and the values can be changed in a per-user
  862. # configuration file. The user-config file is being looked for as
  863. # ~/.cpan/CPAN/MyConfig.pm.
  864. EOF
  865. $msg ||= "\n";
  866. my($fh) = FileHandle->new;
  867. rename $configpm, "$configpm~" if -f $configpm;
  868. open $fh, ">$configpm" or warn "Couldn't open >$configpm: $!";
  869. $fh->print(qq[$msg\$CPAN::Config = \{\n]);
  870. foreach (sort keys %$CPAN::Config) {
  871. $fh->print(
  872. " '$_' => ",
  873. ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
  874. ",\n"
  875. );
  876. }
  877. $fh->print("};\n1;\n__END__\n");
  878. close $fh;
  879. #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
  880. #chmod $mode, $configpm;
  881. ###why was that so? $self->defaults;
  882. $CPAN::Frontend->myprint("commit: wrote $configpm\n");
  883. 1;
  884. }
  885. *default = \&defaults;
  886. #-> sub CPAN::Config::defaults ;
  887. sub defaults {
  888. my($self) = @_;
  889. $self->unload;
  890. $self->load;
  891. 1;
  892. }
  893. sub init {
  894. my($self) = @_;
  895. undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
  896. # have the least
  897. # important
  898. # variable
  899. # undefined
  900. $self->load;
  901. 1;
  902. }
  903. #-> sub CPAN::Config::load ;
  904. sub load {
  905. my($self) = shift;
  906. my(@miss);
  907. use Carp;
  908. eval {require CPAN::Config;}; # We eval because of some
  909. # MakeMaker problems
  910. unless ($dot_cpan++){
  911. unshift @INC, MM->catdir($ENV{HOME},".cpan");
  912. eval {require CPAN::MyConfig;}; # where you can override
  913. # system wide settings
  914. shift @INC;
  915. }
  916. return unless @miss = $self->not_loaded;
  917. # XXX better check for arrayrefs too
  918. require CPAN::FirstTime;
  919. my($configpm,$fh,$redo,$theycalled);
  920. $redo ||= "";
  921. $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
  922. if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
  923. $configpm = $INC{"CPAN/Config.pm"};
  924. $redo++;
  925. } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
  926. $configpm = $INC{"CPAN/MyConfig.pm"};
  927. $redo++;
  928. } else {
  929. my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
  930. my($configpmdir) = MM->catdir($path_to_cpan,"CPAN");
  931. my($configpmtest) = MM->catfile($configpmdir,"Config.pm");
  932. if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
  933. if (-w $configpmtest) {
  934. $configpm = $configpmtest;
  935. } elsif (-w $configpmdir) {
  936. #_#_# following code dumped core on me with 5.003_11, a.k.
  937. unlink "$configpmtest.bak" if -f "$configpmtest.bak";
  938. rename $configpmtest, "$configpmtest.bak" if -f $configpmtest;
  939. my $fh = FileHandle->new;
  940. if ($fh->open(">$configpmtest")) {
  941. $fh->print("1;\n");
  942. $configpm = $configpmtest;
  943. } else {
  944. # Should never happen
  945. Carp::confess("Cannot open >$configpmtest");
  946. }
  947. }
  948. }
  949. unless ($configpm) {
  950. $configpmdir = MM->catdir($ENV{HOME},".cpan","CPAN");
  951. File::Path::mkpath($configpmdir);
  952. $configpmtest = MM->catfile($configpmdir,"MyConfig.pm");
  953. if (-w $configpmtest) {
  954. $configpm = $configpmtest;
  955. } elsif (-w $configpmdir) {
  956. #_#_# following code dumped core on me with 5.003_11, a.k.
  957. my $fh = FileHandle->new;
  958. if ($fh->open(">$configpmtest")) {
  959. $fh->print("1;\n");
  960. $configpm = $configpmtest;
  961. } else {
  962. # Should never happen
  963. Carp::confess("Cannot open >$configpmtest");
  964. }
  965. } else {
  966. Carp::confess(qq{WARNING: CPAN.pm is unable to }.
  967. qq{create a configuration file.});
  968. }
  969. }
  970. }
  971. local($") = ", ";
  972. $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
  973. We have to reconfigure CPAN.pm due to following uninitialized parameters:
  974. @miss
  975. END
  976. $CPAN::Frontend->myprint(qq{
  977. $configpm initialized.
  978. });
  979. sleep 2;
  980. CPAN::FirstTime::init($configpm);
  981. }
  982. #-> sub CPAN::Config::not_loaded ;
  983. sub not_loaded {
  984. my(@miss);
  985. for (qw(
  986. cpan_home keep_source_where build_dir build_cache scan_cache
  987. index_expire gzip tar unzip make pager makepl_arg make_arg
  988. make_install_arg urllist inhibit_startup_message
  989. ftp_proxy http_proxy no_proxy prerequisites_policy
  990. )) {
  991. push @miss, $_ unless defined $CPAN::Config->{$_};
  992. }
  993. return @miss;
  994. }
  995. #-> sub CPAN::Config::unload ;
  996. sub unload {
  997. delete $INC{'CPAN/MyConfig.pm'};
  998. delete $INC{'CPAN/Config.pm'};
  999. }
  1000. #-> sub CPAN::Config::help ;
  1001. sub help {
  1002. $CPAN::Frontend->myprint(q[
  1003. Known options:
  1004. defaults reload default config values from disk
  1005. commit commit session changes to disk
  1006. init go through a dialog to set all parameters
  1007. You may edit key values in the follow fashion:
  1008. o conf build_cache 15
  1009. o conf build_dir "/foo/bar"
  1010. o conf urllist shift
  1011. o conf urllist unshift ftp://ftp.foo.bar/
  1012. ]);
  1013. undef; #don't reprint CPAN::Config
  1014. }
  1015. #-> sub CPAN::Config::cpl ;
  1016. sub cpl {
  1017. my($word,$line,$pos) = @_;
  1018. $word ||= "";
  1019. CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1020. my(@words) = split " ", substr($line,0,$pos+1);
  1021. if (
  1022. defined($words[2])
  1023. and
  1024. (
  1025. $words[2] =~ /list$/ && @words == 3
  1026. ||
  1027. $words[2] =~ /list$/ && @words == 4 && length($word)
  1028. )
  1029. ) {
  1030. return grep /^\Q$word\E/, qw(splice shift unshift pop push);
  1031. } elsif (@words >= 4) {
  1032. return ();
  1033. }
  1034. my(@o_conf) = (keys %CPAN::Config::can, keys %$CPAN::Config);
  1035. return grep /^\Q$word\E/, @o_conf;
  1036. }
  1037. package CPAN::Shell;
  1038. #-> sub CPAN::Shell::h ;
  1039. sub h {
  1040. my($class,$about) = @_;
  1041. if (defined $about) {
  1042. $CPAN::Frontend->myprint("Detailed help not yet implemented\n");
  1043. } else {
  1044. $CPAN::Frontend->myprint(q{
  1045. command arguments description
  1046. a string authors
  1047. b or display bundles
  1048. d /regex/ info distributions
  1049. m or about modules
  1050. i none anything of above
  1051. r as reinstall recommendations
  1052. u above uninstalled distributions
  1053. See manpage for autobundle, recompile, force, look, etc.
  1054. make make
  1055. test modules, make test (implies make)
  1056. install dists, bundles, make install (implies test)
  1057. clean "r" or "u" make clean
  1058. readme display the README file
  1059. reload index|cpan load most recent indices/CPAN.pm
  1060. h or ? display this menu
  1061. o various set and query options
  1062. ! perl-code eval a perl command
  1063. q quit the shell subroutine
  1064. });
  1065. }
  1066. }
  1067. *help = \&h;
  1068. #-> sub CPAN::Shell::a ;
  1069. sub a { $CPAN::Frontend->myprint(shift->format_result('Author',@_));}
  1070. #-> sub CPAN::Shell::b ;
  1071. sub b {
  1072. my($self,@which) = @_;
  1073. CPAN->debug("which[@which]") if $CPAN::DEBUG;
  1074. my($incdir,$bdir,$dh);
  1075. foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  1076. $bdir = MM->catdir($incdir,"Bundle");
  1077. if ($dh = DirHandle->new($bdir)) { # may fail
  1078. my($entry);
  1079. for $entry ($dh->read) {
  1080. next if -d MM->catdir($bdir,$entry);
  1081. next unless $entry =~ s/\.pm$//;
  1082. $CPAN::META->instance('CPAN::Bundle',"Bundle::$entry");
  1083. }
  1084. }
  1085. }
  1086. $CPAN::Frontend->myprint($self->format_result('Bundle',@which));
  1087. }
  1088. #-> sub CPAN::Shell::d ;
  1089. sub d { $CPAN::Frontend->myprint(shift->format_result('Distribution',@_));}
  1090. #-> sub CPAN::Shell::m ;
  1091. sub m { # emacs confused here }; sub mimimimimi { # emacs in sync here
  1092. $CPAN::Frontend->myprint(shift->format_result('Module',@_));
  1093. }
  1094. #-> sub CPAN::Shell::i ;
  1095. sub i {
  1096. my($self) = shift;
  1097. my(@args) = @_;
  1098. my(@type,$type,@m);
  1099. @type = qw/Author Bundle Distribution Module/;
  1100. @args = '/./' unless @args;
  1101. my(@result);
  1102. for $type (@type) {
  1103. push @result, $self->expand($type,@args);
  1104. }
  1105. my $result = @result == 1 ?
  1106. $result[0]->as_string :
  1107. join "", map {$_->as_glimpse} @result;
  1108. $result ||= "No objects found of any type for argument @args\n";
  1109. $CPAN::Frontend->myprint($result);
  1110. }
  1111. #-> sub CPAN::Shell::o ;
  1112. sub o {
  1113. my($self,$o_type,@o_what) = @_;
  1114. $o_type ||= "";
  1115. CPAN->debug("o_type[$o_type] o_what[".join(" | ",@o_what)."]\n");
  1116. if ($o_type eq 'conf') {
  1117. shift @o_what if @o_what && $o_what[0] eq 'help';
  1118. if (!@o_what) {
  1119. my($k,$v);
  1120. $CPAN::Frontend->myprint("CPAN::Config options");
  1121. if (exists $INC{'CPAN/Config.pm'}) {
  1122. $CPAN::Frontend->myprint(" from $INC{'CPAN/Config.pm'}");
  1123. }
  1124. if (exists $INC{'CPAN/MyConfig.pm'}) {
  1125. $CPAN::Frontend->myprint(" and $INC{'CPAN/MyConfig.pm'}");
  1126. }
  1127. $CPAN::Frontend->myprint(":\n");
  1128. for $k (sort keys %CPAN::Config::can) {
  1129. $v = $CPAN::Config::can{$k};
  1130. $CPAN::Frontend->myprint(sprintf " %-18s %s\n", $k, $v);
  1131. }
  1132. $CPAN::Frontend->myprint("\n");
  1133. for $k (sort keys %$CPAN::Config) {
  1134. $v = $CPAN::Config->{$k};
  1135. if (ref $v) {
  1136. $CPAN::Frontend->myprint(
  1137. join(
  1138. "",
  1139. sprintf(
  1140. " %-18s\n",
  1141. $k
  1142. ),
  1143. map {"\t$_\n"} @{$v}
  1144. )
  1145. );
  1146. } else {
  1147. $CPAN::Frontend->myprint(sprintf " %-18s %s\n", $k, $v);
  1148. }
  1149. }
  1150. $CPAN::Frontend->myprint("\n");
  1151. } elsif (!CPAN::Config->edit(@o_what)) {
  1152. $CPAN::Frontend->myprint(qq[Type 'o conf' to view configuration edit options\n\n]);
  1153. }
  1154. } elsif ($o_type eq 'debug') {
  1155. my(%valid);
  1156. @o_what = () if defined $o_what[0] && $o_what[0] =~ /help/i;
  1157. if (@o_what) {
  1158. while (@o_what) {
  1159. my($what) = shift @o_what;
  1160. if ( exists $CPAN::DEBUG{$what} ) {
  1161. $CPAN::DEBUG |= $CPAN::DEBUG{$what};
  1162. } elsif ($what =~ /^\d/) {
  1163. $CPAN::DEBUG = $what;
  1164. } elsif (lc $what eq 'all') {
  1165. my($max) = 0;
  1166. for (values %CPAN::DEBUG) {
  1167. $max += $_;
  1168. }
  1169. $CPAN::DEBUG = $max;
  1170. } else {
  1171. my($known) = 0;
  1172. for (keys %CPAN::DEBUG) {
  1173. next unless lc($_) eq lc($what);
  1174. $CPAN::DEBUG |= $CPAN::DEBUG{$_};
  1175. $known = 1;
  1176. }
  1177. $CPAN::Frontend->myprint("unknown argument [$what]\n")
  1178. unless $known;
  1179. }
  1180. }
  1181. } else {
  1182. $CPAN::Frontend->myprint("Valid options for debug are ".
  1183. join(", ",sort(keys %CPAN::DEBUG), 'all').
  1184. qq{ or a number. Completion works on the options. }.
  1185. qq{Case is ignored.\n\n});
  1186. }
  1187. if ($CPAN::DEBUG) {
  1188. $CPAN::Frontend->myprint("Options set for debugging:\n");
  1189. my($k,$v);
  1190. for $k (sort {$CPAN::DEBUG{$a} <=> $CPAN::DEBUG{$b}} keys %CPAN::DEBUG) {
  1191. $v = $CPAN::DEBUG{$k};
  1192. $CPAN::Frontend->myprint(sprintf " %-14s(%s)\n", $k, $v) if $v & $CPAN::DEBUG;
  1193. }
  1194. } else {
  1195. $CPAN::Frontend->myprint("Debugging turned off completely.\n");
  1196. }
  1197. } else {
  1198. $CPAN::Frontend->myprint(qq{
  1199. Known options:
  1200. conf set or get configuration variables
  1201. debug set or get debugging options
  1202. });
  1203. }
  1204. }
  1205. sub dotdot_onreload {
  1206. my($ref) = shift;
  1207. sub {
  1208. if ( $_[0] =~ /Subroutine (\w+) redefined/ ) {
  1209. my($subr) = $1;
  1210. ++$$ref;
  1211. local($|) = 1;
  1212. # $CPAN::Frontend->myprint(".($subr)");
  1213. $CPAN::Frontend->myprint(".");
  1214. return;
  1215. }
  1216. warn @_;
  1217. };
  1218. }
  1219. #-> sub CPAN::Shell::reload ;
  1220. sub reload {
  1221. my($self,$command,@arg) = @_;
  1222. $command ||= "";
  1223. $self->debug("self[$self]command[$command]arg[@arg]") if $CPAN::DEBUG;
  1224. if ($command =~ /cpan/i) {
  1225. CPAN->debug("reloading the whole CPAN.pm") if $CPAN::DEBUG;
  1226. my $fh = FileHandle->new($INC{'CPAN.pm'});
  1227. local($/);
  1228. $redef = 0;
  1229. local($SIG{__WARN__}) = dotdot_onreload(\$redef);
  1230. eval <$fh>;
  1231. warn $@ if $@;
  1232. $CPAN::Frontend->myprint("\n$redef subroutines redefined\n");
  1233. } elsif ($command =~ /index/) {
  1234. CPAN::Index->force_reload;
  1235. } else {
  1236. $CPAN::Frontend->myprint(qq{cpan re-evals the CPAN.pm file
  1237. index re-reads the index files\n});
  1238. }
  1239. }
  1240. #-> sub CPAN::Shell::_binary_extensions ;
  1241. sub _binary_extensions {
  1242. my($self) = shift @_;
  1243. my(@result,$module,%seen,%need,$headerdone);
  1244. my $isaperl = q{perl5[._-]\\d{3}(_[0-4][0-9])?\\.tar[._-]gz$};
  1245. for $module ($self->expand('Module','/./')) {
  1246. my $file = $module->cpan_file;
  1247. next if $file eq "N/A";
  1248. next if $file =~ /^Contact Author/;
  1249. next if $file =~ / $isaperl /xo;
  1250. next unless $module->xs_file;
  1251. local($|) = 1;
  1252. $CPAN::Frontend->myprint(".");
  1253. push @result, $module;
  1254. }
  1255. # print join " | ", @result;
  1256. $CPAN::Frontend->myprint("\n");
  1257. return @result;
  1258. }
  1259. #-> sub CPAN::Shell::recompile ;
  1260. sub recompile {
  1261. my($self) = shift @_;
  1262. my($module,@module,$cpan_file,%dist);
  1263. @module = $self->_binary_extensions();
  1264. for $module (@module){ # we force now and compile later, so we
  1265. # don't do it twice
  1266. $cpan_file = $module->cpan_file;
  1267. my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1268. $pack->force;
  1269. $dist{$cpan_file}++;
  1270. }
  1271. for $cpan_file (sort keys %dist) {
  1272. $CPAN::Frontend->myprint(" CPAN: Recompiling $cpan_file\n\n");
  1273. my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1274. $pack->install;
  1275. $CPAN::Signal = 0; # it's tempting to reset Signal, so we can
  1276. # stop a package from recompiling,
  1277. # e.g. IO-1.12 when we have perl5.003_10
  1278. }
  1279. }
  1280. #-> sub CPAN::Shell::_u_r_common ;
  1281. sub _u_r_common {
  1282. my($self) = shift @_;
  1283. my($what) = shift @_;
  1284. CPAN->debug("self[$self] what[$what] args[@_]") if $CPAN::DEBUG;
  1285. Carp::croak "Usage: \$obj->_u_r_common($what)" unless defined $what;
  1286. Carp::croak "Usage: \$obj->_u_r_common(a|r|u)" unless $what =~ /^[aru]$/;
  1287. my(@args) = @_;
  1288. @args = '/./' unless @args;
  1289. my(@result,$module,%seen,%need,$headerdone,
  1290. $version_undefs,$version_zeroes);
  1291. $version_undefs = $version_zeroes = 0;
  1292. my $sprintf = "%-25s %9s %9s %s\n";
  1293. for $module ($self->expand('Module',@args)) {
  1294. my $file = $module->cpan_file;
  1295. next unless defined $file; # ??
  1296. my($latest) = $module->cpan_version;
  1297. my($inst_file) = $module->inst_file;
  1298. my($have);
  1299. return if $CPAN::Signal;
  1300. if ($inst_file){
  1301. if ($what eq "a") {
  1302. $have = $module->inst_version;
  1303. } elsif ($what eq "r") {
  1304. $have = $module->inst_version;
  1305. local($^W) = 0;
  1306. if ($have eq "undef"){
  1307. $version_undefs++;
  1308. } elsif ($have == 0){
  1309. $version_zeroes++;
  1310. }
  1311. next if $have >= $latest;
  1312. # to be pedantic we should probably say:
  1313. # && !($have eq "undef" && $latest ne "undef" && $latest gt "");
  1314. # to catch the case where CPAN has a version 0 and we have a version undef
  1315. } elsif ($what eq "u") {
  1316. next;
  1317. }
  1318. } else {
  1319. if ($what eq "a") {
  1320. next;
  1321. } elsif ($what eq "r") {
  1322. next;
  1323. } elsif ($what eq "u") {
  1324. $have = "-";
  1325. }
  1326. }
  1327. return if $CPAN::Signal; # this is sometimes lengthy
  1328. $seen{$file} ||= 0;
  1329. if ($what eq "a") {
  1330. push @result, sprintf "%s %s\n", $module->id, $have;
  1331. } elsif ($what eq "r") {
  1332. push @result, $module->id;
  1333. next if $seen{$file}++;
  1334. } elsif ($what eq "u") {
  1335. push @result, $module->id;
  1336. next if $seen{$file}++;
  1337. next if $file =~ /^Contact/;
  1338. }
  1339. unless ($headerdone++){
  1340. $CPAN::Frontend->myprint("\n");
  1341. $CPAN::Frontend->myprint(sprintf(
  1342. $sprintf,
  1343. "Package namespace",
  1344. "installed",
  1345. "latest",
  1346. "in CPAN file"
  1347. ));
  1348. }
  1349. $latest = substr($latest,0,8) if length($latest) > 8;
  1350. $have = substr($have,0,8) if length($have) > 8;
  1351. $CPAN::Frontend->myprint(sprintf $sprintf, $module->id, $have, $latest, $file);
  1352. $need{$module->id}++;
  1353. }
  1354. unless (%need) {
  1355. if ($what eq "u") {
  1356. $CPAN::Frontend->myprint("No modules found for @args\n");
  1357. } elsif ($what eq "r") {
  1358. $CPAN::Frontend->myprint("All modules are up to date for @args\n");
  1359. }
  1360. }
  1361. if ($what eq "r") {
  1362. if ($version_zeroes) {
  1363. my $s_has = $version_zeroes > 1 ? "s have" : " has";
  1364. $CPAN::Frontend->myprint(qq{$version_zeroes installed module$s_has }.
  1365. qq{a version number of 0\n});
  1366. }
  1367. if ($version_undefs) {
  1368. my $s_has = $version_undefs > 1 ? "s have" : " has";
  1369. $CPAN::Frontend->myprint(qq{$version_undefs installed module$s_has no }.
  1370. qq{parseable version number\n});
  1371. }
  1372. }
  1373. @result;
  1374. }
  1375. #-> sub CPAN::Shell::r ;
  1376. sub r {
  1377. shift->_u_r_common("r",@_);
  1378. }
  1379. #-> sub CPAN::Shell::u ;
  1380. sub u {
  1381. shift->_u_r_common("u",@_);
  1382. }
  1383. #-> sub CPAN::Shell::autobundle ;
  1384. sub autobundle {
  1385. my($self) = shift;
  1386. CPAN::Config->load unless $CPAN::Config_loaded++;
  1387. my(@bundle) = $self->_u_r_common("a",@_);
  1388. my($todir) = MM->catdir($CPAN::Config->{'cpan_home'},"Bundle");
  1389. File::Path::mkpath($todir);
  1390. unless (-d $todir) {
  1391. $CPAN::Frontend->myprint("Couldn't mkdir $todir for some reason\n");
  1392. return;
  1393. }
  1394. my($y,$m,$d) = (localtime)[5,4,3];
  1395. $y+=1900;
  1396. $m++;
  1397. my($c) = 0;
  1398. my($me) = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, $c;
  1399. my($to) = MM->catfile($todir,"$me.pm");
  1400. while (-f $to) {
  1401. $me = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, ++$c;
  1402. $to = MM->catfile($todir,"$me.pm");
  1403. }
  1404. my($fh) = FileHandle->new(">$to") or Carp::croak "Can't open >$to: $!";
  1405. $fh->print(
  1406. "package Bundle::$me;\n\n",
  1407. "\$VERSION = '0.01';\n\n",
  1408. "1;\n\n",
  1409. "__END__\n\n",
  1410. "=head1 NAME\n\n",
  1411. "Bundle::$me - Snapshot of installation on ",
  1412. $Config::Config{'myhostname'},
  1413. " on ",
  1414. scalar(localtime),
  1415. "\n\n=head1 SYNOPSIS\n\n",
  1416. "perl -MCPAN -e 'install Bundle::$me'\n\n",
  1417. "=head1 CONTENTS\n\n",
  1418. join("\n", @bundle),
  1419. "\n\n=head1 CONFIGURATION\n\n",
  1420. Config->myconfig,
  1421. "\n\n=head1 AUTHOR\n\n",
  1422. "This Bundle has been generated automatically ",
  1423. "by the autobundle routine in CPAN.pm.\n",
  1424. );
  1425. $fh->close;
  1426. $CPAN::Frontend->myprint("\nWrote bundle file
  1427. $to\n\n");
  1428. }
  1429. #-> sub CPAN::Shell::expand ;
  1430. sub expand {
  1431. shift;
  1432. my($type,@args) = @_;
  1433. my($arg,@m);
  1434. for $arg (@args) {
  1435. my $regex;
  1436. if ($arg =~ m|^/(.*)/$|) {
  1437. $regex = $1;
  1438. }
  1439. my $class = "CPAN::$type";
  1440. my $obj;
  1441. if (defined $regex) {
  1442. for $obj ( sort {$a->id cmp $b->id} $CPAN::META->all_objects($class)) {
  1443. push @m, $obj
  1444. if
  1445. $obj->id =~ /$regex/i
  1446. or
  1447. (
  1448. (
  1449. $] < 5.00303 ### provide sort of compatibility with 5.003
  1450. ||
  1451. $obj->can('name')
  1452. )
  1453. &&
  1454. $obj->name =~ /$regex/i
  1455. );
  1456. }
  1457. } else {
  1458. my($xarg) = $arg;
  1459. if ( $type eq 'Bundle' ) {
  1460. $xarg =~ s/^(Bundle::)?(.*)/Bundle::$2/;
  1461. }
  1462. if ($CPAN::META->exists($class,$xarg)) {
  1463. $obj = $CPAN::META->instance($class,$xarg);
  1464. } elsif ($CPAN::META->exists($class,$arg)) {
  1465. $obj = $CPAN::META->instance($class,$arg);
  1466. } else {
  1467. next;
  1468. }
  1469. push @m, $obj;
  1470. }
  1471. }
  1472. return wantarray ? @m : $m[0];
  1473. }
  1474. #-> sub CPAN::Shell::format_result ;
  1475. sub format_result {
  1476. my($self) = shift;
  1477. my($type,@args) = @_;
  1478. @args = '/./' unless @args;
  1479. my(@result) = $self->expand($type,@args);
  1480. my $result = @result == 1 ?
  1481. $result[0]->as_string :
  1482. join "", map {$_->as_glimpse} @result;
  1483. $result ||= "No objects of type $type found for argument @args\n";
  1484. $result;
  1485. }
  1486. # The only reason for this method is currently to have a reliable
  1487. # debugging utility that reveals which output is going through which
  1488. # channel. No, I don't like the colors ;-)
  1489. sub print_ornamented {
  1490. my($self,$what,$ornament) = @_;
  1491. my $longest = 0;
  1492. my $ornamenting = 0; # turn the colors on
  1493. if ($ornamenting) {
  1494. unless (defined &color) {
  1495. if ($CPAN::META->has_inst("Term::ANSIColor")) {
  1496. import Term::ANSIColor "color";
  1497. } else {
  1498. *color = sub { return "" };
  1499. }
  1500. }
  1501. my $line;
  1502. for $line (split /\n/, $what) {
  1503. $longest = length($line) if length($line) > $longest;
  1504. }
  1505. my $sprintf = "%-" . $longest . "s";
  1506. while ($what){
  1507. $what =~ s/(.*\n?)//m;
  1508. my $line = $1;
  1509. last unless $line;
  1510. my($nl) = chomp $line ? "\n" : "";
  1511. # print "line[$line]ornament[$ornament]sprintf[$sprintf]\n";
  1512. print color($ornament), sprintf($sprintf,$line), color("reset"), $nl;
  1513. }
  1514. } else {
  1515. print $what;
  1516. }
  1517. }
  1518. sub myprint {
  1519. my($self,$what) = @_;
  1520. $self->print_ornamented($what, 'bold blue on_yellow');
  1521. }
  1522. sub myexit {
  1523. my($self,$what) = @_;
  1524. $self->myprint($what);
  1525. exit;
  1526. }
  1527. sub mywarn {
  1528. my($self,$what) = @_;
  1529. $self->print_ornamented($what, 'bold red on_yellow');
  1530. }
  1531. sub myconfess {
  1532. my($self,$what) = @_;
  1533. $self->print_ornamented($what, 'bold red on_white');
  1534. Carp::confess "died";
  1535. }
  1536. sub mydie {
  1537. my($self,$what) = @_;
  1538. $self->print_ornamented($what, 'bold red on_white');
  1539. die "\n";
  1540. }
  1541. #-> sub CPAN::Shell::rematein ;
  1542. # RE-adme||MA-ke||TE-st||IN-stall
  1543. sub rematein {
  1544. shift;
  1545. my($meth,@some) = @_;
  1546. my $pragma = "";
  1547. if ($meth eq 'force') {
  1548. $pragma = $meth;
  1549. $meth = shift @some;
  1550. }
  1551. CPAN->debug("pragma[$pragma]meth[$meth] some[@some]") if $CPAN::DEBUG;
  1552. my($s,@s);
  1553. foreach $s (@some) {
  1554. CPAN::Queue->new($s);
  1555. }
  1556. while ($s = CPAN::Queue->first) {
  1557. my $obj;
  1558. if (ref $s) {
  1559. $obj = $s;
  1560. } elsif ($s =~ m|/|) { # looks like a file
  1561. $obj = $CPAN::META->instance('CPAN::Distribution',$s);
  1562. } elsif ($s =~ m|^Bundle::|) {
  1563. $obj = $CPAN::META->instance('CPAN::Bundle',$s);
  1564. } else {
  1565. $obj = $CPAN::META->instance('CPAN::Module',$s)
  1566. if $CPAN::META->exists('CPAN::Module',$s);
  1567. }
  1568. if (ref $obj) {
  1569. CPAN->debug(
  1570. qq{pragma[$pragma]meth[$meth]obj[$obj]as_string\[}.
  1571. $obj->as_string.
  1572. qq{\]}
  1573. ) if $CPAN::DEBUG;
  1574. $obj->$pragma()
  1575. if
  1576. $pragma
  1577. &&
  1578. ($] < 5.00303 || $obj->can($pragma)); ###
  1579. ### compatibility
  1580. ### with
  1581. ### 5.003
  1582. if ($]>=5.00303 && $obj->can('called_for')) {
  1583. $obj->called_for($s);
  1584. }
  1585. CPAN::Queue->delete($s) if $obj->$meth(); # if it is more
  1586. # than once in
  1587. # the queue
  1588. } elsif ($CPAN::META->exists('CPAN::Author',$s)) {
  1589. $obj = $CPAN::META->instance('CPAN::Author',$s);
  1590. $CPAN::Frontend->myprint(
  1591. join "",
  1592. "Don't be silly, you can't $meth ",
  1593. $obj->fullname,
  1594. " ;-)\n"
  1595. );
  1596. } else {
  1597. $CPAN::Frontend
  1598. ->myprint(qq{Warning: Cannot $meth $s, }.
  1599. qq{don\'t know what it is.
  1600. Try the command
  1601. i /$s/
  1602. to find objects with similar identifiers.
  1603. });
  1604. }
  1605. CPAN::Queue->delete_first($s);
  1606. }
  1607. }
  1608. #-> sub CPAN::Shell::force ;
  1609. sub force { shift->rematein('force',@_); }
  1610. #-> sub CPAN::Shell::get ;
  1611. sub get { shift->rematein('get',@_); }
  1612. #-> sub CPAN::Shell::readme ;
  1613. sub readme { shift->rematein('readme',@_); }
  1614. #-> sub CPAN::Shell::make ;
  1615. sub make { shift->rematein('make',@_); }
  1616. #-> sub CPAN::Shell::test ;
  1617. sub test { shift->rematein('test',@_); }
  1618. #-> sub CPAN::Shell::install ;
  1619. sub install { shift->rematein('install',@_); }
  1620. #-> sub CPAN::Shell::clean ;
  1621. sub clean { shift->rematein('clean',@_); }
  1622. #-> sub CPAN::Shell::look ;
  1623. sub look { shift->rematein('look',@_); }
  1624. package CPAN::FTP;
  1625. #-> sub CPAN::FTP::ftp_get ;
  1626. sub ftp_get {
  1627. my($class,$host,$dir,$file,$target) = @_;
  1628. $class->debug(
  1629. qq[Going to fetch file [$file] from dir [$dir]
  1630. on host [$host] as local [$target]\n]
  1631. ) if $CPAN::DEBUG;
  1632. my $ftp = Net::FTP->new($host);
  1633. return 0 unless defined $ftp;
  1634. $ftp->debug(1) if $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG;
  1635. $class->debug(qq[Going to ->login("anonymous","$Config::Config{'cf_email'}")\n]);
  1636. unless ( $ftp->login("anonymous",$Config::Config{'cf_email'}) ){
  1637. warn "Couldn't login on $host";
  1638. return;
  1639. }
  1640. unless ( $ftp->cwd($dir) ){
  1641. warn "Couldn't cwd $dir";
  1642. return;
  1643. }
  1644. $ftp->binary;
  1645. $class->debug(qq[Going to ->get("$file","$target")\n]) if $CPAN::DEBUG;
  1646. unless ( $ftp->get($file,$target) ){
  1647. warn "Couldn't fetch $file from $host\n";
  1648. return;
  1649. }
  1650. $ftp->quit; # it's ok if this fails
  1651. return 1;
  1652. }
  1653. # If more accuracy is wanted/needed, Chris Leach sent me this patch...
  1654. # leach,> *** /install/perl/live/lib/CPAN.pm- Wed Sep 24 13:08:48 1997
  1655. # leach,> --- /tmp/cp Wed Sep 24 13:26:40 1997
  1656. # leach,> ***************
  1657. # leach,> *** 1562,1567 ****
  1658. # leach,> --- 1562,1580 ----
  1659. # leach,> return 1 if substr($url,0,4) eq "file";
  1660. # leach,> return 1 unless $url =~ m|://([^/]+)|;
  1661. # leach,> my $host = $1;
  1662. # leach,> + my $proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
  1663. # leach,> + if ($proxy) {
  1664. # leach,> + $proxy =~ m|://([^/:]+)|;
  1665. # leach,> + $proxy = $1;
  1666. # leach,> + my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
  1667. # leach,> + if ($noproxy) {
  1668. # leach,> + if ($host !~ /$noproxy$/) {
  1669. # leach,> + $host = $proxy;
  1670. # leach,> + }
  1671. # leach,> + } else {
  1672. # leach,> + $host = $proxy;
  1673. # leach,> + }
  1674. # leach,> + }
  1675. # leach,> require Net::Ping;
  1676. # leach,> return 1 unless $Net::Ping::VERSION >= 2;
  1677. # leach,> my $p;
  1678. # this is quite optimistic and returns one on several occasions where
  1679. # inappropriate. But this does no harm. It would do harm if we were
  1680. # too pessimistic (as I was before the http_proxy
  1681. sub is_reachable {
  1682. my($self,$url) = @_;
  1683. return 1; # we can't simply roll our own, firewalls may break ping
  1684. return 0 unless $url;
  1685. return 1 if substr($url,0,4) eq "file";
  1686. return 1 unless $url =~ m|^(\w+)://([^/]+)|;
  1687. my $proxytype = $1 . "_proxy"; # ftp_proxy or http_proxy
  1688. my $host = $2;
  1689. return 1 if $CPAN::Config->{$proxytype} || $ENV{$proxytype};
  1690. require Net::Ping;
  1691. return 1 unless $Net::Ping::VERSION >= 2;
  1692. my $p;
  1693. # 1.3101 had it different: only if the first eval raised an
  1694. # exception we tried it with TCP. Now we are happy if icmp wins
  1695. # the order and return, we don't even check for $@. Thanks to
  1696. # [email protected] for the suggestion.
  1697. eval {$p = Net::Ping->new("icmp");};
  1698. return 1 if $p && ref($p) && $p->ping($host, 10);
  1699. eval {$p = Net::Ping->new("tcp");};
  1700. $CPAN::Frontend->mydie($@) if $@;
  1701. return $p->ping($host, 10);
  1702. }
  1703. #-> sub CPAN::FTP::localize ;
  1704. # sorry for the ugly code here, I'll clean it up as soon as Net::FTP
  1705. # is in the core
  1706. sub localize {
  1707. my($self,$file,$aslocal,$force) = @_;
  1708. $force ||= 0;
  1709. Carp::croak "Usage: ->localize(cpan_file,as_local_file[,$force])"
  1710. unless defined $aslocal;
  1711. $self->debug("file[$file] aslocal[$aslocal] force[$force]")
  1712. if $CPAN::DEBUG;
  1713. if ($^O eq 'MacOS') {
  1714. my($name, $path) = File::Basename::fileparse($aslocal, '');
  1715. if (length($name) > 31) {
  1716. $name =~ s/(\.(readme(\.(gz|Z))?|(tar\.)?(gz|Z)|tgz|zip|pm\.(gz|Z)))$//;
  1717. my $suf = $1;
  1718. my $size = 31 - length($suf);
  1719. while (length($name) > $size) {
  1720. chop $name;
  1721. }
  1722. $name .= $suf;
  1723. $aslocal = File::Spec->catfile($path, $name);
  1724. }
  1725. }
  1726. return $aslocal if -f $aslocal && -r _ && !($force & 1);
  1727. my($restore) = 0;
  1728. if (-f $aslocal){
  1729. rename $aslocal, "$aslocal.bak";
  1730. $restore++;
  1731. }
  1732. my($aslocal_dir) = File::Basename::dirname($aslocal);
  1733. File::Path::mkpath($aslocal_dir);
  1734. $CPAN::Frontend->mywarn(qq{Warning: You are not allowed to write into }.
  1735. qq{directory "$aslocal_dir".
  1736. I\'ll continue, but if you encounter problems, they may be due
  1737. to insufficient permissions.\n}) unless -w $aslocal_dir;
  1738. # Inheritance is not easier to manage than a few if/else branches
  1739. if ($CPAN::META->has_inst('LWP::UserAgent')) {
  1740. require LWP::UserAgent;
  1741. unless ($Ua) {
  1742. $Ua = LWP::UserAgent->new;
  1743. my($var);
  1744. $Ua->proxy('ftp', $var)
  1745. if $var = $CPAN::Config->{'ftp_proxy'} || $ENV{'ftp_proxy'};
  1746. $Ua->proxy('http', $var)
  1747. if $var = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
  1748. $Ua->no_proxy($var)
  1749. if $var = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
  1750. }
  1751. }
  1752. # Try the list of urls for each single object. We keep a record
  1753. # where we did get a file from
  1754. my(@reordered,$last);
  1755. $CPAN::Config->{urllist} ||= [];
  1756. $last = $#{$CPAN::Config->{urllist}};
  1757. if ($force & 2) { # local cpans probably out of date, don't reorder
  1758. @reordered = (0..$last);
  1759. } else {
  1760. @reordered =
  1761. sort {
  1762. (substr($CPAN::Config->{urllist}[$b],0,4) eq "file")
  1763. <=>
  1764. (substr($CPAN::Config->{urllist}[$a],0,4) eq "file")
  1765. or
  1766. defined($Thesite)
  1767. and
  1768. ($b == $Thesite)
  1769. <=>
  1770. ($a == $Thesite)
  1771. } 0..$last;
  1772. }
  1773. my($level,@levels);
  1774. if ($Themethod) {
  1775. @levels = ($Themethod, grep {$_ ne $Themethod} qw/easy hard hardest/);
  1776. } else {
  1777. @levels = qw/easy hard hardest/;
  1778. }
  1779. @levels = qw/easy/ if $^O eq 'MacOS';
  1780. for $level (@levels) {
  1781. my $method = "host$level";
  1782. my @host_seq = $level eq "easy" ?
  1783. @reordered : 0..$last; # reordered has CDROM up front
  1784. @host_seq = (0) unless @host_seq;
  1785. my $ret = $self->$method(\@host_seq,$file,$aslocal);
  1786. if ($ret) {
  1787. $Themethod = $level;
  1788. $self->debug("level[$level]") if $CPAN::DEBUG;
  1789. return $ret;
  1790. } else {
  1791. unlink $aslocal;
  1792. }
  1793. }
  1794. my(@mess);
  1795. push @mess,
  1796. qq{Please check, if the URLs I found in your configuration file \(}.
  1797. join(", ", @{$CPAN::Config->{urllist}}).
  1798. qq{\) are valid. The urllist can be edited.},
  1799. qq{E.g. with ``o conf urllist push ftp://myurl/''};
  1800. $CPAN::Frontend->myprint(Text::Wrap::wrap("","",@mess). "\n\n");
  1801. sleep 2;
  1802. $CPAN::Frontend->myprint("Cannot fetch $file\n\n");
  1803. if ($restore) {
  1804. rename "$aslocal.bak", $aslocal;
  1805. $CPAN::Frontend->myprint("Trying to get away with old file:\n" .
  1806. $self->ls($aslocal));
  1807. return $aslocal;
  1808. }
  1809. return;
  1810. }
  1811. sub hosteasy {
  1812. my($self,$host_seq,$file,$aslocal) = @_;
  1813. my($i);
  1814. HOSTEASY: for $i (@$host_seq) {
  1815. my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  1816. unless ($self->is_reachable($url)) {
  1817. $CPAN::Frontend->myprint("Skipping $url (seems to be not reachable)\n");
  1818. sleep 2;
  1819. next;
  1820. }
  1821. $url .= "/" unless substr($url,-1) eq "/";
  1822. $url .= $file;
  1823. $self->debug("localizing perlish[$url]") if $CPAN::DEBUG;
  1824. if ($url =~ /^file:/) {
  1825. my $l;
  1826. if ($CPAN::META->has_inst('LWP')) {
  1827. require URI::URL;
  1828. my $u = URI::URL->new($url);
  1829. $l = $u->path;
  1830. } else { # works only on Unix, is poorly constructed, but
  1831. # hopefully better than nothing.
  1832. # RFC 1738 says fileurl BNF is
  1833. # fileurl = "file://" [ host | "localhost" ] "/" fpath
  1834. # Thanks to "Mark D. Baushke" <[email protected]> for
  1835. # the code
  1836. ($l = $url) =~ s|^file://[^/]*/|/|; # discard the host part
  1837. $l =~ s|^file:||; # assume they
  1838. # meant
  1839. # file://localhost
  1840. $l =~ s|^/|| unless -f $l; # e.g. /P:
  1841. }
  1842. if ( -f $l && -r _) {
  1843. $Thesite = $i;
  1844. return $l;
  1845. }
  1846. # Maybe mirror has compressed it?
  1847. if (-f "$l.gz") {
  1848. $self->debug("found compressed $l.gz") if $CPAN::DEBUG;
  1849. CPAN::Tarzip->gunzip("$l.gz", $aslocal);
  1850. if ( -f $aslocal) {
  1851. $Thesite = $i;
  1852. return $aslocal;
  1853. }
  1854. }
  1855. }
  1856. if ($CPAN::META->has_inst('LWP')) {
  1857. $CPAN::Frontend->myprint("Fetching with LWP:
  1858. $url
  1859. ");
  1860. unless ($Ua) {
  1861. require LWP::UserAgent;
  1862. $Ua = LWP::UserAgent->new;
  1863. }
  1864. my $res = $Ua->mirror($url, $aslocal);
  1865. if ($res->is_success) {
  1866. $Thesite = $i;
  1867. return $aslocal;
  1868. } elsif ($url !~ /\.gz$/) {
  1869. my $gzurl = "$url.gz";
  1870. $CPAN::Frontend->myprint("Fetching with LWP:
  1871. $gzurl
  1872. ");
  1873. $res = $Ua->mirror($gzurl, "$aslocal.gz");
  1874. if ($res->is_success &&
  1875. CPAN::Tarzip->gunzip("$aslocal.gz",$aslocal)
  1876. ) {
  1877. $Thesite = $i;
  1878. return $aslocal;
  1879. } else {
  1880. # next HOSTEASY ;
  1881. }
  1882. } else {
  1883. # Alan Burlison informed me that in firewall envs Net::FTP
  1884. # can still succeed where LWP fails. So we do not skip
  1885. # Net::FTP anymore when LWP is available.
  1886. # next HOSTEASY ;
  1887. }
  1888. } else {
  1889. $self->debug("LWP not installed") if $CPAN::DEBUG;
  1890. }
  1891. if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  1892. # that's the nice and easy way thanks to Graham
  1893. my($host,$dir,$getfile) = ($1,$2,$3);
  1894. if ($CPAN::META->has_inst('Net::FTP')) {
  1895. $dir =~ s|/+|/|g;
  1896. $CPAN::Frontend->myprint("Fetching with Net::FTP:
  1897. $url
  1898. ");
  1899. $self->debug("getfile[$getfile]dir[$dir]host[$host]" .
  1900. "aslocal[$aslocal]") if $CPAN::DEBUG;
  1901. if (CPAN::FTP->ftp_get($host,$dir,$getfile,$aslocal)) {
  1902. $Thesite = $i;
  1903. return $aslocal;
  1904. }
  1905. if ($aslocal !~ /\.gz$/) {
  1906. my $gz = "$aslocal.gz";
  1907. $CPAN::Frontend->myprint("Fetching with Net::FTP
  1908. $url.gz
  1909. ");
  1910. if (CPAN::FTP->ftp_get($host,
  1911. $dir,
  1912. "$getfile.gz",
  1913. $gz) &&
  1914. CPAN::Tarzip->gunzip($gz,$aslocal)
  1915. ){
  1916. $Thesite = $i;
  1917. return $aslocal;
  1918. }
  1919. }
  1920. # next HOSTEASY;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. sub hosthard {
  1926. my($self,$host_seq,$file,$aslocal) = @_;
  1927. # Came back if Net::FTP couldn't establish connection (or
  1928. # failed otherwise) Maybe they are behind a firewall, but they
  1929. # gave us a socksified (or other) ftp program...
  1930. my($i);
  1931. my($devnull) = $CPAN::Config->{devnull} || "";
  1932. # < /dev/null ";
  1933. my($aslocal_dir) = File::Basename::dirname($aslocal);
  1934. File::Path::mkpath($aslocal_dir);
  1935. HOSTHARD: for $i (@$host_seq) {
  1936. my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  1937. unless ($self->is_reachable($url)) {
  1938. $CPAN::Frontend->myprint("Skipping $url (not reachable)\n");
  1939. next;
  1940. }
  1941. $url .= "/" unless substr($url,-1) eq "/";
  1942. $url .= $file;
  1943. my($proto,$host,$dir,$getfile);
  1944. # Courtesy Mark Conty [email protected] change from
  1945. # if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  1946. # to
  1947. if ($url =~ m|^([^:]+)://(.*?)/(.*)/(.*)|) {
  1948. # proto not yet used
  1949. ($proto,$host,$dir,$getfile) = ($1,$2,$3,$4);
  1950. } else {
  1951. next HOSTHARD; # who said, we could ftp anything except ftp?
  1952. }
  1953. $self->debug("localizing funkyftpwise[$url]") if $CPAN::DEBUG;
  1954. my($f,$funkyftp);
  1955. for $f ('lynx','ncftpget','ncftp') {
  1956. next unless exists $CPAN::Config->{$f};
  1957. $funkyftp = $CPAN::Config->{$f};
  1958. next unless defined $funkyftp;
  1959. next if $funkyftp =~ /^\s*$/;
  1960. my($want_compressed);
  1961. my $aslocal_uncompressed;
  1962. ($aslocal_uncompressed = $aslocal) =~ s/\.gz//;
  1963. my($source_switch) = "";
  1964. $source_switch = " -source" if $funkyftp =~ /\blynx$/;
  1965. $source_switch = " -c" if $funkyftp =~ /\bncftp$/;
  1966. $CPAN::Frontend->myprint(
  1967. qq[
  1968. Trying with "$funkyftp$source_switch" to get
  1969. $url
  1970. ]);
  1971. my($system) = "$funkyftp$source_switch '$url' $devnull > ".
  1972. "$aslocal_uncompressed";
  1973. $self->debug("system[$system]") if $CPAN::DEBUG;
  1974. my($wstatus);
  1975. if (($wstatus = system($system)) == 0
  1976. &&
  1977. -s $aslocal_uncompressed # lynx returns 0 on my
  1978. # system even if it fails
  1979. ) {
  1980. if ($aslocal_uncompressed ne $aslocal) {
  1981. # test gzip integrity
  1982. if (
  1983. CPAN::Tarzip->gtest($aslocal_uncompressed)
  1984. ) {
  1985. rename $aslocal_uncompressed, $aslocal;
  1986. } else {
  1987. CPAN::Tarzip->gzip($aslocal_uncompressed,
  1988. "$aslocal_uncompressed.gz");
  1989. }
  1990. }
  1991. $Thesite = $i;
  1992. return $aslocal;
  1993. } elsif ($url !~ /\.gz$/) {
  1994. unlink $aslocal_uncompressed if
  1995. -f $aslocal_uncompressed && -s _ == 0;
  1996. my $gz = "$aslocal.gz";
  1997. my $gzurl = "$url.gz";
  1998. $CPAN::Frontend->myprint(
  1999. qq[
  2000. Trying with "$funkyftp$source_switch" to get
  2001. $url.gz
  2002. ]);
  2003. my($system) = "$funkyftp$source_switch '$url.gz' $devnull > ".
  2004. "$aslocal_uncompressed.gz";
  2005. $self->debug("system[$system]") if $CPAN::DEBUG;
  2006. my($wstatus);
  2007. if (($wstatus = system($system)) == 0
  2008. &&
  2009. -s "$aslocal_uncompressed.gz"
  2010. ) {
  2011. # test gzip integrity
  2012. if (CPAN::Tarzip->gtest("$aslocal_uncompressed.gz")) {
  2013. CPAN::Tarzip->gunzip("$aslocal_uncompressed.gz",
  2014. $aslocal);
  2015. } else {
  2016. rename $aslocal_uncompressed, $aslocal;
  2017. }
  2018. $Thesite = $i;
  2019. return $aslocal;
  2020. } else {
  2021. unlink "$aslocal_uncompressed.gz" if
  2022. -f "$aslocal_uncompressed.gz";
  2023. }
  2024. } else {
  2025. my $estatus = $wstatus >> 8;
  2026. my $size = -f $aslocal ? ", left\n$aslocal with size ".-s _ : "";
  2027. $CPAN::Frontend->myprint(qq{
  2028. System call "$system"
  2029. returned status $estatus (wstat $wstatus)$size
  2030. });
  2031. }
  2032. }
  2033. }
  2034. }
  2035. sub hosthardest {
  2036. my($self,$host_seq,$file,$aslocal) = @_;
  2037. my($i);
  2038. my($aslocal_dir) = File::Basename::dirname($aslocal);
  2039. File::Path::mkpath($aslocal_dir);
  2040. HOSTHARDEST: for $i (@$host_seq) {
  2041. unless (length $CPAN::Config->{'ftp'}) {
  2042. $CPAN::Frontend->myprint("No external ftp command available\n\n");
  2043. last HOSTHARDEST;
  2044. }
  2045. my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2046. unless ($self->is_reachable($url)) {
  2047. $CPAN::Frontend->myprint("Skipping $url (not reachable)\n");
  2048. next;
  2049. }
  2050. $url .= "/" unless substr($url,-1) eq "/";
  2051. $url .= $file;
  2052. $self->debug("localizing ftpwise[$url]") if $CPAN::DEBUG;
  2053. unless ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2054. next;
  2055. }
  2056. my($host,$dir,$getfile) = ($1,$2,$3);
  2057. my($netrcfile,$fh);
  2058. my $timestamp = 0;
  2059. my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
  2060. $ctime,$blksize,$blocks) = stat($aslocal);
  2061. $timestamp = $mtime ||= 0;
  2062. my($netrc) = CPAN::FTP::netrc->new;
  2063. my($verbose) = $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG ? " -v" : "";
  2064. my $targetfile = File::Basename::basename($aslocal);
  2065. my(@dialog);
  2066. push(
  2067. @dialog,
  2068. "lcd $aslocal_dir",
  2069. "cd /",
  2070. map("cd $_", split "/", $dir), # RFC 1738
  2071. "bin",
  2072. "get $getfile $targetfile",
  2073. "quit"
  2074. );
  2075. if (! $netrc->netrc) {
  2076. CPAN->debug("No ~/.netrc file found") if $CPAN::DEBUG;
  2077. } elsif ($netrc->hasdefault || $netrc->contains($host)) {
  2078. CPAN->debug(sprintf("hasdef[%d]cont($host)[%d]",
  2079. $netrc->hasdefault,
  2080. $netrc->contains($host))) if $CPAN::DEBUG;
  2081. if ($netrc->protected) {
  2082. $CPAN::Frontend->myprint(qq{
  2083. Trying with external ftp to get
  2084. $url
  2085. As this requires some features that are not thoroughly tested, we\'re
  2086. not sure, that we get it right....
  2087. }
  2088. );
  2089. $self->talk_ftp("$CPAN::Config->{'ftp'}$verbose $host",
  2090. @dialog);
  2091. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2092. $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  2093. $mtime ||= 0;
  2094. if ($mtime > $timestamp) {
  2095. $CPAN::Frontend->myprint("GOT $aslocal\n");
  2096. $Thesite = $i;
  2097. return $aslocal;
  2098. } else {
  2099. $CPAN::Frontend->myprint("Hmm... Still failed!\n");
  2100. }
  2101. } else {
  2102. $CPAN::Frontend->mywarn(qq{Your $netrcfile is not }.
  2103. qq{correctly protected.\n});
  2104. }
  2105. } else {
  2106. $CPAN::Frontend->mywarn("Your ~/.netrc neither contains $host
  2107. nor does it have a default entry\n");
  2108. }
  2109. # OK, they don't have a valid ~/.netrc. Use 'ftp -n'
  2110. # then and login manually to host, using e-mail as
  2111. # password.
  2112. $CPAN::Frontend->myprint(qq{Issuing "$CPAN::Config->{'ftp'}$verbose -n"\n});
  2113. unshift(
  2114. @dialog,
  2115. "open $host",
  2116. "user anonymous $Config::Config{'cf_email'}"
  2117. );
  2118. $self->talk_ftp("$CPAN::Config->{'ftp'}$verbose -n", @dialog);
  2119. ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2120. $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  2121. $mtime ||= 0;
  2122. if ($mtime > $timestamp) {
  2123. $CPAN::Frontend->myprint("GOT $aslocal\n");
  2124. $Thesite = $i;
  2125. return $aslocal;
  2126. } else {
  2127. $CPAN::Frontend->myprint("Bad luck... Still failed!\n");
  2128. }
  2129. $CPAN::Frontend->myprint("Can't access URL $url.\n\n");
  2130. sleep 2;
  2131. }
  2132. }
  2133. sub talk_ftp {
  2134. my($self,$command,@dialog) = @_;
  2135. my $fh = FileHandle->new;
  2136. $fh->open("|$command") or die "Couldn't open ftp: $!";
  2137. foreach (@dialog) { $fh->print("$_\n") }
  2138. $fh->close; # Wait for process to complete
  2139. my $wstatus = $?;
  2140. my $estatus = $wstatus >> 8;
  2141. $CPAN::Frontend->myprint(qq{
  2142. Subprocess "|$command"
  2143. returned status $estatus (wstat $wstatus)
  2144. }) if $wstatus;
  2145. }
  2146. # find2perl needs modularization, too, all the following is stolen
  2147. # from there
  2148. # CPAN::FTP::ls
  2149. sub ls {
  2150. my($self,$name) = @_;
  2151. my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
  2152. $atime,$mtime,$ctime,$blksize,$blocks) = lstat($name);
  2153. my($perms,%user,%group);
  2154. my $pname = $name;
  2155. if ($blocks) {
  2156. $blocks = int(($blocks + 1) / 2);
  2157. }
  2158. else {
  2159. $blocks = int(($sizemm + 1023) / 1024);
  2160. }
  2161. if (-f _) { $perms = '-'; }
  2162. elsif (-d _) { $perms = 'd'; }
  2163. elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  2164. elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  2165. elsif (-p _) { $perms = 'p'; }
  2166. elsif (-S _) { $perms = 's'; }
  2167. else { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  2168. my(@rwx) = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  2169. my(@moname) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  2170. my $tmpmode = $mode;
  2171. my $tmp = $rwx[$tmpmode & 7];
  2172. $tmpmode >>= 3;
  2173. $tmp = $rwx[$tmpmode & 7] . $tmp;
  2174. $tmpmode >>= 3;
  2175. $tmp = $rwx[$tmpmode & 7] . $tmp;
  2176. substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  2177. substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  2178. substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  2179. $perms .= $tmp;
  2180. my $user = $user{$uid} || $uid; # too lazy to implement lookup
  2181. my $group = $group{$gid} || $gid;
  2182. my($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  2183. my($timeyear);
  2184. my($moname) = $moname[$mon];
  2185. if (-M _ > 365.25 / 2) {
  2186. $timeyear = $year + 1900;
  2187. }
  2188. else {
  2189. $timeyear = sprintf("%02d:%02d", $hour, $min);
  2190. }
  2191. sprintf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  2192. $ino,
  2193. $blocks,
  2194. $perms,
  2195. $nlink,
  2196. $user,
  2197. $group,
  2198. $sizemm,
  2199. $moname,
  2200. $mday,
  2201. $timeyear,
  2202. $pname;
  2203. }
  2204. package CPAN::FTP::netrc;
  2205. sub new {
  2206. my($class) = @_;
  2207. my $file = MM->catfile($ENV{HOME},".netrc");
  2208. my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2209. $atime,$mtime,$ctime,$blksize,$blocks)
  2210. = stat($file);
  2211. $mode ||= 0;
  2212. my $protected = 0;
  2213. my($fh,@machines,$hasdefault);
  2214. $hasdefault = 0;
  2215. $fh = FileHandle->new or die "Could not create a filehandle";
  2216. if($fh->open($file)){
  2217. $protected = ($mode & 077) == 0;
  2218. local($/) = "";
  2219. NETRC: while (<$fh>) {
  2220. my(@tokens) = split " ", $_;
  2221. TOKEN: while (@tokens) {
  2222. my($t) = shift @tokens;
  2223. if ($t eq "default"){
  2224. $hasdefault++;
  2225. last NETRC;
  2226. }
  2227. last TOKEN if $t eq "macdef";
  2228. if ($t eq "machine") {
  2229. push @machines, shift @tokens;
  2230. }
  2231. }
  2232. }
  2233. } else {
  2234. $file = $hasdefault = $protected = "";
  2235. }
  2236. bless {
  2237. 'mach' => [@machines],
  2238. 'netrc' => $file,
  2239. 'hasdefault' => $hasdefault,
  2240. 'protected' => $protected,
  2241. }, $class;
  2242. }
  2243. sub hasdefault { shift->{'hasdefault'} }
  2244. sub netrc { shift->{'netrc'} }
  2245. sub protected { shift->{'protected'} }
  2246. sub contains {
  2247. my($self,$mach) = @_;
  2248. for ( @{$self->{'mach'}} ) {
  2249. return 1 if $_ eq $mach;
  2250. }
  2251. return 0;
  2252. }
  2253. package CPAN::Complete;
  2254. sub gnu_cpl {
  2255. my($text, $line, $start, $end) = @_;
  2256. my(@perlret) = cpl($text, $line, $start);
  2257. # find longest common match. Can anybody show me how to peruse
  2258. # T::R::Gnu to have this done automatically? Seems expensive.
  2259. return () unless @perlret;
  2260. my($newtext) = $text;
  2261. for (my $i = length($text)+1;;$i++) {
  2262. last unless length($perlret[0]) && length($perlret[0]) >= $i;
  2263. my $try = substr($perlret[0],0,$i);
  2264. my @tries = grep {substr($_,0,$i) eq $try} @perlret;
  2265. # warn "try[$try]tries[@tries]";
  2266. if (@tries == @perlret) {
  2267. $newtext = $try;
  2268. } else {
  2269. last;
  2270. }
  2271. }
  2272. ($newtext,@perlret);
  2273. }
  2274. #-> sub CPAN::Complete::cpl ;
  2275. sub cpl {
  2276. my($word,$line,$pos) = @_;
  2277. $word ||= "";
  2278. $line ||= "";
  2279. $pos ||= 0;
  2280. CPAN->debug("word [$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2281. $line =~ s/^\s*//;
  2282. if ($line =~ s/^(force\s*)//) {
  2283. $pos -= length($1);
  2284. }
  2285. my @return;
  2286. if ($pos == 0) {
  2287. @return = grep(
  2288. /^$word/,
  2289. sort qw(
  2290. ! a b d h i m o q r u autobundle clean
  2291. make test install force reload look
  2292. )
  2293. );
  2294. } elsif ( $line !~ /^[\!abdhimorutl]/ ) {
  2295. @return = ();
  2296. } elsif ($line =~ /^a\s/) {
  2297. @return = cplx('CPAN::Author',$word);
  2298. } elsif ($line =~ /^b\s/) {
  2299. @return = cplx('CPAN::Bundle',$word);
  2300. } elsif ($line =~ /^d\s/) {
  2301. @return = cplx('CPAN::Distribution',$word);
  2302. } elsif ($line =~ /^([mru]|make|clean|test|install|readme|look)\s/ ) {
  2303. @return = (cplx('CPAN::Module',$word),cplx('CPAN::Bundle',$word));
  2304. } elsif ($line =~ /^i\s/) {
  2305. @return = cpl_any($word);
  2306. } elsif ($line =~ /^reload\s/) {
  2307. @return = cpl_reload($word,$line,$pos);
  2308. } elsif ($line =~ /^o\s/) {
  2309. @return = cpl_option($word,$line,$pos);
  2310. } else {
  2311. @return = ();
  2312. }
  2313. return @return;
  2314. }
  2315. #-> sub CPAN::Complete::cplx ;
  2316. sub cplx {
  2317. my($class, $word) = @_;
  2318. grep /^\Q$word\E/, map { $_->id } $CPAN::META->all_objects($class);
  2319. }
  2320. #-> sub CPAN::Complete::cpl_any ;
  2321. sub cpl_any {
  2322. my($word) = shift;
  2323. return (
  2324. cplx('CPAN::Author',$word),
  2325. cplx('CPAN::Bundle',$word),
  2326. cplx('CPAN::Distribution',$word),
  2327. cplx('CPAN::Module',$word),
  2328. );
  2329. }
  2330. #-> sub CPAN::Complete::cpl_reload ;
  2331. sub cpl_reload {
  2332. my($word,$line,$pos) = @_;
  2333. $word ||= "";
  2334. my(@words) = split " ", $line;
  2335. CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2336. my(@ok) = qw(cpan index);
  2337. return @ok if @words == 1;
  2338. return grep /^\Q$word\E/, @ok if @words == 2 && $word;
  2339. }
  2340. #-> sub CPAN::Complete::cpl_option ;
  2341. sub cpl_option {
  2342. my($word,$line,$pos) = @_;
  2343. $word ||= "";
  2344. my(@words) = split " ", $line;
  2345. CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2346. my(@ok) = qw(conf debug);
  2347. return @ok if @words == 1;
  2348. return grep /^\Q$word\E/, @ok if @words == 2 && length($word);
  2349. if (0) {
  2350. } elsif ($words[1] eq 'index') {
  2351. return ();
  2352. } elsif ($words[1] eq 'conf') {
  2353. return CPAN::Config::cpl(@_);
  2354. } elsif ($words[1] eq 'debug') {
  2355. return sort grep /^\Q$word\E/, sort keys %CPAN::DEBUG, 'all';
  2356. }
  2357. }
  2358. package CPAN::Index;
  2359. #-> sub CPAN::Index::force_reload ;
  2360. sub force_reload {
  2361. my($class) = @_;
  2362. $CPAN::Index::last_time = 0;
  2363. $class->reload(1);
  2364. }
  2365. #-> sub CPAN::Index::reload ;
  2366. sub reload {
  2367. my($cl,$force) = @_;
  2368. my $time = time;
  2369. # XXX check if a newer one is available. (We currently read it
  2370. # from time to time)
  2371. for ($CPAN::Config->{index_expire}) {
  2372. $_ = 0.001 unless $_ > 0.001;
  2373. }
  2374. return if $last_time + $CPAN::Config->{index_expire}*86400 > $time
  2375. and ! $force;
  2376. my($debug,$t2);
  2377. $last_time = $time;
  2378. my $needshort = $^O eq "dos";
  2379. $cl->rd_authindex($cl
  2380. ->reload_x(
  2381. "authors/01mailrc.txt.gz",
  2382. $needshort ?
  2383. File::Spec->catfile('authors', '01mailrc.gz') :
  2384. File::Spec->catfile('authors', '01mailrc.txt.gz'),
  2385. $force));
  2386. $t2 = time;
  2387. $debug = "timing reading 01[".($t2 - $time)."]";
  2388. $time = $t2;
  2389. return if $CPAN::Signal; # this is sometimes lengthy
  2390. $cl->rd_modpacks($cl
  2391. ->reload_x(
  2392. "modules/02packages.details.txt.gz",
  2393. $needshort ?
  2394. File::Spec->catfile('modules', '02packag.gz') :
  2395. File::Spec->catfile('modules', '02packages.details.txt.gz'),
  2396. $force));
  2397. $t2 = time;
  2398. $debug .= "02[".($t2 - $time)."]";
  2399. $time = $t2;
  2400. return if $CPAN::Signal; # this is sometimes lengthy
  2401. $cl->rd_modlist($cl
  2402. ->reload_x(
  2403. "modules/03modlist.data.gz",
  2404. $needshort ?
  2405. File::Spec->catfile('modules', '03mlist.gz') :
  2406. File::Spec->catfile('modules', '03modlist.data.gz'),
  2407. $force));
  2408. $t2 = time;
  2409. $debug .= "03[".($t2 - $time)."]";
  2410. $time = $t2;
  2411. CPAN->debug($debug) if $CPAN::DEBUG;
  2412. }
  2413. #-> sub CPAN::Index::reload_x ;
  2414. sub reload_x {
  2415. my($cl,$wanted,$localname,$force) = @_;
  2416. $force |= 2; # means we're dealing with an index here
  2417. CPAN::Config->load; # we should guarantee loading wherever we rely
  2418. # on Config XXX
  2419. $localname ||= $wanted;
  2420. my $abs_wanted = MM->catfile($CPAN::Config->{'keep_source_where'},
  2421. $localname);
  2422. if (
  2423. -f $abs_wanted &&
  2424. -M $abs_wanted < $CPAN::Config->{'index_expire'} &&
  2425. !($force & 1)
  2426. ) {
  2427. my $s = $CPAN::Config->{'index_expire'} == 1 ? "" : "s";
  2428. $cl->debug(qq{$abs_wanted younger than $CPAN::Config->{'index_expire'} }.
  2429. qq{day$s. I\'ll use that.});
  2430. return $abs_wanted;
  2431. } else {
  2432. $force |= 1; # means we're quite serious about it.
  2433. }
  2434. return CPAN::FTP->localize($wanted,$abs_wanted,$force);
  2435. }
  2436. #-> sub CPAN::Index::rd_authindex ;
  2437. sub rd_authindex {
  2438. my($cl, $index_target) = @_;
  2439. my @lines;
  2440. return unless defined $index_target;
  2441. $CPAN::Frontend->myprint("Going to read $index_target\n");
  2442. # my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  2443. # while ($_ = $fh->READLINE) {
  2444. # no strict 'refs';
  2445. local(*FH);
  2446. tie *FH, CPAN::Tarzip, $index_target;
  2447. local($/) = "\n";
  2448. push @lines, split /\012/ while <FH>;
  2449. foreach (@lines) {
  2450. my($userid,$fullname,$email) =
  2451. m/alias\s+(\S+)\s+\"([^\"\<]+)\s+\<([^\>]+)\>\"/;
  2452. next unless $userid && $fullname && $email;
  2453. # instantiate an author object
  2454. my $userobj = $CPAN::META->instance('CPAN::Author',$userid);
  2455. $userobj->set('FULLNAME' => $fullname, 'EMAIL' => $email);
  2456. return if $CPAN::Signal;
  2457. }
  2458. }
  2459. sub userid {
  2460. my($self,$dist) = @_;
  2461. $dist = $self->{'id'} unless defined $dist;
  2462. my($ret) = $dist =~ m|(?:\w/\w\w/)?([^/]+)/|;
  2463. $ret;
  2464. }
  2465. #-> sub CPAN::Index::rd_modpacks ;
  2466. sub rd_modpacks {
  2467. my($cl, $index_target) = @_;
  2468. my @lines;
  2469. return unless defined $index_target;
  2470. $CPAN::Frontend->myprint("Going to read $index_target\n");
  2471. my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  2472. local($/) = "\n";
  2473. while ($_ = $fh->READLINE) {
  2474. s/\012/\n/g;
  2475. my @ls = map {"$_\n"} split /\n/, $_;
  2476. unshift @ls, "\n" x length($1) if /^(\n+)/;
  2477. push @lines, @ls;
  2478. }
  2479. while (@lines) {
  2480. my $shift = shift(@lines);
  2481. last if $shift =~ /^\s*$/;
  2482. }
  2483. foreach (@lines) {
  2484. chomp;
  2485. my($mod,$version,$dist) = split;
  2486. ### $version =~ s/^\+//;
  2487. # if it is a bundle, instatiate a bundle object
  2488. my($bundle,$id,$userid);
  2489. if ($mod eq 'CPAN' &&
  2490. ! (
  2491. CPAN::Queue->exists('Bundle::CPAN') ||
  2492. CPAN::Queue->exists('CPAN')
  2493. )
  2494. ) {
  2495. local($^W)= 0;
  2496. if ($version > $CPAN::VERSION){
  2497. $CPAN::Frontend->myprint(qq{
  2498. There\'s a new CPAN.pm version (v$version) available!
  2499. You might want to try
  2500. install Bundle::CPAN
  2501. reload cpan
  2502. without quitting the current session. It should be a seamless upgrade
  2503. while we are running...
  2504. });
  2505. sleep 2;
  2506. $CPAN::Frontend->myprint(qq{\n});
  2507. }
  2508. last if $CPAN::Signal;
  2509. } elsif ($mod =~ /^Bundle::(.*)/) {
  2510. $bundle = $1;
  2511. }
  2512. if ($bundle){
  2513. $id = $CPAN::META->instance('CPAN::Bundle',$mod);
  2514. # warn "made mod[$mod]a bundle";
  2515. # Let's make it a module too, because bundles have so much
  2516. # in common with modules
  2517. $CPAN::META->instance('CPAN::Module',$mod);
  2518. # warn "made mod[$mod]a module";
  2519. # This "next" makes us faster but if the job is running long, we ignore
  2520. # rereads which is bad. So we have to be a bit slower again.
  2521. # } elsif ($CPAN::META->exists('CPAN::Module',$mod)) {
  2522. # next;
  2523. }
  2524. else {
  2525. # instantiate a module object
  2526. $id = $CPAN::META->instance('CPAN::Module',$mod);
  2527. }
  2528. if ($id->cpan_file ne $dist){
  2529. $userid = $cl->userid($dist);
  2530. $id->set(
  2531. 'CPAN_USERID' => $userid,
  2532. 'CPAN_VERSION' => $version,
  2533. 'CPAN_FILE' => $dist
  2534. );
  2535. }
  2536. # instantiate a distribution object
  2537. unless ($CPAN::META->exists('CPAN::Distribution',$dist)) {
  2538. $CPAN::META->instance(
  2539. 'CPAN::Distribution' => $dist
  2540. )->set(
  2541. 'CPAN_USERID' => $userid
  2542. );
  2543. }
  2544. return if $CPAN::Signal;
  2545. }
  2546. undef $fh;
  2547. }
  2548. #-> sub CPAN::Index::rd_modlist ;
  2549. sub rd_modlist {
  2550. my($cl,$index_target) = @_;
  2551. return unless defined $index_target;
  2552. $CPAN::Frontend->myprint("Going to read $index_target\n");
  2553. my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  2554. my @eval;
  2555. local($/) = "\n";
  2556. while ($_ = $fh->READLINE) {
  2557. s/\012/\n/g;
  2558. my @ls = map {"$_\n"} split /\n/, $_;
  2559. unshift @ls, "\n" x length($1) if /^(\n+)/;
  2560. push @eval, @ls;
  2561. }
  2562. while (@eval) {
  2563. my $shift = shift(@eval);
  2564. if ($shift =~ /^Date:\s+(.*)/){
  2565. return if $date_of_03 eq $1;
  2566. ($date_of_03) = $1;
  2567. }
  2568. last if $shift =~ /^\s*$/;
  2569. }
  2570. undef $fh;
  2571. push @eval, q{CPAN::Modulelist->data;};
  2572. local($^W) = 0;
  2573. my($comp) = Safe->new("CPAN::Safe1");
  2574. my($eval) = join("", @eval);
  2575. my $ret = $comp->reval($eval);
  2576. Carp::confess($@) if $@;
  2577. return if $CPAN::Signal;
  2578. for (keys %$ret) {
  2579. my $obj = $CPAN::META->instance(CPAN::Module,$_);
  2580. $obj->set(%{$ret->{$_}});
  2581. return if $CPAN::Signal;
  2582. }
  2583. }
  2584. package CPAN::InfoObj;
  2585. #-> sub CPAN::InfoObj::new ;
  2586. sub new { my $this = bless {}, shift; %$this = @_; $this }
  2587. #-> sub CPAN::InfoObj::set ;
  2588. sub set {
  2589. my($self,%att) = @_;
  2590. my(%oldatt) = %$self;
  2591. %$self = (%oldatt, %att);
  2592. }
  2593. #-> sub CPAN::InfoObj::id ;
  2594. sub id { shift->{'ID'} }
  2595. #-> sub CPAN::InfoObj::as_glimpse ;
  2596. sub as_glimpse {
  2597. my($self) = @_;
  2598. my(@m);
  2599. my $class = ref($self);
  2600. $class =~ s/^CPAN:://;
  2601. push @m, sprintf "%-15s %s\n", $class, $self->{ID};
  2602. join "", @m;
  2603. }
  2604. #-> sub CPAN::InfoObj::as_string ;
  2605. sub as_string {
  2606. my($self) = @_;
  2607. my(@m);
  2608. my $class = ref($self);
  2609. $class =~ s/^CPAN:://;
  2610. push @m, $class, " id = $self->{ID}\n";
  2611. for (sort keys %$self) {
  2612. next if $_ eq 'ID';
  2613. my $extra = "";
  2614. if ($_ eq "CPAN_USERID") {
  2615. $extra .= " (".$self->author;
  2616. my $email; # old perls!
  2617. if ($email = $CPAN::META->instance(CPAN::Author,
  2618. $self->{$_}
  2619. )->email) {
  2620. $extra .= " <$email>";
  2621. } else {
  2622. $extra .= " <no email>";
  2623. }
  2624. $extra .= ")";
  2625. }
  2626. if (ref($self->{$_}) eq "ARRAY") { # language interface? XXX
  2627. push @m, sprintf " %-12s %s%s\n", $_, "@{$self->{$_}}", $extra;
  2628. } else {
  2629. push @m, sprintf " %-12s %s%s\n", $_, $self->{$_}, $extra;
  2630. }
  2631. }
  2632. join "", @m, "\n";
  2633. }
  2634. #-> sub CPAN::InfoObj::author ;
  2635. sub author {
  2636. my($self) = @_;
  2637. $CPAN::META->instance(CPAN::Author,$self->{CPAN_USERID})->fullname;
  2638. }
  2639. package CPAN::Author;
  2640. #-> sub CPAN::Author::as_glimpse ;
  2641. sub as_glimpse {
  2642. my($self) = @_;
  2643. my(@m);
  2644. my $class = ref($self);
  2645. $class =~ s/^CPAN:://;
  2646. push @m, sprintf "%-15s %s (%s)\n", $class, $self->{ID}, $self->fullname;
  2647. join "", @m;
  2648. }
  2649. # Dead code, I would have liked to have,,, but it was never reached,,,
  2650. #sub make {
  2651. # my($self) = @_;
  2652. # return "Don't be silly, you can't make $self->{FULLNAME} ;-)\n";
  2653. #}
  2654. #-> sub CPAN::Author::fullname ;
  2655. sub fullname { shift->{'FULLNAME'} }
  2656. *name = \&fullname;
  2657. #-> sub CPAN::Author::email ;
  2658. sub email { shift->{'EMAIL'} }
  2659. package CPAN::Distribution;
  2660. #-> sub CPAN::Distribution::called_for ;
  2661. sub called_for {
  2662. my($self,$id) = @_;
  2663. $self->{'CALLED_FOR'} = $id if defined $id;
  2664. return $self->{'CALLED_FOR'};
  2665. }
  2666. #-> sub CPAN::Distribution::get ;
  2667. sub get {
  2668. my($self) = @_;
  2669. EXCUSE: {
  2670. my @e;
  2671. exists $self->{'build_dir'} and push @e,
  2672. "Unwrapped into directory $self->{'build_dir'}";
  2673. $CPAN::Frontend->myprint(join "", map {" $_\n"} @e) and return if @e;
  2674. }
  2675. my($local_file);
  2676. my($local_wanted) =
  2677. MM->catfile(
  2678. $CPAN::Config->{keep_source_where},
  2679. "authors",
  2680. "id",
  2681. split("/",$self->{ID})
  2682. );
  2683. $self->debug("Doing localize") if $CPAN::DEBUG;
  2684. $local_file =
  2685. CPAN::FTP->localize("authors/id/$self->{ID}", $local_wanted)
  2686. or $CPAN::Frontend->mydie("Giving up on '$local_wanted'\n");
  2687. $self->{localfile} = $local_file;
  2688. my $builddir = $CPAN::META->{cachemgr}->dir;
  2689. $self->debug("doing chdir $builddir") if $CPAN::DEBUG;
  2690. chdir $builddir or Carp::croak("Couldn't chdir $builddir: $!");
  2691. my $packagedir;
  2692. $self->debug("local_file[$local_file]") if $CPAN::DEBUG;
  2693. if ($CPAN::META->has_inst('MD5')) {
  2694. $self->debug("MD5 is installed, verifying");
  2695. $self->verifyMD5;
  2696. } else {
  2697. $self->debug("MD5 is NOT installed");
  2698. }
  2699. $self->debug("Removing tmp") if $CPAN::DEBUG;
  2700. File::Path::rmtree("tmp");
  2701. mkdir "tmp", 0755 or Carp::croak "Couldn't mkdir tmp: $!";
  2702. chdir "tmp";
  2703. $self->debug("Changed directory to tmp") if $CPAN::DEBUG;
  2704. if (! $local_file) {
  2705. Carp::croak "bad download, can't do anything :-(\n";
  2706. } elsif ($local_file =~ /(\.tar\.(gz|Z)|\.tgz)$/i){
  2707. $self->untar_me($local_file);
  2708. } elsif ( $local_file =~ /\.zip$/i ) {
  2709. $self->unzip_me($local_file);
  2710. } elsif ( $local_file =~ /\.pm\.(gz|Z)$/) {
  2711. $self->pm2dir_me($local_file);
  2712. } else {
  2713. $self->{archived} = "NO";
  2714. }
  2715. chdir File::Spec->updir;
  2716. if ($self->{archived} ne 'NO') {
  2717. chdir File::Spec->catdir(File::Spec->curdir, "tmp");
  2718. # Let's check if the package has its own directory.
  2719. my $dh = DirHandle->new(File::Spec->curdir)
  2720. or Carp::croak("Couldn't opendir .: $!");
  2721. my @readdir = grep $_ !~ /^\.\.?$/, $dh->read; ### MAC??
  2722. $dh->close;
  2723. my ($distdir,$packagedir);
  2724. if (@readdir == 1 && -d $readdir[0]) {
  2725. $distdir = $readdir[0];
  2726. $packagedir = MM->catdir($builddir,$distdir);
  2727. -d $packagedir and $CPAN::Frontend->myprint("Removing previously used $packagedir\n");
  2728. File::Path::rmtree($packagedir);
  2729. rename($distdir,$packagedir) or Carp::confess("Couldn't rename $distdir to $packagedir: $!");
  2730. } else {
  2731. my $pragmatic_dir = $self->{'CPAN_USERID'} . '000';
  2732. $pragmatic_dir =~ s/\W_//g;
  2733. $pragmatic_dir++ while -d "../$pragmatic_dir";
  2734. $packagedir = MM->catdir($builddir,$pragmatic_dir);
  2735. File::Path::mkpath($packagedir);
  2736. my($f);
  2737. for $f (@readdir) { # is already without "." and ".."
  2738. my $to = MM->catdir($packagedir,$f);
  2739. rename($f,$to) or Carp::confess("Couldn't rename $f to $to: $!");
  2740. }
  2741. }
  2742. $self->{'build_dir'} = $packagedir;
  2743. chdir File::Spec->updir;
  2744. $self->debug("Changed directory to .. (self is $self [".$self->as_string."])")
  2745. if $CPAN::DEBUG;
  2746. File::Path::rmtree("tmp");
  2747. if ($CPAN::Config->{keep_source_where} =~ /^no/i ){
  2748. $CPAN::Frontend->myprint("Going to unlink $local_file\n");
  2749. unlink $local_file or Carp::carp "Couldn't unlink $local_file";
  2750. }
  2751. my($makefilepl) = MM->catfile($packagedir,"Makefile.PL");
  2752. unless (-f $makefilepl) {
  2753. my($configure) = MM->catfile($packagedir,"Configure");
  2754. if (-f $configure) {
  2755. # do we have anything to do?
  2756. $self->{'configure'} = $configure;
  2757. } elsif (-f MM->catfile($packagedir,"Makefile")) {
  2758. $CPAN::Frontend->myprint(qq{
  2759. Package comes with a Makefile and without a Makefile.PL.
  2760. We\'ll try to build it with that Makefile then.
  2761. });
  2762. $self->{writemakefile} = "YES";
  2763. sleep 2;
  2764. } else {
  2765. my $fh = FileHandle->new(">$makefilepl")
  2766. or Carp::croak("Could not open >$makefilepl");
  2767. my $cf = $self->called_for || "unknown";
  2768. $fh->print(
  2769. qq{# This Makefile.PL has been autogenerated by the module CPAN.pm
  2770. # because there was no Makefile.PL supplied.
  2771. # Autogenerated on: }.scalar localtime().qq{
  2772. use ExtUtils::MakeMaker;
  2773. WriteMakefile(NAME => q[$cf]);
  2774. });
  2775. $CPAN::Frontend->myprint(qq{Package comes without Makefile.PL.
  2776. Writing one on our own (calling it $cf)\n});
  2777. }
  2778. }
  2779. }
  2780. return $self;
  2781. }
  2782. sub untar_me {
  2783. my($self,$local_file) = @_;
  2784. $self->{archived} = "tar";
  2785. if (CPAN::Tarzip->untar($local_file)) {
  2786. $self->{unwrapped} = "YES";
  2787. } else {
  2788. $self->{unwrapped} = "NO";
  2789. }
  2790. }
  2791. sub unzip_me {
  2792. my($self,$local_file) = @_;
  2793. $self->{archived} = "zip";
  2794. my $system = "$CPAN::Config->{unzip} $local_file";
  2795. if (system($system) == 0) {
  2796. $self->{unwrapped} = "YES";
  2797. } else {
  2798. $self->{unwrapped} = "NO";
  2799. }
  2800. }
  2801. sub pm2dir_me {
  2802. my($self,$local_file) = @_;
  2803. $self->{archived} = "pm";
  2804. my $to = File::Basename::basename($local_file);
  2805. $to =~ s/\.(gz|Z)$//;
  2806. if (CPAN::Tarzip->gunzip($local_file,$to)) {
  2807. $self->{unwrapped} = "YES";
  2808. } else {
  2809. $self->{unwrapped} = "NO";
  2810. }
  2811. }
  2812. #-> sub CPAN::Distribution::new ;
  2813. sub new {
  2814. my($class,%att) = @_;
  2815. $CPAN::META->{cachemgr} ||= CPAN::CacheMgr->new();
  2816. my $this = { %att };
  2817. return bless $this, $class;
  2818. }
  2819. #-> sub CPAN::Distribution::look ;
  2820. sub look {
  2821. my($self) = @_;
  2822. if ($^O eq 'MacOS') {
  2823. $self->ExtUtils::MM_MacOS::look;
  2824. return;
  2825. }
  2826. if ( $CPAN::Config->{'shell'} ) {
  2827. $CPAN::Frontend->myprint(qq{
  2828. Trying to open a subshell in the build directory...
  2829. });
  2830. } else {
  2831. $CPAN::Frontend->myprint(qq{
  2832. Your configuration does not define a value for subshells.
  2833. Please define it with "o conf shell <your shell>"
  2834. });
  2835. return;
  2836. }
  2837. my $dist = $self->id;
  2838. my $dir = $self->dir or $self->get;
  2839. $dir = $self->dir;
  2840. my $getcwd;
  2841. $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  2842. my $pwd = CPAN->$getcwd();
  2843. chdir($dir);
  2844. $CPAN::Frontend->myprint(qq{Working directory is $dir\n});
  2845. system($CPAN::Config->{'shell'}) == 0
  2846. or $CPAN::Frontend->mydie("Subprocess shell error");
  2847. chdir($pwd);
  2848. }
  2849. #-> sub CPAN::Distribution::readme ;
  2850. sub readme {
  2851. my($self) = @_;
  2852. my($dist) = $self->id;
  2853. my($sans,$suffix) = $dist =~ /(.+)\.(tgz|tar[\._-]gz|tar\.Z|zip)$/;
  2854. $self->debug("sans[$sans] suffix[$suffix]\n") if $CPAN::DEBUG;
  2855. my($local_file);
  2856. my($local_wanted) =
  2857. MM->catfile(
  2858. $CPAN::Config->{keep_source_where},
  2859. "authors",
  2860. "id",
  2861. split("/","$sans.readme"),
  2862. );
  2863. $self->debug("Doing localize") if $CPAN::DEBUG;
  2864. $local_file = CPAN::FTP->localize("authors/id/$sans.readme",
  2865. $local_wanted)
  2866. or $CPAN::Frontend->mydie(qq{No $sans.readme found});;
  2867. if ($^O eq 'MacOS') {
  2868. ExtUtils::MM_MacOS::launch_file($local_file);
  2869. return;
  2870. }
  2871. my $fh_pager = FileHandle->new;
  2872. local($SIG{PIPE}) = "IGNORE";
  2873. $fh_pager->open("|$CPAN::Config->{'pager'}")
  2874. or die "Could not open pager $CPAN::Config->{'pager'}: $!";
  2875. my $fh_readme = FileHandle->new;
  2876. $fh_readme->open($local_file)
  2877. or $CPAN::Frontend->mydie(qq{Could not open "$local_file": $!});
  2878. $CPAN::Frontend->myprint(qq{
  2879. Displaying file
  2880. $local_file
  2881. with pager "$CPAN::Config->{'pager'}"
  2882. });
  2883. sleep 2;
  2884. $fh_pager->print(<$fh_readme>);
  2885. }
  2886. #-> sub CPAN::Distribution::verifyMD5 ;
  2887. sub verifyMD5 {
  2888. my($self) = @_;
  2889. EXCUSE: {
  2890. my @e;
  2891. $self->{MD5_STATUS} ||= "";
  2892. $self->{MD5_STATUS} eq "OK" and push @e, "MD5 Checksum was ok";
  2893. $CPAN::Frontend->myprint(join "", map {" $_\n"} @e) and return if @e;
  2894. }
  2895. my($lc_want,$lc_file,@local,$basename);
  2896. @local = split("/",$self->{ID});
  2897. pop @local;
  2898. push @local, "CHECKSUMS";
  2899. $lc_want =
  2900. MM->catfile($CPAN::Config->{keep_source_where},
  2901. "authors", "id", @local);
  2902. local($") = "/";
  2903. if (
  2904. -s $lc_want
  2905. &&
  2906. $self->MD5_check_file($lc_want)
  2907. ) {
  2908. return $self->{MD5_STATUS} = "OK";
  2909. }
  2910. $lc_file = CPAN::FTP->localize("authors/id/@local",
  2911. $lc_want,1);
  2912. unless ($lc_file) {
  2913. $local[-1] .= ".gz";
  2914. $lc_file = CPAN::FTP->localize("authors/id/@local",
  2915. "$lc_want.gz",1);
  2916. if ($lc_file) {
  2917. $lc_file =~ s/\.gz$//;
  2918. CPAN::Tarzip->gunzip("$lc_file.gz",$lc_file);
  2919. } else {
  2920. return;
  2921. }
  2922. }
  2923. $self->MD5_check_file($lc_file);
  2924. }
  2925. #-> sub CPAN::Distribution::MD5_check_file ;
  2926. sub MD5_check_file {
  2927. my($self,$chk_file) = @_;
  2928. my($cksum,$file,$basename);
  2929. $file = $self->{localfile};
  2930. $basename = File::Basename::basename($file);
  2931. my $fh = FileHandle->new;
  2932. if (open $fh, $chk_file){
  2933. local($/);
  2934. my $eval = <$fh>;
  2935. $eval =~ s/\015?\012/\n/g;
  2936. close $fh;
  2937. my($comp) = Safe->new();
  2938. $cksum = $comp->reval($eval);
  2939. if ($@) {
  2940. rename $chk_file, "$chk_file.bad";
  2941. Carp::confess($@) if $@;
  2942. }
  2943. } else {
  2944. Carp::carp "Could not open $chk_file for reading";
  2945. }
  2946. if (exists $cksum->{$basename}{md5}) {
  2947. $self->debug("Found checksum for $basename:" .
  2948. "$cksum->{$basename}{md5}\n") if $CPAN::DEBUG;
  2949. open($fh, $file);
  2950. binmode $fh;
  2951. my $eq = $self->eq_MD5($fh,$cksum->{$basename}{'md5'});
  2952. $fh->close;
  2953. $fh = CPAN::Tarzip->TIEHANDLE($file);
  2954. unless ($eq) {
  2955. # had to inline it, when I tied it, the tiedness got lost on
  2956. # the call to eq_MD5. (Jan 1998)
  2957. my $md5 = MD5->new;
  2958. my($data,$ref);
  2959. $ref = \$data;
  2960. while ($fh->READ($ref, 4096)){
  2961. $md5->add($data);
  2962. }
  2963. my $hexdigest = $md5->hexdigest;
  2964. $eq += $hexdigest eq $cksum->{$basename}{'md5-ungz'};
  2965. }
  2966. if ($eq) {
  2967. $CPAN::Frontend->myprint("Checksum for $file ok\n");
  2968. return $self->{MD5_STATUS} = "OK";
  2969. } else {
  2970. $CPAN::Frontend->myprint(qq{Checksum mismatch for }.
  2971. qq{distribution file. }.
  2972. qq{Please investigate.\n\n}.
  2973. $self->as_string,
  2974. $CPAN::META->instance(
  2975. 'CPAN::Author',
  2976. $self->{CPAN_USERID}
  2977. )->as_string);
  2978. my $wrap = qq{I\'d recommend removing $file. It seems to
  2979. be a bogus file. Maybe you have configured your \`urllist\' with a
  2980. bad URL. Please check this array with \`o conf urllist\', and
  2981. retry.};
  2982. $CPAN::Frontend->myprint(Text::Wrap::wrap("","",$wrap));
  2983. $CPAN::Frontend->myprint("\n\n");
  2984. sleep 3;
  2985. return;
  2986. }
  2987. # close $fh if fileno($fh);
  2988. } else {
  2989. $self->{MD5_STATUS} ||= "";
  2990. if ($self->{MD5_STATUS} eq "NIL") {
  2991. $CPAN::Frontend->myprint(qq{
  2992. No md5 checksum for $basename in local $chk_file.
  2993. Removing $chk_file
  2994. });
  2995. unlink $chk_file or $CPAN::Frontend->myprint("Could not unlink: $!");
  2996. sleep 1;
  2997. }
  2998. $self->{MD5_STATUS} = "NIL";
  2999. return;
  3000. }
  3001. }
  3002. #-> sub CPAN::Distribution::eq_MD5 ;
  3003. sub eq_MD5 {
  3004. my($self,$fh,$expectMD5) = @_;
  3005. my $md5 = MD5->new;
  3006. my($data);
  3007. while (read($fh, $data, 4096)){
  3008. $md5->add($data);
  3009. }
  3010. # $md5->addfile($fh);
  3011. my $hexdigest = $md5->hexdigest;
  3012. # warn "fh[$fh] hex[$hexdigest] aexp[$expectMD5]";
  3013. $hexdigest eq $expectMD5;
  3014. }
  3015. #-> sub CPAN::Distribution::force ;
  3016. sub force {
  3017. my($self) = @_;
  3018. $self->{'force_update'}++;
  3019. for my $att (qw(
  3020. MD5_STATUS archived build_dir localfile make install unwrapped
  3021. writemakefile have_sponsored
  3022. )) {
  3023. delete $self->{$att};
  3024. }
  3025. }
  3026. sub isa_perl {
  3027. my($self) = @_;
  3028. my $file = File::Basename::basename($self->id);
  3029. return unless $file =~ m{ ^ perl
  3030. (5)
  3031. ([._-])
  3032. (\d{3}(_[0-4][0-9])?)
  3033. \.tar[._-]gz
  3034. $
  3035. }x;
  3036. "$1.$3";
  3037. }
  3038. #-> sub CPAN::Distribution::perl ;
  3039. sub perl {
  3040. my($self) = @_;
  3041. my($perl) = MM->file_name_is_absolute($^X) ? $^X : "";
  3042. my $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  3043. my $pwd = CPAN->$getcwd();
  3044. my $candidate = MM->catfile($pwd,$^X);
  3045. $perl ||= $candidate if MM->maybe_command($candidate);
  3046. unless ($perl) {
  3047. my ($component,$perl_name);
  3048. DIST_PERLNAME: foreach $perl_name ($^X, 'perl', 'perl5', "perl$]") {
  3049. PATH_COMPONENT: foreach $component (MM->path(),
  3050. $Config::Config{'binexp'}) {
  3051. next unless defined($component) && $component;
  3052. my($abs) = MM->catfile($component,$perl_name);
  3053. if (MM->maybe_command($abs)) {
  3054. $perl = $abs;
  3055. last DIST_PERLNAME;
  3056. }
  3057. }
  3058. }
  3059. }
  3060. $perl;
  3061. }
  3062. #-> sub CPAN::Distribution::make ;
  3063. sub make {
  3064. my($self) = @_;
  3065. $CPAN::Frontend->myprint(sprintf "Running make for %s\n", $self->id);
  3066. # Emergency brake if they said install Pippi and get newest perl
  3067. if ($self->isa_perl) {
  3068. if (
  3069. $self->called_for ne $self->id && ! $self->{'force_update'}
  3070. ) {
  3071. $CPAN::Frontend->mydie(sprintf qq{
  3072. The most recent version "%s" of the module "%s"
  3073. comes with the current version of perl (%s).
  3074. I\'ll build that only if you ask for something like
  3075. force install %s
  3076. or
  3077. install %s
  3078. },
  3079. $CPAN::META->instance(
  3080. 'CPAN::Module',
  3081. $self->called_for
  3082. )->cpan_version,
  3083. $self->called_for,
  3084. $self->isa_perl,
  3085. $self->called_for,
  3086. $self->id);
  3087. }
  3088. }
  3089. $self->get;
  3090. EXCUSE: {
  3091. my @e;
  3092. $self->{archived} eq "NO" and push @e,
  3093. "Is neither a tar nor a zip archive.";
  3094. $self->{unwrapped} eq "NO" and push @e,
  3095. "had problems unarchiving. Please build manually";
  3096. exists $self->{writemakefile} &&
  3097. $self->{writemakefile} eq "NO" and push @e,
  3098. "Had some problem writing Makefile";
  3099. defined $self->{'make'} and push @e,
  3100. "Has already been processed within this session";
  3101. $CPAN::Frontend->myprint(join "", map {" $_\n"} @e) and return if @e;
  3102. }
  3103. $CPAN::Frontend->myprint("\n CPAN.pm: Going to build ".$self->id."\n\n");
  3104. my $builddir = $self->dir;
  3105. chdir $builddir or Carp::croak("Couldn't chdir $builddir: $!");
  3106. $self->debug("Changed directory to $builddir") if $CPAN::DEBUG;
  3107. if ($^O eq 'MacOS') {
  3108. ExtUtils::MM_MacOS::make($self);
  3109. return;
  3110. }
  3111. my $system;
  3112. if ($self->{'configure'}) {
  3113. $system = $self->{'configure'};
  3114. } else {
  3115. my($perl) = $self->perl or die "Couldn\'t find executable perl\n";
  3116. my $switch = "";
  3117. # This needs a handler that can be turned on or off:
  3118. # $switch = "-MExtUtils::MakeMaker ".
  3119. # "-Mops=:default,:filesys_read,:filesys_open,require,chdir"
  3120. # if $] > 5.00310;
  3121. $system = "$perl $switch Makefile.PL $CPAN::Config->{makepl_arg}";
  3122. }
  3123. unless (exists $self->{writemakefile}) {
  3124. local($SIG{ALRM}) = sub { die "inactivity_timeout reached\n" };
  3125. my($ret,$pid);
  3126. $@ = "";
  3127. if ($CPAN::Config->{inactivity_timeout}) {
  3128. eval {
  3129. alarm $CPAN::Config->{inactivity_timeout};
  3130. local $SIG{CHLD}; # = sub { wait };
  3131. if (defined($pid = fork)) {
  3132. if ($pid) { #parent
  3133. # wait;
  3134. waitpid $pid, 0;
  3135. } else { #child
  3136. # note, this exec isn't necessary if
  3137. # inactivity_timeout is 0. On the Mac I'd
  3138. # suggest, we set it always to 0.
  3139. exec $system;
  3140. }
  3141. } else {
  3142. $CPAN::Frontend->myprint("Cannot fork: $!");
  3143. return;
  3144. }
  3145. };
  3146. alarm 0;
  3147. if ($@){
  3148. kill 9, $pid;
  3149. waitpid $pid, 0;
  3150. $CPAN::Frontend->myprint($@);
  3151. $self->{writemakefile} = "NO - $@";
  3152. $@ = "";
  3153. return;
  3154. }
  3155. } else {
  3156. $ret = system($system);
  3157. if ($ret != 0) {
  3158. $self->{writemakefile} = "NO";
  3159. return;
  3160. }
  3161. }
  3162. $self->{writemakefile} = "YES";
  3163. }
  3164. return if $CPAN::Signal;
  3165. if (my @prereq = $self->needs_prereq){
  3166. my $id = $self->id;
  3167. $CPAN::Frontend->myprint("---- Dependencies detected ".
  3168. "during [$id] -----\n");
  3169. for my $p (@prereq) {
  3170. $CPAN::Frontend->myprint(" $p\n");
  3171. }
  3172. my $follow = 0;
  3173. if ($CPAN::Config->{prerequisites_policy} eq "follow") {
  3174. $follow = 1;
  3175. } elsif ($CPAN::Config->{prerequisites_policy} eq "ask") {
  3176. require ExtUtils::MakeMaker;
  3177. my $answer = ExtUtils::MakeMaker::prompt(
  3178. "Shall I follow them and prepend them to the queue
  3179. of modules we are processing right now?", "yes");
  3180. $follow = $answer =~ /^\s*y/i;
  3181. } else {
  3182. local($") = ", ";
  3183. $CPAN::Frontend->myprint(" Ignoring dependencies on modules @prereq\n");
  3184. }
  3185. if ($follow) {
  3186. CPAN::Queue->jumpqueue(@prereq,$id); # requeue yourself
  3187. return;
  3188. }
  3189. }
  3190. $system = join " ", $CPAN::Config->{'make'}, $CPAN::Config->{make_arg};
  3191. if (system($system) == 0) {
  3192. $CPAN::Frontend->myprint(" $system -- OK\n");
  3193. $self->{'make'} = "YES";
  3194. } else {
  3195. $self->{writemakefile} = "YES";
  3196. $self->{'make'} = "NO";
  3197. $CPAN::Frontend->myprint(" $system -- NOT OK\n");
  3198. }
  3199. }
  3200. #-> sub CPAN::Distribution::needs_prereq ;
  3201. sub needs_prereq {
  3202. my($self) = @_;
  3203. return unless -f "Makefile"; # we cannot say much
  3204. my $fh = FileHandle->new("<Makefile") or
  3205. $CPAN::Frontend->mydie("Couldn't open Makefile: $!");
  3206. local($/) = "\n";
  3207. my(@p,@need);
  3208. while (<$fh>) {
  3209. last if /MakeMaker post_initialize section/;
  3210. my($p) = m{^[\#]
  3211. \s+PREREQ_PM\s+=>\s+(.+)
  3212. }x;
  3213. next unless $p;
  3214. # warn "Found prereq expr[$p]";
  3215. while ( $p =~ m/(?:\s)([\w\:]+)=>q\[.*?\],?/g ){
  3216. push @p, $1;
  3217. }
  3218. last;
  3219. }
  3220. for my $p (@p) {
  3221. my $mo = $CPAN::META->instance("CPAN::Module",$p);
  3222. next if $mo->uptodate;
  3223. # it's not needed, so don't push it. We cannot omit this step, because
  3224. # if 'force' is in effect, nobody else will check.
  3225. if ($self->{'have_sponsored'}{$p}++){
  3226. # We have already sponsored it and for some reason it's still
  3227. # not available. So we do nothing. Or what should we do?
  3228. # if we push it again, we have a potential infinite loop
  3229. next;
  3230. }
  3231. push @need, $p;
  3232. }
  3233. return @need;
  3234. }
  3235. #-> sub CPAN::Distribution::test ;
  3236. sub test {
  3237. my($self) = @_;
  3238. $self->make;
  3239. return if $CPAN::Signal;
  3240. $CPAN::Frontend->myprint("Running make test\n");
  3241. EXCUSE: {
  3242. my @e;
  3243. exists $self->{'make'} or push @e,
  3244. "Make had some problems, maybe interrupted? Won't test";
  3245. exists $self->{'make'} and
  3246. $self->{'make'} eq 'NO' and
  3247. push @e, "Oops, make had returned bad status";
  3248. exists $self->{'build_dir'} or push @e, "Has no own directory";
  3249. $CPAN::Frontend->myprint(join "", map {" $_\n"} @e) and return if @e;
  3250. }
  3251. chdir $self->{'build_dir'} or
  3252. Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  3253. $self->debug("Changed directory to $self->{'build_dir'}")
  3254. if $CPAN::DEBUG;
  3255. if ($^O eq 'MacOS') {
  3256. ExtUtils::MM_MacOS::make_test($self);
  3257. return;
  3258. }
  3259. my $system = join " ", $CPAN::Config->{'make'}, "test";
  3260. if (system($system) == 0) {
  3261. $CPAN::Frontend->myprint(" $system -- OK\n");
  3262. $self->{'make_test'} = "YES";
  3263. } else {
  3264. $self->{'make_test'} = "NO";
  3265. $CPAN::Frontend->myprint(" $system -- NOT OK\n");
  3266. }
  3267. }
  3268. #-> sub CPAN::Distribution::clean ;
  3269. sub clean {
  3270. my($self) = @_;
  3271. $CPAN::Frontend->myprint("Running make clean\n");
  3272. EXCUSE: {
  3273. my @e;
  3274. exists $self->{'build_dir'} or push @e, "Has no own directory";
  3275. $CPAN::Frontend->myprint(join "", map {" $_\n"} @e) and return if @e;
  3276. }
  3277. chdir $self->{'build_dir'} or
  3278. Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  3279. $self->debug("Changed directory to $self->{'build_dir'}") if $CPAN::DEBUG;
  3280. if ($^O eq 'MacOS') {
  3281. ExtUtils::MM_MacOS::make_clean($self);
  3282. return;
  3283. }
  3284. my $system = join " ", $CPAN::Config->{'make'}, "clean";
  3285. if (system($system) == 0) {
  3286. $CPAN::Frontend->myprint(" $system -- OK\n");
  3287. $self->force;
  3288. } else {
  3289. # Hmmm, what to do if make clean failed?
  3290. }
  3291. }
  3292. #-> sub CPAN::Distribution::install ;
  3293. sub install {
  3294. my($self) = @_;
  3295. $self->test;
  3296. return if $CPAN::Signal;
  3297. $CPAN::Frontend->myprint("Running make install\n");
  3298. EXCUSE: {
  3299. my @e;
  3300. exists $self->{'build_dir'} or push @e, "Has no own directory";
  3301. exists $self->{'make'} or push @e,
  3302. "Make had some problems, maybe interrupted? Won't install";
  3303. exists $self->{'make'} and
  3304. $self->{'make'} eq 'NO' and
  3305. push @e, "Oops, make had returned bad status";
  3306. push @e, "make test had returned bad status, ".
  3307. "won't install without force"
  3308. if exists $self->{'make_test'} and
  3309. $self->{'make_test'} eq 'NO' and
  3310. ! $self->{'force_update'};
  3311. exists $self->{'install'} and push @e,
  3312. $self->{'install'} eq "YES" ?
  3313. "Already done" : "Already tried without success";
  3314. $CPAN::Frontend->myprint(join "", map {" $_\n"} @e) and return if @e;
  3315. }
  3316. chdir $self->{'build_dir'} or
  3317. Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  3318. $self->debug("Changed directory to $self->{'build_dir'}")
  3319. if $CPAN::DEBUG;
  3320. if ($^O eq 'MacOS') {
  3321. ExtUtils::MM_MacOS::make_install($self);
  3322. return;
  3323. }
  3324. my $system = join(" ", $CPAN::Config->{'make'},
  3325. "install", $CPAN::Config->{make_install_arg});
  3326. my($stderr) = $^O =~ /Win/i ? "" : " 2>&1 ";
  3327. my($pipe) = FileHandle->new("$system $stderr |");
  3328. my($makeout) = "";
  3329. while (<$pipe>){
  3330. $CPAN::Frontend->myprint($_);
  3331. $makeout .= $_;
  3332. }
  3333. $pipe->close;
  3334. if ($?==0) {
  3335. $CPAN::Frontend->myprint(" $system -- OK\n");
  3336. return $self->{'install'} = "YES";
  3337. } else {
  3338. $self->{'install'} = "NO";
  3339. $CPAN::Frontend->myprint(" $system -- NOT OK\n");
  3340. if ($makeout =~ /permission/s && $> > 0) {
  3341. $CPAN::Frontend->myprint(qq{ You may have to su }.
  3342. qq{to root to install the package\n});
  3343. }
  3344. }
  3345. }
  3346. #-> sub CPAN::Distribution::dir ;
  3347. sub dir {
  3348. shift->{'build_dir'};
  3349. }
  3350. package CPAN::Bundle;
  3351. #-> sub CPAN::Bundle::as_string ;
  3352. sub as_string {
  3353. my($self) = @_;
  3354. $self->contains;
  3355. $self->{INST_VERSION} = $self->inst_version;
  3356. return $self->SUPER::as_string;
  3357. }
  3358. #-> sub CPAN::Bundle::contains ;
  3359. sub contains {
  3360. my($self) = @_;
  3361. my($parsefile) = $self->inst_file;
  3362. my($id) = $self->id;
  3363. $self->debug("parsefile[$parsefile]id[$id]") if $CPAN::DEBUG;
  3364. unless ($parsefile) {
  3365. # Try to get at it in the cpan directory
  3366. $self->debug("no parsefile") if $CPAN::DEBUG;
  3367. Carp::confess "I don't know a $id" unless $self->{CPAN_FILE};
  3368. my $dist = $CPAN::META->instance('CPAN::Distribution',
  3369. $self->{CPAN_FILE});
  3370. $dist->get;
  3371. $self->debug($dist->as_string) if $CPAN::DEBUG;
  3372. my($todir) = $CPAN::Config->{'cpan_home'};
  3373. my(@me,$from,$to,$me);
  3374. @me = split /::/, $self->id;
  3375. $me[-1] .= ".pm";
  3376. $me = MM->catfile(@me);
  3377. $from = $self->find_bundle_file($dist->{'build_dir'},$me);
  3378. $to = MM->catfile($todir,$me);
  3379. File::Path::mkpath(File::Basename::dirname($to));
  3380. File::Copy::copy($from, $to)
  3381. or Carp::confess("Couldn't copy $from to $to: $!");
  3382. $parsefile = $to;
  3383. }
  3384. my @result;
  3385. my $fh = FileHandle->new;
  3386. local $/ = "\n";
  3387. open($fh,$parsefile) or die "Could not open '$parsefile': $!";
  3388. my $inpod = 0;
  3389. $self->debug("parsefile[$parsefile]") if $CPAN::DEBUG;
  3390. while (<$fh>) {
  3391. $inpod = m/^=(?!head1\s+CONTENTS)/ ? 0 :
  3392. m/^=head1\s+CONTENTS/ ? 1 : $inpod;
  3393. next unless $inpod;
  3394. next if /^=/;
  3395. next if /^\s+$/;
  3396. chomp;
  3397. push @result, (split " ", $_, 2)[0];
  3398. }
  3399. close $fh;
  3400. delete $self->{STATUS};
  3401. $self->{CONTAINS} = join ", ", @result;
  3402. $self->debug("CONTAINS[@result]") if $CPAN::DEBUG;
  3403. unless (@result) {
  3404. $CPAN::Frontend->mywarn(qq{
  3405. The bundle file "$parsefile" may be a broken
  3406. bundlefile. It seems not to contain any bundle definition.
  3407. Please check the file and if it is bogus, please delete it.
  3408. Sorry for the inconvenience.
  3409. });
  3410. }
  3411. @result;
  3412. }
  3413. #-> sub CPAN::Bundle::find_bundle_file
  3414. sub find_bundle_file {
  3415. my($self,$where,$what) = @_;
  3416. $self->debug("where[$where]what[$what]") if $CPAN::DEBUG;
  3417. ### The following two lines let CPAN.pm become Bundle/CPAN.pm :-(
  3418. ### my $bu = MM->catfile($where,$what);
  3419. ### return $bu if -f $bu;
  3420. my $manifest = MM->catfile($where,"MANIFEST");
  3421. unless (-f $manifest) {
  3422. require ExtUtils::Manifest;
  3423. my $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  3424. my $cwd = CPAN->$getcwd();
  3425. chdir $where;
  3426. ExtUtils::Manifest::mkmanifest();
  3427. chdir $cwd;
  3428. }
  3429. my $fh = FileHandle->new($manifest)
  3430. or Carp::croak("Couldn't open $manifest: $!");
  3431. local($/) = "\n";
  3432. my $what2 = $what;
  3433. if ($^O eq 'MacOS') {
  3434. $what =~ s/^://;
  3435. $what2 =~ tr|:|/|;
  3436. $what2 =~ s/:Bundle://;
  3437. $what2 =~ tr|:|/|;
  3438. } else {
  3439. $what2 =~ s|Bundle/||;
  3440. }
  3441. my $bu;
  3442. while (<$fh>) {
  3443. next if /^\s*\#/;
  3444. my($file) = /(\S+)/;
  3445. if ($file =~ m|\Q$what\E$|) {
  3446. $bu = $file;
  3447. # return MM->catfile($where,$bu); # bad
  3448. last;
  3449. }
  3450. # retry if she managed to
  3451. # have no Bundle directory
  3452. $bu = $file if $file =~ m|\Q$what2\E$|;
  3453. }
  3454. $bu =~ tr|/|:| if $^O eq 'MacOS';
  3455. return MM->catfile($where, $bu) if $bu;
  3456. Carp::croak("Couldn't find a Bundle file in $where");
  3457. }
  3458. #-> sub CPAN::Bundle::inst_file ;
  3459. sub inst_file {
  3460. my($self) = @_;
  3461. my($me,$inst_file);
  3462. ($me = $self->id) =~ s/.*://;
  3463. ## my(@me,$inst_file);
  3464. ## @me = split /::/, $self->id;
  3465. ## $me[-1] .= ".pm";
  3466. $inst_file = MM->catfile($CPAN::Config->{'cpan_home'},
  3467. "Bundle", "$me.pm");
  3468. ## "Bundle", @me);
  3469. return $self->{'INST_FILE'} = $inst_file if -f $inst_file;
  3470. # $inst_file =
  3471. $self->SUPER::inst_file;
  3472. # return $self->{'INST_FILE'} = $inst_file if -f $inst_file;
  3473. # return $self->{'INST_FILE'}; # even if undefined?
  3474. }
  3475. #-> sub CPAN::Bundle::rematein ;
  3476. sub rematein {
  3477. my($self,$meth) = @_;
  3478. $self->debug("self[$self] meth[$meth]") if $CPAN::DEBUG;
  3479. my($id) = $self->id;
  3480. Carp::croak "Can't $meth $id, don't have an associated bundle file. :-(\n"
  3481. unless $self->inst_file || $self->{CPAN_FILE};
  3482. my($s,%fail);
  3483. for $s ($self->contains) {
  3484. my($type) = $s =~ m|/| ? 'CPAN::Distribution' :
  3485. $s =~ m|^Bundle::| ? 'CPAN::Bundle' : 'CPAN::Module';
  3486. if ($type eq 'CPAN::Distribution') {
  3487. $CPAN::Frontend->mywarn(qq{
  3488. The Bundle }.$self->id.qq{ contains
  3489. explicitly a file $s.
  3490. });
  3491. sleep 3;
  3492. }
  3493. # possibly noisy action:
  3494. my $obj = $CPAN::META->instance($type,$s);
  3495. $obj->$meth();
  3496. my $success = $obj->can("uptodate") ? $obj->uptodate : 0;
  3497. $success ||= $obj->{'install'} && $obj->{'install'} eq "YES";
  3498. $fail{$s} = 1 unless $success;
  3499. }
  3500. # recap with less noise
  3501. if ( $meth eq "install") {
  3502. if (%fail) {
  3503. $CPAN::Frontend->myprint(qq{\nBundle summary: }.
  3504. qq{The following items seem to }.
  3505. qq{have had installation problems:\n});
  3506. for $s ($self->contains) {
  3507. $CPAN::Frontend->myprint( "$s " ) if $fail{$s};
  3508. }
  3509. $CPAN::Frontend->myprint(qq{\n});
  3510. } else {
  3511. $self->{'install'} = 'YES';
  3512. }
  3513. }
  3514. }
  3515. #sub CPAN::Bundle::xs_file
  3516. sub xs_file {
  3517. # If a bundle contains another that contains an xs_file we have
  3518. # here, we just don't bother I suppose
  3519. return 0;
  3520. }
  3521. #-> sub CPAN::Bundle::force ;
  3522. sub force { shift->rematein('force',@_); }
  3523. #-> sub CPAN::Bundle::get ;
  3524. sub get { shift->rematein('get',@_); }
  3525. #-> sub CPAN::Bundle::make ;
  3526. sub make { shift->rematein('make',@_); }
  3527. #-> sub CPAN::Bundle::test ;
  3528. sub test { shift->rematein('test',@_); }
  3529. #-> sub CPAN::Bundle::install ;
  3530. sub install {
  3531. my $self = shift;
  3532. $self->rematein('install',@_);
  3533. }
  3534. #-> sub CPAN::Bundle::clean ;
  3535. sub clean { shift->rematein('clean',@_); }
  3536. #-> sub CPAN::Bundle::readme ;
  3537. sub readme {
  3538. my($self) = @_;
  3539. my($file) = $self->cpan_file or $CPAN::Frontend->myprint(qq{
  3540. No File found for bundle } . $self->id . qq{\n}), return;
  3541. $self->debug("self[$self] file[$file]") if $CPAN::DEBUG;
  3542. $CPAN::META->instance('CPAN::Distribution',$file)->readme;
  3543. }
  3544. package CPAN::Module;
  3545. #-> sub CPAN::Module::as_glimpse ;
  3546. sub as_glimpse {
  3547. my($self) = @_;
  3548. my(@m);
  3549. my $class = ref($self);
  3550. $class =~ s/^CPAN:://;
  3551. push @m, sprintf("%-15s %-15s (%s)\n", $class, $self->{ID},
  3552. $self->cpan_file);
  3553. join "", @m;
  3554. }
  3555. #-> sub CPAN::Module::as_string ;
  3556. sub as_string {
  3557. my($self) = @_;
  3558. my(@m);
  3559. CPAN->debug($self) if $CPAN::DEBUG;
  3560. my $class = ref($self);
  3561. $class =~ s/^CPAN:://;
  3562. local($^W) = 0;
  3563. push @m, $class, " id = $self->{ID}\n";
  3564. my $sprintf = " %-12s %s\n";
  3565. push @m, sprintf($sprintf, 'DESCRIPTION', $self->{description})
  3566. if $self->{description};
  3567. my $sprintf2 = " %-12s %s (%s)\n";
  3568. my($userid);
  3569. if ($userid = $self->{'CPAN_USERID'} || $self->{'userid'}){
  3570. my $author;
  3571. if ($author = CPAN::Shell->expand('Author',$userid)) {
  3572. my $email = "";
  3573. my $m; # old perls
  3574. if ($m = $author->email) {
  3575. $email = " <$m>";
  3576. }
  3577. push @m, sprintf(
  3578. $sprintf2,
  3579. 'CPAN_USERID',
  3580. $userid,
  3581. $author->fullname . $email
  3582. );
  3583. }
  3584. }
  3585. push @m, sprintf($sprintf, 'CPAN_VERSION', $self->{CPAN_VERSION})
  3586. if $self->{CPAN_VERSION};
  3587. push @m, sprintf($sprintf, 'CPAN_FILE', $self->{CPAN_FILE})
  3588. if $self->{CPAN_FILE};
  3589. my $sprintf3 = " %-12s %1s%1s%1s%1s (%s,%s,%s,%s)\n";
  3590. my(%statd,%stats,%statl,%stati);
  3591. @statd{qw,? i c a b R M S,} = qw,unknown idea
  3592. pre-alpha alpha beta released mature standard,;
  3593. @stats{qw,? m d u n,} = qw,unknown mailing-list
  3594. developer comp.lang.perl.* none,;
  3595. @statl{qw,? p c + o h,} = qw,unknown perl C C++ other hybrid,;
  3596. @stati{qw,? f r O h,} = qw,unknown functions
  3597. references+ties object-oriented hybrid,;
  3598. $statd{' '} = 'unknown';
  3599. $stats{' '} = 'unknown';
  3600. $statl{' '} = 'unknown';
  3601. $stati{' '} = 'unknown';
  3602. push @m, sprintf(
  3603. $sprintf3,
  3604. 'DSLI_STATUS',
  3605. $self->{statd},
  3606. $self->{stats},
  3607. $self->{statl},
  3608. $self->{stati},
  3609. $statd{$self->{statd}},
  3610. $stats{$self->{stats}},
  3611. $statl{$self->{statl}},
  3612. $stati{$self->{stati}}
  3613. ) if $self->{statd};
  3614. my $local_file = $self->inst_file;
  3615. if ($local_file) {
  3616. $self->{MANPAGE} ||= $self->manpage_headline($local_file);
  3617. }
  3618. my($item);
  3619. for $item (qw/MANPAGE CONTAINS/) {
  3620. push @m, sprintf($sprintf, $item, $self->{$item})
  3621. if exists $self->{$item};
  3622. }
  3623. push @m, sprintf($sprintf, 'INST_FILE',
  3624. $local_file || "(not installed)");
  3625. push @m, sprintf($sprintf, 'INST_VERSION',
  3626. $self->inst_version) if $local_file;
  3627. join "", @m, "\n";
  3628. }
  3629. sub manpage_headline {
  3630. my($self,$local_file) = @_;
  3631. my(@local_file) = $local_file;
  3632. $local_file =~ s/\.pm$/.pod/;
  3633. push @local_file, $local_file;
  3634. my(@result,$locf);
  3635. for $locf (@local_file) {
  3636. next unless -f $locf;
  3637. my $fh = FileHandle->new($locf)
  3638. or $Carp::Frontend->mydie("Couldn't open $locf: $!");
  3639. my $inpod = 0;
  3640. local $/ = "\n";
  3641. while (<$fh>) {
  3642. $inpod = m/^=(?!head1\s+NAME)/ ? 0 :
  3643. m/^=head1\s+NAME/ ? 1 : $inpod;
  3644. next unless $inpod;
  3645. next if /^=/;
  3646. next if /^\s+$/;
  3647. chomp;
  3648. push @result, $_;
  3649. }
  3650. close $fh;
  3651. last if @result;
  3652. }
  3653. join " ", @result;
  3654. }
  3655. #-> sub CPAN::Module::cpan_file ;
  3656. sub cpan_file {
  3657. my $self = shift;
  3658. CPAN->debug($self->id) if $CPAN::DEBUG;
  3659. unless (defined $self->{'CPAN_FILE'}) {
  3660. CPAN::Index->reload;
  3661. }
  3662. if (exists $self->{'CPAN_FILE'} && defined $self->{'CPAN_FILE'}){
  3663. return $self->{'CPAN_FILE'};
  3664. } elsif (exists $self->{'userid'} && defined $self->{'userid'}) {
  3665. my $fullname = $CPAN::META->instance(CPAN::Author,
  3666. $self->{'userid'})->fullname;
  3667. my $email = $CPAN::META->instance(CPAN::Author,
  3668. $self->{'userid'})->email;
  3669. unless (defined $fullname && defined $email) {
  3670. return "Contact Author $self->{userid} (Try ``a $self->{userid}'')";
  3671. }
  3672. return "Contact Author $fullname <$email>";
  3673. } else {
  3674. return "N/A";
  3675. }
  3676. }
  3677. *name = \&cpan_file;
  3678. #-> sub CPAN::Module::cpan_version ;
  3679. sub cpan_version {
  3680. my $self = shift;
  3681. $self->{'CPAN_VERSION'} = 'undef'
  3682. unless defined $self->{'CPAN_VERSION'}; # I believe this is
  3683. # always a bug in the
  3684. # index and should be
  3685. # reported as such,
  3686. # but usually I find
  3687. # out such an error
  3688. # and do not want to
  3689. # provoke too many
  3690. # bugreports
  3691. $self->{'CPAN_VERSION'};
  3692. }
  3693. #-> sub CPAN::Module::force ;
  3694. sub force {
  3695. my($self) = @_;
  3696. $self->{'force_update'}++;
  3697. }
  3698. #-> sub CPAN::Module::rematein ;
  3699. sub rematein {
  3700. my($self,$meth) = @_;
  3701. $self->debug($self->id) if $CPAN::DEBUG;
  3702. my $cpan_file = $self->cpan_file;
  3703. if ($cpan_file eq "N/A" || $cpan_file =~ /^Contact Author/){
  3704. $CPAN::Frontend->mywarn(sprintf qq{
  3705. The module %s isn\'t available on CPAN.
  3706. Either the module has not yet been uploaded to CPAN, or it is
  3707. temporary unavailable. Please contact the author to find out
  3708. more about the status. Try ``i %s''.
  3709. },
  3710. $self->id,
  3711. $self->id,
  3712. );
  3713. return;
  3714. }
  3715. my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  3716. $pack->called_for($self->id);
  3717. $pack->force if exists $self->{'force_update'};
  3718. $pack->$meth();
  3719. delete $self->{'force_update'};
  3720. }
  3721. #-> sub CPAN::Module::readme ;
  3722. sub readme { shift->rematein('readme') }
  3723. #-> sub CPAN::Module::look ;
  3724. sub look { shift->rematein('look') }
  3725. #-> sub CPAN::Module::get ;
  3726. sub get { shift->rematein('get',@_); }
  3727. #-> sub CPAN::Module::make ;
  3728. sub make { shift->rematein('make') }
  3729. #-> sub CPAN::Module::test ;
  3730. sub test { shift->rematein('test') }
  3731. #-> sub CPAN::Module::uptodate ;
  3732. sub uptodate {
  3733. my($self) = @_;
  3734. my($latest) = $self->cpan_version;
  3735. $latest ||= 0;
  3736. my($inst_file) = $self->inst_file;
  3737. my($have) = 0;
  3738. if (defined $inst_file) {
  3739. $have = $self->inst_version;
  3740. }
  3741. local($^W)=0;
  3742. if ($inst_file
  3743. &&
  3744. $have >= $latest
  3745. ) {
  3746. return 1;
  3747. }
  3748. return;
  3749. }
  3750. #-> sub CPAN::Module::install ;
  3751. sub install {
  3752. my($self) = @_;
  3753. my($doit) = 0;
  3754. if ($self->uptodate
  3755. &&
  3756. not exists $self->{'force_update'}
  3757. ) {
  3758. $CPAN::Frontend->myprint( $self->id. " is up to date.\n");
  3759. } else {
  3760. $doit = 1;
  3761. }
  3762. $self->rematein('install') if $doit;
  3763. }
  3764. #-> sub CPAN::Module::clean ;
  3765. sub clean { shift->rematein('clean') }
  3766. #-> sub CPAN::Module::inst_file ;
  3767. sub inst_file {
  3768. my($self) = @_;
  3769. my($dir,@packpath);
  3770. @packpath = split /::/, $self->{ID};
  3771. $packpath[-1] .= ".pm";
  3772. foreach $dir (@INC) {
  3773. my $pmfile = MM->catfile($dir,@packpath);
  3774. if (-f $pmfile){
  3775. return $pmfile;
  3776. }
  3777. }
  3778. return;
  3779. }
  3780. #-> sub CPAN::Module::xs_file ;
  3781. sub xs_file {
  3782. my($self) = @_;
  3783. my($dir,@packpath);
  3784. @packpath = split /::/, $self->{ID};
  3785. push @packpath, $packpath[-1];
  3786. $packpath[-1] .= "." . $Config::Config{'dlext'};
  3787. foreach $dir (@INC) {
  3788. my $xsfile = MM->catfile($dir,'auto',@packpath);
  3789. if (-f $xsfile){
  3790. return $xsfile;
  3791. }
  3792. }
  3793. return;
  3794. }
  3795. #-> sub CPAN::Module::inst_version ;
  3796. sub inst_version {
  3797. my($self) = @_;
  3798. my $parsefile = $self->inst_file or return;
  3799. local($^W) = 0 if $] < 5.00303 && $ExtUtils::MakeMaker::VERSION < 5.38;
  3800. # warn "HERE";
  3801. my $have = MM->parse_version($parsefile) || "undef";
  3802. $have =~ s/\s+//g;
  3803. $have;
  3804. }
  3805. package CPAN::Tarzip;
  3806. sub gzip {
  3807. my($class,$read,$write) = @_;
  3808. if ($CPAN::META->has_inst("Compress::Zlib")) {
  3809. my($buffer,$fhw);
  3810. $fhw = FileHandle->new($read)
  3811. or $CPAN::Frontend->mydie("Could not open $read: $!");
  3812. my $gz = Compress::Zlib::gzopen($write, "wb")
  3813. or $CPAN::Frontend->mydie("Cannot gzopen $write: $!\n");
  3814. $gz->gzwrite($buffer)
  3815. while read($fhw,$buffer,4096) > 0 ;
  3816. $gz->gzclose() ;
  3817. $fhw->close;
  3818. return 1;
  3819. } else {
  3820. system("$CPAN::Config->{'gzip'} -c $read > $write")==0;
  3821. }
  3822. }
  3823. sub gunzip {
  3824. my($class,$read,$write) = @_;
  3825. if ($CPAN::META->has_inst("Compress::Zlib")) {
  3826. my($buffer,$fhw);
  3827. $fhw = FileHandle->new(">$write")
  3828. or $CPAN::Frontend->mydie("Could not open >$write: $!");
  3829. my $gz = Compress::Zlib::gzopen($read, "rb")
  3830. or $CPAN::Frontend->mydie("Cannot gzopen $read: $!\n");
  3831. $fhw->print($buffer)
  3832. while $gz->gzread($buffer) > 0 ;
  3833. $CPAN::Frontend->mydie("Error reading from $read: $!\n")
  3834. if $gz->gzerror != Compress::Zlib::Z_STREAM_END();
  3835. $gz->gzclose() ;
  3836. $fhw->close;
  3837. return 1;
  3838. } else {
  3839. system("$CPAN::Config->{'gzip'} -dc $read > $write")==0;
  3840. }
  3841. }
  3842. sub gtest {
  3843. my($class,$read) = @_;
  3844. if ($CPAN::META->has_inst("Compress::Zlib")) {
  3845. my($buffer);
  3846. my $gz = Compress::Zlib::gzopen($read, "rb")
  3847. or $CPAN::Frontend->mydie("Cannot open $read: $!\n");
  3848. 1 while $gz->gzread($buffer) > 0 ;
  3849. $CPAN::Frontend->mydie("Error reading from $read: $!\n")
  3850. if $gz->gzerror != Compress::Zlib::Z_STREAM_END();
  3851. $gz->gzclose() ;
  3852. return 1;
  3853. } else {
  3854. return system("$CPAN::Config->{'gzip'} -dt $read")==0;
  3855. }
  3856. }
  3857. sub TIEHANDLE {
  3858. my($class,$file) = @_;
  3859. my $ret;
  3860. $class->debug("file[$file]");
  3861. if ($CPAN::META->has_inst("Compress::Zlib")) {
  3862. my $gz = Compress::Zlib::gzopen($file,"rb") or
  3863. die "Could not gzopen $file";
  3864. $ret = bless {GZ => $gz}, $class;
  3865. } else {
  3866. my $pipe = "$CPAN::Config->{'gzip'} --decompress --stdout $file |";
  3867. my $fh = FileHandle->new($pipe) or die "Could pipe[$pipe]: $!";
  3868. binmode $fh;
  3869. $ret = bless {FH => $fh}, $class;
  3870. }
  3871. $ret;
  3872. }
  3873. sub READLINE {
  3874. my($self) = @_;
  3875. if (exists $self->{GZ}) {
  3876. my $gz = $self->{GZ};
  3877. my($line,$bytesread);
  3878. $bytesread = $gz->gzreadline($line);
  3879. return undef if $bytesread == 0;
  3880. return $line;
  3881. } else {
  3882. my $fh = $self->{FH};
  3883. return scalar <$fh>;
  3884. }
  3885. }
  3886. sub READ {
  3887. my($self,$ref,$length,$offset) = @_;
  3888. die "read with offset not implemented" if defined $offset;
  3889. if (exists $self->{GZ}) {
  3890. my $gz = $self->{GZ};
  3891. my $byteread = $gz->gzread($$ref,$length);# 30eaf79e8b446ef52464b5422da328a8
  3892. return $byteread;
  3893. } else {
  3894. my $fh = $self->{FH};
  3895. return read($fh,$$ref,$length);
  3896. }
  3897. }
  3898. sub DESTROY {
  3899. my($self) = @_;
  3900. if (exists $self->{GZ}) {
  3901. my $gz = $self->{GZ};
  3902. $gz->gzclose();
  3903. } else {
  3904. my $fh = $self->{FH};
  3905. $fh->close;
  3906. }
  3907. undef $self;
  3908. }
  3909. sub untar {
  3910. my($class,$file) = @_;
  3911. # had to disable, because version 0.07 seems to be buggy
  3912. if (MM->maybe_command($CPAN::Config->{'gzip'})
  3913. &&
  3914. MM->maybe_command($CPAN::Config->{'tar'})) {
  3915. if ($^O =~ /win/i) { # irgggh
  3916. # people find the most curious tar binaries that cannot handle
  3917. # pipes
  3918. my $system = "$CPAN::Config->{'gzip'} --decompress $file";
  3919. if (system($system)==0) {
  3920. $CPAN::Frontend->myprint(qq{Uncompressed $file successfully\n});
  3921. } else {
  3922. $CPAN::Frontend->mydie(
  3923. qq{Couldn\'t uncompress $file\n}
  3924. );
  3925. }
  3926. $file =~ s/\.gz$//;
  3927. $system = "$CPAN::Config->{tar} xvf $file";
  3928. if (system($system)==0) {
  3929. $CPAN::Frontend->myprint(qq{Untarred $file successfully\n});
  3930. } else {
  3931. $CPAN::Frontend->mydie(qq{Couldn\'t untar $file\n});
  3932. }
  3933. return 1;
  3934. } else {
  3935. my $system = "$CPAN::Config->{'gzip'} --decompress --stdout " .
  3936. "< $file | $CPAN::Config->{tar} xvf -";
  3937. return system($system) == 0;
  3938. }
  3939. } elsif ($CPAN::META->has_inst("Archive::Tar")
  3940. &&
  3941. $CPAN::META->has_inst("Compress::Zlib") ) {
  3942. my $tar = Archive::Tar->new($file,1);
  3943. $tar->extract($tar->list_files); # I'm pretty sure we have nothing
  3944. # that isn't compressed
  3945. ExtUtils::MM_MacOS::convert_files([$tar->list_files], 1)
  3946. if ($^O eq 'MacOS');
  3947. return 1;
  3948. } else {
  3949. $CPAN::Frontend->mydie(qq{
  3950. CPAN.pm needs either both external programs tar and gzip installed or
  3951. both the modules Archive::Tar and Compress::Zlib. Neither prerequisite
  3952. is available. Can\'t continue.
  3953. });
  3954. }
  3955. }
  3956. package CPAN;
  3957. 1;
  3958. __END__
  3959. =head1 NAME
  3960. CPAN - query, download and build perl modules from CPAN sites
  3961. =head1 SYNOPSIS
  3962. Interactive mode:
  3963. perl -MCPAN -e shell;
  3964. Batch mode:
  3965. use CPAN;
  3966. autobundle, clean, install, make, recompile, test
  3967. =head1 DESCRIPTION
  3968. The CPAN module is designed to automate the make and install of perl
  3969. modules and extensions. It includes some searching capabilities and
  3970. knows how to use Net::FTP or LWP (or lynx or an external ftp client)
  3971. to fetch the raw data from the net.
  3972. Modules are fetched from one or more of the mirrored CPAN
  3973. (Comprehensive Perl Archive Network) sites and unpacked in a dedicated
  3974. directory.
  3975. The CPAN module also supports the concept of named and versioned
  3976. 'bundles' of modules. Bundles simplify the handling of sets of
  3977. related modules. See BUNDLES below.
  3978. The package contains a session manager and a cache manager. There is
  3979. no status retained between sessions. The session manager keeps track
  3980. of what has been fetched, built and installed in the current
  3981. session. The cache manager keeps track of the disk space occupied by
  3982. the make processes and deletes excess space according to a simple FIFO
  3983. mechanism.
  3984. For extended searching capabilities there's a plugin for CPAN available,
  3985. L<CPAN::WAIT>. C<CPAN::WAIT> is a full-text search engine that indexes
  3986. all documents available in CPAN authors directories. If C<CPAN::WAIT>
  3987. is installed on your system, the interactive shell of <CPAN.pm> will
  3988. enable the C<wq>, C<wr>, C<wd>, C<wl>, and C<wh> commands which send
  3989. queries to the WAIT server that has been configured for your
  3990. installation.
  3991. All other methods provided are accessible in a programmer style and in an
  3992. interactive shell style.
  3993. =head2 Interactive Mode
  3994. The interactive mode is entered by running
  3995. perl -MCPAN -e shell
  3996. which puts you into a readline interface. You will have the most fun if
  3997. you install Term::ReadKey and Term::ReadLine to enjoy both history and
  3998. command completion.
  3999. Once you are on the command line, type 'h' and the rest should be
  4000. self-explanatory.
  4001. The most common uses of the interactive modes are
  4002. =over 2
  4003. =item Searching for authors, bundles, distribution files and modules
  4004. There are corresponding one-letter commands C<a>, C<b>, C<d>, and C<m>
  4005. for each of the four categories and another, C<i> for any of the
  4006. mentioned four. Each of the four entities is implemented as a class
  4007. with slightly differing methods for displaying an object.
  4008. Arguments you pass to these commands are either strings exactly matching
  4009. the identification string of an object or regular expressions that are
  4010. then matched case-insensitively against various attributes of the
  4011. objects. The parser recognizes a regular expression only if you
  4012. enclose it between two slashes.
  4013. The principle is that the number of found objects influences how an
  4014. item is displayed. If the search finds one item, the result is displayed
  4015. as object-E<gt>as_string, but if we find more than one, we display
  4016. each as object-E<gt>as_glimpse. E.g.
  4017. cpan> a ANDK
  4018. Author id = ANDK
  4019. EMAIL a.koenig@franz.ww.TU-Berlin.DE
  4020. FULLNAME Andreas Knig
  4021. cpan> a /andk/
  4022. Author id = ANDK
  4023. EMAIL a.koenig@franz.ww.TU-Berlin.DE
  4024. FULLNAME Andreas Knig
  4025. cpan> a /and.*rt/
  4026. Author ANDYD (Andy Dougherty)
  4027. Author MERLYN (Randal L. Schwartz)
  4028. =item make, test, install, clean modules or distributions
  4029. These commands take any number of arguments and investigates what is
  4030. necessary to perform the action. If the argument is a distribution
  4031. file name (recognized by embedded slashes), it is processed. If it is
  4032. a module, CPAN determines the distribution file in which this module
  4033. is included and processes that, following any dependencies named in
  4034. the module's Makefile.PL (this behavior is controlled by
  4035. I<prerequisites_policy>.)
  4036. Any C<make> or C<test> are run unconditionally. An
  4037. install <distribution_file>
  4038. also is run unconditionally. But for
  4039. install <module>
  4040. CPAN checks if an install is actually needed for it and prints
  4041. I<module up to date> in the case that the distribution file containing
  4042. the module doesnE<39>t need to be updated.
  4043. CPAN also keeps track of what it has done within the current session
  4044. and doesnE<39>t try to build a package a second time regardless if it
  4045. succeeded or not. The C<force> command takes as a first argument the
  4046. method to invoke (currently: C<make>, C<test>, or C<install>) and executes the
  4047. command from scratch.
  4048. Example:
  4049. cpan> install OpenGL
  4050. OpenGL is up to date.
  4051. cpan> force install OpenGL
  4052. Running make
  4053. OpenGL-0.4/
  4054. OpenGL-0.4/COPYRIGHT
  4055. [...]
  4056. A C<clean> command results in a
  4057. make clean
  4058. being executed within the distribution file's working directory.
  4059. =item readme, look module or distribution
  4060. These two commands take only one argument, be it a module or a
  4061. distribution file. C<readme> unconditionally runs, displaying the
  4062. README of the associated distribution file. C<Look> gets and
  4063. untars (if not yet done) the distribution file, changes to the
  4064. appropriate directory and opens a subshell process in that directory.
  4065. =item Signals
  4066. CPAN.pm installs signal handlers for SIGINT and SIGTERM. While you are
  4067. in the cpan-shell it is intended that you can press C<^C> anytime and
  4068. return to the cpan-shell prompt. A SIGTERM will cause the cpan-shell
  4069. to clean up and leave the shell loop. You can emulate the effect of a
  4070. SIGTERM by sending two consecutive SIGINTs, which usually means by
  4071. pressing C<^C> twice.
  4072. CPAN.pm ignores a SIGPIPE. If the user sets inactivity_timeout, a
  4073. SIGALRM is used during the run of the C<perl Makefile.PL> subprocess.
  4074. =back
  4075. =head2 CPAN::Shell
  4076. The commands that are available in the shell interface are methods in
  4077. the package CPAN::Shell. If you enter the shell command, all your
  4078. input is split by the Text::ParseWords::shellwords() routine which
  4079. acts like most shells do. The first word is being interpreted as the
  4080. method to be called and the rest of the words are treated as arguments
  4081. to this method. Continuation lines are supported if a line ends with a
  4082. literal backslash.
  4083. =head2 autobundle
  4084. C<autobundle> writes a bundle file into the
  4085. C<$CPAN::Config-E<gt>{cpan_home}/Bundle> directory. The file contains
  4086. a list of all modules that are both available from CPAN and currently
  4087. installed within @INC. The name of the bundle file is based on the
  4088. current date and a counter.
  4089. =head2 recompile
  4090. recompile() is a very special command in that it takes no argument and
  4091. runs the make/test/install cycle with brute force over all installed
  4092. dynamically loadable extensions (aka XS modules) with 'force' in
  4093. effect. The primary purpose of this command is to finish a network
  4094. installation. Imagine, you have a common source tree for two different
  4095. architectures. You decide to do a completely independent fresh
  4096. installation. You start on one architecture with the help of a Bundle
  4097. file produced earlier. CPAN installs the whole Bundle for you, but
  4098. when you try to repeat the job on the second architecture, CPAN
  4099. responds with a C<"Foo up to date"> message for all modules. So you
  4100. invoke CPAN's recompile on the second architecture and youE<39>re done.
  4101. Another popular use for C<recompile> is to act as a rescue in case your
  4102. perl breaks binary compatibility. If one of the modules that CPAN uses
  4103. is in turn depending on binary compatibility (so you cannot run CPAN
  4104. commands), then you should try the CPAN::Nox module for recovery.
  4105. =head2 The four C<CPAN::*> Classes: Author, Bundle, Module, Distribution
  4106. Although it may be considered internal, the class hierarchy does matter
  4107. for both users and programmer. CPAN.pm deals with above mentioned four
  4108. classes, and all those classes share a set of methods. A classical
  4109. single polymorphism is in effect. A metaclass object registers all
  4110. objects of all kinds and indexes them with a string. The strings
  4111. referencing objects have a separated namespace (well, not completely
  4112. separated):
  4113. Namespace Class
  4114. words containing a "/" (slash) Distribution
  4115. words starting with Bundle:: Bundle
  4116. everything else Module or Author
  4117. Modules know their associated Distribution objects. They always refer
  4118. to the most recent official release. Developers may mark their releases
  4119. as unstable development versions (by inserting an underbar into the
  4120. visible version number), so the really hottest and newest distribution
  4121. file is not always the default. If a module Foo circulates on CPAN in
  4122. both version 1.23 and 1.23_90, CPAN.pm offers a convenient way to
  4123. install version 1.23 by saying
  4124. install Foo
  4125. This would install the complete distribution file (say
  4126. BAR/Foo-1.23.tar.gz) with all accompanying material. But if you would
  4127. like to install version 1.23_90, you need to know where the
  4128. distribution file resides on CPAN relative to the authors/id/
  4129. directory. If the author is BAR, this might be BAR/Foo-1.23_90.tar.gz;
  4130. so you would have to say
  4131. install BAR/Foo-1.23_90.tar.gz
  4132. The first example will be driven by an object of the class
  4133. CPAN::Module, the second by an object of class CPAN::Distribution.
  4134. =head2 ProgrammerE<39>s interface
  4135. If you do not enter the shell, the available shell commands are both
  4136. available as methods (C<CPAN::Shell-E<gt>install(...)>) and as
  4137. functions in the calling package (C<install(...)>).
  4138. There's currently only one class that has a stable interface -
  4139. CPAN::Shell. All commands that are available in the CPAN shell are
  4140. methods of the class CPAN::Shell. Each of the commands that produce
  4141. listings of modules (C<r>, C<autobundle>, C<u>) returns a list of the
  4142. IDs of all modules within the list.
  4143. =over 2
  4144. =item expand($type,@things)
  4145. The IDs of all objects available within a program are strings that can
  4146. be expanded to the corresponding real objects with the
  4147. C<CPAN::Shell-E<gt>expand("Module",@things)> method. Expand returns a
  4148. list of CPAN::Module objects according to the C<@things> arguments
  4149. given. In scalar context it only returns the first element of the
  4150. list.
  4151. =item Programming Examples
  4152. This enables the programmer to do operations that combine
  4153. functionalities that are available in the shell.
  4154. # install everything that is outdated on my disk:
  4155. perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
  4156. # install my favorite programs if necessary:
  4157. for $mod (qw(Net::FTP MD5 Data::Dumper)){
  4158. my $obj = CPAN::Shell->expand('Module',$mod);
  4159. $obj->install;
  4160. }
  4161. # list all modules on my disk that have no VERSION number
  4162. for $mod (CPAN::Shell->expand("Module","/./")){
  4163. next unless $mod->inst_file;
  4164. # MakeMaker convention for undefined $VERSION:
  4165. next unless $mod->inst_version eq "undef";
  4166. print "No VERSION in ", $mod->id, "\n";
  4167. }
  4168. =back
  4169. =head2 Methods in the four Classes
  4170. =head2 Cache Manager
  4171. Currently the cache manager only keeps track of the build directory
  4172. ($CPAN::Config->{build_dir}). It is a simple FIFO mechanism that
  4173. deletes complete directories below C<build_dir> as soon as the size of
  4174. all directories there gets bigger than $CPAN::Config->{build_cache}
  4175. (in MB). The contents of this cache may be used for later
  4176. re-installations that you intend to do manually, but will never be
  4177. trusted by CPAN itself. This is due to the fact that the user might
  4178. use these directories for building modules on different architectures.
  4179. There is another directory ($CPAN::Config->{keep_source_where}) where
  4180. the original distribution files are kept. This directory is not
  4181. covered by the cache manager and must be controlled by the user. If
  4182. you choose to have the same directory as build_dir and as
  4183. keep_source_where directory, then your sources will be deleted with
  4184. the same fifo mechanism.
  4185. =head2 Bundles
  4186. A bundle is just a perl module in the namespace Bundle:: that does not
  4187. define any functions or methods. It usually only contains documentation.
  4188. It starts like a perl module with a package declaration and a $VERSION
  4189. variable. After that the pod section looks like any other pod with the
  4190. only difference being that I<one special pod section> exists starting with
  4191. (verbatim):
  4192. =head1 CONTENTS
  4193. In this pod section each line obeys the format
  4194. Module_Name [Version_String] [- optional text]
  4195. The only required part is the first field, the name of a module
  4196. (e.g. Foo::Bar, ie. I<not> the name of the distribution file). The rest
  4197. of the line is optional. The comment part is delimited by a dash just
  4198. as in the man page header.
  4199. The distribution of a bundle should follow the same convention as
  4200. other distributions.
  4201. Bundles are treated specially in the CPAN package. If you say 'install
  4202. Bundle::Tkkit' (assuming such a bundle exists), CPAN will install all
  4203. the modules in the CONTENTS section of the pod. You can install your
  4204. own Bundles locally by placing a conformant Bundle file somewhere into
  4205. your @INC path. The autobundle() command which is available in the
  4206. shell interface does that for you by including all currently installed
  4207. modules in a snapshot bundle file.
  4208. =head2 Prerequisites
  4209. If you have a local mirror of CPAN and can access all files with
  4210. "file:" URLs, then you only need a perl better than perl5.003 to run
  4211. this module. Otherwise Net::FTP is strongly recommended. LWP may be
  4212. required for non-UNIX systems or if your nearest CPAN site is
  4213. associated with an URL that is not C<ftp:>.
  4214. If you have neither Net::FTP nor LWP, there is a fallback mechanism
  4215. implemented for an external ftp command or for an external lynx
  4216. command.
  4217. =head2 Finding packages and VERSION
  4218. This module presumes that all packages on CPAN
  4219. =over 2
  4220. =item *
  4221. declare their $VERSION variable in an easy to parse manner. This
  4222. prerequisite can hardly be relaxed because it consumes far too much
  4223. memory to load all packages into the running program just to determine
  4224. the $VERSION variable. Currently all programs that are dealing with
  4225. version use something like this
  4226. perl -MExtUtils::MakeMaker -le \
  4227. 'print MM->parse_version(shift)' filename
  4228. If you are author of a package and wonder if your $VERSION can be
  4229. parsed, please try the above method.
  4230. =item *
  4231. come as compressed or gzipped tarfiles or as zip files and contain a
  4232. Makefile.PL (well, we try to handle a bit more, but without much
  4233. enthusiasm).
  4234. =back
  4235. =head2 Debugging
  4236. The debugging of this module is pretty difficult, because we have
  4237. interferences of the software producing the indices on CPAN, of the
  4238. mirroring process on CPAN, of packaging, of configuration, of
  4239. synchronicity, and of bugs within CPAN.pm.
  4240. In interactive mode you can try "o debug" which will list options for
  4241. debugging the various parts of the package. The output may not be very
  4242. useful for you as it's just a by-product of my own testing, but if you
  4243. have an idea which part of the package may have a bug, it's sometimes
  4244. worth to give it a try and send me more specific output. You should
  4245. know that "o debug" has built-in completion support.
  4246. =head2 Floppy, Zip, Offline Mode
  4247. CPAN.pm works nicely without network too. If you maintain machines
  4248. that are not networked at all, you should consider working with file:
  4249. URLs. Of course, you have to collect your modules somewhere first. So
  4250. you might use CPAN.pm to put together all you need on a networked
  4251. machine. Then copy the $CPAN::Config->{keep_source_where} (but not
  4252. $CPAN::Config->{build_dir}) directory on a floppy. This floppy is kind
  4253. of a personal CPAN. CPAN.pm on the non-networked machines works nicely
  4254. with this floppy.
  4255. =head1 CONFIGURATION
  4256. When the CPAN module is installed, a site wide configuration file is
  4257. created as CPAN/Config.pm. The default values defined there can be
  4258. overridden in another configuration file: CPAN/MyConfig.pm. You can
  4259. store this file in $HOME/.cpan/CPAN/MyConfig.pm if you want, because
  4260. $HOME/.cpan is added to the search path of the CPAN module before the
  4261. use() or require() statements.
  4262. Currently the following keys in the hash reference $CPAN::Config are
  4263. defined:
  4264. build_cache size of cache for directories to build modules
  4265. build_dir locally accessible directory to build modules
  4266. index_expire after this many days refetch index files
  4267. cpan_home local directory reserved for this package
  4268. gzip location of external program gzip
  4269. inactivity_timeout breaks interactive Makefile.PLs after this
  4270. many seconds inactivity. Set to 0 to never break.
  4271. inhibit_startup_message
  4272. if true, does not print the startup message
  4273. keep_source keep the source in a local directory?
  4274. keep_source_where directory in which to keep the source (if we do)
  4275. make location of external make program
  4276. make_arg arguments that should always be passed to 'make'
  4277. make_install_arg same as make_arg for 'make install'
  4278. makepl_arg arguments passed to 'perl Makefile.PL'
  4279. pager location of external program more (or any pager)
  4280. prerequisites_policy
  4281. what to do if you are missing module prerequisites
  4282. ('follow' automatically, 'ask' me, or 'ignore')
  4283. scan_cache controls scanning of cache ('atstart' or 'never')
  4284. tar location of external program tar
  4285. unzip location of external program unzip
  4286. urllist arrayref to nearby CPAN sites (or equivalent locations)
  4287. wait_list arrayref to a wait server to try (See CPAN::WAIT)
  4288. ftp_proxy, } the three usual variables for configuring
  4289. http_proxy, } proxy requests. Both as CPAN::Config variables
  4290. no_proxy } and as environment variables configurable.
  4291. You can set and query each of these options interactively in the cpan
  4292. shell with the command set defined within the C<o conf> command:
  4293. =over 2
  4294. =item o conf E<lt>scalar optionE<gt>
  4295. prints the current value of the I<scalar option>
  4296. =item o conf E<lt>scalar optionE<gt> E<lt>valueE<gt>
  4297. Sets the value of the I<scalar option> to I<value>
  4298. =item o conf E<lt>list optionE<gt>
  4299. prints the current value of the I<list option> in MakeMaker's
  4300. neatvalue format.
  4301. =item o conf E<lt>list optionE<gt> [shift|pop]
  4302. shifts or pops the array in the I<list option> variable
  4303. =item o conf E<lt>list optionE<gt> [unshift|push|splice] E<lt>listE<gt>
  4304. works like the corresponding perl commands.
  4305. =back
  4306. =head2 urllist parameter has CD-ROM support
  4307. The C<urllist> parameter of the configuration table contains a list of
  4308. URLs that are to be used for downloading. If the list contains any
  4309. C<file> URLs, CPAN always tries to get files from there first. This
  4310. feature is disabled for index files. So the recommendation for the
  4311. owner of a CD-ROM with CPAN contents is: include your local, possibly
  4312. outdated CD-ROM as a C<file> URL at the end of urllist, e.g.
  4313. o conf urllist push file://localhost/CDROM/CPAN
  4314. CPAN.pm will then fetch the index files from one of the CPAN sites
  4315. that come at the beginning of urllist. It will later check for each
  4316. module if there is a local copy of the most recent version.
  4317. Another peculiarity of urllist is that the site that we could
  4318. successfully fetch the last file from automatically gets a preference
  4319. token and is tried as the first site for the next request. So if you
  4320. add a new site at runtime it may happen that the previously preferred
  4321. site will be tried another time. This means that if you want to disallow
  4322. a site for the next transfer, it must be explicitly removed from
  4323. urllist.
  4324. =head1 SECURITY
  4325. There's no strong security layer in CPAN.pm. CPAN.pm helps you to
  4326. install foreign, unmasked, unsigned code on your machine. We compare
  4327. to a checksum that comes from the net just as the distribution file
  4328. itself. If somebody has managed to tamper with the distribution file,
  4329. they may have as well tampered with the CHECKSUMS file. Future
  4330. development will go towards strong authentication.
  4331. =head1 EXPORT
  4332. Most functions in package CPAN are exported per default. The reason
  4333. for this is that the primary use is intended for the cpan shell or for
  4334. oneliners.
  4335. =head1 POPULATE AN INSTALLATION WITH LOTS OF MODULES
  4336. To populate a freshly installed perl with my favorite modules is pretty
  4337. easiest by maintaining a private bundle definition file. To get a useful
  4338. blueprint of a bundle definition file, the command autobundle can be used
  4339. on the CPAN shell command line. This command writes a bundle definition
  4340. file for all modules that re installed for the currently running perl
  4341. interpreter. It's recommended to run this command only once and from then
  4342. on maintain the file manually under a private name, say
  4343. Bundle/my_bundle.pm. With a clever bundle file you can then simply say
  4344. cpan> install Bundle::my_bundle
  4345. then answer a few questions and then go out.
  4346. Maintaining a bundle definition file means to keep track of two things:
  4347. dependencies and interactivity. CPAN.pm (currently) does not take into
  4348. account dependencies between distributions, so a bundle definition file
  4349. should specify distributions that depend on others B<after> the others.
  4350. On the other hand, it's a bit annoying that many distributions need some
  4351. interactive configuring. So what I try to accomplish in my private bundle
  4352. file is to have the packages that need to be configured early in the file
  4353. and the gentle ones later, so I can go out after a few minutes and leave
  4354. CPAN.pm unattained.
  4355. =head1 WORKING WITH CPAN.pm BEHIND FIREWALLS
  4356. Thanks to Graham Barr for contributing the firewall following howto.
  4357. Firewalls can be categorized into three basic types.
  4358. =over
  4359. =item http firewall
  4360. This is where the firewall machine runs a web server and to access the
  4361. outside world you must do it via the web server. If you set environment
  4362. variables like http_proxy or ftp_proxy to a values beginning with http://
  4363. or in your web browser you have to set proxy information then you know
  4364. you are running a http firewall.
  4365. To access servers outside these types of firewalls with perl (even for
  4366. ftp) you will need to use LWP.
  4367. =item ftp firewall
  4368. This where the firewall machine runs a ftp server. This kind of firewall will
  4369. only let you access ftp serves outside the firewall. This is usually done by
  4370. connecting to the firewall with ftp, then entering a username like
  4371. "[email protected]"
  4372. To access servers outside these type of firewalls with perl you
  4373. will need to use Net::FTP.
  4374. =item One way visibility
  4375. I say one way visibility as these firewalls try to make themselve look
  4376. invisible to the users inside the firewall. An FTP data connection is
  4377. normally created by sending the remote server your IP address and then
  4378. listening for the connection. But the remote server will not be able to
  4379. connect to you because of the firewall. So for these types of firewall
  4380. FTP connections need to be done in a passive mode.
  4381. There are two that I can think off.
  4382. =over
  4383. =item SOCKS
  4384. If you are using a SOCKS firewall you will need to compile perl and link
  4385. it with the SOCKS library, this is what is normally called a ``socksified''
  4386. perl. With this executable you will be able to connect to servers outside
  4387. the firewall as if it is not there.
  4388. =item IP Masquerade
  4389. This is the firewall implemented in the Linux kernel, it allows you to
  4390. hide a complete network behind one IP address. With this firewall no
  4391. special compiling is need as you can access hosts directly.
  4392. =back
  4393. =back
  4394. =head1 BUGS
  4395. We should give coverage for _all_ of the CPAN and not just the PAUSE
  4396. part, right? In this discussion CPAN and PAUSE have become equal --
  4397. but they are not. PAUSE is authors/ and modules/. CPAN is PAUSE plus
  4398. the clpa/, doc/, misc/, ports/, src/, scripts/.
  4399. Future development should be directed towards a better integration of
  4400. the other parts.
  4401. If a Makefile.PL requires special customization of libraries, prompts
  4402. the user for special input, etc. then you may find CPAN is not able to
  4403. build the distribution. In that case, you should attempt the
  4404. traditional method of building a Perl module package from a shell.
  4405. =head1 AUTHOR
  4406. Andreas Knig E<lt>a.koenig@kulturbox.deE<gt>
  4407. =head1 SEE ALSO
  4408. perl(1), CPAN::Nox(3)
  4409. =cut