1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $dot_cpan);
6 'commit' => "Commit changes to disk",
7 'defaults' => "Reload defaults from disk",
8 'init' => "Interactive setting of all options",
11 %keys = map { $_ => undef } qw(
12 build_cache build_dir bzip2
13 cache_metadata cpan_home curl
17 histfile histsize http_proxy
18 inactivity_timeout index_expire inhibit_startup_message
21 make make_arg make_install_arg make_install_make_command makepl_arg
22 mbuild_arg mbuild_install_arg mbuild_install_build_command mbuildpl_arg
23 ncftp ncftpget no_proxy pager
24 prefer_installer prerequisites_policy
25 scan_cache shell show_upload_date
31 # returns true on successful action
35 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
36 my($o,$str,$func,$args,$key_exists);
42 CPAN->debug("o[$o]") if $CPAN::DEBUG;
43 unless (exists $keys{$o}) {
44 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
49 CPAN->debug("func[$func]") if $CPAN::DEBUG;
51 # Let's avoid eval, it's easier to comprehend without.
52 if ($func eq "push") {
53 push @{$CPAN::Config->{$o}}, @args;
55 } elsif ($func eq "pop") {
56 pop @{$CPAN::Config->{$o}};
58 } elsif ($func eq "shift") {
59 shift @{$CPAN::Config->{$o}};
61 } elsif ($func eq "unshift") {
62 unshift @{$CPAN::Config->{$o}}, @args;
64 } elsif ($func eq "splice") {
65 splice @{$CPAN::Config->{$o}}, @args;
68 $CPAN::Config->{$o} = [@args];
71 $self->prettyprint($o);
73 if ($o eq "urllist" && $changed) {
74 # reset the cached values
75 undef $CPAN::FTP::Thesite;
76 undef $CPAN::FTP::Themethod;
80 $CPAN::Config->{$o} = $args[0] if defined $args[0];
81 $self->prettyprint($o);
88 my $v = $CPAN::Config->{$k};
90 my(@report) = ref $v eq "ARRAY" ?
92 map { sprintf(" %-18s => [%s]\n",
94 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
96 $CPAN::Frontend->myprint(
103 map {"\t[$_]\n"} @report
106 } elsif (defined $v) {
107 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
109 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
114 my($self,$configpm) = @_;
115 unless (defined $configpm){
116 $configpm ||= $INC{"CPAN/MyConfig.pm"};
117 $configpm ||= $INC{"CPAN/Config.pm"};
118 $configpm || Carp::confess(q{
119 CPAN::Config::commit called without an argument.
120 Please specify a filename where to save the configuration or try
121 "o conf init" to have an interactive course through configing.
126 $mode = (stat $configpm)[2];
127 if ($mode && ! -w _) {
128 Carp::confess("$configpm is not writable");
133 $msg = <<EOF unless $configpm =~ /MyConfig/;
135 # This is CPAN.pm's systemwide configuration file. This file provides
136 # defaults for users, and the values can be changed in a per-user
137 # configuration file. The user-config file is being looked for as
138 # ~/.cpan/CPAN/MyConfig.pm.
142 my($fh) = FileHandle->new;
143 rename $configpm, "$configpm~" if -f $configpm;
144 open $fh, ">$configpm" or
145 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
146 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
147 foreach (sort keys %$CPAN::Config) {
150 ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
155 $fh->print("};\n1;\n__END__\n");
158 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
159 #chmod $mode, $configpm;
160 ###why was that so? $self->defaults;
161 $CPAN::Frontend->myprint("commit: wrote $configpm\n");
165 *default = \&defaults;
175 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
184 # This is a piece of repeated code that is abstracted here for
185 # maintainability. RMB
188 my($configpmdir, $configpmtest) = @_;
189 if (-w $configpmtest) {
190 return $configpmtest;
191 } elsif (-w $configpmdir) {
192 #_#_# following code dumped core on me with 5.003_11, a.k.
193 my $configpm_bak = "$configpmtest.bak";
194 unlink $configpm_bak if -f $configpm_bak;
195 if( -f $configpmtest ) {
196 if( rename $configpmtest, $configpm_bak ) {
197 $CPAN::Frontend->mywarn(<<END);
198 Old configuration file $configpmtest
199 moved to $configpm_bak
203 my $fh = FileHandle->new;
204 if ($fh->open(">$configpmtest")) {
206 return $configpmtest;
208 # Should never happen
209 Carp::confess("Cannot open >$configpmtest");
215 my($self, %args) = @_;
216 $CPAN::Be_Silent++ if $args{be_silent};
220 eval {require CPAN::Config;}; # We eval because of some
222 unless ($dot_cpan++){
223 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
224 eval {require CPAN::MyConfig;}; # where you can override
225 # system wide settings
228 return unless @miss = $self->missing_config_data;
230 require CPAN::FirstTime;
231 my($configpm,$fh,$redo,$theycalled);
233 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
234 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
235 $configpm = $INC{"CPAN/Config.pm"};
237 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
238 $configpm = $INC{"CPAN/MyConfig.pm"};
241 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
242 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
243 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
244 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
245 $configpm = _configpmtest($configpmdir,$configpmtest);
248 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
249 File::Path::mkpath($configpmdir);
250 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
251 $configpm = _configpmtest($configpmdir,$configpmtest);
253 my $text = qq{WARNING: CPAN.pm is unable to } .
254 qq{create a configuration file.};
255 output($text, 'confess');
260 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
261 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
262 the following indispensable but missing parameters:
266 $CPAN::Frontend->myprint(qq{
267 $configpm initialized.
271 CPAN::FirstTime::init($configpm, %args);
274 sub missing_config_data {
285 "inhibit_startup_message",
292 "mbuild_install_arg",
293 "mbuild_install_build_command",
297 "prerequisites_policy",
303 push @miss, $_ unless defined $CPAN::Config->{$_};
309 delete $INC{'CPAN/MyConfig.pm'};
310 delete $INC{'CPAN/Config.pm'};
314 $CPAN::Frontend->myprint(q[
316 defaults reload default config values from disk
317 commit commit session changes to disk
318 init go through a dialog to set all parameters
320 You may edit key values in the follow fashion (the "o" is a literal
323 o conf build_cache 15
325 o conf build_dir "/foo/bar"
329 o conf urllist unshift ftp://ftp.foo.bar/
332 undef; #don't reprint CPAN::Config
336 my($word,$line,$pos) = @_;
338 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
339 my(@words) = split " ", substr($line,0,$pos+1);
344 $words[2] =~ /list$/ && @words == 3
346 $words[2] =~ /list$/ && @words == 4 && length($word)
349 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
350 } elsif (@words >= 4) {
354 my(@o_conf) = sort grep { !$seen{$_}++ }
358 return grep /^\Q$word\E/, @o_conf;
366 # cperl-indent-level: 2