1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $dot_cpan $VERSION);
5 $VERSION = sprintf "%.2f", substr(q$Rev: 431 $,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
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;
83 $CPAN::Config->{$o} = $args[0] if defined $args[0];
84 $self->prettyprint($o);
91 my $v = $CPAN::Config->{$k};
94 if (ref $v eq "ARRAY") {
95 @report = map {"\t[$_]\n"} @$v;
97 @report = map { sprintf("\t%-18s => %s\n",
99 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
102 $CPAN::Frontend->myprint(
112 } elsif (defined $v) {
113 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
115 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
120 my($self,@args) = @_;
123 if ($args[0] eq "args") {
124 # we have not signed that contract
126 $configpm = $args[0];
129 unless (defined $configpm){
130 $configpm ||= $INC{"CPAN/MyConfig.pm"};
131 $configpm ||= $INC{"CPAN/Config.pm"};
132 $configpm || Carp::confess(q{
133 CPAN::Config::commit called without an argument.
134 Please specify a filename where to save the configuration or try
135 "o conf init" to have an interactive course through configing.
140 $mode = (stat $configpm)[2];
141 if ($mode && ! -w _) {
142 Carp::confess("$configpm is not writable");
147 $msg = <<EOF unless $configpm =~ /MyConfig/;
149 # This is CPAN.pm's systemwide configuration file. This file provides
150 # defaults for users, and the values can be changed in a per-user
151 # configuration file. The user-config file is being looked for as
152 # ~/.cpan/CPAN/MyConfig.pm.
156 my($fh) = FileHandle->new;
157 rename $configpm, "$configpm~" if -f $configpm;
158 open $fh, ">$configpm" or
159 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
160 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
161 foreach (sort keys %$CPAN::Config) {
164 ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
169 $fh->print("};\n1;\n__END__\n");
172 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
173 #chmod $mode, $configpm;
174 ###why was that so? $self->defaults;
175 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
179 *default = \&defaults;
183 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
184 CPAN::Shell->reload_this($config) and $done++;
191 my($self,@args) = @_;
192 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
201 # This is a piece of repeated code that is abstracted here for
202 # maintainability. RMB
205 my($configpmdir, $configpmtest) = @_;
206 if (-w $configpmtest) {
207 return $configpmtest;
208 } elsif (-w $configpmdir) {
209 #_#_# following code dumped core on me with 5.003_11, a.k.
210 my $configpm_bak = "$configpmtest.bak";
211 unlink $configpm_bak if -f $configpm_bak;
212 if( -f $configpmtest ) {
213 if( rename $configpmtest, $configpm_bak ) {
214 $CPAN::Frontend->mywarn(<<END);
215 Old configuration file $configpmtest
216 moved to $configpm_bak
220 my $fh = FileHandle->new;
221 if ($fh->open(">$configpmtest")) {
223 return $configpmtest;
225 # Should never happen
226 Carp::confess("Cannot open >$configpmtest");
232 my($self, %args) = @_;
233 $CPAN::Be_Silent++ if $args{be_silent};
237 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
238 eval {require CPAN::Config;}; # not everybody has one
240 unless ($dot_cpan++){
241 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
242 eval {require CPAN::MyConfig;}; # override system wide settings
245 return unless @miss = $self->missing_config_data;
247 require CPAN::FirstTime;
248 my($configpm,$fh,$redo,$theycalled);
250 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
251 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
252 $configpm = $INC{"CPAN/Config.pm"};
254 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
255 $configpm = $INC{"CPAN/MyConfig.pm"};
258 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
259 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
260 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
262 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
263 $configpm = _configpmtest($configpmdir,$configpmtest);
264 $inc_key = "CPAN/Config.pm";
267 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
268 File::Path::mkpath($configpmdir);
269 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
270 $configpm = _configpmtest($configpmdir,$configpmtest);
271 $inc_key = "CPAN/MyConfig.pm";
274 $INC{$inc_key} = $configpm;
276 my $text = qq{WARNING: CPAN.pm is unable to } .
277 qq{create a configuration file.};
278 output($text, 'confess');
283 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
284 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
285 the following indispensable but missing parameters:
289 $CPAN::Frontend->myprint(qq{
290 $configpm initialized.
294 CPAN::FirstTime::init($configpm, %args);
297 sub missing_config_data {
308 "inhibit_startup_message",
315 "mbuild_install_arg",
316 "mbuild_install_build_command",
320 "prerequisites_policy",
326 push @miss, $_ unless defined $CPAN::Config->{$_};
332 $CPAN::Frontend->myprint(q[
334 defaults reload default config values from disk
335 commit commit session changes to disk
336 init go through a dialog to set all parameters
338 You may edit key values in the follow fashion (the "o" is a literal
341 o conf build_cache 15
343 o conf build_dir "/foo/bar"
347 o conf urllist unshift ftp://ftp.foo.bar/
350 undef; #don't reprint CPAN::Config
354 my($word,$line,$pos) = @_;
356 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
357 my(@words) = split " ", substr($line,0,$pos+1);
362 $words[2] =~ /list$/ && @words == 3
364 $words[2] =~ /list$/ && @words == 4 && length($word)
367 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
368 } elsif (@words >= 4) {
372 my(@o_conf) = sort grep { !$seen{$_}++ }
376 return grep /^\Q$word\E/, @o_conf;
380 package ####::###### #hide from indexer
382 # note: J. Nick Koston wrote me that they are using
383 # CPAN::Config->commit although undocumented. I suggested
384 # CPAN::Shell->o("conf","commit") even when ugly it is at least
387 # that's why I added the CPAN::Config class with autoload and
391 use vars qw($AUTOLOAD $VERSION);
392 $VERSION = sprintf "%.2f", substr(q$Rev: 431 $,4)/100;
394 # formerly CPAN::HandleConfig was known as CPAN::Config
397 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
399 CPAN::HandleConfig->$l(@_);
407 # cperl-indent-level: 2