1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $loading $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 2212 $,4)/1000000 + 5.4;
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 %keys = map { $_ => undef }
24 "build_requires_install_policy",
32 "commandnumber_in_prompt",
36 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
49 "inhibit_startup_message",
51 "load_module_verbosity",
56 "make_install_make_command",
60 "mbuild_install_build_command",
70 "prerequisites_policy",
76 "show_unparsable_versions",
94 my %prefssupport = map { $_ => 1 }
96 "build_requires_install_policy",
99 "make_install_make_command",
104 if ($^O eq "MSWin32") {
106 mbuild_install_build_command
107 make_install_make_command
110 if (exists $CPAN::Config->{$k}) {
111 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
112 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
114 delete $CPAN::Config->{$k};
119 # returns true on successful action
121 my($self,@args) = @_;
123 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
124 my($o,$str,$func,$args,$key_exists);
128 $self->$o(args => \@args); # o conf init => sub init => sub load
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");
292 $msg = <<EOF unless $configpm =~ /MyConfig/;
294 # This is CPAN.pm's systemwide configuration file. This file provides
295 # defaults for users, and the values can be changed in a per-user
296 # configuration file. The user-config file is being looked for as
297 # ~/.cpan/CPAN/MyConfig.pm.
301 my($fh) = FileHandle->new;
302 rename $configpm, "$configpm~" if -f $configpm;
303 open $fh, ">$configpm" or
304 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
305 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
306 foreach (sort keys %$CPAN::Config) {
307 unless (exists $keys{$_}) {
308 # do not drop them: forward compatibility!
309 $CPAN::Frontend->mywarn("Unknown config variable '$_'\n");
314 $self->neatvalue($CPAN::Config->{$_}),
319 $fh->print("};\n1;\n__END__\n");
322 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
323 #chmod $mode, $configpm;
324 ###why was that so? $self->defaults;
325 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
326 $CPAN::CONFIG_DIRTY = 0;
330 # stolen from MakeMaker; not taking the original because it is buggy;
331 # bugreport will have to say: keys of hashes remain unquoted and can
332 # produce syntax errors
335 return "undef" unless defined $v;
344 foreach my $elem (@$v) {
345 push @neat, "q[$elem]";
347 push @m, join ", ", @neat;
351 return "$v" unless $t eq 'HASH';
353 while (($key,$val) = each %$v) {
354 last unless defined $key; # cautious programming in case (undef,undef) is true
355 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
357 return "{ ".join(', ',@m)." }";
362 if ($CPAN::RUN_DEGRADED) {
363 $CPAN::Frontend->mydie(
364 "'o conf defaults' disabled in ".
365 "degraded mode. Maybe try\n".
366 " !undef \$CPAN::RUN_DEGRADED\n"
370 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
372 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
373 CPAN::Shell->_reload_this($config,{reloforce => 1});
374 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
378 $CPAN::CONFIG_DIRTY = 0;
382 =head2 C<< CLASS->safe_quote ITEM >>
384 Quotes an item to become safe against spaces
385 in shell interpolation. An item is enclosed
388 - the item contains spaces in the middle
389 - the item does not start with a quote
391 This happens to avoid shell interpolation
392 problems when whitespace is present in
395 This method uses C<commands_quote> to determine
396 the correct quote. If C<commands_quote> is
397 a space, no quoting will take place.
400 if it starts and ends with the same quote character: leave it as it is
402 if it contains no whitespace: leave it as it is
404 if it contains whitespace, then
406 if it contains quotes: better leave it as it is
408 else: quote it with the correct quote type for the box we're on
413 # Instead of patching the guess, set commands_quote
415 my ($quotes,$use_quote)
422 my ($self, $command) = @_;
423 # Set up quote/default quote
424 my $quote = $CPAN::Config->{commands_quote} || $quotes;
427 and defined($command )
429 and $command !~ /[$quote]/) {
430 return qq<$use_quote$command$use_quote>
437 my($self,@args) = @_;
438 CPAN->debug("self[$self]args[".join(",",@args)."]");
439 $self->load(doit => 1, @args);
443 # This is a piece of repeated code that is abstracted here for
444 # maintainability. RMB
447 my($configpmdir, $configpmtest) = @_;
448 if (-w $configpmtest) {
449 return $configpmtest;
450 } elsif (-w $configpmdir) {
451 #_#_# following code dumped core on me with 5.003_11, a.k.
452 my $configpm_bak = "$configpmtest.bak";
453 unlink $configpm_bak if -f $configpm_bak;
454 if( -f $configpmtest ) {
455 if( rename $configpmtest, $configpm_bak ) {
456 $CPAN::Frontend->mywarn(<<END);
457 Old configuration file $configpmtest
458 moved to $configpm_bak
462 my $fh = FileHandle->new;
463 if ($fh->open(">$configpmtest")) {
465 return $configpmtest;
467 # Should never happen
468 Carp::confess("Cannot open >$configpmtest");
473 sub require_myconfig_or_config () {
474 return if $INC{"CPAN/MyConfig.pm"};
477 unshift @INC, File::Spec->catdir($home,'.cpan');
478 eval { require CPAN::MyConfig };
479 my $err_myconfig = $@;
480 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
481 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
483 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
484 eval {require CPAN::Config;}; # not everybody has one
486 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
487 die "Error while requiring CPAN::Config:\n$err_config";
494 if ($CPAN::META->has_usable("File::HomeDir")) {
495 $home = File::HomeDir->my_data;
496 unless (defined $home) {
497 $home = File::HomeDir->my_home
500 unless (defined $home) {
507 my($self, %args) = @_;
508 $CPAN::Be_Silent++ if $args{be_silent};
510 $doit = delete $args{doit};
513 require_myconfig_or_config;
514 my @miss = $self->missing_config_data;
515 return unless $doit || @miss;
519 require CPAN::FirstTime;
520 my($configpm,$fh,$redo);
522 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
523 $configpm = $INC{"CPAN/Config.pm"};
525 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
526 $configpm = $INC{"CPAN/MyConfig.pm"};
529 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
530 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
531 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
533 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
534 $configpm = _configpmtest($configpmdir,$configpmtest);
535 $inc_key = "CPAN/Config.pm";
538 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
539 File::Path::mkpath($configpmdir);
540 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
541 $configpm = _configpmtest($configpmdir,$configpmtest);
542 $inc_key = "CPAN/MyConfig.pm";
545 $INC{$inc_key} = $configpm;
547 my $text = qq{WARNING: CPAN.pm is unable to } .
548 qq{create a configuration file.};
549 output($text, 'confess');
554 if ($redo && !$doit) {
555 $CPAN::Frontend->myprint(<<END);
556 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
557 some missing parameters...
560 $args{args} = \@miss;
562 CPAN::FirstTime::init($configpm, %args);
568 # returns mandatory but missing entries in the Config
569 sub missing_config_data {
581 #"inhibit_startup_message",
588 "mbuild_install_arg",
589 "mbuild_install_build_command",
593 "prerequisites_policy",
599 next unless exists $keys{$_};
600 push @miss, $_ unless defined $CPAN::Config->{$_};
606 $CPAN::Frontend->myprint(q[
608 commit commit session changes to disk
609 defaults reload default config values from disk
611 init enter a dialog to set all or a set of parameters
613 Edit key values as in the following (the "o" is a literal letter o):
614 o conf build_cache 15
615 o conf build_dir "/foo/bar"
617 o conf urllist unshift ftp://ftp.foo.bar/
618 o conf inhibit_startup_message 1
621 undef; #don't reprint CPAN::Config
625 my($word,$line,$pos) = @_;
627 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
628 my(@words) = split " ", substr($line,0,$pos+1);
637 @words == 4 && length($word)
640 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
641 } elsif (defined($words[2])
648 @words >= 4 && length($word)
650 return sort grep /^\Q$word\E/, keys %keys;
651 } elsif (@words >= 4) {
655 my(@o_conf) = sort grep { !$seen{$_}++ }
659 return grep /^\Q$word\E/, @o_conf;
663 my($self,$distro,$what) = @_;
665 if ($prefssupport{$what}) {
666 return $CPAN::Config->{$what} unless
669 and $distro->prefs->{cpanconfig}
670 and defined $distro->prefs->{cpanconfig}{$what};
671 return $distro->prefs->{cpanconfig}{$what};
673 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
674 "supported for distroprefs, doing a normal lookup");
675 return $CPAN::Config->{$what};
682 CPAN::Config; ####::###### #hide from indexer
683 # note: J. Nick Koston wrote me that they are using
684 # CPAN::Config->commit although undocumented. I suggested
685 # CPAN::Shell->o("conf","commit") even when ugly it is at least
688 # that's why I added the CPAN::Config class with autoload and
692 use vars qw($AUTOLOAD $VERSION);
693 $VERSION = sprintf "%.2f", substr(q$Rev: 2212 $,4)/100;
695 # formerly CPAN::HandleConfig was known as CPAN::Config
697 my $class = shift; # e.g. in dh-make-perl: CPAN::Config
699 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
701 CPAN::HandleConfig->$l(@_);
711 This program is free software; you can redistribute it and/or
712 modify it under the same terms as Perl itself.
718 # cperl-indent-level: 4