3 use vars qw($AUTOLOAD);
5 # formerly CPAN::HandleConfig was known as CPAN::Config
8 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
10 CPAN::HandleConfig->$l(@_);
13 # note: J. Nick Koston wrote me that they are using
14 # CPAN::Config->commit although undocumented. I suggested
15 # CPAN::Shell->o("conf","commit") even when ugly it is at least
18 # that's why I added the CPAN::Config class with autoload and
21 package CPAN::HandleConfig;
23 use vars qw(%can %keys $dot_cpan $VERSION);
25 $VERSION = sprintf "%.2f", substr(q$Rev: 337 $,4)/100;
28 'commit' => "Commit changes to disk",
29 'defaults' => "Reload defaults from disk",
30 'init' => "Interactive setting of all options",
33 %keys = map { $_ => undef } qw(
34 build_cache build_dir bzip2
35 cache_metadata cpan_home curl
39 histfile histsize http_proxy
40 inactivity_timeout index_expire inhibit_startup_message
43 make make_arg make_install_arg make_install_make_command makepl_arg
44 mbuild_arg mbuild_install_arg mbuild_install_build_command mbuildpl_arg
45 ncftp ncftpget no_proxy pager
46 prefer_installer prerequisites_policy
47 scan_cache shell show_upload_date
53 # returns true on successful action
57 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
58 my($o,$str,$func,$args,$key_exists);
64 CPAN->debug("o[$o]") if $CPAN::DEBUG;
65 unless (exists $keys{$o}) {
66 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
71 CPAN->debug("func[$func]") if $CPAN::DEBUG;
73 # Let's avoid eval, it's easier to comprehend without.
74 if ($func eq "push") {
75 push @{$CPAN::Config->{$o}}, @args;
77 } elsif ($func eq "pop") {
78 pop @{$CPAN::Config->{$o}};
80 } elsif ($func eq "shift") {
81 shift @{$CPAN::Config->{$o}};
83 } elsif ($func eq "unshift") {
84 unshift @{$CPAN::Config->{$o}}, @args;
86 } elsif ($func eq "splice") {
87 splice @{$CPAN::Config->{$o}}, @args;
90 $CPAN::Config->{$o} = [@args];
93 $self->prettyprint($o);
95 if ($o eq "urllist" && $changed) {
96 # reset the cached values
97 undef $CPAN::FTP::Thesite;
98 undef $CPAN::FTP::Themethod;
102 $CPAN::Config->{$o} = $args[0] if defined $args[0];
103 $self->prettyprint($o);
110 my $v = $CPAN::Config->{$k};
112 my(@report) = ref $v eq "ARRAY" ?
114 map { sprintf(" %-18s => [%s]\n",
116 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
118 $CPAN::Frontend->myprint(
125 map {"\t[$_]\n"} @report
128 } elsif (defined $v) {
129 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
131 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
136 my($self,$configpm) = @_;
137 unless (defined $configpm){
138 $configpm ||= $INC{"CPAN/MyConfig.pm"};
139 $configpm ||= $INC{"CPAN/Config.pm"};
140 $configpm || Carp::confess(q{
141 CPAN::Config::commit called without an argument.
142 Please specify a filename where to save the configuration or try
143 "o conf init" to have an interactive course through configing.
148 $mode = (stat $configpm)[2];
149 if ($mode && ! -w _) {
150 Carp::confess("$configpm is not writable");
155 $msg = <<EOF unless $configpm =~ /MyConfig/;
157 # This is CPAN.pm's systemwide configuration file. This file provides
158 # defaults for users, and the values can be changed in a per-user
159 # configuration file. The user-config file is being looked for as
160 # ~/.cpan/CPAN/MyConfig.pm.
164 my($fh) = FileHandle->new;
165 rename $configpm, "$configpm~" if -f $configpm;
166 open $fh, ">$configpm" or
167 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
168 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
169 foreach (sort keys %$CPAN::Config) {
172 ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
177 $fh->print("};\n1;\n__END__\n");
180 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
181 #chmod $mode, $configpm;
182 ###why was that so? $self->defaults;
183 $CPAN::Frontend->myprint("commit: wrote $configpm\n");
187 *default = \&defaults;
197 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
206 # This is a piece of repeated code that is abstracted here for
207 # maintainability. RMB
210 my($configpmdir, $configpmtest) = @_;
211 if (-w $configpmtest) {
212 return $configpmtest;
213 } elsif (-w $configpmdir) {
214 #_#_# following code dumped core on me with 5.003_11, a.k.
215 my $configpm_bak = "$configpmtest.bak";
216 unlink $configpm_bak if -f $configpm_bak;
217 if( -f $configpmtest ) {
218 if( rename $configpmtest, $configpm_bak ) {
219 $CPAN::Frontend->mywarn(<<END);
220 Old configuration file $configpmtest
221 moved to $configpm_bak
225 my $fh = FileHandle->new;
226 if ($fh->open(">$configpmtest")) {
228 return $configpmtest;
230 # Should never happen
231 Carp::confess("Cannot open >$configpmtest");
237 my($self, %args) = @_;
238 $CPAN::Be_Silent++ if $args{be_silent};
242 eval {require CPAN::Config;}; # We eval because of some
244 unless ($dot_cpan++){
245 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
246 eval {require CPAN::MyConfig;}; # where you can override
247 # system wide settings
250 return unless @miss = $self->missing_config_data;
252 require CPAN::FirstTime;
253 my($configpm,$fh,$redo,$theycalled);
255 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
256 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
257 $configpm = $INC{"CPAN/Config.pm"};
259 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
260 $configpm = $INC{"CPAN/MyConfig.pm"};
263 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
264 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
265 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
266 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
267 $configpm = _configpmtest($configpmdir,$configpmtest);
270 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
271 File::Path::mkpath($configpmdir);
272 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
273 $configpm = _configpmtest($configpmdir,$configpmtest);
275 my $text = qq{WARNING: CPAN.pm is unable to } .
276 qq{create a configuration file.};
277 output($text, 'confess');
282 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
283 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
284 the following indispensable but missing parameters:
288 $CPAN::Frontend->myprint(qq{
289 $configpm initialized.
293 CPAN::FirstTime::init($configpm, %args);
296 sub missing_config_data {
307 "inhibit_startup_message",
314 "mbuild_install_arg",
315 "mbuild_install_build_command",
319 "prerequisites_policy",
325 push @miss, $_ unless defined $CPAN::Config->{$_};
331 delete $INC{'CPAN/MyConfig.pm'};
332 delete $INC{'CPAN/Config.pm'};
336 $CPAN::Frontend->myprint(q[
338 defaults reload default config values from disk
339 commit commit session changes to disk
340 init go through a dialog to set all parameters
342 You may edit key values in the follow fashion (the "o" is a literal
345 o conf build_cache 15
347 o conf build_dir "/foo/bar"
351 o conf urllist unshift ftp://ftp.foo.bar/
354 undef; #don't reprint CPAN::Config
358 my($word,$line,$pos) = @_;
360 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
361 my(@words) = split " ", substr($line,0,$pos+1);
366 $words[2] =~ /list$/ && @words == 3
368 $words[2] =~ /list$/ && @words == 4 && length($word)
371 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
372 } elsif (@words >= 4) {
376 my(@o_conf) = sort grep { !$seen{$_}++ }
380 return grep /^\Q$word\E/, @o_conf;
388 # cperl-indent-level: 2