forked from andk/cpanpm
/
CPAN.pm
3885 lines (3071 loc) · 134 KB
/
CPAN.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
# vim: ts=4 sts=4 sw=4:
use strict;
package CPAN;
$CPAN::VERSION = '1.9800';
$CPAN::VERSION =~ s/_//;
# we need to run chdir all over and we would get at wrong libraries
# there
use File::Spec ();
BEGIN {
if (File::Spec->can("rel2abs")) {
for my $inc (@INC) {
$inc = File::Spec->rel2abs($inc) unless ref $inc;
}
}
}
use CPAN::Author;
use CPAN::HandleConfig;
use CPAN::Version;
use CPAN::Bundle;
use CPAN::CacheMgr;
use CPAN::Complete;
use CPAN::Debug;
use CPAN::Distribution;
use CPAN::Distrostatus;
use CPAN::FTP;
use CPAN::Index 1.93; # https://rt.cpan.org/Ticket/Display.html?id=43349
use CPAN::InfoObj;
use CPAN::Module;
use CPAN::Prompt;
use CPAN::URL;
use CPAN::Queue;
use CPAN::Tarzip;
use CPAN::DeferredCode;
use CPAN::Shell;
use CPAN::LWP::UserAgent;
use CPAN::Exception::RecursiveDependency;
use CPAN::Exception::yaml_not_installed;
use CPAN::Exception::yaml_process_error;
use Carp ();
use Config ();
use Cwd qw(chdir);
use DirHandle ();
use Exporter ();
use ExtUtils::MakeMaker qw(prompt); # for some unknown reason,
# 5.005_04 does not work without
# this
use File::Basename ();
use File::Copy ();
use File::Find;
use File::Path ();
use FileHandle ();
use Fcntl qw(:flock);
use Safe ();
use Sys::Hostname qw(hostname);
use Text::ParseWords ();
use Text::Wrap ();
# protect against "called too early"
sub find_perl ();
sub anycwd ();
sub _uniq;
no lib ".";
require Mac::BuildTools if $^O eq 'MacOS';
if ($ENV{PERL5_CPAN_IS_RUNNING} && $$ != $ENV{PERL5_CPAN_IS_RUNNING}) {
$ENV{PERL5_CPAN_IS_RUNNING_IN_RECURSION} ||= $ENV{PERL5_CPAN_IS_RUNNING};
my @rec = _uniq split(/,/, $ENV{PERL5_CPAN_IS_RUNNING_IN_RECURSION}), $$;
$ENV{PERL5_CPAN_IS_RUNNING_IN_RECURSION} = join ",", @rec;
# warn "# Note: Recursive call of CPAN.pm detected\n";
my $w = sprintf "# Note: CPAN.pm is running in process %d now", pop @rec;
my %sleep = (
5 => 30,
6 => 60,
7 => 120,
);
my $sleep = @rec > 7 ? 300 : ($sleep{scalar @rec}||0);
my $verbose = @rec >= 4;
while (@rec) {
$w .= sprintf " which has been called by process %d", pop @rec;
}
if ($sleep) {
$w .= ".\n\n# Sleeping $sleep seconds to protect other processes\n";
}
if ($verbose) {
warn $w;
}
local $| = 1;
while ($sleep > 0) {
printf "\r#%5d", --$sleep;
sleep 1;
}
print "\n";
}
$ENV{PERL5_CPAN_IS_RUNNING}=$$;
$ENV{PERL5_CPANPLUS_IS_RUNNING}=$$; # https://rt.cpan.org/Ticket/Display.html?id=23735
END { $CPAN::End++; &cleanup; }
$CPAN::Signal ||= 0;
$CPAN::Frontend ||= "CPAN::Shell";
unless (@CPAN::Defaultsites) {
@CPAN::Defaultsites = map {
CPAN::URL->new(TEXT => $_, FROM => "DEF")
}
"http://www.perl.org/CPAN/",
"ftp://ftp.perl.org/pub/CPAN/";
}
# $CPAN::iCwd (i for initial)
$CPAN::iCwd ||= CPAN::anycwd();
$CPAN::Perl ||= CPAN::find_perl();
$CPAN::Defaultdocs ||= "http://search.cpan.org/perldoc?";
$CPAN::Defaultrecent ||= "http://search.cpan.org/uploads.rdf";
$CPAN::Defaultrecent ||= "http://cpan.uwinnipeg.ca/htdocs/cpan.xml";
# our globals are getting a mess
use vars qw(
$AUTOLOAD
$Be_Silent
$CONFIG_DIRTY
$Defaultdocs
$Echo_readline
$Frontend
$GOTOSHELL
$HAS_USABLE
$Have_warned
$MAX_RECURSION
$META
$RUN_DEGRADED
$Signal
$SQLite
$Suppress_readline
$VERSION
$autoload_recursion
$term
@Defaultsites
@EXPORT
);
$MAX_RECURSION = 32;
@CPAN::ISA = qw(CPAN::Debug Exporter);
# note that these functions live in CPAN::Shell and get executed via
# AUTOLOAD when called directly
@EXPORT = qw(
autobundle
bundle
clean
cvs_import
expand
force
fforce
get
install
install_tested
is_tested
make
mkmyconfig
notest
perldoc
readme
recent
recompile
report
shell
smoke
test
upgrade
);
sub soft_chdir_with_alternatives ($);
{
$autoload_recursion ||= 0;
#-> sub CPAN::AUTOLOAD ;
sub AUTOLOAD { ## no critic
$autoload_recursion++;
my($l) = $AUTOLOAD;
$l =~ s/.*:://;
if ($CPAN::Signal) {
warn "Refusing to autoload '$l' while signal pending";
$autoload_recursion--;
return;
}
if ($autoload_recursion > 1) {
my $fullcommand = join " ", map { "'$_'" } $l, @_;
warn "Refusing to autoload $fullcommand in recursion\n";
$autoload_recursion--;
return;
}
my(%export);
@export{@EXPORT} = '';
CPAN::HandleConfig->load unless $CPAN::Config_loaded++;
if (exists $export{$l}) {
CPAN::Shell->$l(@_);
} else {
die(qq{Unknown CPAN command "$AUTOLOAD". }.
qq{Type ? for help.\n});
}
$autoload_recursion--;
}
}
{
my $x = *SAVEOUT; # avoid warning
open($x,">&STDOUT") or die "dup failed";
my $redir = 0;
sub _redirect(@) {
#die if $redir;
local $_;
push(@_,undef);
while(defined($_=shift)) {
if (s/^\s*>//){
my ($m) = s/^>// ? ">" : "";
s/\s+//;
$_=shift unless length;
die "no dest" unless defined;
open(STDOUT,">$m$_") or die "open:$_:$!\n";
$redir=1;
} elsif ( s/^\s*\|\s*// ) {
my $pipe="| $_";
while(defined($_[0])){
$pipe .= ' ' . shift;
}
open(STDOUT,$pipe) or die "open:$pipe:$!\n";
$redir=1;
} else {
push(@_,$_);
}
}
return @_;
}
sub _unredirect {
return unless $redir;
$redir = 0;
## redirect: unredirect and propagate errors. explicit close to wait for pipe.
close(STDOUT);
open(STDOUT,">&SAVEOUT");
die "$@" if "$@";
## redirect: done
}
}
sub _uniq {
my(@list) = @_;
my %seen;
return grep { !$seen{$_}++ } @list;
}
#-> sub CPAN::shell ;
sub shell {
my($self) = @_;
$Suppress_readline = ! -t STDIN unless defined $Suppress_readline;
CPAN::HandleConfig->load unless $CPAN::Config_loaded++;
my $oprompt = shift || CPAN::Prompt->new;
my $prompt = $oprompt;
my $commandline = shift || "";
$CPAN::CurrentCommandId ||= 1;
local($^W) = 1;
unless ($Suppress_readline) {
require Term::ReadLine;
if (! $term
or
$term->ReadLine eq "Term::ReadLine::Stub"
) {
$term = Term::ReadLine->new('CPAN Monitor');
}
if ($term->ReadLine eq "Term::ReadLine::Gnu") {
my $attribs = $term->Attribs;
$attribs->{attempted_completion_function} = sub {
&CPAN::Complete::gnu_cpl;
}
} else {
$readline::rl_completion_function =
$readline::rl_completion_function = 'CPAN::Complete::cpl';
}
if (my $histfile = $CPAN::Config->{'histfile'}) {{
unless ($term->can("AddHistory")) {
$CPAN::Frontend->mywarn("Terminal does not support AddHistory.\n");
last;
}
$META->readhist($term,$histfile);
}}
for ($CPAN::Config->{term_ornaments}) { # alias
local $Term::ReadLine::termcap_nowarn = 1;
$term->ornaments($_) if defined;
}
# $term->OUT is autoflushed anyway
my $odef = select STDERR;
$| = 1;
select STDOUT;
$| = 1;
select $odef;
}
$META->checklock();
my @cwd = grep { defined $_ and length $_ }
CPAN::anycwd(),
File::Spec->can("tmpdir") ? File::Spec->tmpdir() : (),
File::Spec->rootdir();
my $try_detect_readline;
$try_detect_readline = $term->ReadLine eq "Term::ReadLine::Stub" if $term;
unless ($CPAN::Config->{inhibit_startup_message}) {
my $rl_avail = $Suppress_readline ? "suppressed" :
($term->ReadLine ne "Term::ReadLine::Stub") ? "enabled" :
"available (maybe install Bundle::CPAN or Bundle::CPANxxl?)";
$CPAN::Frontend->myprint(
sprintf qq{
cpan shell -- CPAN exploration and modules installation (v%s)
Enter 'h' for help.
},
$CPAN::VERSION,
$rl_avail
)
}
my($continuation) = "";
my $last_term_ornaments;
SHELLCOMMAND: while () {
if ($Suppress_readline) {
if ($Echo_readline) {
$|=1;
}
print $prompt;
last SHELLCOMMAND unless defined ($_ = <> );
if ($Echo_readline) {
# backdoor: I could not find a way to record sessions
print $_;
}
chomp;
} else {
last SHELLCOMMAND unless
defined ($_ = $term->readline($prompt, $commandline));
}
$_ = "$continuation$_" if $continuation;
s/^\s+//;
next SHELLCOMMAND if /^$/;
s/^\s*\?\s*/help /;
if (/^(?:q(?:uit)?|bye|exit)\s*$/i) {
last SHELLCOMMAND;
} elsif (s/\\$//s) {
chomp;
$continuation = $_;
$prompt = " > ";
} elsif (/^\!/) {
s/^\!//;
my($eval) = $_;
package
CPAN::Eval; # hide from the indexer
use strict;
use vars qw($import_done);
CPAN->import(':DEFAULT') unless $import_done++;
CPAN->debug("eval[$eval]") if $CPAN::DEBUG;
eval($eval);
warn $@ if $@;
$continuation = "";
$prompt = $oprompt;
} elsif (/./) {
my(@line);
eval { @line = Text::ParseWords::shellwords($_) };
warn($@), next SHELLCOMMAND if $@;
warn("Text::Parsewords could not parse the line [$_]"),
next SHELLCOMMAND unless @line;
$CPAN::META->debug("line[".join("|",@line)."]") if $CPAN::DEBUG;
my $command = shift @line;
eval {
local (*STDOUT)=*STDOUT;
@line = _redirect(@line);
CPAN::Shell->$command(@line)
};
my $command_error = $@;
_unredirect;
my $reported_error;
if ($command_error) {
my $err = $command_error;
if (ref $err and $err->isa('CPAN::Exception::blocked_urllist')) {
$CPAN::Frontend->mywarn("Client not fully configured, please proceed with configuring.$err");
$reported_error = ref $err;
} else {
# I'd prefer never to arrive here and make all errors exception objects
if ($err =~ /\S/) {
require Carp;
require Dumpvalue;
my $dv = Dumpvalue->new(tick => '"');
Carp::cluck(sprintf "Catching error: %s", $dv->stringify($err));
}
}
}
if ($command =~ /^(
# classic commands
make
|test
|install
|clean
# pragmas for classic commands
|ff?orce
|notest
# compounds
|report
|smoke
|upgrade
)$/x) {
# only commands that tell us something about failed distros
# eval necessary for people without an urllist
eval {CPAN::Shell->failed($CPAN::CurrentCommandId,1);};
if (my $err = $@) {
unless (ref $err and $reported_error eq ref $err) {
die $@;
}
}
}
soft_chdir_with_alternatives(\@cwd);
$CPAN::Frontend->myprint("\n");
$continuation = "";
$CPAN::CurrentCommandId++;
$prompt = $oprompt;
}
} continue {
$commandline = ""; # I do want to be able to pass a default to
# shell, but on the second command I see no
# use in that
$Signal=0;
CPAN::Queue->nullify_queue;
if ($try_detect_readline) {
if ($CPAN::META->has_inst("Term::ReadLine::Gnu")
||
$CPAN::META->has_inst("Term::ReadLine::Perl")
) {
delete $INC{"Term/ReadLine.pm"};
my $redef = 0;
local($SIG{__WARN__}) = CPAN::Shell::paintdots_onreload(\$redef);
require Term::ReadLine;
$CPAN::Frontend->myprint("\n$redef subroutines in ".
"Term::ReadLine redefined\n");
$GOTOSHELL = 1;
}
}
if ($term and $term->can("ornaments")) {
for ($CPAN::Config->{term_ornaments}) { # alias
if (defined $_) {
if (not defined $last_term_ornaments
or $_ != $last_term_ornaments
) {
local $Term::ReadLine::termcap_nowarn = 1;
$term->ornaments($_);
$last_term_ornaments = $_;
}
} else {
undef $last_term_ornaments;
}
}
}
for my $class (qw(Module Distribution)) {
# again unsafe meta access?
for my $dm (keys %{$CPAN::META->{readwrite}{"CPAN::$class"}}) {
next unless $CPAN::META->{readwrite}{"CPAN::$class"}{$dm}{incommandcolor};
CPAN->debug("BUG: $class '$dm' was in command state, resetting");
delete $CPAN::META->{readwrite}{"CPAN::$class"}{$dm}{incommandcolor};
}
}
if ($GOTOSHELL) {
$GOTOSHELL = 0; # not too often
$META->savehist if $CPAN::term && $CPAN::term->can("GetHistory");
@_ = ($oprompt,"");
goto &shell;
}
}
soft_chdir_with_alternatives(\@cwd);
}
#-> CPAN::soft_chdir_with_alternatives ;
sub soft_chdir_with_alternatives ($) {
my($cwd) = @_;
unless (@$cwd) {
my $root = File::Spec->rootdir();
$CPAN::Frontend->mywarn(qq{Warning: no good directory to chdir to!
Trying '$root' as temporary haven.
});
push @$cwd, $root;
}
while () {
if (chdir $cwd->[0]) {
return;
} else {
if (@$cwd>1) {
$CPAN::Frontend->mywarn(qq{Could not chdir to "$cwd->[0]": $!
Trying to chdir to "$cwd->[1]" instead.
});
shift @$cwd;
} else {
$CPAN::Frontend->mydie(qq{Could not chdir to "$cwd->[0]": $!});
}
}
}
}
sub _flock {
my($fh,$mode) = @_;
if ( $Config::Config{d_flock} || $Config::Config{d_fcntl_can_lock} ) {
return flock $fh, $mode;
} elsif (!$Have_warned->{"d_flock"}++) {
$CPAN::Frontend->mywarn("Your OS does not seem to support locking; continuing and ignoring all locking issues\n");
$CPAN::Frontend->mysleep(5);
return 1;
} else {
return 1;
}
}
sub _yaml_module () {
my $yaml_module = $CPAN::Config->{yaml_module} || "YAML";
if (
$yaml_module ne "YAML"
&&
!$CPAN::META->has_inst($yaml_module)
) {
# $CPAN::Frontend->mywarn("'$yaml_module' not installed, falling back to 'YAML'\n");
$yaml_module = "YAML";
}
if ($yaml_module eq "YAML"
&&
$CPAN::META->has_inst($yaml_module)
&&
$YAML::VERSION < 0.60
&&
!$Have_warned->{"YAML"}++
) {
$CPAN::Frontend->mywarn("Warning: YAML version '$YAML::VERSION' is too low, please upgrade!\n".
"I'll continue but problems are *very* likely to happen.\n"
);
$CPAN::Frontend->mysleep(5);
}
return $yaml_module;
}
# CPAN::_yaml_loadfile
sub _yaml_loadfile {
my($self,$local_file) = @_;
return +[] unless -s $local_file;
my $yaml_module = _yaml_module;
if ($CPAN::META->has_inst($yaml_module)) {
# temporarily enable yaml code deserialisation
no strict 'refs';
# 5.6.2 could not do the local() with the reference
# so we do it manually instead
my $old_loadcode = ${"$yaml_module\::LoadCode"};
${ "$yaml_module\::LoadCode" } = $CPAN::Config->{yaml_load_code} || 0;
my ($code, @yaml);
if ($code = UNIVERSAL::can($yaml_module, "LoadFile")) {
eval { @yaml = $code->($local_file); };
if ($@) {
# this shall not be done by the frontend
die CPAN::Exception::yaml_process_error->new($yaml_module,$local_file,"parse",$@);
}
} elsif ($code = UNIVERSAL::can($yaml_module, "Load")) {
local *FH;
open FH, $local_file or die "Could not open '$local_file': $!";
local $/;
my $ystream = <FH>;
eval { @yaml = $code->($ystream); };
if ($@) {
# this shall not be done by the frontend
die CPAN::Exception::yaml_process_error->new($yaml_module,$local_file,"parse",$@);
}
}
${"$yaml_module\::LoadCode"} = $old_loadcode;
return \@yaml;
} else {
# this shall not be done by the frontend
die CPAN::Exception::yaml_not_installed->new($yaml_module, $local_file, "parse");
}
return +[];
}
# CPAN::_yaml_dumpfile
sub _yaml_dumpfile {
my($self,$local_file,@what) = @_;
my $yaml_module = _yaml_module;
if ($CPAN::META->has_inst($yaml_module)) {
my $code;
if (UNIVERSAL::isa($local_file, "FileHandle")) {
$code = UNIVERSAL::can($yaml_module, "Dump");
eval { print $local_file $code->(@what) };
} elsif ($code = UNIVERSAL::can($yaml_module, "DumpFile")) {
eval { $code->($local_file,@what); };
} elsif ($code = UNIVERSAL::can($yaml_module, "Dump")) {
local *FH;
open FH, ">$local_file" or die "Could not open '$local_file': $!";
print FH $code->(@what);
}
if ($@) {
die CPAN::Exception::yaml_process_error->new($yaml_module,$local_file,"dump",$@);
}
} else {
if (UNIVERSAL::isa($local_file, "FileHandle")) {
# I think this case does not justify a warning at all
} else {
die CPAN::Exception::yaml_not_installed->new($yaml_module, $local_file, "dump");
}
}
}
sub _init_sqlite () {
unless ($CPAN::META->has_inst("CPAN::SQLite")) {
$CPAN::Frontend->mywarn(qq{CPAN::SQLite not installed, trying to work without\n})
unless $Have_warned->{"CPAN::SQLite"}++;
return;
}
require CPAN::SQLite::META; # not needed since CVS version of 2006-12-17
$CPAN::SQLite ||= CPAN::SQLite::META->new($CPAN::META);
}
{
my $negative_cache = {};
sub _sqlite_running {
if ($negative_cache->{time} && time < $negative_cache->{time} + 60) {
# need to cache the result, otherwise too slow
return $negative_cache->{fact};
} else {
$negative_cache = {}; # reset
}
my $ret = $CPAN::Config->{use_sqlite} && ($CPAN::SQLite || _init_sqlite());
return $ret if $ret; # fast anyway
$negative_cache->{time} = time;
return $negative_cache->{fact} = $ret;
}
}
$META ||= CPAN->new; # In case we re-eval ourselves we need the ||
# from here on only subs.
################################################################################
sub _perl_fingerprint {
my($self,$other_fingerprint) = @_;
my $dll = eval {OS2::DLLname()};
my $mtime_dll = 0;
if (defined $dll) {
$mtime_dll = (-f $dll ? (stat(_))[9] : '-1');
}
my $mtime_perl = (-f CPAN::find_perl ? (stat(_))[9] : '-1');
my $this_fingerprint = {
'$^X' => CPAN::find_perl,
sitearchexp => $Config::Config{sitearchexp},
'mtime_$^X' => $mtime_perl,
'mtime_dll' => $mtime_dll,
};
if ($other_fingerprint) {
if (exists $other_fingerprint->{'stat($^X)'}) { # repair fp from rev. 1.88_57
$other_fingerprint->{'mtime_$^X'} = $other_fingerprint->{'stat($^X)'}[9];
}
# mandatory keys since 1.88_57
for my $key (qw($^X sitearchexp mtime_dll mtime_$^X)) {
return unless $other_fingerprint->{$key} eq $this_fingerprint->{$key};
}
return 1;
} else {
return $this_fingerprint;
}
}
sub suggest_myconfig () {
SUGGEST_MYCONFIG: if(!$INC{'CPAN/MyConfig.pm'}) {
$CPAN::Frontend->myprint("You don't seem to have a user ".
"configuration (MyConfig.pm) yet.\n");
my $new = CPAN::Shell::colorable_makemaker_prompt("Do you want to create a ".
"user configuration now? (Y/n)",
"yes");
if($new =~ m{^y}i) {
CPAN::Shell->mkmyconfig();
return &checklock;
} else {
$CPAN::Frontend->mydie("OK, giving up.");
}
}
}
#-> sub CPAN::all_objects ;
sub all_objects {
my($mgr,$class) = @_;
CPAN::HandleConfig->load unless $CPAN::Config_loaded++;
CPAN->debug("mgr[$mgr] class[$class]") if $CPAN::DEBUG;
CPAN::Index->reload;
values %{ $META->{readwrite}{$class} }; # unsafe meta access, ok
}
# Called by shell, not in batch mode. In batch mode I see no risk in
# having many processes updating something as installations are
# continually checked at runtime. In shell mode I suspect it is
# unintentional to open more than one shell at a time
#-> sub CPAN::checklock ;
sub checklock {
my($self) = @_;
my $lockfile = File::Spec->catfile($CPAN::Config->{cpan_home},".lock");
if (-f $lockfile && -M _ > 0) {
my $fh = FileHandle->new($lockfile) or
$CPAN::Frontend->mydie("Could not open lockfile '$lockfile': $!");
my $otherpid = <$fh>;
my $otherhost = <$fh>;
$fh->close;
if (defined $otherpid && $otherpid) {
chomp $otherpid;
}
if (defined $otherhost && $otherhost) {
chomp $otherhost;
}
my $thishost = hostname();
if (defined $otherhost && defined $thishost &&
$otherhost ne '' && $thishost ne '' &&
$otherhost ne $thishost) {
$CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Lockfile '$lockfile'\n".
"reports other host $otherhost and other ".
"process $otherpid.\n".
"Cannot proceed.\n"));
} elsif ($RUN_DEGRADED) {
$CPAN::Frontend->mywarn("Running in downgraded mode (experimental)\n");
} elsif (defined $otherpid && $otherpid) {
return if $$ == $otherpid; # should never happen
$CPAN::Frontend->mywarn(
qq{
There seems to be running another CPAN process (pid $otherpid). Contacting...
});
if (kill 0, $otherpid or $!{EPERM}) {
$CPAN::Frontend->mywarn(qq{Other job is running.\n});
my($ans) =
CPAN::Shell::colorable_makemaker_prompt
(qq{Shall I try to run in downgraded }.
qq{mode? (Y/n)},"y");
if ($ans =~ /^y/i) {
$CPAN::Frontend->mywarn("Running in downgraded mode (experimental).
Please report if something unexpected happens\n");
$RUN_DEGRADED = 1;
for ($CPAN::Config) {
# XXX
# $_->{build_dir_reuse} = 0; # 2006-11-17 akoenig Why was that?
$_->{commandnumber_in_prompt} = 0; # visibility
$_->{histfile} = ""; # who should win otherwise?
$_->{cache_metadata} = 0; # better would be a lock?
$_->{use_sqlite} = 0; # better would be a write lock!
$_->{auto_commit} = 0; # we are violent, do not persist
$_->{test_report} = 0; # Oliver Paukstadt had sent wrong reports in degraded mode
}
} else {
$CPAN::Frontend->mydie("
You may want to kill the other job and delete the lockfile. On UNIX try:
kill $otherpid
rm $lockfile
");
}
} elsif (-w $lockfile) {
my($ans) =
CPAN::Shell::colorable_makemaker_prompt
(qq{Other job not responding. Shall I overwrite }.
qq{the lockfile '$lockfile'? (Y/n)},"y");
$CPAN::Frontend->myexit("Ok, bye\n")
unless $ans =~ /^y/i;
} else {
Carp::croak(
qq{Lockfile '$lockfile' not writable by you. }.
qq{Cannot proceed.\n}.
qq{ On UNIX try:\n}.
qq{ rm '$lockfile'\n}.
qq{ and then rerun us.\n}
);
}
} else {
$CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Found invalid lockfile ".
"'$lockfile', please remove. Cannot proceed.\n"));
}
}
my $dotcpan = $CPAN::Config->{cpan_home};
eval { File::Path::mkpath($dotcpan);};
if ($@) {
# A special case at least for Jarkko.
my $firsterror = $@;
my $seconderror;
my $symlinkcpan;
if (-l $dotcpan) {
$symlinkcpan = readlink $dotcpan;
die "readlink $dotcpan failed: $!" unless defined $symlinkcpan;
eval { File::Path::mkpath($symlinkcpan); };
if ($@) {
$seconderror = $@;
} else {
$CPAN::Frontend->mywarn(qq{
Working directory $symlinkcpan created.
});
}
}
unless (-d $dotcpan) {
my $mess = qq{
Your configuration suggests "$dotcpan" as your
CPAN.pm working directory. I could not create this directory due
to this error: $firsterror\n};
$mess .= qq{
As "$dotcpan" is a symlink to "$symlinkcpan",
I tried to create that, but I failed with this error: $seconderror
} if $seconderror;
$mess .= qq{
Please make sure the directory exists and is writable.
};
$CPAN::Frontend->mywarn($mess);
return suggest_myconfig;
}
} # $@ after eval mkpath $dotcpan
if (0) { # to test what happens when a race condition occurs
for (reverse 1..10) {
print $_, "\n";
sleep 1;
}
}
# locking
if (!$RUN_DEGRADED && !$self->{LOCKFH}) {
my $fh;
unless ($fh = FileHandle->new("+>>$lockfile")) {
$CPAN::Frontend->mywarn(qq{
Your configuration suggests that CPAN.pm should use a working
directory of
$CPAN::Config->{cpan_home}
Unfortunately we could not create the lock file
$lockfile
due to '$!'.
Please make sure that the configuration variable
\$CPAN::Config->{cpan_home}
points to a directory where you can write a .lock file. You can set
this variable in either a CPAN/MyConfig.pm or a CPAN/Config.pm in your
\@INC path;
});
return suggest_myconfig;
}
my $sleep = 1;
while (!CPAN::_flock($fh, LOCK_EX|LOCK_NB)) {
if ($sleep>10) {
$CPAN::Frontend->mydie("Giving up\n");
}
$CPAN::Frontend->mysleep($sleep++);
$CPAN::Frontend->mywarn("Could not lock lockfile with flock: $!; retrying\n");
}
seek $fh, 0, 0;
truncate $fh, 0;
$fh->autoflush(1);
$fh->print($$, "\n");
$fh->print(hostname(), "\n");
$self->{LOCK} = $lockfile;
$self->{LOCKFH} = $fh;
}
$SIG{TERM} = sub {
my $sig = shift;
&cleanup;
$CPAN::Frontend->mydie("Got SIG$sig, leaving");
};
$SIG{INT} = sub {
# no blocks!!!
my $sig = shift;
&cleanup if $Signal;
die "Got yet another signal" if $Signal > 1;
$CPAN::Frontend->mydie("Got another SIG$sig") if $Signal;
$CPAN::Frontend->mywarn("Caught SIG$sig, trying to continue\n");
$Signal++;
};
# From: Larry Wall <larry@wall.org>
# Subject: Re: deprecating SIGDIE
# To: perl5-porters@perl.org
# Date: Thu, 30 Sep 1999 14:58:40 -0700 (PDT)
#
# The original intent of __DIE__ was only to allow you to substitute one
# kind of death for another on an application-wide basis without respect
# to whether you were in an eval or not. As a global backstop, it should
# not be used any more lightly (or any more heavily :-) than class
# UNIVERSAL. Any attempt to build a general exception model on it should
# be politely squashed. Any bug that causes every eval {} to have to be
# modified should be not so politely squashed.
#
# Those are my current opinions. It is also my optinion that polite
# arguments degenerate to personal arguments far too frequently, and that
# when they do, it's because both people wanted it to, or at least didn't
# sufficiently want it not to.
#
# Larry
# global backstop to cleanup if we should really die
$SIG{__DIE__} = \&cleanup;
$self->debug("Signal handler set.") if $CPAN::DEBUG;
}
#-> sub CPAN::DESTROY ;
sub DESTROY {
&cleanup; # need an eval?
}
#-> sub CPAN::anycwd ;
sub anycwd () {
my $getcwd;
$getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
CPAN->$getcwd();
}
#-> sub CPAN::cwd ;
sub cwd {Cwd::cwd();}
#-> sub CPAN::getcwd ;
sub getcwd {Cwd::getcwd();}
#-> sub CPAN::fastcwd ;
sub fastcwd {Cwd::fastcwd();}
#-> sub CPAN::backtickcwd ;
sub backtickcwd {my $cwd = `cwd`; chomp $cwd; $cwd}
# Adapted from Probe::Perl
#-> sub CPAN::_perl_is_same
sub _perl_is_same {
my ($perl) = @_;
return MM->maybe_command($perl)
&& `$perl -MConfig=myconfig -e print -e myconfig` eq Config->myconfig;
}
# Adapted in part from Probe::Perl
#-> sub CPAN::find_perl ;
sub find_perl () {
if ( File::Spec->file_name_is_absolute($^X) ) {
return $^X;
}
else {
my $exe = $Config::Config{exe_ext};
my @candidates = (
File::Spec->catfile($CPAN::iCwd,$^X),
$Config::Config{'perlpath'},
);
for my $perl_name ($^X, 'perl', 'perl5', "perl$]") {
for my $path (File::Spec->path(), $Config::Config{'binexp'}) {
if ( defined($path) && length $path && -d $path ) {
my $perl = File::Spec->catfile($path,$perl_name);
push @candidates, $perl;
# try with extension if not provided already
if ($^O eq 'VMS') {
# VMS might have a file version at the end
push @candidates, $perl . $exe
unless $perl =~ m/$exe(;\d+)?$/i;
} elsif (defined $exe && length $exe) {
push @candidates, $perl . $exe
unless $perl =~ m/$exe$/i;
}
}
}
}
for my $perl ( @candidates ) {
if (MM->maybe_command($perl) && _perl_is_same($perl)) {
$^X = $perl;
return $perl;
}
}
}
return $^X; # default fall back
}
#-> sub CPAN::exists ;
sub exists {
my($mgr,$class,$id) = @_;
CPAN::HandleConfig->load unless $CPAN::Config_loaded++;
CPAN::Index->reload;
### Carp::croak "exists called without class argument" unless $class;
$id ||= "";
$id =~ s/:+/::/g if $class eq "CPAN::Module";
my $exists;
if (CPAN::_sqlite_running) {
$exists = (exists $META->{readonly}{$class}{$id} or
$CPAN::SQLite->set($class, $id));
} else {
$exists = exists $META->{readonly}{$class}{$id};
}
$exists ||= exists $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
}
#-> sub CPAN::delete ;
sub delete {
my($mgr,$class,$id) = @_;
delete $META->{readonly}{$class}{$id}; # unsafe meta access, ok
delete $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
}
#-> sub CPAN::has_usable
# has_inst is sometimes too optimistic, we should replace it with this
# has_usable whenever a case is given
sub has_usable {