1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 987 $,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 } (
18 # allow_unauthenticated ?? some day...
21 "build_requires_install_policy",
28 "commandnumber_in_prompt",
32 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
45 "inhibit_startup_message",
51 "make_install_make_command",
55 "mbuild_install_build_command",
63 "prerequisites_policy",
81 if ($^O eq "MSWin32") {
83 mbuild_install_build_command
84 make_install_make_command
87 if (exists $CPAN::Config->{$k}) {
88 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
89 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
91 delete $CPAN::Config->{$k};
96 # returns true on successful action
100 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
101 my($o,$str,$func,$args,$key_exists);
105 $self->$o(args => \@args); # o conf init => sub init => sub load
108 CPAN->debug("o[$o]") if $CPAN::DEBUG;
109 unless (exists $keys{$o}) {
110 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
115 CPAN->debug("func[$func]") if $CPAN::DEBUG;
117 # Let's avoid eval, it's easier to comprehend without.
118 if ($func eq "push") {
119 push @{$CPAN::Config->{$o}}, @args;
121 } elsif ($func eq "pop") {
122 pop @{$CPAN::Config->{$o}};
124 } elsif ($func eq "shift") {
125 shift @{$CPAN::Config->{$o}};
127 } elsif ($func eq "unshift") {
128 unshift @{$CPAN::Config->{$o}}, @args;
130 } elsif ($func eq "splice") {
131 splice @{$CPAN::Config->{$o}}, @args;
134 $CPAN::Config->{$o} = [@args];
137 $self->prettyprint($o);
140 if ($o eq "urllist") {
141 # reset the cached values
142 undef $CPAN::FTP::Thesite;
143 undef $CPAN::FTP::Themethod;
144 } elsif ($o eq "dontload_list") {
145 # empty it, it will be built up again
146 $CPAN::META->{dontload_hash} = {};
150 } elsif ($o =~ /_hash$/) {
151 @args = () if @args==1 && $args[0] eq "";
152 push @args, "" if @args % 2;
153 $CPAN::Config->{$o} = { @args };
155 $CPAN::Config->{$o} = $args[0] if defined $args[0];
156 $self->prettyprint($o)
157 if exists $keys{$o} or defined $CPAN::Config->{$o};
165 my $v = $CPAN::Config->{$k};
168 if (ref $v eq "ARRAY") {
169 @report = map {"\t[$_]\n"} @$v;
171 @report = map { sprintf("\t%-18s => %s\n",
173 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
176 $CPAN::Frontend->myprint(
186 } elsif (defined $v) {
187 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
189 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
194 my($self,@args) = @_;
197 if ($args[0] eq "args") {
198 # we have not signed that contract
200 $configpm = $args[0];
203 unless (defined $configpm){
204 $configpm ||= $INC{"CPAN/MyConfig.pm"};
205 $configpm ||= $INC{"CPAN/Config.pm"};
206 $configpm || Carp::confess(q{
207 CPAN::Config::commit called without an argument.
208 Please specify a filename where to save the configuration or try
209 "o conf init" to have an interactive course through configing.
214 $mode = (stat $configpm)[2];
215 if ($mode && ! -w _) {
216 Carp::confess("$configpm is not writable");
221 $msg = <<EOF unless $configpm =~ /MyConfig/;
223 # This is CPAN.pm's systemwide configuration file. This file provides
224 # defaults for users, and the values can be changed in a per-user
225 # configuration file. The user-config file is being looked for as
226 # ~/.cpan/CPAN/MyConfig.pm.
230 my($fh) = FileHandle->new;
231 rename $configpm, "$configpm~" if -f $configpm;
232 open $fh, ">$configpm" or
233 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
234 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
235 foreach (sort keys %$CPAN::Config) {
236 unless (exists $keys{$_}) {
237 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
238 delete $CPAN::Config->{$_};
243 $self->neatvalue($CPAN::Config->{$_}),
248 $fh->print("};\n1;\n__END__\n");
251 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
252 #chmod $mode, $configpm;
253 ###why was that so? $self->defaults;
254 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
258 # stolen from MakeMaker; not taking the original because it is buggy;
259 # bugreport will have to say: keys of hashes remain unquoted and can
260 # produce syntax errors
263 return "undef" unless defined $v;
265 return "q[$v]" unless $t;
269 foreach my $elem (@$v) {
270 push @neat, "q[$elem]";
272 push @m, join ", ", @neat;
276 return "$v" unless $t eq 'HASH';
278 while (($key,$val) = each %$v){
279 last unless defined $key; # cautious programming in case (undef,undef) is true
280 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
282 return "{ ".join(', ',@m)." }";
288 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
290 CPAN::Shell->reload_this($config,{force => 1});
291 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
298 =head2 C<< CLASS->safe_quote ITEM >>
300 Quotes an item to become safe against spaces
301 in shell interpolation. An item is enclosed
304 - the item contains spaces in the middle
305 - the item does not start with a quote
307 This happens to avoid shell interpolation
308 problems when whitespace is present in
311 This method uses C<commands_quote> to determine
312 the correct quote. If C<commands_quote> is
313 a space, no quoting will take place.
316 if it starts and ends with the same quote character: leave it as it is
318 if it contains no whitespace: leave it as it is
320 if it contains whitespace, then
322 if it contains quotes: better leave it as it is
324 else: quote it with the correct quote type for the box we're on
329 # Instead of patching the guess, set commands_quote
331 my ($quotes,$use_quote)
338 my ($self, $command) = @_;
339 # Set up quote/default quote
340 my $quote = $CPAN::Config->{commands_quote} || $quotes;
344 and $command !~ /[$quote]/) {
345 return qq<$use_quote$command$use_quote>
352 my($self,@args) = @_;
353 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
362 # This is a piece of repeated code that is abstracted here for
363 # maintainability. RMB
366 my($configpmdir, $configpmtest) = @_;
367 if (-w $configpmtest) {
368 return $configpmtest;
369 } elsif (-w $configpmdir) {
370 #_#_# following code dumped core on me with 5.003_11, a.k.
371 my $configpm_bak = "$configpmtest.bak";
372 unlink $configpm_bak if -f $configpm_bak;
373 if( -f $configpmtest ) {
374 if( rename $configpmtest, $configpm_bak ) {
375 $CPAN::Frontend->mywarn(<<END);
376 Old configuration file $configpmtest
377 moved to $configpm_bak
381 my $fh = FileHandle->new;
382 if ($fh->open(">$configpmtest")) {
384 return $configpmtest;
386 # Should never happen
387 Carp::confess("Cannot open >$configpmtest");
392 sub require_myconfig_or_config () {
393 return if $INC{"CPAN/MyConfig.pm"};
396 unshift @INC, File::Spec->catdir($home,'.cpan');
397 eval { require CPAN::MyConfig };
398 my $err_myconfig = $@;
399 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
400 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
402 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
403 eval {require CPAN::Config;}; # not everybody has one
405 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
406 die "Error while requiring CPAN::Config:\n$err_config";
413 if ($CPAN::META->has_usable("File::HomeDir")) {
414 $home = File::HomeDir->my_data;
422 my($self, %args) = @_;
423 $CPAN::Be_Silent++ if $args{be_silent};
427 require_myconfig_or_config;
428 return unless @miss = $self->missing_config_data;
430 require CPAN::FirstTime;
431 my($configpm,$fh,$redo,$theycalled);
433 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
434 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
435 $configpm = $INC{"CPAN/Config.pm"};
437 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
438 $configpm = $INC{"CPAN/MyConfig.pm"};
441 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
442 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
443 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
445 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
446 $configpm = _configpmtest($configpmdir,$configpmtest);
447 $inc_key = "CPAN/Config.pm";
450 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
451 File::Path::mkpath($configpmdir);
452 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
453 $configpm = _configpmtest($configpmdir,$configpmtest);
454 $inc_key = "CPAN/MyConfig.pm";
457 $INC{$inc_key} = $configpm;
459 my $text = qq{WARNING: CPAN.pm is unable to } .
460 qq{create a configuration file.};
461 output($text, 'confess');
466 if ($redo && ! $theycalled){
467 $CPAN::Frontend->myprint(<<END);
468 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
469 the following indispensable but missing parameters:
473 $args{args} = \@miss;
476 # where do we need this?
477 $CPAN::Frontend->myprint(qq{
478 $configpm initialized.
481 CPAN::FirstTime::init($configpm, %args);
484 sub missing_config_data {
495 "inhibit_startup_message",
502 "mbuild_install_arg",
503 "mbuild_install_build_command",
507 "prerequisites_policy",
513 next unless exists $keys{$_};
514 push @miss, $_ unless defined $CPAN::Config->{$_};
520 $CPAN::Frontend->myprint(q[
522 commit commit session changes to disk
523 defaults reload default config values from disk
525 init enter a dialog to set all or a set of parameters
527 Edit key values as in the following (the "o" is a literal letter o):
528 o conf build_cache 15
529 o conf build_dir "/foo/bar"
531 o conf urllist unshift ftp://ftp.foo.bar/
532 o conf inhibit_startup_message 1
535 undef; #don't reprint CPAN::Config
539 my($word,$line,$pos) = @_;
541 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
542 my(@words) = split " ", substr($line,0,$pos+1);
551 @words == 4 && length($word)
554 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
555 } elsif (defined($words[2])
562 @words >= 4 && length($word)
564 return sort grep /^\Q$word\E/, keys %keys;
565 } elsif (@words >= 4) {
569 my(@o_conf) = sort grep { !$seen{$_}++ }
573 return grep /^\Q$word\E/, @o_conf;
578 CPAN::Config; ####::###### #hide from indexer
579 # note: J. Nick Koston wrote me that they are using
580 # CPAN::Config->commit although undocumented. I suggested
581 # CPAN::Shell->o("conf","commit") even when ugly it is at least
584 # that's why I added the CPAN::Config class with autoload and
588 use vars qw($AUTOLOAD $VERSION);
589 $VERSION = sprintf "%.2f", substr(q$Rev: 987 $,4)/100;
591 # formerly CPAN::HandleConfig was known as CPAN::Config
594 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
596 CPAN::HandleConfig->$l(@_);
605 This program is free software; you can redistribute it and/or
606 modify it under the same terms as Perl itself.
612 # cperl-indent-level: 4