1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $dot_cpan $VERSION);
5 $VERSION = sprintf "%.2f", substr(q$Rev: 423 $,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;
188 my($self,@args) = @_;
189 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
198 # This is a piece of repeated code that is abstracted here for
199 # maintainability. RMB
202 my($configpmdir, $configpmtest) = @_;
203 if (-w $configpmtest) {
204 return $configpmtest;
205 } elsif (-w $configpmdir) {
206 #_#_# following code dumped core on me with 5.003_11, a.k.
207 my $configpm_bak = "$configpmtest.bak";
208 unlink $configpm_bak if -f $configpm_bak;
209 if( -f $configpmtest ) {
210 if( rename $configpmtest, $configpm_bak ) {
211 $CPAN::Frontend->mywarn(<<END);
212 Old configuration file $configpmtest
213 moved to $configpm_bak
217 my $fh = FileHandle->new;
218 if ($fh->open(">$configpmtest")) {
220 return $configpmtest;
222 # Should never happen
223 Carp::confess("Cannot open >$configpmtest");
229 my($self, %args) = @_;
230 $CPAN::Be_Silent++ if $args{be_silent};
234 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
235 eval {require CPAN::Config;}; # not everybody has one
237 unless ($dot_cpan++){
238 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
239 eval {require CPAN::MyConfig;}; # override system wide settings
242 return unless @miss = $self->missing_config_data;
244 require CPAN::FirstTime;
245 my($configpm,$fh,$redo,$theycalled);
247 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
248 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
249 $configpm = $INC{"CPAN/Config.pm"};
251 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
252 $configpm = $INC{"CPAN/MyConfig.pm"};
255 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
256 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
257 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
258 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
259 $configpm = _configpmtest($configpmdir,$configpmtest);
262 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
263 File::Path::mkpath($configpmdir);
264 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
265 $configpm = _configpmtest($configpmdir,$configpmtest);
267 my $text = qq{WARNING: CPAN.pm is unable to } .
268 qq{create a configuration file.};
269 output($text, 'confess');
274 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
275 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
276 the following indispensable but missing parameters:
280 $CPAN::Frontend->myprint(qq{
281 $configpm initialized.
285 CPAN::FirstTime::init($configpm, %args);
288 sub missing_config_data {
299 "inhibit_startup_message",
306 "mbuild_install_arg",
307 "mbuild_install_build_command",
311 "prerequisites_policy",
317 push @miss, $_ unless defined $CPAN::Config->{$_};
323 delete $INC{'CPAN/MyConfig.pm'};
324 delete $INC{'CPAN/Config.pm'};
328 $CPAN::Frontend->myprint(q[
330 defaults reload default config values from disk
331 commit commit session changes to disk
332 init go through a dialog to set all parameters
334 You may edit key values in the follow fashion (the "o" is a literal
337 o conf build_cache 15
339 o conf build_dir "/foo/bar"
343 o conf urllist unshift ftp://ftp.foo.bar/
346 undef; #don't reprint CPAN::Config
350 my($word,$line,$pos) = @_;
352 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
353 my(@words) = split " ", substr($line,0,$pos+1);
358 $words[2] =~ /list$/ && @words == 3
360 $words[2] =~ /list$/ && @words == 4 && length($word)
363 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
364 } elsif (@words >= 4) {
368 my(@o_conf) = sort grep { !$seen{$_}++ }
372 return grep /^\Q$word\E/, @o_conf;
376 package ####::###### #hide from indexer
378 # note: J. Nick Koston wrote me that they are using
379 # CPAN::Config->commit although undocumented. I suggested
380 # CPAN::Shell->o("conf","commit") even when ugly it is at least
383 # that's why I added the CPAN::Config class with autoload and
387 use vars qw($AUTOLOAD $VERSION);
388 $VERSION = sprintf "%.2f", substr(q$Rev: 423 $,4)/100;
390 # formerly CPAN::HandleConfig was known as CPAN::Config
393 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
395 CPAN::HandleConfig->$l(@_);
403 # cperl-indent-level: 2