1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 1744 $,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",
55 "make_install_make_command",
59 "mbuild_install_build_command",
68 "prerequisites_policy",
89 my %prefssupport = map { $_ => 1 }
91 "build_requires_install_policy",
94 "make_install_make_command",
99 if ($^O eq "MSWin32") {
101 mbuild_install_build_command
102 make_install_make_command
105 if (exists $CPAN::Config->{$k}) {
106 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
107 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
109 delete $CPAN::Config->{$k};
114 # returns true on successful action
116 my($self,@args) = @_;
118 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
119 my($o,$str,$func,$args,$key_exists);
123 $self->$o(args => \@args); # o conf init => sub init => sub load
126 CPAN->debug("o[$o]") if $CPAN::DEBUG;
127 unless (exists $keys{$o}) {
128 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
133 # one day I used randomize_urllist for a boolean, so we must
134 # list them explicitly --ak
136 } elsif ($o =~ /^(wait_list|urllist|dontload_list)$/) {
144 CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG;
145 # Let's avoid eval, it's easier to comprehend without.
146 if ($func eq "push") {
147 push @{$CPAN::Config->{$o}}, @args;
149 } elsif ($func eq "pop") {
150 pop @{$CPAN::Config->{$o}};
152 } elsif ($func eq "shift") {
153 shift @{$CPAN::Config->{$o}};
155 } elsif ($func eq "unshift") {
156 unshift @{$CPAN::Config->{$o}}, @args;
158 } elsif ($func eq "splice") {
159 my $offset = shift @args || 0;
160 my $length = shift @args || 0;
161 splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn
164 $CPAN::Config->{$o} = [$func, @args];
167 $self->prettyprint($o);
170 if ($o eq "urllist") {
171 # reset the cached values
172 undef $CPAN::FTP::Thesite;
173 undef $CPAN::FTP::Themethod;
174 $CPAN::Index::LAST_TIME = 0;
175 } elsif ($o eq "dontload_list") {
176 # empty it, it will be built up again
177 $CPAN::META->{dontload_hash} = {};
180 } elsif ($o =~ /_hash$/) {
186 if (@args==1 && $args[0] eq ""){
188 } elsif (@args % 2) {
191 $CPAN::Config->{$o} = { @args };
199 if (defined $args[0]){
200 $CPAN::CONFIG_DIRTY = 1;
201 $CPAN::Config->{$o} = $args[0];
204 $self->prettyprint($o)
205 if exists $keys{$o} or defined $CPAN::Config->{$o};
208 if ($CPAN::Config->{auto_commit}) {
211 $CPAN::CONFIG_DIRTY = 1;
212 $CPAN::Frontend->myprint("Please use 'o conf commit' to ".
213 "make the config permanent!\n\n");
221 my $v = $CPAN::Config->{$k};
224 if (ref $v eq "ARRAY") {
225 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
227 @report = map { sprintf("\t%-18s => %s\n",
229 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
232 $CPAN::Frontend->myprint(
242 } elsif (defined $v) {
243 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
245 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
250 my($self,@args) = @_;
251 CPAN->debug("args[@args]") if $CPAN::DEBUG;
252 if ($CPAN::RUN_DEGRADED) {
253 $CPAN::Frontend->mydie(
254 "'o conf commit' disabled in ".
255 "degraded mode. Maybe try\n".
256 " !undef \$CPAN::RUN_DEGRADED\n"
261 if ($args[0] eq "args") {
262 # we have not signed that contract
264 $configpm = $args[0];
267 unless (defined $configpm){
268 $configpm ||= $INC{"CPAN/MyConfig.pm"};
269 $configpm ||= $INC{"CPAN/Config.pm"};
270 $configpm || Carp::confess(q{
271 CPAN::Config::commit called without an argument.
272 Please specify a filename where to save the configuration or try
273 "o conf init" to have an interactive course through configing.
278 $mode = (stat $configpm)[2];
279 if ($mode && ! -w _) {
280 Carp::confess("$configpm is not writable");
285 $msg = <<EOF unless $configpm =~ /MyConfig/;
287 # This is CPAN.pm's systemwide configuration file. This file provides
288 # defaults for users, and the values can be changed in a per-user
289 # configuration file. The user-config file is being looked for as
290 # ~/.cpan/CPAN/MyConfig.pm.
294 my($fh) = FileHandle->new;
295 rename $configpm, "$configpm~" if -f $configpm;
296 open $fh, ">$configpm" or
297 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
298 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
299 foreach (sort keys %$CPAN::Config) {
300 unless (exists $keys{$_}) {
301 # do not drop them: forward compatibility!
302 $CPAN::Frontend->mywarn("Unknown config variable '$_'\n");
307 $self->neatvalue($CPAN::Config->{$_}),
312 $fh->print("};\n1;\n__END__\n");
315 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
316 #chmod $mode, $configpm;
317 ###why was that so? $self->defaults;
318 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
319 $CPAN::CONFIG_DIRTY = 0;
323 # stolen from MakeMaker; not taking the original because it is buggy;
324 # bugreport will have to say: keys of hashes remain unquoted and can
325 # produce syntax errors
328 return "undef" unless defined $v;
337 foreach my $elem (@$v) {
338 push @neat, "q[$elem]";
340 push @m, join ", ", @neat;
344 return "$v" unless $t eq 'HASH';
346 while (($key,$val) = each %$v){
347 last unless defined $key; # cautious programming in case (undef,undef) is true
348 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
350 return "{ ".join(', ',@m)." }";
355 if ($CPAN::RUN_DEGRADED) {
356 $CPAN::Frontend->mydie(
357 "'o conf defaults' disabled in ".
358 "degraded mode. Maybe try\n".
359 " !undef \$CPAN::RUN_DEGRADED\n"
363 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
365 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
366 CPAN::Shell->_reload_this($config,{reloforce => 1});
367 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
371 $CPAN::CONFIG_DIRTY = 0;
375 =head2 C<< CLASS->safe_quote ITEM >>
377 Quotes an item to become safe against spaces
378 in shell interpolation. An item is enclosed
381 - the item contains spaces in the middle
382 - the item does not start with a quote
384 This happens to avoid shell interpolation
385 problems when whitespace is present in
388 This method uses C<commands_quote> to determine
389 the correct quote. If C<commands_quote> is
390 a space, no quoting will take place.
393 if it starts and ends with the same quote character: leave it as it is
395 if it contains no whitespace: leave it as it is
397 if it contains whitespace, then
399 if it contains quotes: better leave it as it is
401 else: quote it with the correct quote type for the box we're on
406 # Instead of patching the guess, set commands_quote
408 my ($quotes,$use_quote)
415 my ($self, $command) = @_;
416 # Set up quote/default quote
417 my $quote = $CPAN::Config->{commands_quote} || $quotes;
420 and defined($command )
422 and $command !~ /[$quote]/) {
423 return qq<$use_quote$command$use_quote>
430 my($self,@args) = @_;
431 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
440 # This is a piece of repeated code that is abstracted here for
441 # maintainability. RMB
444 my($configpmdir, $configpmtest) = @_;
445 if (-w $configpmtest) {
446 return $configpmtest;
447 } elsif (-w $configpmdir) {
448 #_#_# following code dumped core on me with 5.003_11, a.k.
449 my $configpm_bak = "$configpmtest.bak";
450 unlink $configpm_bak if -f $configpm_bak;
451 if( -f $configpmtest ) {
452 if( rename $configpmtest, $configpm_bak ) {
453 $CPAN::Frontend->mywarn(<<END);
454 Old configuration file $configpmtest
455 moved to $configpm_bak
459 my $fh = FileHandle->new;
460 if ($fh->open(">$configpmtest")) {
462 return $configpmtest;
464 # Should never happen
465 Carp::confess("Cannot open >$configpmtest");
470 sub require_myconfig_or_config () {
471 return if $INC{"CPAN/MyConfig.pm"};
474 unshift @INC, File::Spec->catdir($home,'.cpan');
475 eval { require CPAN::MyConfig };
476 my $err_myconfig = $@;
477 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
478 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
480 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
481 eval {require CPAN::Config;}; # not everybody has one
483 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
484 die "Error while requiring CPAN::Config:\n$err_config";
491 if ($CPAN::META->has_usable("File::HomeDir")) {
492 $home = File::HomeDir->my_data;
500 my($self, %args) = @_;
501 $CPAN::Be_Silent++ if $args{be_silent};
505 require_myconfig_or_config;
506 return unless @miss = $self->missing_config_data;
508 require CPAN::FirstTime;
509 my($configpm,$fh,$redo,$theycalled);
511 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
512 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
513 $configpm = $INC{"CPAN/Config.pm"};
515 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
516 $configpm = $INC{"CPAN/MyConfig.pm"};
519 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
520 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
521 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
523 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
524 $configpm = _configpmtest($configpmdir,$configpmtest);
525 $inc_key = "CPAN/Config.pm";
528 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
529 File::Path::mkpath($configpmdir);
530 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
531 $configpm = _configpmtest($configpmdir,$configpmtest);
532 $inc_key = "CPAN/MyConfig.pm";
535 $INC{$inc_key} = $configpm;
537 my $text = qq{WARNING: CPAN.pm is unable to } .
538 qq{create a configuration file.};
539 output($text, 'confess');
544 if ($redo && ! $theycalled){
545 $CPAN::Frontend->myprint(<<END);
546 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
547 the following indispensable but missing parameters:
551 $args{args} = \@miss;
554 # where do we need this?
555 $CPAN::Frontend->myprint(qq{
556 $configpm initialized.
559 CPAN::FirstTime::init($configpm, %args);
563 # returns mandatory but missing entries in the Config
564 sub missing_config_data {
576 "inhibit_startup_message",
583 "mbuild_install_arg",
584 "mbuild_install_build_command",
588 "prerequisites_policy",
594 next unless exists $keys{$_};
595 push @miss, $_ unless defined $CPAN::Config->{$_};
601 $CPAN::Frontend->myprint(q[
603 commit commit session changes to disk
604 defaults reload default config values from disk
606 init enter a dialog to set all or a set of parameters
608 Edit key values as in the following (the "o" is a literal letter o):
609 o conf build_cache 15
610 o conf build_dir "/foo/bar"
612 o conf urllist unshift ftp://ftp.foo.bar/
613 o conf inhibit_startup_message 1
616 undef; #don't reprint CPAN::Config
620 my($word,$line,$pos) = @_;
622 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
623 my(@words) = split " ", substr($line,0,$pos+1);
632 @words == 4 && length($word)
635 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
636 } elsif (defined($words[2])
643 @words >= 4 && length($word)
645 return sort grep /^\Q$word\E/, keys %keys;
646 } elsif (@words >= 4) {
650 my(@o_conf) = sort grep { !$seen{$_}++ }
654 return grep /^\Q$word\E/, @o_conf;
658 my($self,$distro,$what) = @_;
660 if ($prefssupport{$what}) {
661 return $CPAN::Config->{$what} unless
664 and $distro->prefs->{cpanconfig}
665 and defined $distro->prefs->{cpanconfig}{$what};
666 return $distro->prefs->{cpanconfig}{$what};
668 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
669 "supported for distroprefs, doing a normal lookup");
670 return $CPAN::Config->{$what};
677 CPAN::Config; ####::###### #hide from indexer
678 # note: J. Nick Koston wrote me that they are using
679 # CPAN::Config->commit although undocumented. I suggested
680 # CPAN::Shell->o("conf","commit") even when ugly it is at least
683 # that's why I added the CPAN::Config class with autoload and
687 use vars qw($AUTOLOAD $VERSION);
688 $VERSION = sprintf "%.2f", substr(q$Rev: 1744 $,4)/100;
690 # formerly CPAN::HandleConfig was known as CPAN::Config
693 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
695 CPAN::HandleConfig->$l(@_);
705 This program is free software; you can redistribute it and/or
706 modify it under the same terms as Perl itself.
712 # cperl-indent-level: 4