1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $loading $VERSION);
8 $VERSION = "5.5002"; # see also CPAN::Config::VERSION at end of file
11 commit => "Commit changes to disk",
12 defaults => "Reload defaults from disk",
13 help => "Short help about 'o conf' usage",
14 init => "Interactive setting of all options",
17 # Q: where is the "How do I add a new config option" HOWTO?
18 # A1: svn diff -r 757:758 # where dagolden added test_report [git e997b71de88f1019a1472fc13cb97b1b7f96610f]
19 # A2: svn diff -r 985:986 # where andk added yaml_module [git 312b6d9b12b1bdec0b6e282d853482145475021f]
20 # A3: 1. add new config option to %keys below
21 # 2. add a Pod description in CPAN::FirstTime; it should include a
22 # prompt line; see others for examples
23 # 3. add a "matcher" section in CPAN::FirstTime::init that includes
24 # a prompt function; see others for examples
25 # 4. add config option to documentation section in CPAN.pm
27 %keys = map { $_ => undef }
34 "build_requires_install_policy",
42 "commandnumber_in_prompt",
44 "connect_to_internet_ok",
47 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
63 "inhibit_startup_message",
65 "load_module_verbosity",
70 "make_install_make_command",
74 "mbuild_install_build_command",
84 "prefer_external_tar",
87 "prerequisites_policy",
93 "show_unparsable_versions",
101 "trust_test_report_history",
113 my %prefssupport = map { $_ => 1 }
115 "build_requires_install_policy",
118 "make_install_make_command",
123 # returns true on successful action
125 my($self,@args) = @_;
127 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
128 my($o,$str,$func,$args,$key_exists);
131 my $success = $self->$o(args => \@args); # o conf init => sub init => sub load
133 die "Panic: could not configure CPAN.pm for args [@args]. Giving up.";
136 CPAN->debug("o[$o]") if $CPAN::DEBUG;
137 unless (exists $keys{$o}) {
138 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
143 # one day I used randomize_urllist for a boolean, so we must
144 # list them explicitly --ak
146 } elsif ($o =~ /^(wait_list|urllist|dontload_list)$/) {
154 CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG;
155 # Let's avoid eval, it's easier to comprehend without.
156 if ($func eq "push") {
157 push @{$CPAN::Config->{$o}}, @args;
159 } elsif ($func eq "pop") {
160 pop @{$CPAN::Config->{$o}};
162 } elsif ($func eq "shift") {
163 shift @{$CPAN::Config->{$o}};
165 } elsif ($func eq "unshift") {
166 unshift @{$CPAN::Config->{$o}}, @args;
168 } elsif ($func eq "splice") {
169 my $offset = shift @args || 0;
170 my $length = shift @args || 0;
171 splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn
174 $CPAN::Config->{$o} = [$func, @args];
177 $self->prettyprint($o);
180 if ($o eq "urllist") {
181 # reset the cached values
182 undef $CPAN::FTP::Thesite;
183 undef $CPAN::FTP::Themethod;
184 $CPAN::Index::LAST_TIME = 0;
185 } elsif ($o eq "dontload_list") {
186 # empty it, it will be built up again
187 $CPAN::META->{dontload_hash} = {};
190 } elsif ($o =~ /_hash$/) {
196 if (@args==1 && $args[0] eq "") {
198 } elsif (@args % 2) {
201 $CPAN::Config->{$o} = { @args };
209 if (defined $args[0]) {
210 $CPAN::CONFIG_DIRTY = 1;
211 $CPAN::Config->{$o} = $args[0];
214 $self->prettyprint($o)
215 if exists $keys{$o} or defined $CPAN::Config->{$o};
218 if ($CPAN::Config->{auto_commit}) {
221 $CPAN::CONFIG_DIRTY = 1;
222 $CPAN::Frontend->myprint("Please use 'o conf commit' to ".
223 "make the config permanent!\n\n");
231 my $v = $CPAN::Config->{$k};
234 if (ref $v eq "ARRAY") {
235 @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v;
239 sprintf "\t%-18s => %s\n",
241 defined $v->{$_} ? "[$v->{$_}]" : "undef"
244 $CPAN::Frontend->myprint(
254 } elsif (defined $v) {
255 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
257 $CPAN::Frontend->myprint(sprintf " %-18s undef\n", $k);
261 # generally, this should be called without arguments so that the currently
262 # loaded config file is where changes are committed.
264 my($self,@args) = @_;
265 CPAN->debug("args[@args]") if $CPAN::DEBUG;
266 if ($CPAN::RUN_DEGRADED) {
267 $CPAN::Frontend->mydie(
268 "'o conf commit' disabled in ".
269 "degraded mode. Maybe try\n".
270 " !undef \$CPAN::RUN_DEGRADED\n"
273 my ($configpm, $must_reload);
275 # XXX does anything do this? can it be simplified? -- dagolden, 2011-01-19
277 if ($args[0] eq "args") {
278 # we have not signed that contract
280 $configpm = $args[0];
284 # use provided name or the current config or create a new MyConfig
285 $configpm ||= require_myconfig_or_config() || make_new_config();
287 # commit to MyConfig if we can't write to Config
288 if ( ! -w $configpm && $configpm =~ m{CPAN/Config\.pm} ) {
289 my $myconfig = _new_config_name();
290 $CPAN::Frontend->mywarn(
291 "Your $configpm file\n".
292 "is not writable. I will attempt to write your configuration to\n" .
293 "$myconfig instead.\n\n"
295 $configpm = make_new_config();
296 $must_reload++; # so it gets loaded as $INC{'CPAN/MyConfig.pm'}
299 # XXX why not just "-w $configpm"? -- dagolden, 2011-01-19
302 $mode = (stat $configpm)[2];
303 if ($mode && ! -w _) {
304 _die_cant_write_config($configpm);
308 $self->_write_config_file($configpm);
309 require_myconfig_or_config() if $must_reload;
311 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
312 #chmod $mode, $configpm;
313 ###why was that so? $self->defaults;
314 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
315 $CPAN::CONFIG_DIRTY = 0;
319 sub _write_config_file {
320 my ($self, $configpm) = @_;
322 $msg = <<EOF if $configpm =~ m{CPAN/Config\.pm};
324 # This is CPAN.pm's systemwide configuration file. This file provides
325 # defaults for users, and the values can be changed in a per-user
326 # configuration file.
330 my($fh) = FileHandle->new;
331 rename $configpm, "$configpm~" if -f $configpm;
332 open $fh, ">$configpm" or
333 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
334 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
335 foreach (sort keys %$CPAN::Config) {
336 unless (exists $keys{$_}) {
337 # do not drop them: forward compatibility!
338 $CPAN::Frontend->mywarn("Unknown config variable '$_'\n");
343 $self->neatvalue($CPAN::Config->{$_}),
347 $fh->print("};\n1;\n__END__\n");
354 # stolen from MakeMaker; not taking the original because it is buggy;
355 # bugreport will have to say: keys of hashes remain unquoted and can
356 # produce syntax errors
359 return "undef" unless defined $v;
368 foreach my $elem (@$v) {
369 push @neat, "q[$elem]";
371 push @m, join ", ", @neat;
375 return "$v" unless $t eq 'HASH';
377 while (($key,$val) = each %$v) {
378 last unless defined $key; # cautious programming in case (undef,undef) is true
379 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
381 return "{ ".join(', ',@m)." }";
386 if ($CPAN::RUN_DEGRADED) {
387 $CPAN::Frontend->mydie(
388 "'o conf defaults' disabled in ".
389 "degraded mode. Maybe try\n".
390 " !undef \$CPAN::RUN_DEGRADED\n"
394 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
396 CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG;
397 CPAN::Shell->_reload_this($config,{reloforce => 1});
398 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
402 $CPAN::CONFIG_DIRTY = 0;
406 =head2 C<< CLASS->safe_quote ITEM >>
408 Quotes an item to become safe against spaces
409 in shell interpolation. An item is enclosed
412 - the item contains spaces in the middle
413 - the item does not start with a quote
415 This happens to avoid shell interpolation
416 problems when whitespace is present in
419 This method uses C<commands_quote> to determine
420 the correct quote. If C<commands_quote> is
421 a space, no quoting will take place.
424 if it starts and ends with the same quote character: leave it as it is
426 if it contains no whitespace: leave it as it is
428 if it contains whitespace, then
430 if it contains quotes: better leave it as it is
432 else: quote it with the correct quote type for the box we're on
437 # Instead of patching the guess, set commands_quote
439 my ($quotes,$use_quote)
446 my ($self, $command) = @_;
447 # Set up quote/default quote
448 my $quote = $CPAN::Config->{commands_quote} || $quotes;
451 and defined($command )
453 and $command !~ /[$quote]/) {
454 return qq<$use_quote$command$use_quote>
461 my($self,@args) = @_;
462 CPAN->debug("self[$self]args[".join(",",@args)."]");
463 $self->load(do_init => 1, @args);
467 # Loads CPAN::MyConfig or fall-back to CPAN::Config. Will not reload a file
468 # if already loaded. Returns the path to the file %INC or else the empty string
470 # Note -- if CPAN::Config were loaded and CPAN::MyConfig subsequently
471 # created, calling this again will leave *both* in %INC
473 sub require_myconfig_or_config () {
474 if ( $INC{"CPAN/MyConfig.pm"} || _try_loading("CPAN::MyConfig", cpan_home())) {
475 return $INC{"CPAN/MyConfig.pm"};
477 elsif ( $INC{"CPAN/Config.pm"} || _try_loading("CPAN::Config") ) {
478 return $INC{"CPAN/Config.pm"};
485 # Load a module, but ignore "can't locate..." errors
486 # Optionally take a list of directories to add to @INC for the load
488 my ($module, @dirs) = @_;
489 (my $file = $module) =~ s{::}{/}g;
493 for my $dir ( @dirs ) {
494 if ( -f File::Spec->catfile($dir, $file) ) {
500 eval { require $file };
501 my $err_myconfig = $@;
502 if ($err_myconfig and $err_myconfig !~ m#locate \Q$file\E#) {
503 die "Error while requiring ${module}:\n$err_myconfig";
508 # prioritized list of possible places for finding "CPAN/MyConfig.pm"
509 sub cpan_home_dir_candidates {
511 my $old_v = $CPAN::Config->{load_module_verbosity};
512 $CPAN::Config->{load_module_verbosity} = q[none];
513 if ($CPAN::META->has_usable('File::HomeDir')) {
514 if ($^O ne 'darwin') {
515 push @dirs, File::HomeDir->my_data;
516 # my_data is ~/Library/Application Support on darwin,
517 # which causes issues in the toolchain.
519 push @dirs, File::HomeDir->my_home;
521 push @dirs, $ENV{HOME};
522 $CPAN::Config->{load_module_verbosity} = $old_v;
523 @dirs = map { "$_/.cpan" } @dirs;
524 return wantarray ? @dirs : $dirs[0];
528 my($self, %args) = @_;
529 $CPAN::Be_Silent+=0; # protect against 'used only once'
530 $CPAN::Be_Silent++ if $args{be_silent}; # do not use; planned to be removed in 2011
531 my $do_init = delete $args{do_init} || 0;
532 my $make_myconfig = delete $args{make_myconfig};
533 $loading = 0 unless defined $loading;
535 my $configpm = require_myconfig_or_config;
536 my @miss = $self->missing_config_data;
537 CPAN->debug("do_init[$do_init]loading[$loading]miss[@miss]") if $CPAN::DEBUG;
538 return unless $do_init || @miss;
540 # I'm not how we'd ever wind up in a recursive loop, but I'm leaving
541 # this here for safety's sake -- dagolden, 2011-01-19
543 local $loading = ($loading||0) + 1;
545 # Warn if we have a config file, but things were found missing
546 if ($configpm && @miss && !$do_init) {
547 if ($make_myconfig || ( ! -w $configpm && $configpm =~ m{CPAN/Config\.pm})) {
548 $configpm = make_new_config();
549 $CPAN::Frontend->myprint(<<END);
550 The system CPAN configuration file has provided some default values,
551 but you need to complete the configuration dialog for CPAN.pm.
552 Configuration will be written to
557 $CPAN::Frontend->myprint(<<END);
558 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
559 some missing parameters. Configuration will be written to
566 require CPAN::FirstTime;
567 return CPAN::FirstTime::init($configpm || make_new_config(), %args);
570 # Creates a new, empty config file at the preferred location
571 # Any existing will be renamed with a ".bak" suffix if possible
572 # If the file cannot be created, an exception is thrown
573 sub make_new_config {
574 my $configpm = _new_config_name();
575 my $configpmdir = File::Basename::dirname( $configpm );
576 File::Path::mkpath($configpmdir) unless -d $configpmdir;
578 if ( -w $configpmdir ) {
579 #_#_# following code dumped core on me with 5.003_11, a.k.
581 my $configpm_bak = "$configpm.bak";
582 unlink $configpm_bak if -f $configpm_bak;
583 if( rename $configpm, $configpm_bak ) {
584 $CPAN::Frontend->mywarn(<<END);
585 Old configuration file $configpm
586 moved to $configpm_bak
590 my $fh = FileHandle->new;
591 if ($fh->open(">$configpm")) {
596 _die_cant_write_config($configpm);
599 sub _die_cant_write_config {
601 $CPAN::Frontend->mydie(<<"END");
602 WARNING: CPAN.pm is unable to write a configuration file. You
603 must be able to create and write to '$configpm'.
605 Aborting configuration.
610 # From candidate directories, we would like (in descending preference order):
611 # * the one that contains a MyConfig file
612 # * one that exists (even without MyConfig)
613 # * the first one on the list
615 my @dirs = cpan_home_dir_candidates();
617 return $d if -f "$d/CPAN/MyConfig.pm";
625 sub _new_config_name {
626 return File::Spec->catfile(cpan_home(), 'CPAN', 'MyConfig.pm');
629 # returns mandatory but missing entries in the Config
630 sub missing_config_data {
642 #"inhibit_startup_message",
649 "mbuild_install_arg",
650 ($^O eq "MSWin32" ? "" : "mbuild_install_build_command"),
654 "prerequisites_policy",
660 next unless exists $keys{$_};
661 push @miss, $_ unless defined $CPAN::Config->{$_};
667 $CPAN::Frontend->myprint(q[
669 commit commit session changes to disk
670 defaults reload default config values from disk
672 init enter a dialog to set all or a set of parameters
674 Edit key values as in the following (the "o" is a literal letter o):
675 o conf build_cache 15
676 o conf build_dir "/foo/bar"
678 o conf urllist unshift ftp://ftp.foo.bar/
679 o conf inhibit_startup_message 1
682 1; #don't reprint CPAN::Config
686 my($word,$line,$pos) = @_;
688 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
689 my(@words) = split " ", substr($line,0,$pos+1);
698 @words == 4 && length($word)
701 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
702 } elsif (defined($words[2])
709 @words >= 4 && length($word)
711 return sort grep /^\Q$word\E/, keys %keys;
712 } elsif (@words >= 4) {
716 my(@o_conf) = sort grep { !$seen{$_}++ }
720 return grep /^\Q$word\E/, @o_conf;
724 my($self,$distro,$what) = @_;
726 if ($prefssupport{$what}) {
727 return $CPAN::Config->{$what} unless
730 and $distro->prefs->{cpanconfig}
731 and defined $distro->prefs->{cpanconfig}{$what};
732 return $distro->prefs->{cpanconfig}{$what};
734 $CPAN::Frontend->mywarn("Warning: $what not yet officially ".
735 "supported for distroprefs, doing a normal lookup");
736 return $CPAN::Config->{$what};
743 CPAN::Config; ####::###### #hide from indexer
744 # note: J. Nick Koston wrote me that they are using
745 # CPAN::Config->commit although undocumented. I suggested
746 # CPAN::Shell->o("conf","commit") even when ugly it is at least
749 # that's why I added the CPAN::Config class with autoload and
753 use vars qw($AUTOLOAD $VERSION);
756 # formerly CPAN::HandleConfig was known as CPAN::Config
757 sub AUTOLOAD { ## no critic
758 my $class = shift; # e.g. in dh-make-perl: CPAN::Config
760 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n");
762 CPAN::HandleConfig->$l(@_);
772 This program is free software; you can redistribute it and/or
773 modify it under the same terms as Perl itself.
779 # cperl-indent-level: 4
781 # vim: ts=4 sts=4 sw=4: