PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/linkedfs/usr/lib/perl5/5.8.6/CPAN.pm

https://bitbucket.org/harakiri/trk
Perl | 7169 lines | 6676 code | 175 blank | 318 comment | 385 complexity | 5e757b07ad3ef5e5ad6a09dae26f9563 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
  2. package CPAN;
  3. $VERSION = '1.76_01';
  4. $VERSION = eval $VERSION;
  5. # $Id: CPAN.pm,v 1.412 2003/07/31 14:53:04 k Exp $
  6. # only used during development:
  7. $Revision = "";
  8. # $Revision = "[".substr(q$Revision: 1.412 $, 10)."]";
  9. use Carp ();
  10. use Config ();
  11. use Cwd ();
  12. use DirHandle;
  13. use Exporter ();
  14. use ExtUtils::MakeMaker (); # $SelfLoader::DEBUG=1;
  15. use File::Basename ();
  16. use File::Copy ();
  17. use File::Find;
  18. use File::Path ();
  19. use FileHandle ();
  20. use Safe ();
  21. use Text::ParseWords ();
  22. use Text::Wrap;
  23. use File::Spec;
  24. use Sys::Hostname;
  25. no lib "."; # we need to run chdir all over and we would get at wrong
  26. # libraries there
  27. require Mac::BuildTools if $^O eq 'MacOS';
  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. Version 16384
  45. Queue 32768
  46. ];
  47. $CPAN::DEBUG ||= 0;
  48. $CPAN::Signal ||= 0;
  49. $CPAN::Frontend ||= "CPAN::Shell";
  50. $CPAN::Defaultsite ||= "ftp://ftp.perl.org/pub/CPAN";
  51. package CPAN;
  52. use strict qw(vars);
  53. use vars qw($VERSION @EXPORT $AUTOLOAD $DEBUG $META $HAS_USABLE $term
  54. $Revision $Signal $End $Suppress_readline $Frontend
  55. $Defaultsite $Have_warned);
  56. @CPAN::ISA = qw(CPAN::Debug Exporter);
  57. @EXPORT = qw(
  58. autobundle bundle expand force get cvs_import
  59. install make readme recompile shell test clean
  60. );
  61. #-> sub CPAN::AUTOLOAD ;
  62. sub AUTOLOAD {
  63. my($l) = $AUTOLOAD;
  64. $l =~ s/.*:://;
  65. my(%EXPORT);
  66. @EXPORT{@EXPORT} = '';
  67. CPAN::Config->load unless $CPAN::Config_loaded++;
  68. if (exists $EXPORT{$l}){
  69. CPAN::Shell->$l(@_);
  70. } else {
  71. $CPAN::Frontend->mywarn(qq{Unknown command "$AUTOLOAD". }.
  72. qq{Type ? for help.
  73. });
  74. }
  75. }
  76. #-> sub CPAN::shell ;
  77. sub shell {
  78. my($self) = @_;
  79. $Suppress_readline = ! -t STDIN unless defined $Suppress_readline;
  80. CPAN::Config->load unless $CPAN::Config_loaded++;
  81. my $oprompt = shift || "cpan> ";
  82. my $prompt = $oprompt;
  83. my $commandline = shift || "";
  84. local($^W) = 1;
  85. unless ($Suppress_readline) {
  86. require Term::ReadLine;
  87. if (! $term
  88. or
  89. $term->ReadLine eq "Term::ReadLine::Stub"
  90. ) {
  91. $term = Term::ReadLine->new('CPAN Monitor');
  92. }
  93. if ($term->ReadLine eq "Term::ReadLine::Gnu") {
  94. my $attribs = $term->Attribs;
  95. $attribs->{attempted_completion_function} = sub {
  96. &CPAN::Complete::gnu_cpl;
  97. }
  98. } else {
  99. $readline::rl_completion_function =
  100. $readline::rl_completion_function = 'CPAN::Complete::cpl';
  101. }
  102. if (my $histfile = $CPAN::Config->{'histfile'}) {{
  103. unless ($term->can("AddHistory")) {
  104. $CPAN::Frontend->mywarn("Terminal does not support AddHistory.\n");
  105. last;
  106. }
  107. my($fh) = FileHandle->new;
  108. open $fh, "<$histfile" or last;
  109. local $/ = "\n";
  110. while (<$fh>) {
  111. chomp;
  112. $term->AddHistory($_);
  113. }
  114. close $fh;
  115. }}
  116. # $term->OUT is autoflushed anyway
  117. my $odef = select STDERR;
  118. $| = 1;
  119. select STDOUT;
  120. $| = 1;
  121. select $odef;
  122. }
  123. # no strict; # I do not recall why no strict was here (2000-09-03)
  124. $META->checklock();
  125. my $cwd = CPAN::anycwd();
  126. my $try_detect_readline;
  127. $try_detect_readline = $term->ReadLine eq "Term::ReadLine::Stub" if $term;
  128. my $rl_avail = $Suppress_readline ? "suppressed" :
  129. ($term->ReadLine ne "Term::ReadLine::Stub") ? "enabled" :
  130. "available (try 'install Bundle::CPAN')";
  131. $CPAN::Frontend->myprint(
  132. sprintf qq{
  133. cpan shell -- CPAN exploration and modules installation (v%s%s)
  134. ReadLine support %s
  135. },
  136. $CPAN::VERSION,
  137. $CPAN::Revision,
  138. $rl_avail
  139. )
  140. unless $CPAN::Config->{'inhibit_startup_message'} ;
  141. my($continuation) = "";
  142. SHELLCOMMAND: while () {
  143. if ($Suppress_readline) {
  144. print $prompt;
  145. last SHELLCOMMAND unless defined ($_ = <> );
  146. chomp;
  147. } else {
  148. last SHELLCOMMAND unless
  149. defined ($_ = $term->readline($prompt, $commandline));
  150. }
  151. $_ = "$continuation$_" if $continuation;
  152. s/^\s+//;
  153. next SHELLCOMMAND if /^$/;
  154. $_ = 'h' if /^\s*\?/;
  155. if (/^(?:q(?:uit)?|bye|exit)$/i) {
  156. last SHELLCOMMAND;
  157. } elsif (s/\\$//s) {
  158. chomp;
  159. $continuation = $_;
  160. $prompt = " > ";
  161. } elsif (/^\!/) {
  162. s/^\!//;
  163. my($eval) = $_;
  164. package CPAN::Eval;
  165. use vars qw($import_done);
  166. CPAN->import(':DEFAULT') unless $import_done++;
  167. CPAN->debug("eval[$eval]") if $CPAN::DEBUG;
  168. eval($eval);
  169. warn $@ if $@;
  170. $continuation = "";
  171. $prompt = $oprompt;
  172. } elsif (/./) {
  173. my(@line);
  174. if ($] < 5.00322) { # parsewords had a bug until recently
  175. @line = split;
  176. } else {
  177. eval { @line = Text::ParseWords::shellwords($_) };
  178. warn($@), next SHELLCOMMAND if $@;
  179. warn("Text::Parsewords could not parse the line [$_]"),
  180. next SHELLCOMMAND unless @line;
  181. }
  182. $CPAN::META->debug("line[".join("|",@line)."]") if $CPAN::DEBUG;
  183. my $command = shift @line;
  184. eval { CPAN::Shell->$command(@line) };
  185. warn $@ if $@;
  186. chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  187. $CPAN::Frontend->myprint("\n");
  188. $continuation = "";
  189. $prompt = $oprompt;
  190. }
  191. } continue {
  192. $commandline = ""; # I do want to be able to pass a default to
  193. # shell, but on the second command I see no
  194. # use in that
  195. $Signal=0;
  196. CPAN::Queue->nullify_queue;
  197. if ($try_detect_readline) {
  198. if ($CPAN::META->has_inst("Term::ReadLine::Gnu")
  199. ||
  200. $CPAN::META->has_inst("Term::ReadLine::Perl")
  201. ) {
  202. delete $INC{"Term/ReadLine.pm"};
  203. my $redef = 0;
  204. local($SIG{__WARN__}) = CPAN::Shell::paintdots_onreload(\$redef);
  205. require Term::ReadLine;
  206. $CPAN::Frontend->myprint("\n$redef subroutines in ".
  207. "Term::ReadLine redefined\n");
  208. @_ = ($oprompt,"");
  209. goto &shell;
  210. }
  211. }
  212. }
  213. chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  214. }
  215. package CPAN::CacheMgr;
  216. @CPAN::CacheMgr::ISA = qw(CPAN::InfoObj CPAN);
  217. use File::Find;
  218. package CPAN::Config;
  219. use vars qw(%can $dot_cpan);
  220. %can = (
  221. 'commit' => "Commit changes to disk",
  222. 'defaults' => "Reload defaults from disk",
  223. 'init' => "Interactive setting of all options",
  224. );
  225. package CPAN::FTP;
  226. use vars qw($Ua $Thesite $Themethod);
  227. @CPAN::FTP::ISA = qw(CPAN::Debug);
  228. package CPAN::LWP::UserAgent;
  229. use vars qw(@ISA $USER $PASSWD $SETUPDONE);
  230. # we delay requiring LWP::UserAgent and setting up inheritence until we need it
  231. package CPAN::Complete;
  232. @CPAN::Complete::ISA = qw(CPAN::Debug);
  233. @CPAN::Complete::COMMANDS = sort qw(
  234. ! a b d h i m o q r u autobundle clean dump
  235. make test install force readme reload look
  236. cvs_import ls
  237. ) unless @CPAN::Complete::COMMANDS;
  238. package CPAN::Index;
  239. use vars qw($LAST_TIME $DATE_OF_02 $DATE_OF_03);
  240. @CPAN::Index::ISA = qw(CPAN::Debug);
  241. $LAST_TIME ||= 0;
  242. $DATE_OF_03 ||= 0;
  243. # use constant PROTOCOL => "2.0"; # outcommented to avoid warning on upgrade from 1.57
  244. sub PROTOCOL { 2.0 }
  245. package CPAN::InfoObj;
  246. @CPAN::InfoObj::ISA = qw(CPAN::Debug);
  247. package CPAN::Author;
  248. @CPAN::Author::ISA = qw(CPAN::InfoObj);
  249. package CPAN::Distribution;
  250. @CPAN::Distribution::ISA = qw(CPAN::InfoObj);
  251. package CPAN::Bundle;
  252. @CPAN::Bundle::ISA = qw(CPAN::Module);
  253. package CPAN::Module;
  254. @CPAN::Module::ISA = qw(CPAN::InfoObj);
  255. package CPAN::Exception::RecursiveDependency;
  256. use overload '""' => "as_string";
  257. sub new {
  258. my($class) = shift;
  259. my($deps) = shift;
  260. my @deps;
  261. my %seen;
  262. for my $dep (@$deps) {
  263. push @deps, $dep;
  264. last if $seen{$dep}++;
  265. }
  266. bless { deps => \@deps }, $class;
  267. }
  268. sub as_string {
  269. my($self) = shift;
  270. "\nRecursive dependency detected:\n " .
  271. join("\n => ", @{$self->{deps}}) .
  272. ".\nCannot continue.\n";
  273. }
  274. package CPAN::Shell;
  275. use vars qw($AUTOLOAD @ISA $COLOR_REGISTERED $ADVANCED_QUERY $PRINT_ORNAMENTING);
  276. @CPAN::Shell::ISA = qw(CPAN::Debug);
  277. $COLOR_REGISTERED ||= 0;
  278. $PRINT_ORNAMENTING ||= 0;
  279. #-> sub CPAN::Shell::AUTOLOAD ;
  280. sub AUTOLOAD {
  281. my($autoload) = $AUTOLOAD;
  282. my $class = shift(@_);
  283. # warn "autoload[$autoload] class[$class]";
  284. $autoload =~ s/.*:://;
  285. if ($autoload =~ /^w/) {
  286. if ($CPAN::META->has_inst('CPAN::WAIT')) {
  287. CPAN::WAIT->$autoload(@_);
  288. } else {
  289. $CPAN::Frontend->mywarn(qq{
  290. Commands starting with "w" require CPAN::WAIT to be installed.
  291. Please consider installing CPAN::WAIT to use the fulltext index.
  292. For this you just need to type
  293. install CPAN::WAIT
  294. });
  295. }
  296. } else {
  297. $CPAN::Frontend->mywarn(qq{Unknown command '$autoload'. }.
  298. qq{Type ? for help.
  299. });
  300. }
  301. }
  302. package CPAN::Tarzip;
  303. use vars qw($AUTOLOAD @ISA $BUGHUNTING);
  304. @CPAN::Tarzip::ISA = qw(CPAN::Debug);
  305. $BUGHUNTING = 0; # released code must have turned off
  306. package CPAN::Queue;
  307. # One use of the queue is to determine if we should or shouldn't
  308. # announce the availability of a new CPAN module
  309. # Now we try to use it for dependency tracking. For that to happen
  310. # we need to draw a dependency tree and do the leaves first. This can
  311. # easily be reached by running CPAN.pm recursively, but we don't want
  312. # to waste memory and run into deep recursion. So what we can do is
  313. # this:
  314. # CPAN::Queue is the package where the queue is maintained. Dependencies
  315. # often have high priority and must be brought to the head of the queue,
  316. # possibly by jumping the queue if they are already there. My first code
  317. # attempt tried to be extremely correct. Whenever a module needed
  318. # immediate treatment, I either unshifted it to the front of the queue,
  319. # or, if it was already in the queue, I spliced and let it bypass the
  320. # others. This became a too correct model that made it impossible to put
  321. # an item more than once into the queue. Why would you need that? Well,
  322. # you need temporary duplicates as the manager of the queue is a loop
  323. # that
  324. #
  325. # (1) looks at the first item in the queue without shifting it off
  326. #
  327. # (2) cares for the item
  328. #
  329. # (3) removes the item from the queue, *even if its agenda failed and
  330. # even if the item isn't the first in the queue anymore* (that way
  331. # protecting against never ending queues)
  332. #
  333. # So if an item has prerequisites, the installation fails now, but we
  334. # want to retry later. That's easy if we have it twice in the queue.
  335. #
  336. # I also expect insane dependency situations where an item gets more
  337. # than two lives in the queue. Simplest example is triggered by 'install
  338. # Foo Foo Foo'. People make this kind of mistakes and I don't want to
  339. # get in the way. I wanted the queue manager to be a dumb servant, not
  340. # one that knows everything.
  341. #
  342. # Who would I tell in this model that the user wants to be asked before
  343. # processing? I can't attach that information to the module object,
  344. # because not modules are installed but distributions. So I'd have to
  345. # tell the distribution object that it should ask the user before
  346. # processing. Where would the question be triggered then? Most probably
  347. # in CPAN::Distribution::rematein.
  348. # Hope that makes sense, my head is a bit off:-) -- AK
  349. use vars qw{ @All };
  350. # CPAN::Queue::new ;
  351. sub new {
  352. my($class,$s) = @_;
  353. my $self = bless { qmod => $s }, $class;
  354. push @All, $self;
  355. return $self;
  356. }
  357. # CPAN::Queue::first ;
  358. sub first {
  359. my $obj = $All[0];
  360. $obj->{qmod};
  361. }
  362. # CPAN::Queue::delete_first ;
  363. sub delete_first {
  364. my($class,$what) = @_;
  365. my $i;
  366. for my $i (0..$#All) {
  367. if ( $All[$i]->{qmod} eq $what ) {
  368. splice @All, $i, 1;
  369. return;
  370. }
  371. }
  372. }
  373. # CPAN::Queue::jumpqueue ;
  374. sub jumpqueue {
  375. my $class = shift;
  376. my @what = @_;
  377. CPAN->debug(sprintf("before jumpqueue All[%s] what[%s]",
  378. join(",",map {$_->{qmod}} @All),
  379. join(",",@what)
  380. )) if $CPAN::DEBUG;
  381. WHAT: for my $what (reverse @what) {
  382. my $jumped = 0;
  383. for (my $i=0; $i<$#All;$i++) { #prevent deep recursion
  384. CPAN->debug("i[$All[$i]]what[$what]") if $CPAN::DEBUG;
  385. if ($All[$i]->{qmod} eq $what){
  386. $jumped++;
  387. if ($jumped > 100) { # one's OK if e.g. just
  388. # processing now; more are OK if
  389. # user typed it several 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 { qmod => $what }, $class;
  398. unshift @All, $obj;
  399. }
  400. CPAN->debug(sprintf("after jumpqueue All[%s] what[%s]",
  401. join(",",map {$_->{qmod}} @All),
  402. join(",",@what)
  403. )) if $CPAN::DEBUG;
  404. }
  405. # CPAN::Queue::exists ;
  406. sub exists {
  407. my($self,$what) = @_;
  408. my @all = map { $_->{qmod} } @All;
  409. my $exists = grep { $_->{qmod} eq $what } @All;
  410. # warn "in exists what[$what] all[@all] exists[$exists]";
  411. $exists;
  412. }
  413. # CPAN::Queue::delete ;
  414. sub delete {
  415. my($self,$mod) = @_;
  416. @All = grep { $_->{qmod} ne $mod } @All;
  417. }
  418. # CPAN::Queue::nullify_queue ;
  419. sub nullify_queue {
  420. @All = ();
  421. }
  422. package CPAN;
  423. $META ||= CPAN->new; # In case we re-eval ourselves we need the ||
  424. # from here on only subs.
  425. ################################################################################
  426. #-> sub CPAN::all_objects ;
  427. sub all_objects {
  428. my($mgr,$class) = @_;
  429. CPAN::Config->load unless $CPAN::Config_loaded++;
  430. CPAN->debug("mgr[$mgr] class[$class]") if $CPAN::DEBUG;
  431. CPAN::Index->reload;
  432. values %{ $META->{readwrite}{$class} }; # unsafe meta access, ok
  433. }
  434. *all = \&all_objects;
  435. # Called by shell, not in batch mode. In batch mode I see no risk in
  436. # having many processes updating something as installations are
  437. # continually checked at runtime. In shell mode I suspect it is
  438. # unintentional to open more than one shell at a time
  439. #-> sub CPAN::checklock ;
  440. sub checklock {
  441. my($self) = @_;
  442. my $lockfile = File::Spec->catfile($CPAN::Config->{cpan_home},".lock");
  443. if (-f $lockfile && -M _ > 0) {
  444. my $fh = FileHandle->new($lockfile) or
  445. $CPAN::Frontend->mydie("Could not open $lockfile: $!");
  446. my $otherpid = <$fh>;
  447. my $otherhost = <$fh>;
  448. $fh->close;
  449. if (defined $otherpid && $otherpid) {
  450. chomp $otherpid;
  451. }
  452. if (defined $otherhost && $otherhost) {
  453. chomp $otherhost;
  454. }
  455. my $thishost = hostname();
  456. if (defined $otherhost && defined $thishost &&
  457. $otherhost ne '' && $thishost ne '' &&
  458. $otherhost ne $thishost) {
  459. $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Lockfile $lockfile\n".
  460. "reports other host $otherhost and other process $otherpid.\n".
  461. "Cannot proceed.\n"));
  462. }
  463. elsif (defined $otherpid && $otherpid) {
  464. return if $$ == $otherpid; # should never happen
  465. $CPAN::Frontend->mywarn(
  466. qq{
  467. There seems to be running another CPAN process (pid $otherpid). Contacting...
  468. });
  469. if (kill 0, $otherpid) {
  470. $CPAN::Frontend->mydie(qq{Other job is running.
  471. You may want to kill it and delete the lockfile, maybe. On UNIX try:
  472. kill $otherpid
  473. rm $lockfile
  474. });
  475. } elsif (-w $lockfile) {
  476. my($ans) =
  477. ExtUtils::MakeMaker::prompt
  478. (qq{Other job not responding. Shall I overwrite }.
  479. qq{the lockfile? (Y/N)},"y");
  480. $CPAN::Frontend->myexit("Ok, bye\n")
  481. unless $ans =~ /^y/i;
  482. } else {
  483. Carp::croak(
  484. qq{Lockfile $lockfile not writeable by you. }.
  485. qq{Cannot proceed.\n}.
  486. qq{ On UNIX try:\n}.
  487. qq{ rm $lockfile\n}.
  488. qq{ and then rerun us.\n}
  489. );
  490. }
  491. } else {
  492. $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Lockfile $lockfile\n".
  493. "reports other process with ID ".
  494. "$otherpid. Cannot proceed.\n"));
  495. }
  496. }
  497. my $dotcpan = $CPAN::Config->{cpan_home};
  498. eval { File::Path::mkpath($dotcpan);};
  499. if ($@) {
  500. # A special case at least for Jarkko.
  501. my $firsterror = $@;
  502. my $seconderror;
  503. my $symlinkcpan;
  504. if (-l $dotcpan) {
  505. $symlinkcpan = readlink $dotcpan;
  506. die "readlink $dotcpan failed: $!" unless defined $symlinkcpan;
  507. eval { File::Path::mkpath($symlinkcpan); };
  508. if ($@) {
  509. $seconderror = $@;
  510. } else {
  511. $CPAN::Frontend->mywarn(qq{
  512. Working directory $symlinkcpan created.
  513. });
  514. }
  515. }
  516. unless (-d $dotcpan) {
  517. my $diemess = qq{
  518. Your configuration suggests "$dotcpan" as your
  519. CPAN.pm working directory. I could not create this directory due
  520. to this error: $firsterror\n};
  521. $diemess .= qq{
  522. As "$dotcpan" is a symlink to "$symlinkcpan",
  523. I tried to create that, but I failed with this error: $seconderror
  524. } if $seconderror;
  525. $diemess .= qq{
  526. Please make sure the directory exists and is writable.
  527. };
  528. $CPAN::Frontend->mydie($diemess);
  529. }
  530. }
  531. my $fh;
  532. unless ($fh = FileHandle->new(">$lockfile")) {
  533. if ($! =~ /Permission/) {
  534. my $incc = $INC{'CPAN/Config.pm'};
  535. my $myincc = File::Spec->catfile($ENV{HOME},'.cpan','CPAN','MyConfig.pm');
  536. $CPAN::Frontend->myprint(qq{
  537. Your configuration suggests that CPAN.pm should use a working
  538. directory of
  539. $CPAN::Config->{cpan_home}
  540. Unfortunately we could not create the lock file
  541. $lockfile
  542. due to permission problems.
  543. Please make sure that the configuration variable
  544. \$CPAN::Config->{cpan_home}
  545. points to a directory where you can write a .lock file. You can set
  546. this variable in either
  547. $incc
  548. or
  549. $myincc
  550. });
  551. }
  552. $CPAN::Frontend->mydie("Could not open >$lockfile: $!");
  553. }
  554. $fh->print($$, "\n");
  555. $fh->print(hostname(), "\n");
  556. $self->{LOCK} = $lockfile;
  557. $fh->close;
  558. $SIG{TERM} = sub {
  559. &cleanup;
  560. $CPAN::Frontend->mydie("Got SIGTERM, leaving");
  561. };
  562. $SIG{INT} = sub {
  563. # no blocks!!!
  564. &cleanup if $Signal;
  565. $CPAN::Frontend->mydie("Got another SIGINT") if $Signal;
  566. print "Caught SIGINT\n";
  567. $Signal++;
  568. };
  569. # From: Larry Wall <larry@wall.org>
  570. # Subject: Re: deprecating SIGDIE
  571. # To: perl5-porters@perl.org
  572. # Date: Thu, 30 Sep 1999 14:58:40 -0700 (PDT)
  573. #
  574. # The original intent of __DIE__ was only to allow you to substitute one
  575. # kind of death for another on an application-wide basis without respect
  576. # to whether you were in an eval or not. As a global backstop, it should
  577. # not be used any more lightly (or any more heavily :-) than class
  578. # UNIVERSAL. Any attempt to build a general exception model on it should
  579. # be politely squashed. Any bug that causes every eval {} to have to be
  580. # modified should be not so politely squashed.
  581. #
  582. # Those are my current opinions. It is also my optinion that polite
  583. # arguments degenerate to personal arguments far too frequently, and that
  584. # when they do, it's because both people wanted it to, or at least didn't
  585. # sufficiently want it not to.
  586. #
  587. # Larry
  588. # global backstop to cleanup if we should really die
  589. $SIG{__DIE__} = \&cleanup;
  590. $self->debug("Signal handler set.") if $CPAN::DEBUG;
  591. }
  592. #-> sub CPAN::DESTROY ;
  593. sub DESTROY {
  594. &cleanup; # need an eval?
  595. }
  596. #-> sub CPAN::anycwd ;
  597. sub anycwd () {
  598. my $getcwd;
  599. $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  600. CPAN->$getcwd();
  601. }
  602. #-> sub CPAN::cwd ;
  603. sub cwd {Cwd::cwd();}
  604. #-> sub CPAN::getcwd ;
  605. sub getcwd {Cwd::getcwd();}
  606. #-> sub CPAN::exists ;
  607. sub exists {
  608. my($mgr,$class,$id) = @_;
  609. CPAN::Config->load unless $CPAN::Config_loaded++;
  610. CPAN::Index->reload;
  611. ### Carp::croak "exists called without class argument" unless $class;
  612. $id ||= "";
  613. exists $META->{readonly}{$class}{$id} or
  614. exists $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
  615. }
  616. #-> sub CPAN::delete ;
  617. sub delete {
  618. my($mgr,$class,$id) = @_;
  619. delete $META->{readonly}{$class}{$id}; # unsafe meta access, ok
  620. delete $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
  621. }
  622. #-> sub CPAN::has_usable
  623. # has_inst is sometimes too optimistic, we should replace it with this
  624. # has_usable whenever a case is given
  625. sub has_usable {
  626. my($self,$mod,$message) = @_;
  627. return 1 if $HAS_USABLE->{$mod};
  628. my $has_inst = $self->has_inst($mod,$message);
  629. return unless $has_inst;
  630. my $usable;
  631. $usable = {
  632. LWP => [ # we frequently had "Can't locate object
  633. # method "new" via package "LWP::UserAgent" at
  634. # (eval 69) line 2006
  635. sub {require LWP},
  636. sub {require LWP::UserAgent},
  637. sub {require HTTP::Request},
  638. sub {require URI::URL},
  639. ],
  640. Net::FTP => [
  641. sub {require Net::FTP},
  642. sub {require Net::Config},
  643. ]
  644. };
  645. if ($usable->{$mod}) {
  646. for my $c (0..$#{$usable->{$mod}}) {
  647. my $code = $usable->{$mod}[$c];
  648. my $ret = eval { &$code() };
  649. if ($@) {
  650. warn "DEBUG: c[$c]\$\@[$@]ret[$ret]";
  651. return;
  652. }
  653. }
  654. }
  655. return $HAS_USABLE->{$mod} = 1;
  656. }
  657. #-> sub CPAN::has_inst
  658. sub has_inst {
  659. my($self,$mod,$message) = @_;
  660. Carp::croak("CPAN->has_inst() called without an argument")
  661. unless defined $mod;
  662. if (defined $message && $message eq "no"
  663. ||
  664. exists $CPAN::META->{dontload_hash}{$mod} # unsafe meta access, ok
  665. ||
  666. exists $CPAN::Config->{dontload_hash}{$mod}
  667. ) {
  668. $CPAN::META->{dontload_hash}{$mod}||=1; # unsafe meta access, ok
  669. return 0;
  670. }
  671. my $file = $mod;
  672. my $obj;
  673. $file =~ s|::|/|g;
  674. $file =~ s|/|\\|g if $^O eq 'MSWin32';
  675. $file .= ".pm";
  676. if ($INC{$file}) {
  677. # checking %INC is wrong, because $INC{LWP} may be true
  678. # although $INC{"URI/URL.pm"} may have failed. But as
  679. # I really want to say "bla loaded OK", I have to somehow
  680. # cache results.
  681. ### warn "$file in %INC"; #debug
  682. return 1;
  683. } elsif (eval { require $file }) {
  684. # eval is good: if we haven't yet read the database it's
  685. # perfect and if we have installed the module in the meantime,
  686. # it tries again. The second require is only a NOOP returning
  687. # 1 if we had success, otherwise it's retrying
  688. $CPAN::Frontend->myprint("CPAN: $mod loaded ok\n");
  689. if ($mod eq "CPAN::WAIT") {
  690. push @CPAN::Shell::ISA, CPAN::WAIT;
  691. }
  692. return 1;
  693. } elsif ($mod eq "Net::FTP") {
  694. $CPAN::Frontend->mywarn(qq{
  695. Please, install Net::FTP as soon as possible. CPAN.pm installs it for you
  696. if you just type
  697. install Bundle::libnet
  698. }) unless $Have_warned->{"Net::FTP"}++;
  699. sleep 3;
  700. } elsif ($mod eq "Digest::MD5"){
  701. $CPAN::Frontend->myprint(qq{
  702. CPAN: MD5 security checks disabled because Digest::MD5 not installed.
  703. Please consider installing the Digest::MD5 module.
  704. });
  705. sleep 2;
  706. } else {
  707. delete $INC{$file}; # if it inc'd LWP but failed during, say, URI
  708. }
  709. return 0;
  710. }
  711. #-> sub CPAN::instance ;
  712. sub instance {
  713. my($mgr,$class,$id) = @_;
  714. CPAN::Index->reload;
  715. $id ||= "";
  716. # unsafe meta access, ok?
  717. return $META->{readwrite}{$class}{$id} if exists $META->{readwrite}{$class}{$id};
  718. $META->{readwrite}{$class}{$id} ||= $class->new(ID => $id);
  719. }
  720. #-> sub CPAN::new ;
  721. sub new {
  722. bless {}, shift;
  723. }
  724. #-> sub CPAN::cleanup ;
  725. sub cleanup {
  726. # warn "cleanup called with arg[@_] End[$End] Signal[$Signal]";
  727. local $SIG{__DIE__} = '';
  728. my($message) = @_;
  729. my $i = 0;
  730. my $ineval = 0;
  731. my($subroutine);
  732. while ((undef,undef,undef,$subroutine) = caller(++$i)) {
  733. $ineval = 1, last if
  734. $subroutine eq '(eval)';
  735. }
  736. return if $ineval && !$End;
  737. return unless defined $META->{LOCK};
  738. return unless -f $META->{LOCK};
  739. $META->savehist;
  740. unlink $META->{LOCK};
  741. # require Carp;
  742. # Carp::cluck("DEBUGGING");
  743. $CPAN::Frontend->mywarn("Lockfile removed.\n");
  744. }
  745. #-> sub CPAN::savehist
  746. sub savehist {
  747. my($self) = @_;
  748. my($histfile,$histsize);
  749. unless ($histfile = $CPAN::Config->{'histfile'}){
  750. $CPAN::Frontend->mywarn("No history written (no histfile specified).\n");
  751. return;
  752. }
  753. $histsize = $CPAN::Config->{'histsize'} || 100;
  754. if ($CPAN::term){
  755. unless ($CPAN::term->can("GetHistory")) {
  756. $CPAN::Frontend->mywarn("Terminal does not support GetHistory.\n");
  757. return;
  758. }
  759. } else {
  760. return;
  761. }
  762. my @h = $CPAN::term->GetHistory;
  763. splice @h, 0, @h-$histsize if @h>$histsize;
  764. my($fh) = FileHandle->new;
  765. open $fh, ">$histfile" or $CPAN::Frontend->mydie("Couldn't open >$histfile: $!");
  766. local $\ = local $, = "\n";
  767. print $fh @h;
  768. close $fh;
  769. }
  770. sub is_tested {
  771. my($self,$what) = @_;
  772. $self->{is_tested}{$what} = 1;
  773. }
  774. sub is_installed {
  775. my($self,$what) = @_;
  776. delete $self->{is_tested}{$what};
  777. }
  778. sub set_perl5lib {
  779. my($self) = @_;
  780. $self->{is_tested} ||= {};
  781. return unless %{$self->{is_tested}};
  782. my $env = $ENV{PERL5LIB};
  783. $env = $ENV{PERLLIB} unless defined $env;
  784. my @env;
  785. push @env, $env if defined $env and length $env;
  786. my @dirs = map {("$_/blib/arch", "$_/blib/lib")} keys %{$self->{is_tested}};
  787. $CPAN::Frontend->myprint("Prepending @dirs to PERL5LIB.\n");
  788. $ENV{PERL5LIB} = join $Config::Config{path_sep}, @dirs, @env;
  789. }
  790. package CPAN::CacheMgr;
  791. #-> sub CPAN::CacheMgr::as_string ;
  792. sub as_string {
  793. eval { require Data::Dumper };
  794. if ($@) {
  795. return shift->SUPER::as_string;
  796. } else {
  797. return Data::Dumper::Dumper(shift);
  798. }
  799. }
  800. #-> sub CPAN::CacheMgr::cachesize ;
  801. sub cachesize {
  802. shift->{DU};
  803. }
  804. #-> sub CPAN::CacheMgr::tidyup ;
  805. sub tidyup {
  806. my($self) = @_;
  807. return unless -d $self->{ID};
  808. while ($self->{DU} > $self->{'MAX'} ) {
  809. my($toremove) = shift @{$self->{FIFO}};
  810. $CPAN::Frontend->myprint(sprintf(
  811. "Deleting from cache".
  812. ": $toremove (%.1f>%.1f MB)\n",
  813. $self->{DU}, $self->{'MAX'})
  814. );
  815. return if $CPAN::Signal;
  816. $self->force_clean_cache($toremove);
  817. return if $CPAN::Signal;
  818. }
  819. }
  820. #-> sub CPAN::CacheMgr::dir ;
  821. sub dir {
  822. shift->{ID};
  823. }
  824. #-> sub CPAN::CacheMgr::entries ;
  825. sub entries {
  826. my($self,$dir) = @_;
  827. return unless defined $dir;
  828. $self->debug("reading dir[$dir]") if $CPAN::DEBUG;
  829. $dir ||= $self->{ID};
  830. my($cwd) = CPAN::anycwd();
  831. chdir $dir or Carp::croak("Can't chdir to $dir: $!");
  832. my $dh = DirHandle->new(File::Spec->curdir)
  833. or Carp::croak("Couldn't opendir $dir: $!");
  834. my(@entries);
  835. for ($dh->read) {
  836. next if $_ eq "." || $_ eq "..";
  837. if (-f $_) {
  838. push @entries, File::Spec->catfile($dir,$_);
  839. } elsif (-d _) {
  840. push @entries, File::Spec->catdir($dir,$_);
  841. } else {
  842. $CPAN::Frontend->mywarn("Warning: weird direntry in $dir: $_\n");
  843. }
  844. }
  845. chdir $cwd or Carp::croak("Can't chdir to $cwd: $!");
  846. sort { -M $b <=> -M $a} @entries;
  847. }
  848. #-> sub CPAN::CacheMgr::disk_usage ;
  849. sub disk_usage {
  850. my($self,$dir) = @_;
  851. return if exists $self->{SIZE}{$dir};
  852. return if $CPAN::Signal;
  853. my($Du) = 0;
  854. find(
  855. sub {
  856. $File::Find::prune++ if $CPAN::Signal;
  857. return if -l $_;
  858. if ($^O eq 'MacOS') {
  859. require Mac::Files;
  860. my $cat = Mac::Files::FSpGetCatInfo($_);
  861. $Du += $cat->ioFlLgLen() + $cat->ioFlRLgLen() if $cat;
  862. } else {
  863. $Du += (-s _);
  864. }
  865. },
  866. $dir
  867. );
  868. return if $CPAN::Signal;
  869. $self->{SIZE}{$dir} = $Du/1024/1024;
  870. push @{$self->{FIFO}}, $dir;
  871. $self->debug("measured $dir is $Du") if $CPAN::DEBUG;
  872. $self->{DU} += $Du/1024/1024;
  873. $self->{DU};
  874. }
  875. #-> sub CPAN::CacheMgr::force_clean_cache ;
  876. sub force_clean_cache {
  877. my($self,$dir) = @_;
  878. return unless -e $dir;
  879. $self->debug("have to rmtree $dir, will free $self->{SIZE}{$dir}")
  880. if $CPAN::DEBUG;
  881. File::Path::rmtree($dir);
  882. $self->{DU} -= $self->{SIZE}{$dir};
  883. delete $self->{SIZE}{$dir};
  884. }
  885. #-> sub CPAN::CacheMgr::new ;
  886. sub new {
  887. my $class = shift;
  888. my $time = time;
  889. my($debug,$t2);
  890. $debug = "";
  891. my $self = {
  892. ID => $CPAN::Config->{'build_dir'},
  893. MAX => $CPAN::Config->{'build_cache'},
  894. SCAN => $CPAN::Config->{'scan_cache'} || 'atstart',
  895. DU => 0
  896. };
  897. File::Path::mkpath($self->{ID});
  898. my $dh = DirHandle->new($self->{ID});
  899. bless $self, $class;
  900. $self->scan_cache;
  901. $t2 = time;
  902. $debug .= "timing of CacheMgr->new: ".($t2 - $time);
  903. $time = $t2;
  904. CPAN->debug($debug) if $CPAN::DEBUG;
  905. $self;
  906. }
  907. #-> sub CPAN::CacheMgr::scan_cache ;
  908. sub scan_cache {
  909. my $self = shift;
  910. return if $self->{SCAN} eq 'never';
  911. $CPAN::Frontend->mydie("Unknown scan_cache argument: $self->{SCAN}")
  912. unless $self->{SCAN} eq 'atstart';
  913. $CPAN::Frontend->myprint(
  914. sprintf("Scanning cache %s for sizes\n",
  915. $self->{ID}));
  916. my $e;
  917. for $e ($self->entries($self->{ID})) {
  918. next if $e eq ".." || $e eq ".";
  919. $self->disk_usage($e);
  920. return if $CPAN::Signal;
  921. }
  922. $self->tidyup;
  923. }
  924. package CPAN::Debug;
  925. #-> sub CPAN::Debug::debug ;
  926. sub debug {
  927. my($self,$arg) = @_;
  928. my($caller,$func,$line,@rest) = caller(1); # caller(0) eg
  929. # Complete, caller(1)
  930. # eg readline
  931. ($caller) = caller(0);
  932. $caller =~ s/.*:://;
  933. $arg = "" unless defined $arg;
  934. my $rest = join "|", map { defined $_ ? $_ : "UNDEF" } @rest;
  935. if ($CPAN::DEBUG{$caller} & $CPAN::DEBUG){
  936. if ($arg and ref $arg) {
  937. eval { require Data::Dumper };
  938. if ($@) {
  939. $CPAN::Frontend->myprint($arg->as_string);
  940. } else {
  941. $CPAN::Frontend->myprint(Data::Dumper::Dumper($arg));
  942. }
  943. } else {
  944. $CPAN::Frontend->myprint("Debug($caller:$func,$line,[$rest]): $arg\n");
  945. }
  946. }
  947. }
  948. package CPAN::Config;
  949. #-> sub CPAN::Config::edit ;
  950. # returns true on successful action
  951. sub edit {
  952. my($self,@args) = @_;
  953. return unless @args;
  954. CPAN->debug("self[$self]args[".join(" | ",@args)."]");
  955. my($o,$str,$func,$args,$key_exists);
  956. $o = shift @args;
  957. if($can{$o}) {
  958. $self->$o(@args);
  959. return 1;
  960. } else {
  961. CPAN->debug("o[$o]") if $CPAN::DEBUG;
  962. if ($o =~ /list$/) {
  963. $func = shift @args;
  964. $func ||= "";
  965. CPAN->debug("func[$func]") if $CPAN::DEBUG;
  966. my $changed;
  967. # Let's avoid eval, it's easier to comprehend without.
  968. if ($func eq "push") {
  969. push @{$CPAN::Config->{$o}}, @args;
  970. $changed = 1;
  971. } elsif ($func eq "pop") {
  972. pop @{$CPAN::Config->{$o}};
  973. $changed = 1;
  974. } elsif ($func eq "shift") {
  975. shift @{$CPAN::Config->{$o}};
  976. $changed = 1;
  977. } elsif ($func eq "unshift") {
  978. unshift @{$CPAN::Config->{$o}}, @args;
  979. $changed = 1;
  980. } elsif ($func eq "splice") {
  981. splice @{$CPAN::Config->{$o}}, @args;
  982. $changed = 1;
  983. } elsif (@args) {
  984. $CPAN::Config->{$o} = [@args];
  985. $changed = 1;
  986. } else {
  987. $self->prettyprint($o);
  988. }
  989. if ($o eq "urllist" && $changed) {
  990. # reset the cached values
  991. undef $CPAN::FTP::Thesite;
  992. undef $CPAN::FTP::Themethod;
  993. }
  994. return $changed;
  995. } else {
  996. $CPAN::Config->{$o} = $args[0] if defined $args[0];
  997. $self->prettyprint($o);
  998. }
  999. }
  1000. }
  1001. sub prettyprint {
  1002. my($self,$k) = @_;
  1003. my $v = $CPAN::Config->{$k};
  1004. if (ref $v) {
  1005. my(@report) = ref $v eq "ARRAY" ?
  1006. @$v :
  1007. map { sprintf(" %-18s => %s\n",
  1008. $_,
  1009. defined $v->{$_} ? $v->{$_} : "UNDEFINED"
  1010. )} keys %$v;
  1011. $CPAN::Frontend->myprint(
  1012. join(
  1013. "",
  1014. sprintf(
  1015. " %-18s\n",
  1016. $k
  1017. ),
  1018. map {"\t$_\n"} @report
  1019. )
  1020. );
  1021. } elsif (defined $v) {
  1022. $CPAN::Frontend->myprint(sprintf " %-18s %s\n", $k, $v);
  1023. } else {
  1024. $CPAN::Frontend->myprint(sprintf " %-18s %s\n", $k, "UNDEFINED");
  1025. }
  1026. }
  1027. #-> sub CPAN::Config::commit ;
  1028. sub commit {
  1029. my($self,$configpm) = @_;
  1030. unless (defined $configpm){
  1031. $configpm ||= $INC{"CPAN/MyConfig.pm"};
  1032. $configpm ||= $INC{"CPAN/Config.pm"};
  1033. $configpm || Carp::confess(q{
  1034. CPAN::Config::commit called without an argument.
  1035. Please specify a filename where to save the configuration or try
  1036. "o conf init" to have an interactive course through configing.
  1037. });
  1038. }
  1039. my($mode);
  1040. if (-f $configpm) {
  1041. $mode = (stat $configpm)[2];
  1042. if ($mode && ! -w _) {
  1043. Carp::confess("$configpm is not writable");
  1044. }
  1045. }
  1046. my $msg;
  1047. $msg = <<EOF unless $configpm =~ /MyConfig/;
  1048. # This is CPAN.pm's systemwide configuration file. This file provides
  1049. # defaults for users, and the values can be changed in a per-user
  1050. # configuration file. The user-config file is being looked for as
  1051. # ~/.cpan/CPAN/MyConfig.pm.
  1052. EOF
  1053. $msg ||= "\n";
  1054. my($fh) = FileHandle->new;
  1055. rename $configpm, "$configpm~" if -f $configpm;
  1056. open $fh, ">$configpm" or
  1057. $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
  1058. $fh->print(qq[$msg\$CPAN::Config = \{\n]);
  1059. foreach (sort keys %$CPAN::Config) {
  1060. $fh->print(
  1061. " '$_' => ",
  1062. ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
  1063. ",\n"
  1064. );
  1065. }
  1066. $fh->print("};\n1;\n__END__\n");
  1067. close $fh;
  1068. #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
  1069. #chmod $mode, $configpm;
  1070. ###why was that so? $self->defaults;
  1071. $CPAN::Frontend->myprint("commit: wrote $configpm\n");
  1072. 1;
  1073. }
  1074. *default = \&defaults;
  1075. #-> sub CPAN::Config::defaults ;
  1076. sub defaults {
  1077. my($self) = @_;
  1078. $self->unload;
  1079. $self->load;
  1080. 1;
  1081. }
  1082. sub init {
  1083. my($self) = @_;
  1084. undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
  1085. # have the least
  1086. # important
  1087. # variable
  1088. # undefined
  1089. $self->load;
  1090. 1;
  1091. }
  1092. # This is a piece of repeated code that is abstracted here for
  1093. # maintainability. RMB
  1094. #
  1095. sub _configpmtest {
  1096. my($configpmdir, $configpmtest) = @_;
  1097. if (-w $configpmtest) {
  1098. return $configpmtest;
  1099. } elsif (-w $configpmdir) {
  1100. #_#_# following code dumped core on me with 5.003_11, a.k.
  1101. my $configpm_bak = "$configpmtest.bak";
  1102. unlink $configpm_bak if -f $configpm_bak;
  1103. if( -f $configpmtest ) {
  1104. if( rename $configpmtest, $configpm_bak ) {
  1105. $CPAN::Frontend->mywarn(<<END)
  1106. Old configuration file $configpmtest
  1107. moved to $configpm_bak
  1108. END
  1109. }
  1110. }
  1111. my $fh = FileHandle->new;
  1112. if ($fh->open(">$configpmtest")) {
  1113. $fh->print("1;\n");
  1114. return $configpmtest;
  1115. } else {
  1116. # Should never happen
  1117. Carp::confess("Cannot open >$configpmtest");
  1118. }
  1119. } else { return }
  1120. }
  1121. #-> sub CPAN::Config::load ;
  1122. sub load {
  1123. my($self) = shift;
  1124. my(@miss);
  1125. use Carp;
  1126. eval {require CPAN::Config;}; # We eval because of some
  1127. # MakeMaker problems
  1128. unless ($dot_cpan++){
  1129. unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
  1130. eval {require CPAN::MyConfig;}; # where you can override
  1131. # system wide settings
  1132. shift @INC;
  1133. }
  1134. return unless @miss = $self->missing_config_data;
  1135. require CPAN::FirstTime;
  1136. my($configpm,$fh,$redo,$theycalled);
  1137. $redo ||= "";
  1138. $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
  1139. if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
  1140. $configpm = $INC{"CPAN/Config.pm"};
  1141. $redo++;
  1142. } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
  1143. $configpm = $INC{"CPAN/MyConfig.pm"};
  1144. $redo++;
  1145. } else {
  1146. my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
  1147. my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
  1148. my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
  1149. if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
  1150. $configpm = _configpmtest($configpmdir,$configpmtest);
  1151. }
  1152. unless ($configpm) {
  1153. $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
  1154. File::Path::mkpath($configpmdir);
  1155. $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
  1156. $configpm = _configpmtest($configpmdir,$configpmtest);
  1157. unless ($configpm) {
  1158. Carp::confess(qq{WARNING: CPAN.pm is unable to }.
  1159. qq{create a configuration file.});
  1160. }
  1161. }
  1162. }
  1163. local($") = ", ";
  1164. $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
  1165. We have to reconfigure CPAN.pm due to following uninitialized parameters:
  1166. @miss
  1167. END
  1168. $CPAN::Frontend->myprint(qq{
  1169. $configpm initialized.
  1170. });
  1171. sleep 2;
  1172. CPAN::FirstTime::init($configpm);
  1173. }
  1174. #-> sub CPAN::Config::missing_config_data ;
  1175. sub missing_config_data {
  1176. my(@miss);
  1177. for (
  1178. "cpan_home", "keep_source_where", "build_dir", "build_cache",
  1179. "scan_cache", "index_expire", "gzip", "tar", "unzip", "make",
  1180. "pager",
  1181. "makepl_arg", "make_arg", "make_install_arg", "urllist",
  1182. "inhibit_startup_message", "ftp_proxy", "http_proxy", "no_proxy",
  1183. "prerequisites_policy",
  1184. "cache_metadata",
  1185. ) {
  1186. push @miss, $_ unless defined $CPAN::Config->{$_};
  1187. }
  1188. return @miss;
  1189. }
  1190. #-> sub CPAN::Config::unload ;
  1191. sub unload {
  1192. delete $INC{'CPAN/MyConfig.pm'};
  1193. delete $INC{'CPAN/Config.pm'};
  1194. }
  1195. #-> sub CPAN::Config::help ;
  1196. sub help {
  1197. $CPAN::Frontend->myprint(q[
  1198. Known options:
  1199. defaults reload default config values from disk
  1200. commit commit session changes to disk
  1201. init go through a dialog to set all parameters
  1202. You may edit key values in the follow fashion (the "o" is a literal
  1203. letter o):
  1204. o conf build_cache 15
  1205. o conf build_dir "/foo/bar"
  1206. o conf urllist shift
  1207. o conf urllist unshift ftp://ftp.foo.bar/
  1208. ]);
  1209. undef; #don't reprint CPAN::Config
  1210. }
  1211. #-> sub CPAN::Config::cpl ;
  1212. sub cpl {
  1213. my($word,$line,$pos) = @_;
  1214. $word ||= "";
  1215. CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1216. my(@words) = split " ", substr($line,0,$pos+1);
  1217. if (
  1218. defined($words[2])
  1219. and
  1220. (
  1221. $words[2] =~ /list$/ && @words == 3
  1222. ||
  1223. $words[2] =~ /list$/ && @words == 4 && length($word)
  1224. )
  1225. ) {
  1226. return grep /^\Q$word\E/, qw(splice shift unshift pop push);
  1227. } elsif (@words >= 4) {
  1228. return ();
  1229. }
  1230. my(@o_conf) = (keys %CPAN::Config::can, keys %$CPAN::Config);
  1231. return grep /^\Q$word\E/, @o_conf;
  1232. }
  1233. package CPAN::Shell;
  1234. #-> sub CPAN::Shell::h ;
  1235. sub h {
  1236. my($class,$about) = @_;
  1237. if (defined $about) {
  1238. $CPAN::Frontend->myprint("Detailed help not yet implemented\n");
  1239. } else {
  1240. $CPAN::Frontend->myprint(q{
  1241. Display Information
  1242. command argument description
  1243. a,b,d,m WORD or /REGEXP/ about authors, bundles, distributions, modules
  1244. i WORD or /REGEXP/ about anything of above
  1245. r NONE reinstall recommendations
  1246. ls AUTHOR about files in the author's directory
  1247. Download, Test, Make, Install...
  1248. get download
  1249. make make (implies get)
  1250. test MODULES, make test (implies make)
  1251. install DISTS, BUNDLES make install (implies test)
  1252. clean make clean
  1253. look open subshell in these dists' directories
  1254. readme display these dists' README files
  1255. Other
  1256. h,? display this menu ! perl-code eval a perl command
  1257. o conf [opt] set and query options q quit the cpan shell
  1258. reload cpan load CPAN.pm again reload index load newer indices
  1259. autobundle Snapshot force cmd unconditionally do cmd});
  1260. }
  1261. }
  1262. *help = \&h;
  1263. #-> sub CPAN::Shell::a ;
  1264. sub a {
  1265. my($self,@arg) = @_;
  1266. # authors are always UPPERCASE
  1267. for (@arg) {
  1268. $_ = uc $_ unless /=/;
  1269. }
  1270. $CPAN::Frontend->myprint($self->format_result('Author',@arg));
  1271. }
  1272. #-> sub CPAN::Shell::ls ;
  1273. sub ls {
  1274. my($self,@arg) = @_;
  1275. my @accept;
  1276. for (@arg) {
  1277. unless (/^[A-Z\-]+$/i) {
  1278. $CPAN::Frontend->mywarn("ls command rejects argument $_: not an author\n");
  1279. next;
  1280. }
  1281. push @accept, uc $_;
  1282. }
  1283. for my $a (@accept){
  1284. my $author = $self->expand('Author',$a) or die "No author found for $a";
  1285. $author->ls;
  1286. }
  1287. }
  1288. #-> sub CPAN::Shell::local_bundles ;
  1289. sub local_bundles {
  1290. my($self,@which) = @_;
  1291. my($incdir,$bdir,$dh);
  1292. foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  1293. my @bbase = "Bundle";
  1294. while (my $bbase = shift @bbase) {
  1295. $bdir = File::Spec->catdir($incdir,split /::/, $bbase);
  1296. CPAN->debug("bdir[$bdir]\@bbase[@bbase]") if $CPAN::DEBUG;
  1297. if ($dh = DirHandle->new($bdir)) { # may fail
  1298. my($entry);
  1299. for $entry ($dh->read) {
  1300. next if $entry =~ /^\./;
  1301. if (-d File::Spec->catdir($bdir,$entry)){
  1302. push @bbase, "$bbase\::$entry";
  1303. } else {
  1304. next unless $entry =~ s/\.pm(?!\n)\Z//;
  1305. $CPAN::META->instance('CPAN::Bundle',"$bbase\::$entry");
  1306. }
  1307. }
  1308. }
  1309. }
  1310. }
  1311. }
  1312. #-> sub CPAN::Shell::b ;
  1313. sub b {
  1314. my($self,@which) = @_;
  1315. CPAN->debug("which[@which]") if $CPAN::DEBUG;
  1316. $self->local_bundles;
  1317. $CPAN::Frontend->myprint($self->format_result('Bundle',@which));
  1318. }
  1319. #-> sub CPAN::Shell::d ;
  1320. sub d { $CPAN::Frontend->myprint(shift->format_result('Distribution',@_));}
  1321. #-> sub CPAN::Shell::m ;
  1322. sub m { # emacs confused here }; sub mimimimimi { # emacs in sync here
  1323. my $self = shift;
  1324. $CPAN::Frontend->myprint($self->format_result('Module',@_));
  1325. }
  1326. #-> sub CPAN::Shell::i ;
  1327. sub i {
  1328. my($self) = shift;
  1329. my(@args) = @_;
  1330. my(@type,$type,@m);
  1331. @type = qw/Author Bundle Distribution Module/;
  1332. @args = '/./' unless @args;
  1333. my(@result);
  1334. for $type (@type) {
  1335. push @result, $self->expand($type,@args);
  1336. }
  1337. my $result = @result == 1 ?
  1338. $result[0]->as_string :
  1339. @result == 0 ?
  1340. "No objects found of any type for argument @args\n" :
  1341. join("",
  1342. (map {$_->as_glimpse} @result),
  1343. scalar @result, " items found\n",
  1344. );
  1345. $CPAN::Frontend->myprint($result);
  1346. }
  1347. #-> sub CPAN::Shell::o ;
  1348. # CPAN::Shell::o and CPAN::Config::edit are closely related. 'o conf'
  1349. # should have been called set and 'o debug' maybe 'set debug'
  1350. sub o {
  1351. my($self,$o_type,@o_what) = @_;
  1352. $o_type ||= "";
  1353. CPAN->debug("o_type[$o_type] o_what[".join(" | ",@o_what)."]\n");
  1354. if ($o_type eq 'conf') {
  1355. shift @o_what if @o_what && $o_what[0] eq 'help';
  1356. if (!@o_what) { # print all things, "o conf"
  1357. my($k,$v);
  1358. $CPAN::Frontend->myprint("CPAN::Config options");
  1359. if (exists $INC{'CPAN/Config.pm'}) {
  1360. $CPAN::Frontend->myprint(" from $INC{'CPAN/Config.pm'}");
  1361. }
  1362. if (exists $INC{'CPAN/MyConfig.pm'}) {
  1363. $CPAN::Frontend->myprint(" and $INC{'CPAN/MyConfig.pm'}");
  1364. }
  1365. $CPAN::Frontend->myprint(":\n");
  1366. for $k (sort keys %CPAN::Config::can) {
  1367. $v = $CPAN::Config::can{$k};
  1368. $CPAN::Frontend->myprint(sprintf " %-18s %s\n", $k, $v);
  1369. }
  1370. $CPAN::Frontend->myprint("\n");
  1371. for $k (sort keys %$CPAN::Config) {
  1372. CPAN::Config->prettyprint($k);
  1373. }
  1374. $CPAN::Frontend->myprint("\n");
  1375. } elsif (!CPAN::Config->edit(@o_what)) {
  1376. $CPAN::Frontend->myprint(qq{Type 'o conf' to view configuration }.
  1377. qq{edit options\n\n});
  1378. }
  1379. } elsif ($o_type eq 'debug') {
  1380. my(%valid);
  1381. @o_what = () if defined $o_what[0] && $o_what[0] =~ /help/i;
  1382. if (@o_what) {
  1383. while (@o_what) {
  1384. my($what) = shift @o_what;
  1385. if ($what =~ s/^-// && exists $CPAN::DEBUG{$what}) {
  1386. $CPAN::DEBUG &= $CPAN::DEBUG ^ $CPAN::DEBUG{$what};
  1387. next;
  1388. }
  1389. if ( exists $CPAN::DEBUG{$what} ) {
  1390. $CPAN::DEBUG |= $CPAN::DEBUG{$what};
  1391. } elsif ($what =~ /^\d/) {
  1392. $CPAN::DEBUG = $what;
  1393. } elsif (lc $what eq 'all') {
  1394. my($max) = 0;
  1395. for (values %CPAN::DEBUG) {
  1396. $max += $_;
  1397. }
  1398. $CPAN::DEBUG = $max;
  1399. } else {
  1400. my($known) = 0;
  1401. for (keys %CPAN::DEBUG) {
  1402. next unless lc($_) eq lc($what);
  1403. $CPAN::DEBUG |= $CPAN::DEBUG{$_};
  1404. $known = 1;
  1405. }
  1406. $CPAN::Frontend->myprint("unknown argument [$what]\n")
  1407. unless $known;
  1408. }
  1409. }
  1410. } else {
  1411. my $raw = "Valid options for debug are ".
  1412. join(", ",sort(keys %CPAN::DEBUG), 'all').
  1413. qq{ or a number. Completion works on the options. }.
  1414. qq{Case is ignored.};
  1415. require Text::Wrap;
  1416. $CPAN::Frontend->myprint(Text::Wrap::fill("","",$raw));
  1417. $CPAN::Frontend->myprint("\n\n");
  1418. }
  1419. if ($CPAN::DEBUG) {
  1420. $CPAN::Frontend->myprint("Options set for debugging:\n");
  1421. my($k,$v);
  1422. for $k (sort {$CPAN::DEBUG{$a} <=> $CPAN::DEBUG{$b}} keys %CPAN::DEBUG) {
  1423. $v = $CPAN::DEBUG{$k};
  1424. $CPAN::Frontend->myprint(sprintf " %-14s(%s)\n", $k, $v)
  1425. if $v & $CPAN::DEBUG;
  1426. }
  1427. } else {
  1428. $CPAN::Frontend->myprint("Debugging turned off completely.\n");
  1429. }
  1430. } else {
  1431. $CPAN::Frontend->myprint(qq{
  1432. Known options:
  1433. conf set or get configuration variables
  1434. debug set or get debugging options
  1435. });
  1436. }
  1437. }
  1438. sub paintdots_onreload {
  1439. my($ref) = shift;
  1440. sub {
  1441. if ( $_[0] =~ /[Ss]ubroutine ([\w:]+) redefined/ ) {
  1442. my($subr) = $1;
  1443. ++$$ref;
  1444. local($|) = 1;
  1445. # $CPAN::Frontend->myprint(".($subr)");
  1446. $CPAN::Frontend->myprint(".");
  1447. return;
  1448. }
  1449. warn @_;
  1450. };
  1451. }
  1452. #-> sub CPAN::Shell::reload ;
  1453. sub reload {
  1454. my($self,$command,@arg) = @_;
  1455. $command ||= "";
  1456. $self->debug("self[$self]command[$command]arg[@arg]") if $CPAN::DEBUG;
  1457. if ($command =~ /cpan/i) {
  1458. for my $f (qw(CPAN.pm CPAN/FirstTime.pm)) {
  1459. next unless $INC{$f};
  1460. CPAN->debug("reloading the whole $f") if $CPAN::DEBUG;
  1461. my $fh = FileHandle->new($INC{$f});
  1462. local($/);
  1463. my $redef = 0;
  1464. local($SIG{__WARN__}) = paintdots_onreload(\$redef);
  1465. eval <$fh>;
  1466. warn $@ if $@;
  1467. $CPAN::Frontend->myprint("\n$redef subroutines redefined\n");
  1468. }
  1469. } elsif ($command =~ /index/) {
  1470. CPAN::Index->force_reload;
  1471. } else {
  1472. $CPAN::Frontend->myprint(qq{cpan re-evals the CPAN.pm file
  1473. index re-reads the index files\n});
  1474. }
  1475. }
  1476. #-> sub CPAN::Shell::_binary_extensions ;
  1477. sub _binary_extensions {
  1478. my($self) = shift @_;
  1479. my(@result,$module,%seen,%need,$headerdone);
  1480. for $module ($self->expand('Module','/./')) {
  1481. my $file = $module->cpan_file;
  1482. next if $file eq "N/A";
  1483. next if $file =~ /^Contact Author/;
  1484. my $dist = $CPAN::META->instance('CPAN::Distribution',$file);
  1485. next if $dist->isa_perl;
  1486. next unless $module->xs_file;
  1487. local($|) = 1;
  1488. $CPAN::Frontend->myprint(".");
  1489. push @result, $module;
  1490. }
  1491. # print join " | ", @result;
  1492. $CPAN::Frontend->myprint("\n");
  1493. return @result;
  1494. }
  1495. #-> sub CPAN::Shell::recompile ;
  1496. sub recompile {
  1497. my($self) = shift @_;
  1498. my($module,@module,$cpan_file,%dist);
  1499. @module = $self->_binary_extensions();
  1500. for $module (@module){ # we force now and compile later, so we
  1501. # don't do it twice
  1502. $cpan_file = $module->cpan_file;
  1503. my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1504. $pack->force;
  1505. $dist{$cpan_file}++;
  1506. }
  1507. for $cpan_file (sort keys %dist) {
  1508. $CPAN::Frontend->myprint(" CPAN: Recompiling $cpan_file\n\n");
  1509. my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1510. $pack->install;
  1511. $CPAN::Signal = 0; # it's tempting to reset Signal, so we can
  1512. # stop a package from recompiling,
  1513. # e.g. IO-1.12 when we have perl5.003_10
  1514. }
  1515. }
  1516. #-> sub CPAN::Shell::_u_r_common ;
  1517. sub _u_r_common {
  1518. my($self) = shift @_;
  1519. my($what) = shift @_;
  1520. CPAN->debug("self[$self] what[$what] args[@_]") if $CPAN::DEBUG;
  1521. Carp::croak "Usage: \$obj->_u_r_common(a|r|u)" unless
  1522. $what && $what =~ /^[aru]$/;
  1523. my(@args) = @_;
  1524. @args = '/./' unless @args;
  1525. my(@result,$module,%seen,%need,$headerdone,
  1526. $version_undefs,$version_zeroes);
  1527. $version_undefs = $version_zeroes = 0;
  1528. my $sprintf = "%s%-25s%s %9s %9s %s\n";
  1529. my @expand = $self->expand('Module',@args);
  1530. my $expand = scalar @expand;
  1531. if (0) { # Looks like noise to me, was very useful for debugging
  1532. # for metadata cache
  1533. $CPAN::Frontend->myprint(sprintf "%d matches in the database\n", $expand);
  1534. }
  1535. for $module (@expand) {
  1536. my $file = $module->cpan_file;
  1537. next unless defined $file; # ??
  1538. my($latest) = $module->cpan_version;
  1539. my($inst_file) = $module->inst_file;
  1540. my($have);
  1541. return if $CPAN::Signal;
  1542. if ($inst_file){
  1543. if ($what eq "a") {
  1544. $have = $module->inst_version;
  1545. } elsif ($what eq "r") {
  1546. $have = $module->inst_version;
  1547. local($^W) = 0;
  1548. if ($have eq "undef"){
  1549. $version_undefs++;
  1550. } elsif ($have == 0){
  1551. $version_zeroes++;
  1552. }
  1553. next unless CPAN::Version->vgt($latest, $have);
  1554. # to be pedantic w

Large files files are truncated, but you can click here to view the full file