1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $loading $VERSION);
5 $VERSION = "5.5001"; # see also CPAN::Config::VERSION at end of file
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",
109 my %prefssupport = map { $_ => 1 }
111 "build_requires_install_policy",
114 "make_install_make_command",
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);
127 my $success = $self->$o(args => \@args); # o conf init => sub init => sub load
129 die "Panic: could not configure CPAN.pm for args [@args]. Giving up.";
132 CPAN->debug("o[$o]") if $CPAN::DEBUG;
133 unless (exists $keys{$o}) {
134 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
139 # one day I used randomize_urllist for a boolean, so we must
140 # list them explicitly --ak
142 } elsif ($o =~ /^(wait_list|urllist|dontload_list)$/) {
150 CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG;
151 # Let's avoid eval, it's easier to comprehend without.
152 if ($func eq "push") {
153 push @{$CPAN::Config->{$o}}, @args;
155 } elsif ($func eq "pop") {
156 pop @{$CPAN::Config->{$o}};
158 } elsif ($func eq "shift") {
159 shift @{$CPAN::Config->{$o}};
161 } elsif ($func eq "unshift") {
162 unshift @{$CPAN::Config->{$o}}, @args;
164 } elsif ($func eq "splice") {
165 my $offset = shift @args || 0;
166 my $length = shift @args || 0;
167 splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn
170 $CPAN::Config->{$o} = [$func, @args];
173 $self->prettyprint($o);
176 if ($o eq "urllist") {
177 # reset the cached values
178 undef $CPAN::FTP::Thesite;
179 undef $CPAN::FTP::Themethod;
180 $CPAN::Index::LAST_TIME = 0;
181 } elsif ($o eq "dontload_list") {
182 # empty it, it will be built up again
183 $CPAN::META->{dontload_hash} = {};
186 } elsif ($o =~ /_hash$/) {
192 if (@args==1 && $args[0] eq "") {
194 } elsif (@args % 2) {
197 $CPAN::Config->{$o} = { @args };
205 if (defined $args[0]) {
206 $CPAN::CONFIG_DIRTY = 1;
207 $CPAN::Config->{$o} = $args[0];
210 $self->prettyprint($o)
211 if exists $keys{$o} or defined $CPAN::Config->{$o};
214 if ($CPAN::Config->{auto_commit}) {
217 $CPAN::CONFIG_DIRTY = 1;
218 $CPAN::Frontend->myprint("Please use 'o conf commit' to ".
219 "make the config permanent!\n\n");
227 my $v = $CPAN::Config->{$k};
230 if (ref $v eq "ARRAY") {
231 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
235 sprintf "\t%-18s => %s\n",
237 defined $v->{$_} ? "[$v->{$_}]" : "undef"
240 $CPAN::Frontend->myprint(
250 } elsif (defined $v) {
251 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
253 $CPAN::Frontend->myprint(sprintf " %-18s undef\n", $k);
258 my($self,@args) = @_;
259 CPAN->debug("args[@args]") if $CPAN::DEBUG;
260 if ($CPAN::RUN_DEGRADED) {
261 $CPAN::Frontend->mydie(
262 "'o conf commit' disabled in ".
263 "degraded mode. Maybe try\n".
264 " !undef \$CPAN::RUN_DEGRADED\n"
269 if ($args[0] eq "args") {
270 # we have not signed that contract
272 $configpm = $args[0];
275 unless (defined $configpm) {
276 $configpm ||= $INC{"CPAN/MyConfig.pm"};
277 $configpm ||= $INC{"CPAN/Config.pm"};
278 $configpm || Carp::confess(q{
279 CPAN::Config::commit called without an argument.
280 Please specify a filename where to save the configuration or try
281 "o conf init" to have an interactive course through configing.
286 $mode = (stat $configpm)[2];
287 if ($mode && ! -w _) {
288 Carp::confess("$configpm is not writable");
294 $msg = <<EOF unless $configpm =~ /MyConfig/;
296 # This is CPAN.pm's systemwide configuration file. This file provides
297 # defaults for users, and the values can be changed in a per-user
298 # configuration file. The user-config file is being looked for as
299 # $home/.cpan/CPAN/MyConfig.pm.
303 my($fh) = FileHandle->new;
304 rename $configpm, "$configpm~" if -f $configpm;
305 open $fh, ">$configpm" or
306 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
307 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
308 foreach (sort keys %$CPAN::Config) {
309 unless (exists $keys{$_}) {
310 # do not drop them: forward compatibility!
311 $CPAN::Frontend->mywarn("Unknown config variable '$_'\n");
316 $self->neatvalue($CPAN::Config->{$_}),
321 $fh->print("};\n1;\n__END__\n");
324 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
325 #chmod $mode, $configpm;
326 ###why was that so? $self->defaults;
327 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
328 $CPAN::CONFIG_DIRTY = 0;
332 # stolen from MakeMaker; not taking the original because it is buggy;
333 # bugreport will have to say: keys of hashes remain unquoted and can
334 # produce syntax errors
337 return "undef" unless defined $v;
346 foreach my $elem (@$v) {
347 push @neat, "q[$elem]";
349 push @m, join ", ", @neat;
353 return "$v" unless $t eq 'HASH';
355 while (($key,$val) = each %$v) {
356 last unless defined $key; # cautious programming in case (undef,undef) is true
357 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
359 return "{ ".join(', ',@m)." }";
364 if ($CPAN::RUN_DEGRADED) {
365 $CPAN::Frontend->mydie(
366 "'o conf defaults' disabled in ".
367 "degraded mode. Maybe try\n".
368 " !undef \$CPAN::RUN_DEGRADED\n"
372 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
374 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
375 CPAN::Shell->_reload_this($config,{reloforce => 1});
376 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
380 $CPAN::CONFIG_DIRTY = 0;
384 =head2 C<< CLASS->safe_quote ITEM >>
386 Quotes an item to become safe against spaces
387 in shell interpolation. An item is enclosed
390 - the item contains spaces in the middle
391 - the item does not start with a quote
393 This happens to avoid shell interpolation
394 problems when whitespace is present in
397 This method uses C<commands_quote> to determine
398 the correct quote. If C<commands_quote> is
399 a space, no quoting will take place.
402 if it starts and ends with the same quote character: leave it as it is
404 if it contains no whitespace: leave it as it is
406 if it contains whitespace, then
408 if it contains quotes: better leave it as it is
410 else: quote it with the correct quote type for the box we're on
415 # Instead of patching the guess, set commands_quote
417 my ($quotes,$use_quote)
424 my ($self, $command) = @_;
425 # Set up quote/default quote
426 my $quote = $CPAN::Config->{commands_quote} || $quotes;
429 and defined($command )
431 and $command !~ /[$quote]/) {
432 return qq<$use_quote$command$use_quote>
439 my($self,@args) = @_;
440 CPAN->debug("self[$self]args[".join(",",@args)."]");
441 $self->load(doit => 1, @args);
445 # This is a piece of repeated code that is abstracted here for
446 # maintainability. RMB
449 my($configpmdir, $configpmtest) = @_;
450 if (-w $configpmtest) {
451 return $configpmtest;
452 } elsif (-w $configpmdir) {
453 #_#_# following code dumped core on me with 5.003_11, a.k.
454 my $configpm_bak = "$configpmtest.bak";
455 unlink $configpm_bak if -f $configpm_bak;
456 if( -f $configpmtest ) {
457 if( rename $configpmtest, $configpm_bak ) {
458 $CPAN::Frontend->mywarn(<<END);
459 Old configuration file $configpmtest
460 moved to $configpm_bak
464 my $fh = FileHandle->new;
465 if ($fh->open(">$configpmtest")) {
467 return $configpmtest;
469 # Should never happen
470 Carp::confess("Cannot open >$configpmtest");
475 sub require_myconfig_or_config () {
476 return if $INC{"CPAN/MyConfig.pm"};
479 unshift @INC, File::Spec->catdir($home,'.cpan');
480 eval { require CPAN::MyConfig };
481 my $err_myconfig = $@;
482 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
483 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
485 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
486 eval {require CPAN::Config;}; # not everybody has one
488 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
489 die "Error while requiring CPAN::Config:\n$err_config";
496 # Suppress load messages until we load the config and know whether
497 # load messages are desired. Otherwise, it's unexpected and odd
498 # why one load message pops up even when verbosity is turned off.
499 # This means File::HomeDir load messages are never seen, but I
500 # think that's probably OK -- DAGOLDEN
502 # 5.6.2 seemed to segfault localizing a value in a hashref
503 # so do it manually instead
504 my $old_v = $CPAN::Config->{load_module_verbosity};
505 $CPAN::Config->{load_module_verbosity} = q[none];
506 if ($CPAN::META->has_usable("File::HomeDir")) {
507 if ($^O eq 'darwin') {
508 $home = File::HomeDir->my_home; # my_data is ~/Library/Application Support on darwin,
509 # which causes issues in the toolchain.
512 $home = File::HomeDir->my_data || File::HomeDir->my_home;
515 unless (defined $home) {
518 $CPAN::Config->{load_module_verbosity} = $old_v;
523 my($self, %args) = @_;
524 $CPAN::Be_Silent++ if $args{be_silent};
526 $doit = delete $args{doit} || 0;
527 $loading = 0 unless defined $loading;
530 require_myconfig_or_config;
531 my @miss = $self->missing_config_data;
532 CPAN->debug("doit[$doit]loading[$loading]miss[@miss]") if $CPAN::DEBUG;
533 return unless $doit || @miss;
535 local $loading = ($loading||0) + 1;
537 require CPAN::FirstTime;
538 my($redo,$configpm,$fh);
539 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
540 $configpm = $INC{"CPAN/Config.pm"};
542 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
543 $configpm = $INC{"CPAN/MyConfig.pm"};
546 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
547 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
548 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
550 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
551 $configpm = _configpmtest($configpmdir,$configpmtest);
552 $inc_key = "CPAN/Config.pm";
555 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
556 File::Path::mkpath($configpmdir);
557 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
558 $configpm = _configpmtest($configpmdir,$configpmtest);
559 $inc_key = "CPAN/MyConfig.pm";
562 $INC{$inc_key} = $configpm;
564 my $myconfigpm = File::Spec->catfile(home,".cpan","CPAN","MyConfig.pm");
565 $CPAN::Frontend->mydie(<<"END");
566 WARNING: CPAN.pm is unable to write a configuration file. You need write
567 access to your default perl library directories or you must be able to
568 create and write to '$myconfigpm'.
570 Aborting configuration.
576 if ($redo && !$doit) {
577 $CPAN::Frontend->myprint(<<END);
578 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
579 some missing parameters... Will write to
583 $args{args} = \@miss;
585 my $initialized = CPAN::FirstTime::init($configpm, %args);
590 # returns mandatory but missing entries in the Config
591 sub missing_config_data {
603 #"inhibit_startup_message",
610 "mbuild_install_arg",
611 ($^O eq "MSWin32" ? "" : "mbuild_install_build_command"),
615 "prerequisites_policy",
621 next unless exists $keys{$_};
622 push @miss, $_ unless defined $CPAN::Config->{$_};
628 $CPAN::Frontend->myprint(q[
630 commit commit session changes to disk
631 defaults reload default config values from disk
633 init enter a dialog to set all or a set of parameters
635 Edit key values as in the following (the "o" is a literal letter o):
636 o conf build_cache 15
637 o conf build_dir "/foo/bar"
639 o conf urllist unshift ftp://ftp.foo.bar/
640 o conf inhibit_startup_message 1
643 1; #don't reprint CPAN::Config
647 my($word,$line,$pos) = @_;
649 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
650 my(@words) = split " ", substr($line,0,$pos+1);
659 @words == 4 && length($word)
662 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
663 } elsif (defined($words[2])
670 @words >= 4 && length($word)
672 return sort grep /^\Q$word\E/, keys %keys;
673 } elsif (@words >= 4) {
677 my(@o_conf) = sort grep { !$seen{$_}++ }
681 return grep /^\Q$word\E/, @o_conf;
685 my($self,$distro,$what) = @_;
687 if ($prefssupport{$what}) {
688 return $CPAN::Config->{$what} unless
691 and $distro->prefs->{cpanconfig}
692 and defined $distro->prefs->{cpanconfig}{$what};
693 return $distro->prefs->{cpanconfig}{$what};
695 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
696 "supported for distroprefs, doing a normal lookup");
697 return $CPAN::Config->{$what};
704 CPAN::Config; ####::###### #hide from indexer
705 # note: J. Nick Koston wrote me that they are using
706 # CPAN::Config->commit although undocumented. I suggested
707 # CPAN::Shell->o("conf","commit") even when ugly it is at least
710 # that's why I added the CPAN::Config class with autoload and
714 use vars qw($AUTOLOAD $VERSION);
717 # formerly CPAN::HandleConfig was known as CPAN::Config
718 sub AUTOLOAD { ## no critic
719 my $class = shift; # e.g. in dh-make-perl: CPAN::Config
721 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
723 CPAN::HandleConfig->$l(@_);
733 This program is free software; you can redistribute it and/or
734 modify it under the same terms as Perl itself.
740 # cperl-indent-level: 4