Upgrade to CPAN-1.87_62
[p5sagit/p5-mst-13.2.git] / lib / CPAN / HandleConfig.pm
CommitLineData
e82b9348 1package CPAN::HandleConfig;
2use strict;
87892b73 3use vars qw(%can %keys $VERSION);
1a43333d 4
8962fc49 5$VERSION = sprintf "%.6f", substr(q$Rev: 809 $,4)/1000000 + 5.4;
e82b9348 6
7%can = (
4d1321a7 8 commit => "Commit changes to disk",
9 defaults => "Reload defaults from disk",
10 help => "Short help about 'o conf' usage",
11 init => "Interactive setting of all options",
e82b9348 12);
13
4d1321a7 14%keys = map { $_ => undef } (
ed84aac9 15 # allow_unauthenticated ?? some day...
4d1321a7 16 "build_cache",
17 "build_dir",
18 "bzip2",
19 "cache_metadata",
ed84aac9 20 "check_sigs",
8962fc49 21 "colorize_output",
22 "colorize_print",
23 "colorize_warn",
4d1321a7 24 "commandnumber_in_prompt",
25 "cpan_home",
26 "curl",
27 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
28 "dontload_list",
29 "ftp",
30 "ftp_passive",
31 "ftp_proxy",
32 "getcwd",
33 "gpg",
34 "gzip",
35 "histfile",
36 "histsize",
37 "http_proxy",
38 "inactivity_timeout",
39 "index_expire",
40 "inhibit_startup_message",
41 "keep_source_where",
42 "lynx",
43 "make",
44 "make_arg",
45 "make_install_arg",
46 "make_install_make_command",
47 "makepl_arg",
48 "mbuild_arg",
49 "mbuild_install_arg",
50 "mbuild_install_build_command",
51 "mbuildpl_arg",
52 "ncftp",
53 "ncftpget",
54 "no_proxy",
55 "pager",
ed84aac9 56 "password",
4d1321a7 57 "prefer_installer",
58 "prerequisites_policy",
8962fc49 59 "proxy_pass",
60 "proxy_user",
4d1321a7 61 "scan_cache",
62 "shell",
63 "show_upload_date",
64 "tar",
65 "term_is_latin",
ed84aac9 66 "term_ornaments",
8962fc49 67 "test_report",
4d1321a7 68 "unzip",
69 "urllist",
ed84aac9 70 "username",
4d1321a7 71 "wait_list",
72 "wget",
73 );
44d21104 74if ($^O eq "MSWin32") {
75 for my $k (qw(
76 mbuild_install_build_command
77 make_install_make_command
78 )) {
79 delete $keys{$k};
80 if (exists $CPAN::Config->{$k}) {
87892b73 81 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
82 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
83 }
44d21104 84 delete $CPAN::Config->{$k};
85 }
86 }
87}
e82b9348 88
89# returns true on successful action
90sub edit {
91 my($self,@args) = @_;
92 return unless @args;
93 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
94 my($o,$str,$func,$args,$key_exists);
95 $o = shift @args;
9ddc4ed0 96 $DB::single = 1;
e82b9348 97 if($can{$o}) {
8962fc49 98 $self->$o(args => \@args); # o conf init => sub init => sub load
e82b9348 99 return 1;
100 } else {
101 CPAN->debug("o[$o]") if $CPAN::DEBUG;
102 unless (exists $keys{$o}) {
103 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
104 }
105 if ($o =~ /list$/) {
106 $func = shift @args;
107 $func ||= "";
108 CPAN->debug("func[$func]") if $CPAN::DEBUG;
109 my $changed;
110 # Let's avoid eval, it's easier to comprehend without.
111 if ($func eq "push") {
112 push @{$CPAN::Config->{$o}}, @args;
113 $changed = 1;
114 } elsif ($func eq "pop") {
115 pop @{$CPAN::Config->{$o}};
116 $changed = 1;
117 } elsif ($func eq "shift") {
118 shift @{$CPAN::Config->{$o}};
119 $changed = 1;
120 } elsif ($func eq "unshift") {
121 unshift @{$CPAN::Config->{$o}}, @args;
122 $changed = 1;
123 } elsif ($func eq "splice") {
124 splice @{$CPAN::Config->{$o}}, @args;
125 $changed = 1;
126 } elsif (@args) {
127 $CPAN::Config->{$o} = [@args];
128 $changed = 1;
129 } else {
130 $self->prettyprint($o);
131 }
4d1321a7 132 if ($changed) {
133 if ($o eq "urllist") {
134 # reset the cached values
135 undef $CPAN::FTP::Thesite;
136 undef $CPAN::FTP::Themethod;
137 } elsif ($o eq "dontload_list") {
138 # empty it, it will be built up again
139 $CPAN::META->{dontload_hash} = {};
140 }
e82b9348 141 }
142 return $changed;
ca79d794 143 } elsif ($o =~ /_hash$/) {
44d21104 144 @args = () if @args==1 && $args[0] eq "";
ca79d794 145 push @args, "" if @args % 2;
146 $CPAN::Config->{$o} = { @args };
147 } else {
e82b9348 148 $CPAN::Config->{$o} = $args[0] if defined $args[0];
149 $self->prettyprint($o);
8962fc49 150 return 1;
e82b9348 151 }
152 }
153}
154
155sub prettyprint {
156 my($self,$k) = @_;
157 my $v = $CPAN::Config->{$k};
158 if (ref $v) {
9ddc4ed0 159 my(@report);
160 if (ref $v eq "ARRAY") {
161 @report = map {"\t[$_]\n"} @$v;
162 } else {
163 @report = map { sprintf("\t%-18s => %s\n",
164 map { "[$_]" } $_,
165 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
166 )} keys %$v;
167 }
e82b9348 168 $CPAN::Frontend->myprint(
169 join(
170 "",
171 sprintf(
172 " %-18s\n",
173 $k
174 ),
9ddc4ed0 175 @report
e82b9348 176 )
177 );
178 } elsif (defined $v) {
179 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
180 } else {
181 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
182 }
183}
184
185sub commit {
9ddc4ed0 186 my($self,@args) = @_;
187 my $configpm;
188 if (@args) {
189 if ($args[0] eq "args") {
190 # we have not signed that contract
191 } else {
192 $configpm = $args[0];
193 }
194 }
e82b9348 195 unless (defined $configpm){
196 $configpm ||= $INC{"CPAN/MyConfig.pm"};
197 $configpm ||= $INC{"CPAN/Config.pm"};
198 $configpm || Carp::confess(q{
199CPAN::Config::commit called without an argument.
200Please specify a filename where to save the configuration or try
201"o conf init" to have an interactive course through configing.
202});
203 }
204 my($mode);
205 if (-f $configpm) {
206 $mode = (stat $configpm)[2];
207 if ($mode && ! -w _) {
208 Carp::confess("$configpm is not writable");
209 }
210 }
211
212 my $msg;
213 $msg = <<EOF unless $configpm =~ /MyConfig/;
214
215# This is CPAN.pm's systemwide configuration file. This file provides
216# defaults for users, and the values can be changed in a per-user
217# configuration file. The user-config file is being looked for as
218# ~/.cpan/CPAN/MyConfig.pm.
219
220EOF
221 $msg ||= "\n";
222 my($fh) = FileHandle->new;
223 rename $configpm, "$configpm~" if -f $configpm;
224 open $fh, ">$configpm" or
225 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
226 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
227 foreach (sort keys %$CPAN::Config) {
44d21104 228 unless (exists $keys{$_}) {
229 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
230 delete $CPAN::Config->{$_};
231 next;
232 }
e82b9348 233 $fh->print(
234 " '$_' => ",
ca79d794 235 $self->neatvalue($CPAN::Config->{$_}),
e82b9348 236 ",\n"
237 );
238 }
239
240 $fh->print("};\n1;\n__END__\n");
241 close $fh;
242
243 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
244 #chmod $mode, $configpm;
245###why was that so? $self->defaults;
9ddc4ed0 246 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
e82b9348 247 1;
248}
249
ca79d794 250# stolen from MakeMaker; not taking the original because it is buggy;
251# bugreport will have to say: keys of hashes remain unquoted and can
252# produce syntax errors
253sub neatvalue {
254 my($self, $v) = @_;
255 return "undef" unless defined $v;
256 my($t) = ref $v;
257 return "q[$v]" unless $t;
258 if ($t eq 'ARRAY') {
259 my(@m, @neat);
260 push @m, "[";
261 foreach my $elem (@$v) {
262 push @neat, "q[$elem]";
263 }
264 push @m, join ", ", @neat;
265 push @m, "]";
266 return join "", @m;
267 }
268 return "$v" unless $t eq 'HASH';
269 my(@m, $key, $val);
270 while (($key,$val) = each %$v){
271 last unless defined $key; # cautious programming in case (undef,undef) is true
272 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
273 }
274 return "{ ".join(', ',@m)." }";
275}
276
e82b9348 277sub defaults {
278 my($self) = @_;
c9869e1c 279 my $done;
280 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
281 CPAN::Shell->reload_this($config) and $done++;
282 last if $done;
283 }
e82b9348 284 1;
285}
286
ed84aac9 287=head2 C<< CLASS->safe_quote ITEM >>
288
289Quotes an item to become safe against spaces
290in shell interpolation. An item is enclosed
291in double quotes if:
292
293 - the item contains spaces in the middle
294 - the item does not start with a quote
295
296This happens to avoid shell interpolation
297problems when whitespace is present in
298directory names.
299
300This method uses C<commands_quote> to determine
301the correct quote. If C<commands_quote> is
302a space, no quoting will take place.
303
304
305if it starts and ends with the same quote character: leave it as it is
306
307if it contains no whitespace: leave it as it is
308
309if it contains whitespace, then
310
311if it contains quotes: better leave it as it is
312
313else: quote it with the correct quote type for the box we're on
314
315=cut
316
317{
318 # Instead of patching the guess, set commands_quote
319 # to the right value
320 my ($quotes,$use_quote)
321 = $^O eq 'MSWin32'
322 ? ('"', '"')
323 : (q<"'>, "'")
324 ;
325
326 sub safe_quote {
327 my ($self, $command) = @_;
328 # Set up quote/default quote
329 my $quote = $CPAN::Config->{commands_quote} || $quotes;
330
331 if ($quote ne ' '
332 and $command =~ /\s/
333 and $command !~ /[$quote]/) {
334 return qq<$use_quote$command$use_quote>
335 }
336 return $command;
337 }
338}
339
e82b9348 340sub init {
9ddc4ed0 341 my($self,@args) = @_;
e82b9348 342 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
343 # have the least
344 # important
345 # variable
346 # undefined
9ddc4ed0 347 $self->load(@args);
e82b9348 348 1;
349}
350
351# This is a piece of repeated code that is abstracted here for
352# maintainability. RMB
353#
354sub _configpmtest {
355 my($configpmdir, $configpmtest) = @_;
356 if (-w $configpmtest) {
357 return $configpmtest;
358 } elsif (-w $configpmdir) {
359 #_#_# following code dumped core on me with 5.003_11, a.k.
360 my $configpm_bak = "$configpmtest.bak";
361 unlink $configpm_bak if -f $configpm_bak;
362 if( -f $configpmtest ) {
363 if( rename $configpmtest, $configpm_bak ) {
364 $CPAN::Frontend->mywarn(<<END);
365Old configuration file $configpmtest
366 moved to $configpm_bak
367END
368 }
369 }
370 my $fh = FileHandle->new;
371 if ($fh->open(">$configpmtest")) {
372 $fh->print("1;\n");
373 return $configpmtest;
374 } else {
375 # Should never happen
376 Carp::confess("Cannot open >$configpmtest");
377 }
378 } else { return }
379}
380
87892b73 381sub require_myconfig_or_config () {
382 return if $INC{"CPAN/MyConfig.pm"};
383 local @INC = @INC;
384 my $home = home();
385 unshift @INC, File::Spec->catdir($home,'.cpan');
386 eval { require CPAN::MyConfig };
ed84aac9 387 my $err_myconfig = $@;
388 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
389 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
390 }
87892b73 391 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
392 eval {require CPAN::Config;}; # not everybody has one
ed84aac9 393 my $err_config = $@;
394 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
395 die "Error while requiring CPAN::Config:\n$err_config";
396 }
87892b73 397 }
398}
399
400sub home () {
401 my $home;
402 if ($CPAN::META->has_usable("File::HomeDir")) {
403 $home = File::HomeDir->my_data;
404 } else {
405 $home = $ENV{HOME};
406 }
407 $home;
408}
409
e82b9348 410sub load {
411 my($self, %args) = @_;
412 $CPAN::Be_Silent++ if $args{be_silent};
413
414 my(@miss);
415 use Carp;
87892b73 416 require_myconfig_or_config;
e82b9348 417 return unless @miss = $self->missing_config_data;
418
419 require CPAN::FirstTime;
420 my($configpm,$fh,$redo,$theycalled);
421 $redo ||= "";
422 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
423 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
424 $configpm = $INC{"CPAN/Config.pm"};
425 $redo++;
426 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
427 $configpm = $INC{"CPAN/MyConfig.pm"};
428 $redo++;
429 } else {
430 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
431 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
432 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
c9869e1c 433 my $inc_key;
e82b9348 434 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
c9869e1c 435 $configpm = _configpmtest($configpmdir,$configpmtest);
436 $inc_key = "CPAN/Config.pm";
e82b9348 437 }
438 unless ($configpm) {
87892b73 439 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
e82b9348 440 File::Path::mkpath($configpmdir);
441 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
c9869e1c 442 $configpm = _configpmtest($configpmdir,$configpmtest);
443 $inc_key = "CPAN/MyConfig.pm";
e82b9348 444 }
c9869e1c 445 if ($configpm) {
446 $INC{$inc_key} = $configpm;
447 } else {
448 my $text = qq{WARNING: CPAN.pm is unable to } .
449 qq{create a configuration file.};
450 output($text, 'confess');
451 }
452
e82b9348 453 }
454 local($") = ", ";
8962fc49 455 if ($redo && ! $theycalled){
456 $CPAN::Frontend->myprint(<<END);
0cf35e6a 457Sorry, we have to rerun the configuration dialog for CPAN.pm due to
458the following indispensable but missing parameters:
e82b9348 459
460@miss
461END
8962fc49 462 $args{args} = ["\\b".join("|",@miss)."\\b"];
463 }
e82b9348 464 $CPAN::Frontend->myprint(qq{
465$configpm initialized.
466});
e82b9348 467 CPAN::FirstTime::init($configpm, %args);
468}
469
470sub missing_config_data {
471 my(@miss);
472 for (
0cf35e6a 473 "build_cache",
474 "build_dir",
475 "cache_metadata",
476 "cpan_home",
477 "ftp_proxy",
ed84aac9 478 #"gzip",
0cf35e6a 479 "http_proxy",
480 "index_expire",
481 "inhibit_startup_message",
482 "keep_source_where",
ed84aac9 483 #"make",
0cf35e6a 484 "make_arg",
485 "make_install_arg",
486 "makepl_arg",
487 "mbuild_arg",
488 "mbuild_install_arg",
489 "mbuild_install_build_command",
490 "mbuildpl_arg",
491 "no_proxy",
ed84aac9 492 #"pager",
e82b9348 493 "prerequisites_policy",
0cf35e6a 494 "scan_cache",
ed84aac9 495 #"tar",
496 #"unzip",
0cf35e6a 497 "urllist",
e82b9348 498 ) {
44d21104 499 next unless exists $keys{$_};
e82b9348 500 push @miss, $_ unless defined $CPAN::Config->{$_};
501 }
502 return @miss;
503}
504
e82b9348 505sub help {
506 $CPAN::Frontend->myprint(q[
507Known options:
e82b9348 508 commit commit session changes to disk
4d1321a7 509 defaults reload default config values from disk
510 help this help
e82b9348 511 init go through a dialog to set all parameters
512
4d1321a7 513Edit key values as in the following (the "o" is a literal letter o):
e82b9348 514 o conf build_cache 15
e82b9348 515 o conf build_dir "/foo/bar"
e82b9348 516 o conf urllist shift
e82b9348 517 o conf urllist unshift ftp://ftp.foo.bar/
4d1321a7 518 o conf inhibit_startup_message 1
e82b9348 519
520]);
521 undef; #don't reprint CPAN::Config
522}
523
524sub cpl {
525 my($word,$line,$pos) = @_;
526 $word ||= "";
527 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
528 my(@words) = split " ", substr($line,0,$pos+1);
529 if (
530 defined($words[2])
531 and
8962fc49 532 $words[2] =~ /list$/
533 and
e82b9348 534 (
8962fc49 535 @words == 3
e82b9348 536 ||
8962fc49 537 @words == 4 && length($word)
e82b9348 538 )
539 ) {
540 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
8962fc49 541 } elsif (defined($words[2])
542 and
543 $words[2] eq "init"
544 and
545 (
546 @words == 3
547 ||
548 @words == 4 && length($word)
549 )) {
550 return sort grep /^\Q$word\E/, keys %keys;
e82b9348 551 } elsif (@words >= 4) {
552 return ();
553 }
554 my %seen;
555 my(@o_conf) = sort grep { !$seen{$_}++ }
556 keys %can,
557 keys %$CPAN::Config,
558 keys %keys;
559 return grep /^\Q$word\E/, @o_conf;
560}
561
9ddc4ed0 562
4d1321a7 563package
564 CPAN::Config; ####::###### #hide from indexer
9ddc4ed0 565# note: J. Nick Koston wrote me that they are using
566# CPAN::Config->commit although undocumented. I suggested
567# CPAN::Shell->o("conf","commit") even when ugly it is at least
568# documented
569
570# that's why I added the CPAN::Config class with autoload and
571# deprecated warning
572
573use strict;
574use vars qw($AUTOLOAD $VERSION);
8962fc49 575$VERSION = sprintf "%.2f", substr(q$Rev: 809 $,4)/100;
9ddc4ed0 576
577# formerly CPAN::HandleConfig was known as CPAN::Config
578sub AUTOLOAD {
579 my($l) = $AUTOLOAD;
580 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
581 $l =~ s/.*:://;
582 CPAN::HandleConfig->$l(@_);
583}
584
e82b9348 5851;
0cf35e6a 586
587__END__
588# Local Variables:
589# mode: cperl
ca79d794 590# cperl-indent-level: 4
0cf35e6a 591# End: