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",
83 "prerequisites_policy",
89 "show_unparsable_versions",
97 "trust_test_report_history",
108 my %prefssupport = map { $_ => 1 }
110 "build_requires_install_policy",
113 "make_install_make_command",
118 # returns true on successful action
120 my($self,@args) = @_;
122 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
123 my($o,$str,$func,$args,$key_exists);
126 my $success = $self->$o(args => \@args); # o conf init => sub init => sub load
128 die "Panic: could not configure CPAN.pm for args [@args]. Giving up.";
131 CPAN->debug("o[$o]") if $CPAN::DEBUG;
132 unless (exists $keys{$o}) {
133 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
138 # one day I used randomize_urllist for a boolean, so we must
139 # list them explicitly --ak
141 } elsif ($o =~ /^(wait_list|urllist|dontload_list)$/) {
149 CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG;
150 # Let's avoid eval, it's easier to comprehend without.
151 if ($func eq "push") {
152 push @{$CPAN::Config->{$o}}, @args;
154 } elsif ($func eq "pop") {
155 pop @{$CPAN::Config->{$o}};
157 } elsif ($func eq "shift") {
158 shift @{$CPAN::Config->{$o}};
160 } elsif ($func eq "unshift") {
161 unshift @{$CPAN::Config->{$o}}, @args;
163 } elsif ($func eq "splice") {
164 my $offset = shift @args || 0;
165 my $length = shift @args || 0;
166 splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn
169 $CPAN::Config->{$o} = [$func, @args];
172 $self->prettyprint($o);
175 if ($o eq "urllist") {
176 # reset the cached values
177 undef $CPAN::FTP::Thesite;
178 undef $CPAN::FTP::Themethod;
179 $CPAN::Index::LAST_TIME = 0;
180 } elsif ($o eq "dontload_list") {
181 # empty it, it will be built up again
182 $CPAN::META->{dontload_hash} = {};
185 } elsif ($o =~ /_hash$/) {
191 if (@args==1 && $args[0] eq "") {
193 } elsif (@args % 2) {
196 $CPAN::Config->{$o} = { @args };
204 if (defined $args[0]) {
205 $CPAN::CONFIG_DIRTY = 1;
206 $CPAN::Config->{$o} = $args[0];
209 $self->prettyprint($o)
210 if exists $keys{$o} or defined $CPAN::Config->{$o};
213 if ($CPAN::Config->{auto_commit}) {
216 $CPAN::CONFIG_DIRTY = 1;
217 $CPAN::Frontend->myprint("Please use 'o conf commit' to ".
218 "make the config permanent!\n\n");
226 my $v = $CPAN::Config->{$k};
229 if (ref $v eq "ARRAY") {
230 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
234 sprintf "\t%-18s => %s\n",
236 defined $v->{$_} ? "[$v->{$_}]" : "undef"
239 $CPAN::Frontend->myprint(
249 } elsif (defined $v) {
250 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
252 $CPAN::Frontend->myprint(sprintf " %-18s undef\n", $k);
257 my($self,@args) = @_;
258 CPAN->debug("args[@args]") if $CPAN::DEBUG;
259 if ($CPAN::RUN_DEGRADED) {
260 $CPAN::Frontend->mydie(
261 "'o conf commit' disabled in ".
262 "degraded mode. Maybe try\n".
263 " !undef \$CPAN::RUN_DEGRADED\n"
268 if ($args[0] eq "args") {
269 # we have not signed that contract
271 $configpm = $args[0];
274 unless (defined $configpm) {
275 $configpm ||= $INC{"CPAN/MyConfig.pm"};
276 $configpm ||= $INC{"CPAN/Config.pm"};
277 $configpm || Carp::confess(q{
278 CPAN::Config::commit called without an argument.
279 Please specify a filename where to save the configuration or try
280 "o conf init" to have an interactive course through configing.
285 $mode = (stat $configpm)[2];
286 if ($mode && ! -w _) {
287 Carp::confess("$configpm is not writable");
293 $msg = <<EOF unless $configpm =~ /MyConfig/;
295 # This is CPAN.pm's systemwide configuration file. This file provides
296 # defaults for users, and the values can be changed in a per-user
297 # configuration file. The user-config file is being looked for as
298 # $home/.cpan/CPAN/MyConfig.pm.
302 my($fh) = FileHandle->new;
303 rename $configpm, "$configpm~" if -f $configpm;
304 open $fh, ">$configpm" or
305 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
306 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
307 foreach (sort keys %$CPAN::Config) {
308 unless (exists $keys{$_}) {
309 # do not drop them: forward compatibility!
310 $CPAN::Frontend->mywarn("Unknown config variable '$_'\n");
315 $self->neatvalue($CPAN::Config->{$_}),
320 $fh->print("};\n1;\n__END__\n");
323 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
324 #chmod $mode, $configpm;
325 ###why was that so? $self->defaults;
326 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
327 $CPAN::CONFIG_DIRTY = 0;
331 # stolen from MakeMaker; not taking the original because it is buggy;
332 # bugreport will have to say: keys of hashes remain unquoted and can
333 # produce syntax errors
336 return "undef" unless defined $v;
345 foreach my $elem (@$v) {
346 push @neat, "q[$elem]";
348 push @m, join ", ", @neat;
352 return "$v" unless $t eq 'HASH';
354 while (($key,$val) = each %$v) {
355 last unless defined $key; # cautious programming in case (undef,undef) is true
356 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
358 return "{ ".join(', ',@m)." }";
363 if ($CPAN::RUN_DEGRADED) {
364 $CPAN::Frontend->mydie(
365 "'o conf defaults' disabled in ".
366 "degraded mode. Maybe try\n".
367 " !undef \$CPAN::RUN_DEGRADED\n"
371 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
373 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
374 CPAN::Shell->_reload_this($config,{reloforce => 1});
375 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
379 $CPAN::CONFIG_DIRTY = 0;
383 =head2 C<< CLASS->safe_quote ITEM >>
385 Quotes an item to become safe against spaces
386 in shell interpolation. An item is enclosed
389 - the item contains spaces in the middle
390 - the item does not start with a quote
392 This happens to avoid shell interpolation
393 problems when whitespace is present in
396 This method uses C<commands_quote> to determine
397 the correct quote. If C<commands_quote> is
398 a space, no quoting will take place.
401 if it starts and ends with the same quote character: leave it as it is
403 if it contains no whitespace: leave it as it is
405 if it contains whitespace, then
407 if it contains quotes: better leave it as it is
409 else: quote it with the correct quote type for the box we're on
414 # Instead of patching the guess, set commands_quote
416 my ($quotes,$use_quote)
423 my ($self, $command) = @_;
424 # Set up quote/default quote
425 my $quote = $CPAN::Config->{commands_quote} || $quotes;
428 and defined($command )
430 and $command !~ /[$quote]/) {
431 return qq<$use_quote$command$use_quote>
438 my($self,@args) = @_;
439 CPAN->debug("self[$self]args[".join(",",@args)."]");
440 $self->load(doit => 1, @args);
444 # This is a piece of repeated code that is abstracted here for
445 # maintainability. RMB
448 my($configpmdir, $configpmtest) = @_;
449 if (-w $configpmtest) {
450 return $configpmtest;
451 } elsif (-w $configpmdir) {
452 #_#_# following code dumped core on me with 5.003_11, a.k.
453 my $configpm_bak = "$configpmtest.bak";
454 unlink $configpm_bak if -f $configpm_bak;
455 if( -f $configpmtest ) {
456 if( rename $configpmtest, $configpm_bak ) {
457 $CPAN::Frontend->mywarn(<<END);
458 Old configuration file $configpmtest
459 moved to $configpm_bak
463 my $fh = FileHandle->new;
464 if ($fh->open(">$configpmtest")) {
466 return $configpmtest;
468 # Should never happen
469 Carp::confess("Cannot open >$configpmtest");
474 sub require_myconfig_or_config () {
475 return if $INC{"CPAN/MyConfig.pm"};
478 unshift @INC, File::Spec->catdir($home,'.cpan');
479 eval { require CPAN::MyConfig };
480 my $err_myconfig = $@;
481 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
482 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
484 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
485 eval {require CPAN::Config;}; # not everybody has one
487 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
488 die "Error while requiring CPAN::Config:\n$err_config";
495 # Suppress load messages until we load the config and know whether
496 # load messages are desired. Otherwise, it's unexpected and odd
497 # why one load message pops up even when verbosity is turned off.
498 # This means File::HomeDir load messages are never seen, but I
499 # think that's probably OK -- DAGOLDEN
501 # 5.6.2 seemed to segfault localizing a value in a hashref
502 # so do it manually instead
503 my $old_v = $CPAN::Config->{load_module_verbosity};
504 $CPAN::Config->{load_module_verbosity} = q[none];
505 if ($CPAN::META->has_usable("File::HomeDir")) {
506 $home = File::HomeDir->can('my_dot_config')
507 ? File::HomeDir->my_dot_config
508 : File::HomeDir->my_data;
509 unless (defined $home) {
510 $home = File::HomeDir->my_home
513 unless (defined $home) {
516 $CPAN::Config->{load_module_verbosity} = $old_v;
521 my($self, %args) = @_;
522 $CPAN::Be_Silent++ if $args{be_silent};
524 $doit = delete $args{doit};
527 require_myconfig_or_config;
528 my @miss = $self->missing_config_data;
529 CPAN->debug("doit[$doit]loading[$loading]miss[@miss]") if $CPAN::DEBUG;
530 return unless $doit || @miss;
534 require CPAN::FirstTime;
535 my($configpm,$fh,$redo);
537 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
538 $configpm = $INC{"CPAN/Config.pm"};
540 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
541 $configpm = $INC{"CPAN/MyConfig.pm"};
544 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
545 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
546 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
548 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
549 $configpm = _configpmtest($configpmdir,$configpmtest);
550 $inc_key = "CPAN/Config.pm";
553 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
554 File::Path::mkpath($configpmdir);
555 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
556 $configpm = _configpmtest($configpmdir,$configpmtest);
557 $inc_key = "CPAN/MyConfig.pm";
560 $INC{$inc_key} = $configpm;
562 my $text = qq{WARNING: CPAN.pm is unable to } .
563 qq{create a configuration file.};
564 output($text, 'confess');
569 if ($redo && !$doit) {
570 $CPAN::Frontend->myprint(<<END);
571 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
572 some missing parameters...
575 $args{args} = \@miss;
577 my $initialized = CPAN::FirstTime::init($configpm, %args);
583 # returns mandatory but missing entries in the Config
584 sub missing_config_data {
596 #"inhibit_startup_message",
603 "mbuild_install_arg",
604 ($^O eq "MSWin32" ? "" : "mbuild_install_build_command"),
608 "prerequisites_policy",
614 next unless exists $keys{$_};
615 push @miss, $_ unless defined $CPAN::Config->{$_};
621 $CPAN::Frontend->myprint(q[
623 commit commit session changes to disk
624 defaults reload default config values from disk
626 init enter a dialog to set all or a set of parameters
628 Edit key values as in the following (the "o" is a literal letter o):
629 o conf build_cache 15
630 o conf build_dir "/foo/bar"
632 o conf urllist unshift ftp://ftp.foo.bar/
633 o conf inhibit_startup_message 1
636 undef; #don't reprint CPAN::Config
640 my($word,$line,$pos) = @_;
642 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
643 my(@words) = split " ", substr($line,0,$pos+1);
652 @words == 4 && length($word)
655 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
656 } elsif (defined($words[2])
663 @words >= 4 && length($word)
665 return sort grep /^\Q$word\E/, keys %keys;
666 } elsif (@words >= 4) {
670 my(@o_conf) = sort grep { !$seen{$_}++ }
674 return grep /^\Q$word\E/, @o_conf;
678 my($self,$distro,$what) = @_;
680 if ($prefssupport{$what}) {
681 return $CPAN::Config->{$what} unless
684 and $distro->prefs->{cpanconfig}
685 and defined $distro->prefs->{cpanconfig}{$what};
686 return $distro->prefs->{cpanconfig}{$what};
688 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
689 "supported for distroprefs, doing a normal lookup");
690 return $CPAN::Config->{$what};
697 CPAN::Config; ####::###### #hide from indexer
698 # note: J. Nick Koston wrote me that they are using
699 # CPAN::Config->commit although undocumented. I suggested
700 # CPAN::Shell->o("conf","commit") even when ugly it is at least
703 # that's why I added the CPAN::Config class with autoload and
707 use vars qw($AUTOLOAD $VERSION);
710 # formerly CPAN::HandleConfig was known as CPAN::Config
711 sub AUTOLOAD { ## no critic
712 my $class = shift; # e.g. in dh-make-perl: CPAN::Config
714 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
716 CPAN::HandleConfig->$l(@_);
726 This program is free software; you can redistribute it and/or
727 modify it under the same terms as Perl itself.
733 # cperl-indent-level: 4