1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $dot_cpan $VERSION);
5 $VERSION = sprintf "%.2f", substr(q$Rev: 469 $,4)/100;
8 'commit' => "Commit changes to disk",
9 'defaults' => "Reload defaults from disk",
10 'init' => "Interactive setting of all options",
13 %keys = map { $_ => undef } qw(
14 build_cache build_dir bzip2
15 cache_metadata commandnumber_in_prompt cpan_home curl
17 ftp ftp_passive ftp_proxy
19 histfile histsize http_proxy
20 inactivity_timeout index_expire inhibit_startup_message
23 make make_arg make_install_arg make_install_make_command makepl_arg
24 mbuild_arg mbuild_install_arg mbuild_install_build_command mbuildpl_arg
25 ncftp ncftpget no_proxy pager
26 prefer_installer prerequisites_policy
27 scan_cache shell show_upload_date
33 # returns true on successful action
37 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
38 my($o,$str,$func,$args,$key_exists);
42 $self->$o(args => \@args);
45 CPAN->debug("o[$o]") if $CPAN::DEBUG;
46 unless (exists $keys{$o}) {
47 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
52 CPAN->debug("func[$func]") if $CPAN::DEBUG;
54 # Let's avoid eval, it's easier to comprehend without.
55 if ($func eq "push") {
56 push @{$CPAN::Config->{$o}}, @args;
58 } elsif ($func eq "pop") {
59 pop @{$CPAN::Config->{$o}};
61 } elsif ($func eq "shift") {
62 shift @{$CPAN::Config->{$o}};
64 } elsif ($func eq "unshift") {
65 unshift @{$CPAN::Config->{$o}}, @args;
67 } elsif ($func eq "splice") {
68 splice @{$CPAN::Config->{$o}}, @args;
71 $CPAN::Config->{$o} = [@args];
74 $self->prettyprint($o);
76 if ($o eq "urllist" && $changed) {
77 # reset the cached values
78 undef $CPAN::FTP::Thesite;
79 undef $CPAN::FTP::Themethod;
82 } elsif ($o =~ /_hash$/) {
83 push @args, "" if @args % 2;
84 $CPAN::Config->{$o} = { @args };
86 $CPAN::Config->{$o} = $args[0] if defined $args[0];
87 $self->prettyprint($o);
94 my $v = $CPAN::Config->{$k};
97 if (ref $v eq "ARRAY") {
98 @report = map {"\t[$_]\n"} @$v;
100 @report = map { sprintf("\t%-18s => %s\n",
102 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
105 $CPAN::Frontend->myprint(
115 } elsif (defined $v) {
116 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
118 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
123 my($self,@args) = @_;
126 if ($args[0] eq "args") {
127 # we have not signed that contract
129 $configpm = $args[0];
132 unless (defined $configpm){
133 $configpm ||= $INC{"CPAN/MyConfig.pm"};
134 $configpm ||= $INC{"CPAN/Config.pm"};
135 $configpm || Carp::confess(q{
136 CPAN::Config::commit called without an argument.
137 Please specify a filename where to save the configuration or try
138 "o conf init" to have an interactive course through configing.
143 $mode = (stat $configpm)[2];
144 if ($mode && ! -w _) {
145 Carp::confess("$configpm is not writable");
150 $msg = <<EOF unless $configpm =~ /MyConfig/;
152 # This is CPAN.pm's systemwide configuration file. This file provides
153 # defaults for users, and the values can be changed in a per-user
154 # configuration file. The user-config file is being looked for as
155 # ~/.cpan/CPAN/MyConfig.pm.
159 my($fh) = FileHandle->new;
160 rename $configpm, "$configpm~" if -f $configpm;
161 open $fh, ">$configpm" or
162 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
163 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
164 foreach (sort keys %$CPAN::Config) {
167 $self->neatvalue($CPAN::Config->{$_}),
172 $fh->print("};\n1;\n__END__\n");
175 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
176 #chmod $mode, $configpm;
177 ###why was that so? $self->defaults;
178 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
182 # stolen from MakeMaker; not taking the original because it is buggy;
183 # bugreport will have to say: keys of hashes remain unquoted and can
184 # produce syntax errors
187 return "undef" unless defined $v;
189 return "q[$v]" unless $t;
193 foreach my $elem (@$v) {
194 push @neat, "q[$elem]";
196 push @m, join ", ", @neat;
200 return "$v" unless $t eq 'HASH';
202 while (($key,$val) = each %$v){
203 last unless defined $key; # cautious programming in case (undef,undef) is true
204 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
206 return "{ ".join(', ',@m)." }";
212 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
213 CPAN::Shell->reload_this($config) and $done++;
220 my($self,@args) = @_;
221 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
230 # This is a piece of repeated code that is abstracted here for
231 # maintainability. RMB
234 my($configpmdir, $configpmtest) = @_;
235 if (-w $configpmtest) {
236 return $configpmtest;
237 } elsif (-w $configpmdir) {
238 #_#_# following code dumped core on me with 5.003_11, a.k.
239 my $configpm_bak = "$configpmtest.bak";
240 unlink $configpm_bak if -f $configpm_bak;
241 if( -f $configpmtest ) {
242 if( rename $configpmtest, $configpm_bak ) {
243 $CPAN::Frontend->mywarn(<<END);
244 Old configuration file $configpmtest
245 moved to $configpm_bak
249 my $fh = FileHandle->new;
250 if ($fh->open(">$configpmtest")) {
252 return $configpmtest;
254 # Should never happen
255 Carp::confess("Cannot open >$configpmtest");
261 my($self, %args) = @_;
262 $CPAN::Be_Silent++ if $args{be_silent};
266 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
267 eval {require CPAN::Config;}; # not everybody has one
269 unless ($dot_cpan++){
270 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
271 eval {require CPAN::MyConfig;}; # override system wide settings
274 return unless @miss = $self->missing_config_data;
276 require CPAN::FirstTime;
277 my($configpm,$fh,$redo,$theycalled);
279 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
280 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
281 $configpm = $INC{"CPAN/Config.pm"};
283 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
284 $configpm = $INC{"CPAN/MyConfig.pm"};
287 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
288 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
289 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
291 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
292 $configpm = _configpmtest($configpmdir,$configpmtest);
293 $inc_key = "CPAN/Config.pm";
296 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
297 File::Path::mkpath($configpmdir);
298 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
299 $configpm = _configpmtest($configpmdir,$configpmtest);
300 $inc_key = "CPAN/MyConfig.pm";
303 $INC{$inc_key} = $configpm;
305 my $text = qq{WARNING: CPAN.pm is unable to } .
306 qq{create a configuration file.};
307 output($text, 'confess');
312 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
313 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
314 the following indispensable but missing parameters:
318 $CPAN::Frontend->myprint(qq{
319 $configpm initialized.
323 CPAN::FirstTime::init($configpm, %args);
326 sub missing_config_data {
337 "inhibit_startup_message",
344 "mbuild_install_arg",
345 "mbuild_install_build_command",
349 "prerequisites_policy",
355 push @miss, $_ unless defined $CPAN::Config->{$_};
361 $CPAN::Frontend->myprint(q[
363 defaults reload default config values from disk
364 commit commit session changes to disk
365 init go through a dialog to set all parameters
367 You may edit key values in the follow fashion (the "o" is a literal
370 o conf build_cache 15
372 o conf build_dir "/foo/bar"
376 o conf urllist unshift ftp://ftp.foo.bar/
379 undef; #don't reprint CPAN::Config
383 my($word,$line,$pos) = @_;
385 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
386 my(@words) = split " ", substr($line,0,$pos+1);
391 $words[2] =~ /list$/ && @words == 3
393 $words[2] =~ /list$/ && @words == 4 && length($word)
396 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
397 } elsif (@words >= 4) {
401 my(@o_conf) = sort grep { !$seen{$_}++ }
405 return grep /^\Q$word\E/, @o_conf;
409 package ####::###### #hide from indexer
411 # note: J. Nick Koston wrote me that they are using
412 # CPAN::Config->commit although undocumented. I suggested
413 # CPAN::Shell->o("conf","commit") even when ugly it is at least
416 # that's why I added the CPAN::Config class with autoload and
420 use vars qw($AUTOLOAD $VERSION);
421 $VERSION = sprintf "%.2f", substr(q$Rev: 469 $,4)/100;
423 # formerly CPAN::HandleConfig was known as CPAN::Config
426 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
428 CPAN::HandleConfig->$l(@_);
436 # cperl-indent-level: 4