1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $loading $VERSION);
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",
14 # Q: where is the "How do I add a new config option" HOWTO?
15 # A1: svn diff -r 757:758 # where dagolden added test_report
16 # A2: svn diff -r 985:986 # where andk added yaml_module
17 # A3: 1. add new config option to %keys below
18 # 2. add a Pod description in CPAN::FirstTime; it should include a
19 # prompt line; see others for examples
20 # 3. add a "matcher" section in CPAN::FirstTime::init that includes
21 # a prompt function; see others for examples
22 # 4. add config option to documentation section in CPAN.pm
24 %keys = map { $_ => undef }
31 "build_requires_install_policy",
39 "commandnumber_in_prompt",
41 "connect_to_internet_ok",
44 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
60 "inhibit_startup_message",
62 "load_module_verbosity",
67 "make_install_make_command",
71 "mbuild_install_build_command",
82 "prerequisites_policy",
88 "show_unparsable_versions",
96 "trust_test_report_history",
107 my %prefssupport = map { $_ => 1 }
109 "build_requires_install_policy",
112 "make_install_make_command",
117 # returns true on successful action
119 my($self,@args) = @_;
121 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
122 my($o,$str,$func,$args,$key_exists);
125 $self->$o(args => \@args); # o conf init => sub init => sub load
128 CPAN->debug("o[$o]") if $CPAN::DEBUG;
129 unless (exists $keys{$o}) {
130 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
135 # one day I used randomize_urllist for a boolean, so we must
136 # list them explicitly --ak
138 } elsif ($o =~ /^(wait_list|urllist|dontload_list)$/) {
146 CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG;
147 # Let's avoid eval, it's easier to comprehend without.
148 if ($func eq "push") {
149 push @{$CPAN::Config->{$o}}, @args;
151 } elsif ($func eq "pop") {
152 pop @{$CPAN::Config->{$o}};
154 } elsif ($func eq "shift") {
155 shift @{$CPAN::Config->{$o}};
157 } elsif ($func eq "unshift") {
158 unshift @{$CPAN::Config->{$o}}, @args;
160 } elsif ($func eq "splice") {
161 my $offset = shift @args || 0;
162 my $length = shift @args || 0;
163 splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn
166 $CPAN::Config->{$o} = [$func, @args];
169 $self->prettyprint($o);
172 if ($o eq "urllist") {
173 # reset the cached values
174 undef $CPAN::FTP::Thesite;
175 undef $CPAN::FTP::Themethod;
176 $CPAN::Index::LAST_TIME = 0;
177 } elsif ($o eq "dontload_list") {
178 # empty it, it will be built up again
179 $CPAN::META->{dontload_hash} = {};
182 } elsif ($o =~ /_hash$/) {
188 if (@args==1 && $args[0] eq "") {
190 } elsif (@args % 2) {
193 $CPAN::Config->{$o} = { @args };
201 if (defined $args[0]) {
202 $CPAN::CONFIG_DIRTY = 1;
203 $CPAN::Config->{$o} = $args[0];
206 $self->prettyprint($o)
207 if exists $keys{$o} or defined $CPAN::Config->{$o};
210 if ($CPAN::Config->{auto_commit}) {
213 $CPAN::CONFIG_DIRTY = 1;
214 $CPAN::Frontend->myprint("Please use 'o conf commit' to ".
215 "make the config permanent!\n\n");
223 my $v = $CPAN::Config->{$k};
226 if (ref $v eq "ARRAY") {
227 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
231 sprintf "\t%-18s => %s\n",
233 defined $v->{$_} ? "[$v->{$_}]" : "undef"
236 $CPAN::Frontend->myprint(
246 } elsif (defined $v) {
247 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
249 $CPAN::Frontend->myprint(sprintf " %-18s undef\n", $k);
254 my($self,@args) = @_;
255 CPAN->debug("args[@args]") if $CPAN::DEBUG;
256 if ($CPAN::RUN_DEGRADED) {
257 $CPAN::Frontend->mydie(
258 "'o conf commit' disabled in ".
259 "degraded mode. Maybe try\n".
260 " !undef \$CPAN::RUN_DEGRADED\n"
265 if ($args[0] eq "args") {
266 # we have not signed that contract
268 $configpm = $args[0];
271 unless (defined $configpm) {
272 $configpm ||= $INC{"CPAN/MyConfig.pm"};
273 $configpm ||= $INC{"CPAN/Config.pm"};
274 $configpm || Carp::confess(q{
275 CPAN::Config::commit called without an argument.
276 Please specify a filename where to save the configuration or try
277 "o conf init" to have an interactive course through configing.
282 $mode = (stat $configpm)[2];
283 if ($mode && ! -w _) {
284 Carp::confess("$configpm is not writable");
290 $msg = <<EOF unless $configpm =~ /MyConfig/;
292 # This is CPAN.pm's systemwide configuration file. This file provides
293 # defaults for users, and the values can be changed in a per-user
294 # configuration file. The user-config file is being looked for as
295 # $home/.cpan/CPAN/MyConfig.pm.
299 my($fh) = FileHandle->new;
300 rename $configpm, "$configpm~" if -f $configpm;
301 open $fh, ">$configpm" or
302 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
303 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
304 foreach (sort keys %$CPAN::Config) {
305 unless (exists $keys{$_}) {
306 # do not drop them: forward compatibility!
307 $CPAN::Frontend->mywarn("Unknown config variable '$_'\n");
312 $self->neatvalue($CPAN::Config->{$_}),
317 $fh->print("};\n1;\n__END__\n");
320 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
321 #chmod $mode, $configpm;
322 ###why was that so? $self->defaults;
323 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
324 $CPAN::CONFIG_DIRTY = 0;
328 # stolen from MakeMaker; not taking the original because it is buggy;
329 # bugreport will have to say: keys of hashes remain unquoted and can
330 # produce syntax errors
333 return "undef" unless defined $v;
342 foreach my $elem (@$v) {
343 push @neat, "q[$elem]";
345 push @m, join ", ", @neat;
349 return "$v" unless $t eq 'HASH';
351 while (($key,$val) = each %$v) {
352 last unless defined $key; # cautious programming in case (undef,undef) is true
353 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
355 return "{ ".join(', ',@m)." }";
360 if ($CPAN::RUN_DEGRADED) {
361 $CPAN::Frontend->mydie(
362 "'o conf defaults' disabled in ".
363 "degraded mode. Maybe try\n".
364 " !undef \$CPAN::RUN_DEGRADED\n"
368 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
370 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
371 CPAN::Shell->_reload_this($config,{reloforce => 1});
372 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
376 $CPAN::CONFIG_DIRTY = 0;
380 =head2 C<< CLASS->safe_quote ITEM >>
382 Quotes an item to become safe against spaces
383 in shell interpolation. An item is enclosed
386 - the item contains spaces in the middle
387 - the item does not start with a quote
389 This happens to avoid shell interpolation
390 problems when whitespace is present in
393 This method uses C<commands_quote> to determine
394 the correct quote. If C<commands_quote> is
395 a space, no quoting will take place.
398 if it starts and ends with the same quote character: leave it as it is
400 if it contains no whitespace: leave it as it is
402 if it contains whitespace, then
404 if it contains quotes: better leave it as it is
406 else: quote it with the correct quote type for the box we're on
411 # Instead of patching the guess, set commands_quote
413 my ($quotes,$use_quote)
420 my ($self, $command) = @_;
421 # Set up quote/default quote
422 my $quote = $CPAN::Config->{commands_quote} || $quotes;
425 and defined($command )
427 and $command !~ /[$quote]/) {
428 return qq<$use_quote$command$use_quote>
435 my($self,@args) = @_;
436 CPAN->debug("self[$self]args[".join(",",@args)."]");
437 $self->load(doit => 1, @args);
441 # This is a piece of repeated code that is abstracted here for
442 # maintainability. RMB
445 my($configpmdir, $configpmtest) = @_;
446 if (-w $configpmtest) {
447 return $configpmtest;
448 } elsif (-w $configpmdir) {
449 #_#_# following code dumped core on me with 5.003_11, a.k.
450 my $configpm_bak = "$configpmtest.bak";
451 unlink $configpm_bak if -f $configpm_bak;
452 if( -f $configpmtest ) {
453 if( rename $configpmtest, $configpm_bak ) {
454 $CPAN::Frontend->mywarn(<<END);
455 Old configuration file $configpmtest
456 moved to $configpm_bak
460 my $fh = FileHandle->new;
461 if ($fh->open(">$configpmtest")) {
463 return $configpmtest;
465 # Should never happen
466 Carp::confess("Cannot open >$configpmtest");
471 sub require_myconfig_or_config () {
472 return if $INC{"CPAN/MyConfig.pm"};
475 unshift @INC, File::Spec->catdir($home,'.cpan');
476 eval { require CPAN::MyConfig };
477 my $err_myconfig = $@;
478 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
479 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
481 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
482 eval {require CPAN::Config;}; # not everybody has one
484 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
485 die "Error while requiring CPAN::Config:\n$err_config";
492 # Suppress load messages until we load the config and know whether
493 # load messages are desired. Otherwise, it's unexpected and odd
494 # why one load message pops up even when verbosity is turned off.
495 # This means File::HomeDir load messages are never seen, but I
496 # think that's probably OK -- DAGOLDEN
498 # 5.6.2 seemed to segfault localizing a value in a hashref
499 # so do it manually instead
500 my $old_v = $CPAN::Config->{load_module_verbosity};
501 $CPAN::Config->{load_module_verbosity} = q[none];
502 if ($CPAN::META->has_usable("File::HomeDir")) {
503 $home = File::HomeDir->can('my_dot_config')
504 ? File::HomeDir->my_dot_config
505 : File::HomeDir->my_data;
506 unless (defined $home) {
507 $home = File::HomeDir->my_home
510 unless (defined $home) {
513 $CPAN::Config->{load_module_verbosity} = $old_v;
518 my($self, %args) = @_;
519 $CPAN::Be_Silent++ if $args{be_silent};
521 $doit = delete $args{doit};
524 require_myconfig_or_config;
525 my @miss = $self->missing_config_data;
526 return unless $doit || @miss;
530 require CPAN::FirstTime;
531 my($configpm,$fh,$redo);
533 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
534 $configpm = $INC{"CPAN/Config.pm"};
536 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
537 $configpm = $INC{"CPAN/MyConfig.pm"};
540 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
541 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
542 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
544 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
545 $configpm = _configpmtest($configpmdir,$configpmtest);
546 $inc_key = "CPAN/Config.pm";
549 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
550 File::Path::mkpath($configpmdir);
551 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
552 $configpm = _configpmtest($configpmdir,$configpmtest);
553 $inc_key = "CPAN/MyConfig.pm";
556 $INC{$inc_key} = $configpm;
558 my $text = qq{WARNING: CPAN.pm is unable to } .
559 qq{create a configuration file.};
560 output($text, 'confess');
565 if ($redo && !$doit) {
566 $CPAN::Frontend->myprint(<<END);
567 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
568 some missing parameters...
571 $args{args} = \@miss;
573 CPAN::FirstTime::init($configpm, %args);
579 # returns mandatory but missing entries in the Config
580 sub missing_config_data {
592 #"inhibit_startup_message",
599 "mbuild_install_arg",
600 ($^O eq "MSWin32" ? "" : "mbuild_install_build_command"),
604 "prerequisites_policy",
610 next unless exists $keys{$_};
611 push @miss, $_ unless defined $CPAN::Config->{$_};
617 $CPAN::Frontend->myprint(q[
619 commit commit session changes to disk
620 defaults reload default config values from disk
622 init enter a dialog to set all or a set of parameters
624 Edit key values as in the following (the "o" is a literal letter o):
625 o conf build_cache 15
626 o conf build_dir "/foo/bar"
628 o conf urllist unshift ftp://ftp.foo.bar/
629 o conf inhibit_startup_message 1
632 undef; #don't reprint CPAN::Config
636 my($word,$line,$pos) = @_;
638 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
639 my(@words) = split " ", substr($line,0,$pos+1);
648 @words == 4 && length($word)
651 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
652 } elsif (defined($words[2])
659 @words >= 4 && length($word)
661 return sort grep /^\Q$word\E/, keys %keys;
662 } elsif (@words >= 4) {
666 my(@o_conf) = sort grep { !$seen{$_}++ }
670 return grep /^\Q$word\E/, @o_conf;
674 my($self,$distro,$what) = @_;
676 if ($prefssupport{$what}) {
677 return $CPAN::Config->{$what} unless
680 and $distro->prefs->{cpanconfig}
681 and defined $distro->prefs->{cpanconfig}{$what};
682 return $distro->prefs->{cpanconfig}{$what};
684 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
685 "supported for distroprefs, doing a normal lookup");
686 return $CPAN::Config->{$what};
693 CPAN::Config; ####::###### #hide from indexer
694 # note: J. Nick Koston wrote me that they are using
695 # CPAN::Config->commit although undocumented. I suggested
696 # CPAN::Shell->o("conf","commit") even when ugly it is at least
699 # that's why I added the CPAN::Config class with autoload and
703 use vars qw($AUTOLOAD $VERSION);
706 # formerly CPAN::HandleConfig was known as CPAN::Config
708 my $class = shift; # e.g. in dh-make-perl: CPAN::Config
710 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
712 CPAN::HandleConfig->$l(@_);
722 This program is free software; you can redistribute it and/or
723 modify it under the same terms as Perl itself.
729 # cperl-indent-level: 4