1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 1467 $,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 } elsif ($o eq "dontload_list") {
175 # empty it, it will be built up again
176 $CPAN::META->{dontload_hash} = {};
179 } elsif ($o =~ /_hash$/) {
185 if (@args==1 && $args[0] eq ""){
187 } elsif (@args % 2) {
190 $CPAN::Config->{$o} = { @args };
198 if (defined $args[0]){
199 $CPAN::CONFIG_DIRTY = 1;
200 $CPAN::Config->{$o} = $args[0];
203 $self->prettyprint($o)
204 if exists $keys{$o} or defined $CPAN::Config->{$o};
207 if ($CPAN::Config->{auto_commit}) {
210 $CPAN::CONFIG_DIRTY = 1;
211 $CPAN::Frontend->myprint("Please use 'o conf commit' to ".
212 "make the config permanent!\n\n");
220 my $v = $CPAN::Config->{$k};
223 if (ref $v eq "ARRAY") {
224 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
226 @report = map { sprintf("\t%-18s => %s\n",
228 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
231 $CPAN::Frontend->myprint(
241 } elsif (defined $v) {
242 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
244 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
249 my($self,@args) = @_;
250 CPAN->debug("args[@args]") if $CPAN::DEBUG;
251 if ($CPAN::RUN_DEGRADED) {
252 $CPAN::Frontend->mydie(
253 "'o conf commit' disabled in ".
254 "degraded mode. Maybe try\n".
255 " !undef \$CPAN::RUN_DEGRADED\n"
260 if ($args[0] eq "args") {
261 # we have not signed that contract
263 $configpm = $args[0];
266 unless (defined $configpm){
267 $configpm ||= $INC{"CPAN/MyConfig.pm"};
268 $configpm ||= $INC{"CPAN/Config.pm"};
269 $configpm || Carp::confess(q{
270 CPAN::Config::commit called without an argument.
271 Please specify a filename where to save the configuration or try
272 "o conf init" to have an interactive course through configing.
277 $mode = (stat $configpm)[2];
278 if ($mode && ! -w _) {
279 Carp::confess("$configpm is not writable");
284 $msg = <<EOF unless $configpm =~ /MyConfig/;
286 # This is CPAN.pm's systemwide configuration file. This file provides
287 # defaults for users, and the values can be changed in a per-user
288 # configuration file. The user-config file is being looked for as
289 # ~/.cpan/CPAN/MyConfig.pm.
293 my($fh) = FileHandle->new;
294 rename $configpm, "$configpm~" if -f $configpm;
295 open $fh, ">$configpm" or
296 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
297 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
298 foreach (sort keys %$CPAN::Config) {
299 unless (exists $keys{$_}) {
300 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
301 delete $CPAN::Config->{$_};
306 $self->neatvalue($CPAN::Config->{$_}),
311 $fh->print("};\n1;\n__END__\n");
314 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
315 #chmod $mode, $configpm;
316 ###why was that so? $self->defaults;
317 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
318 $CPAN::CONFIG_DIRTY = 0;
322 # stolen from MakeMaker; not taking the original because it is buggy;
323 # bugreport will have to say: keys of hashes remain unquoted and can
324 # produce syntax errors
327 return "undef" unless defined $v;
336 foreach my $elem (@$v) {
337 push @neat, "q[$elem]";
339 push @m, join ", ", @neat;
343 return "$v" unless $t eq 'HASH';
345 while (($key,$val) = each %$v){
346 last unless defined $key; # cautious programming in case (undef,undef) is true
347 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
349 return "{ ".join(', ',@m)." }";
354 if ($CPAN::RUN_DEGRADED) {
355 $CPAN::Frontend->mydie(
356 "'o conf defaults' disabled in ".
357 "degraded mode. Maybe try\n".
358 " !undef \$CPAN::RUN_DEGRADED\n"
362 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
364 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
365 CPAN::Shell->_reload_this($config,{reloforce => 1});
366 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
370 $CPAN::CONFIG_DIRTY = 0;
374 =head2 C<< CLASS->safe_quote ITEM >>
376 Quotes an item to become safe against spaces
377 in shell interpolation. An item is enclosed
380 - the item contains spaces in the middle
381 - the item does not start with a quote
383 This happens to avoid shell interpolation
384 problems when whitespace is present in
387 This method uses C<commands_quote> to determine
388 the correct quote. If C<commands_quote> is
389 a space, no quoting will take place.
392 if it starts and ends with the same quote character: leave it as it is
394 if it contains no whitespace: leave it as it is
396 if it contains whitespace, then
398 if it contains quotes: better leave it as it is
400 else: quote it with the correct quote type for the box we're on
405 # Instead of patching the guess, set commands_quote
407 my ($quotes,$use_quote)
414 my ($self, $command) = @_;
415 # Set up quote/default quote
416 my $quote = $CPAN::Config->{commands_quote} || $quotes;
419 and defined($command )
421 and $command !~ /[$quote]/) {
422 return qq<$use_quote$command$use_quote>
429 my($self,@args) = @_;
430 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
439 # This is a piece of repeated code that is abstracted here for
440 # maintainability. RMB
443 my($configpmdir, $configpmtest) = @_;
444 if (-w $configpmtest) {
445 return $configpmtest;
446 } elsif (-w $configpmdir) {
447 #_#_# following code dumped core on me with 5.003_11, a.k.
448 my $configpm_bak = "$configpmtest.bak";
449 unlink $configpm_bak if -f $configpm_bak;
450 if( -f $configpmtest ) {
451 if( rename $configpmtest, $configpm_bak ) {
452 $CPAN::Frontend->mywarn(<<END);
453 Old configuration file $configpmtest
454 moved to $configpm_bak
458 my $fh = FileHandle->new;
459 if ($fh->open(">$configpmtest")) {
461 return $configpmtest;
463 # Should never happen
464 Carp::confess("Cannot open >$configpmtest");
469 sub require_myconfig_or_config () {
470 return if $INC{"CPAN/MyConfig.pm"};
473 unshift @INC, File::Spec->catdir($home,'.cpan');
474 eval { require CPAN::MyConfig };
475 my $err_myconfig = $@;
476 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
477 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
479 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
480 eval {require CPAN::Config;}; # not everybody has one
482 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
483 die "Error while requiring CPAN::Config:\n$err_config";
490 if ($CPAN::META->has_usable("File::HomeDir")) {
491 $home = File::HomeDir->my_data;
499 my($self, %args) = @_;
500 $CPAN::Be_Silent++ if $args{be_silent};
504 require_myconfig_or_config;
505 return unless @miss = $self->missing_config_data;
507 require CPAN::FirstTime;
508 my($configpm,$fh,$redo,$theycalled);
510 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
511 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
512 $configpm = $INC{"CPAN/Config.pm"};
514 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
515 $configpm = $INC{"CPAN/MyConfig.pm"};
518 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
519 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
520 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
522 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
523 $configpm = _configpmtest($configpmdir,$configpmtest);
524 $inc_key = "CPAN/Config.pm";
527 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
528 File::Path::mkpath($configpmdir);
529 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
530 $configpm = _configpmtest($configpmdir,$configpmtest);
531 $inc_key = "CPAN/MyConfig.pm";
534 $INC{$inc_key} = $configpm;
536 my $text = qq{WARNING: CPAN.pm is unable to } .
537 qq{create a configuration file.};
538 output($text, 'confess');
543 if ($redo && ! $theycalled){
544 $CPAN::Frontend->myprint(<<END);
545 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
546 the following indispensable but missing parameters:
550 $args{args} = \@miss;
553 # where do we need this?
554 $CPAN::Frontend->myprint(qq{
555 $configpm initialized.
558 CPAN::FirstTime::init($configpm, %args);
562 # returns mandatory but missing entries in the Config
563 sub missing_config_data {
575 "inhibit_startup_message",
582 "mbuild_install_arg",
583 "mbuild_install_build_command",
587 "prerequisites_policy",
593 next unless exists $keys{$_};
594 push @miss, $_ unless defined $CPAN::Config->{$_};
600 $CPAN::Frontend->myprint(q[
602 commit commit session changes to disk
603 defaults reload default config values from disk
605 init enter a dialog to set all or a set of parameters
607 Edit key values as in the following (the "o" is a literal letter o):
608 o conf build_cache 15
609 o conf build_dir "/foo/bar"
611 o conf urllist unshift ftp://ftp.foo.bar/
612 o conf inhibit_startup_message 1
615 undef; #don't reprint CPAN::Config
619 my($word,$line,$pos) = @_;
621 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
622 my(@words) = split " ", substr($line,0,$pos+1);
631 @words == 4 && length($word)
634 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
635 } elsif (defined($words[2])
642 @words >= 4 && length($word)
644 return sort grep /^\Q$word\E/, keys %keys;
645 } elsif (@words >= 4) {
649 my(@o_conf) = sort grep { !$seen{$_}++ }
653 return grep /^\Q$word\E/, @o_conf;
657 my($self,$distro,$what) = @_;
659 if ($prefssupport{$what}) {
660 return $CPAN::Config->{$what} unless
663 and $distro->prefs->{cpanconfig}
664 and defined $distro->prefs->{cpanconfig}{$what};
665 return $distro->prefs->{cpanconfig}{$what};
667 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
668 "supported for distroprefs, doing a normal lookup");
669 return $CPAN::Config->{$what};
676 CPAN::Config; ####::###### #hide from indexer
677 # note: J. Nick Koston wrote me that they are using
678 # CPAN::Config->commit although undocumented. I suggested
679 # CPAN::Shell->o("conf","commit") even when ugly it is at least
682 # that's why I added the CPAN::Config class with autoload and
686 use vars qw($AUTOLOAD $VERSION);
687 $VERSION = sprintf "%.2f", substr(q$Rev: 1467 $,4)/100;
689 # formerly CPAN::HandleConfig was known as CPAN::Config
692 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
694 CPAN::HandleConfig->$l(@_);
704 This program is free software; you can redistribute it and/or
705 modify it under the same terms as Perl itself.
711 # cperl-indent-level: 4