1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 1379 $,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 }
22 "build_requires_install_policy",
29 "commandnumber_in_prompt",
33 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
46 "inhibit_startup_message",
52 "make_install_make_command",
56 "mbuild_install_build_command",
65 "prerequisites_policy",
86 my %prefssupport = map { $_ => 1 }
88 "build_requires_install_policy",
91 "make_install_make_command",
96 if ($^O eq "MSWin32") {
98 mbuild_install_build_command
99 make_install_make_command
102 if (exists $CPAN::Config->{$k}) {
103 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
104 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
106 delete $CPAN::Config->{$k};
111 # returns true on successful action
113 my($self,@args) = @_;
115 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
116 my($o,$str,$func,$args,$key_exists);
120 $self->$o(args => \@args); # o conf init => sub init => sub load
123 CPAN->debug("o[$o]") if $CPAN::DEBUG;
124 unless (exists $keys{$o}) {
125 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
127 # one day I used randomize_urllist for a boolean, so we must
128 # list them explicitly --ak
129 if ($o =~ /^(wait_list|urllist|dontload_list)$/) {
132 CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG;
134 # Let's avoid eval, it's easier to comprehend without.
135 if ($func eq "push") {
136 push @{$CPAN::Config->{$o}}, @args;
138 } elsif ($func eq "pop") {
139 pop @{$CPAN::Config->{$o}};
141 } elsif ($func eq "shift") {
142 shift @{$CPAN::Config->{$o}};
144 } elsif ($func eq "unshift") {
145 unshift @{$CPAN::Config->{$o}}, @args;
147 } elsif ($func eq "splice") {
148 my $offset = shift @args || 0;
149 my $length = shift @args || 0;
150 splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn
153 $CPAN::Config->{$o} = [$func, @args];
156 $self->prettyprint($o);
159 $CPAN::CONFIG_DIRTY = 1;
160 if ($o eq "urllist") {
161 # reset the cached values
162 undef $CPAN::FTP::Thesite;
163 undef $CPAN::FTP::Themethod;
164 } elsif ($o eq "dontload_list") {
165 # empty it, it will be built up again
166 $CPAN::META->{dontload_hash} = {};
170 } elsif ($o =~ /_hash$/) {
171 if (@args==1 && $args[0] eq ""){
173 } elsif (@args % 2) {
176 $CPAN::Config->{$o} = { @args };
177 $CPAN::CONFIG_DIRTY = 1;
179 if (defined $args[0]){
180 $CPAN::CONFIG_DIRTY = 1;
181 $CPAN::Config->{$o} = $args[0];
183 $self->prettyprint($o)
184 if exists $keys{$o} or defined $CPAN::Config->{$o};
192 my $v = $CPAN::Config->{$k};
195 if (ref $v eq "ARRAY") {
196 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
198 @report = map { sprintf("\t%-18s => %s\n",
200 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
203 $CPAN::Frontend->myprint(
213 } elsif (defined $v) {
214 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
216 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
221 my($self,@args) = @_;
222 CPAN->debug("args[@args]") if $CPAN::DEBUG;
223 if ($CPAN::RUN_DEGRADED) {
224 $CPAN::Frontend->mydie(
225 "'o conf commit' disabled in ".
226 "degraded mode. Maybe try\n".
227 " !undef \$CPAN::RUN_DEGRADED\n"
232 if ($args[0] eq "args") {
233 # we have not signed that contract
235 $configpm = $args[0];
238 unless (defined $configpm){
239 $configpm ||= $INC{"CPAN/MyConfig.pm"};
240 $configpm ||= $INC{"CPAN/Config.pm"};
241 $configpm || Carp::confess(q{
242 CPAN::Config::commit called without an argument.
243 Please specify a filename where to save the configuration or try
244 "o conf init" to have an interactive course through configing.
249 $mode = (stat $configpm)[2];
250 if ($mode && ! -w _) {
251 Carp::confess("$configpm is not writable");
256 $msg = <<EOF unless $configpm =~ /MyConfig/;
258 # This is CPAN.pm's systemwide configuration file. This file provides
259 # defaults for users, and the values can be changed in a per-user
260 # configuration file. The user-config file is being looked for as
261 # ~/.cpan/CPAN/MyConfig.pm.
265 my($fh) = FileHandle->new;
266 rename $configpm, "$configpm~" if -f $configpm;
267 open $fh, ">$configpm" or
268 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
269 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
270 foreach (sort keys %$CPAN::Config) {
271 unless (exists $keys{$_}) {
272 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
273 delete $CPAN::Config->{$_};
278 $self->neatvalue($CPAN::Config->{$_}),
283 $fh->print("};\n1;\n__END__\n");
286 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
287 #chmod $mode, $configpm;
288 ###why was that so? $self->defaults;
289 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
290 $CPAN::CONFIG_DIRTY = 0;
294 # stolen from MakeMaker; not taking the original because it is buggy;
295 # bugreport will have to say: keys of hashes remain unquoted and can
296 # produce syntax errors
299 return "undef" unless defined $v;
308 foreach my $elem (@$v) {
309 push @neat, "q[$elem]";
311 push @m, join ", ", @neat;
315 return "$v" unless $t eq 'HASH';
317 while (($key,$val) = each %$v){
318 last unless defined $key; # cautious programming in case (undef,undef) is true
319 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
321 return "{ ".join(', ',@m)." }";
326 if ($CPAN::RUN_DEGRADED) {
327 $CPAN::Frontend->mydie(
328 "'o conf defaults' disabled in ".
329 "degraded mode. Maybe try\n".
330 " !undef \$CPAN::RUN_DEGRADED\n"
334 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
336 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
337 CPAN::Shell->_reload_this($config,{reloforce => 1});
338 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
342 $CPAN::CONFIG_DIRTY = 0;
346 =head2 C<< CLASS->safe_quote ITEM >>
348 Quotes an item to become safe against spaces
349 in shell interpolation. An item is enclosed
352 - the item contains spaces in the middle
353 - the item does not start with a quote
355 This happens to avoid shell interpolation
356 problems when whitespace is present in
359 This method uses C<commands_quote> to determine
360 the correct quote. If C<commands_quote> is
361 a space, no quoting will take place.
364 if it starts and ends with the same quote character: leave it as it is
366 if it contains no whitespace: leave it as it is
368 if it contains whitespace, then
370 if it contains quotes: better leave it as it is
372 else: quote it with the correct quote type for the box we're on
377 # Instead of patching the guess, set commands_quote
379 my ($quotes,$use_quote)
386 my ($self, $command) = @_;
387 # Set up quote/default quote
388 my $quote = $CPAN::Config->{commands_quote} || $quotes;
391 and defined($command )
393 and $command !~ /[$quote]/) {
394 return qq<$use_quote$command$use_quote>
401 my($self,@args) = @_;
402 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
411 # This is a piece of repeated code that is abstracted here for
412 # maintainability. RMB
415 my($configpmdir, $configpmtest) = @_;
416 if (-w $configpmtest) {
417 return $configpmtest;
418 } elsif (-w $configpmdir) {
419 #_#_# following code dumped core on me with 5.003_11, a.k.
420 my $configpm_bak = "$configpmtest.bak";
421 unlink $configpm_bak if -f $configpm_bak;
422 if( -f $configpmtest ) {
423 if( rename $configpmtest, $configpm_bak ) {
424 $CPAN::Frontend->mywarn(<<END);
425 Old configuration file $configpmtest
426 moved to $configpm_bak
430 my $fh = FileHandle->new;
431 if ($fh->open(">$configpmtest")) {
433 return $configpmtest;
435 # Should never happen
436 Carp::confess("Cannot open >$configpmtest");
441 sub require_myconfig_or_config () {
442 return if $INC{"CPAN/MyConfig.pm"};
445 unshift @INC, File::Spec->catdir($home,'.cpan');
446 eval { require CPAN::MyConfig };
447 my $err_myconfig = $@;
448 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
449 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
451 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
452 eval {require CPAN::Config;}; # not everybody has one
454 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
455 die "Error while requiring CPAN::Config:\n$err_config";
462 if ($CPAN::META->has_usable("File::HomeDir")) {
463 $home = File::HomeDir->my_data;
471 my($self, %args) = @_;
472 $CPAN::Be_Silent++ if $args{be_silent};
476 require_myconfig_or_config;
477 return unless @miss = $self->missing_config_data;
479 require CPAN::FirstTime;
480 my($configpm,$fh,$redo,$theycalled);
482 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
483 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
484 $configpm = $INC{"CPAN/Config.pm"};
486 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
487 $configpm = $INC{"CPAN/MyConfig.pm"};
490 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
491 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
492 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
494 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
495 $configpm = _configpmtest($configpmdir,$configpmtest);
496 $inc_key = "CPAN/Config.pm";
499 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
500 File::Path::mkpath($configpmdir);
501 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
502 $configpm = _configpmtest($configpmdir,$configpmtest);
503 $inc_key = "CPAN/MyConfig.pm";
506 $INC{$inc_key} = $configpm;
508 my $text = qq{WARNING: CPAN.pm is unable to } .
509 qq{create a configuration file.};
510 output($text, 'confess');
515 if ($redo && ! $theycalled){
516 $CPAN::Frontend->myprint(<<END);
517 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
518 the following indispensable but missing parameters:
522 $args{args} = \@miss;
525 # where do we need this?
526 $CPAN::Frontend->myprint(qq{
527 $configpm initialized.
530 CPAN::FirstTime::init($configpm, %args);
533 sub missing_config_data {
544 "inhibit_startup_message",
551 "mbuild_install_arg",
552 "mbuild_install_build_command",
556 "prerequisites_policy",
562 next unless exists $keys{$_};
563 push @miss, $_ unless defined $CPAN::Config->{$_};
569 $CPAN::Frontend->myprint(q[
571 commit commit session changes to disk
572 defaults reload default config values from disk
574 init enter a dialog to set all or a set of parameters
576 Edit key values as in the following (the "o" is a literal letter o):
577 o conf build_cache 15
578 o conf build_dir "/foo/bar"
580 o conf urllist unshift ftp://ftp.foo.bar/
581 o conf inhibit_startup_message 1
584 undef; #don't reprint CPAN::Config
588 my($word,$line,$pos) = @_;
590 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
591 my(@words) = split " ", substr($line,0,$pos+1);
600 @words == 4 && length($word)
603 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
604 } elsif (defined($words[2])
611 @words >= 4 && length($word)
613 return sort grep /^\Q$word\E/, keys %keys;
614 } elsif (@words >= 4) {
618 my(@o_conf) = sort grep { !$seen{$_}++ }
622 return grep /^\Q$word\E/, @o_conf;
626 my($self,$distro,$what) = @_;
628 if ($prefssupport{$what}) {
629 return $CPAN::Config->{$what} unless
632 and $distro->prefs->{cpanconfig}
633 and defined $distro->prefs->{cpanconfig}{$what};
634 return $distro->prefs->{cpanconfig}{$what};
636 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
637 "supported for distroprefs, doing a normal lookup");
638 return $CPAN::Config->{$what};
645 CPAN::Config; ####::###### #hide from indexer
646 # note: J. Nick Koston wrote me that they are using
647 # CPAN::Config->commit although undocumented. I suggested
648 # CPAN::Shell->o("conf","commit") even when ugly it is at least
651 # that's why I added the CPAN::Config class with autoload and
655 use vars qw($AUTOLOAD $VERSION);
656 $VERSION = sprintf "%.2f", substr(q$Rev: 1379 $,4)/100;
658 # formerly CPAN::HandleConfig was known as CPAN::Config
661 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
663 CPAN::HandleConfig->$l(@_);
673 This program is free software; you can redistribute it and/or
674 modify it under the same terms as Perl itself.
680 # cperl-indent-level: 4