Upgrade to CPAN-1.83_58
[p5sagit/p5-mst-13.2.git] / lib / CPAN / HandleConfig.pm
1 package CPAN::HandleConfig;
2 use strict;
3 use vars qw(%can %keys $dot_cpan $VERSION);
4
5 $VERSION = sprintf "%.2f", substr(q$Rev: 431 $,4)/100;
6
7 %can = (
8   'commit' => "Commit changes to disk",
9   'defaults' => "Reload defaults from disk",
10   'init'   => "Interactive setting of all options",
11 );
12
13 %keys = map { $_ => undef } qw(
14     build_cache build_dir bzip2
15     cache_metadata commandnumber_in_prompt cpan_home curl
16     dontload_hash
17     ftp ftp_proxy
18     getcwd gpg gzip
19     histfile histsize http_proxy
20     inactivity_timeout index_expire inhibit_startup_message
21     keep_source_where
22     lynx
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
28     tar term_is_latin
29     unzip urllist
30     wait_list wget
31 );
32
33 # returns true on successful action
34 sub edit {
35     my($self,@args) = @_;
36     return unless @args;
37     CPAN->debug("self[$self]args[".join(" | ",@args)."]");
38     my($o,$str,$func,$args,$key_exists);
39     $o = shift @args;
40     $DB::single = 1;
41     if($can{$o}) {
42         $self->$o(args => \@args);
43         return 1;
44     } else {
45         CPAN->debug("o[$o]") if $CPAN::DEBUG;
46         unless (exists $keys{$o}) {
47             $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
48         }
49         if ($o =~ /list$/) {
50             $func = shift @args;
51             $func ||= "";
52             CPAN->debug("func[$func]") if $CPAN::DEBUG;
53             my $changed;
54             # Let's avoid eval, it's easier to comprehend without.
55             if ($func eq "push") {
56                 push @{$CPAN::Config->{$o}}, @args;
57                 $changed = 1;
58             } elsif ($func eq "pop") {
59                 pop @{$CPAN::Config->{$o}};
60                 $changed = 1;
61             } elsif ($func eq "shift") {
62                 shift @{$CPAN::Config->{$o}};
63                 $changed = 1;
64             } elsif ($func eq "unshift") {
65                 unshift @{$CPAN::Config->{$o}}, @args;
66                 $changed = 1;
67             } elsif ($func eq "splice") {
68                 splice @{$CPAN::Config->{$o}}, @args;
69                 $changed = 1;
70             } elsif (@args) {
71                 $CPAN::Config->{$o} = [@args];
72                 $changed = 1;
73             } else {
74                 $self->prettyprint($o);
75             }
76             if ($o eq "urllist" && $changed) {
77                 # reset the cached values
78                 undef $CPAN::FTP::Thesite;
79                 undef $CPAN::FTP::Themethod;
80             }
81             return $changed;
82         } else {
83             $CPAN::Config->{$o} = $args[0] if defined $args[0];
84             $self->prettyprint($o);
85         }
86     }
87 }
88
89 sub prettyprint {
90   my($self,$k) = @_;
91   my $v = $CPAN::Config->{$k};
92   if (ref $v) {
93     my(@report);
94     if (ref $v eq "ARRAY") {
95       @report = map {"\t[$_]\n"} @$v;
96     } else {
97       @report = map { sprintf("\t%-18s => %s\n",
98                               map { "[$_]" } $_,
99                               defined $v->{$_} ? $v->{$_} : "UNDEFINED"
100                              )} keys %$v;
101     }
102     $CPAN::Frontend->myprint(
103                              join(
104                                   "",
105                                   sprintf(
106                                           "    %-18s\n",
107                                           $k
108                                          ),
109                                   @report
110                                  )
111                             );
112   } elsif (defined $v) {
113     $CPAN::Frontend->myprint(sprintf "    %-18s [%s]\n", $k, $v);
114   } else {
115     $CPAN::Frontend->myprint(sprintf "    %-18s [%s]\n", $k, "UNDEFINED");
116   }
117 }
118
119 sub commit {
120     my($self,@args) = @_;
121     my $configpm;
122     if (@args) {
123       if ($args[0] eq "args") {
124         # we have not signed that contract
125       } else {
126         $configpm = $args[0];
127       }
128     }
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.
136 });
137     }
138     my($mode);
139     if (-f $configpm) {
140         $mode = (stat $configpm)[2];
141         if ($mode && ! -w _) {
142             Carp::confess("$configpm is not writable");
143         }
144     }
145
146     my $msg;
147     $msg = <<EOF unless $configpm =~ /MyConfig/;
148
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.
153
154 EOF
155     $msg ||= "\n";
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) {
162         $fh->print(
163                    "  '$_' => ",
164                    ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
165                    ",\n"
166                   );
167     }
168
169     $fh->print("};\n1;\n__END__\n");
170     close $fh;
171
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");
176     1;
177 }
178
179 *default = \&defaults;
180 sub defaults {
181     my($self) = @_;
182     my $done;
183     for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
184       CPAN::Shell->reload_this($config) and $done++;
185       last if $done;
186     }
187     1;
188 }
189
190 sub init {
191     my($self,@args) = @_;
192     undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
193                                                       # have the least
194                                                       # important
195                                                       # variable
196                                                       # undefined
197     $self->load(@args);
198     1;
199 }
200
201 # This is a piece of repeated code that is abstracted here for
202 # maintainability.  RMB
203 #
204 sub _configpmtest {
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
217 END
218             }
219         }
220         my $fh = FileHandle->new;
221         if ($fh->open(">$configpmtest")) {
222             $fh->print("1;\n");
223             return $configpmtest;
224         } else {
225             # Should never happen
226             Carp::confess("Cannot open >$configpmtest");
227         }
228     } else { return }
229 }
230
231 sub load {
232     my($self, %args) = @_;
233         $CPAN::Be_Silent++ if $args{be_silent};
234
235     my(@miss);
236     use Carp;
237     unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
238       eval {require CPAN::Config;}; # not everybody has one
239     }
240     unless ($dot_cpan++){
241       unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
242       eval {require CPAN::MyConfig;}; # override system wide settings
243       shift @INC;
244     }
245     return unless @miss = $self->missing_config_data;
246
247     require CPAN::FirstTime;
248     my($configpm,$fh,$redo,$theycalled);
249     $redo ||= "";
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"};
253         $redo++;
254     } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
255         $configpm = $INC{"CPAN/MyConfig.pm"};
256         $redo++;
257     } else {
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");
261         my $inc_key;
262         if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
263             $configpm = _configpmtest($configpmdir,$configpmtest);
264             $inc_key = "CPAN/Config.pm";
265         }
266         unless ($configpm) {
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";
272         }
273         if ($configpm) {
274           $INC{$inc_key} = $configpm;
275         } else {
276           my $text = qq{WARNING: CPAN.pm is unable to } .
277               qq{create a configuration file.};
278           output($text, 'confess');
279         }
280
281     }
282     local($") = ", ";
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:
286
287 @miss
288 END
289     $CPAN::Frontend->myprint(qq{
290 $configpm initialized.
291 });
292
293     sleep 2;
294     CPAN::FirstTime::init($configpm, %args);
295 }
296
297 sub missing_config_data {
298     my(@miss);
299     for (
300          "build_cache",
301          "build_dir",
302          "cache_metadata",
303          "cpan_home",
304          "ftp_proxy",
305          "gzip",
306          "http_proxy",
307          "index_expire",
308          "inhibit_startup_message",
309          "keep_source_where",
310          "make",
311          "make_arg",
312          "make_install_arg",
313          "makepl_arg",
314          "mbuild_arg",
315          "mbuild_install_arg",
316          "mbuild_install_build_command",
317          "mbuildpl_arg",
318          "no_proxy",
319          "pager",
320          "prerequisites_policy",
321          "scan_cache",
322          "tar",
323          "unzip",
324          "urllist",
325         ) {
326         push @miss, $_ unless defined $CPAN::Config->{$_};
327     }
328     return @miss;
329 }
330
331 sub help {
332     $CPAN::Frontend->myprint(q[
333 Known options:
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
337
338 You may edit key values in the follow fashion (the "o" is a literal
339 letter o):
340
341   o conf build_cache 15
342
343   o conf build_dir "/foo/bar"
344
345   o conf urllist shift
346
347   o conf urllist unshift ftp://ftp.foo.bar/
348
349 ]);
350     undef; #don't reprint CPAN::Config
351 }
352
353 sub cpl {
354     my($word,$line,$pos) = @_;
355     $word ||= "";
356     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
357     my(@words) = split " ", substr($line,0,$pos+1);
358     if (
359         defined($words[2])
360         and
361         (
362          $words[2] =~ /list$/ && @words == 3
363          ||
364          $words[2] =~ /list$/ && @words == 4 && length($word)
365         )
366        ) {
367         return grep /^\Q$word\E/, qw(splice shift unshift pop push);
368     } elsif (@words >= 4) {
369         return ();
370     }
371     my %seen;
372     my(@o_conf) =  sort grep { !$seen{$_}++ }
373         keys %can,
374             keys %$CPAN::Config,
375                 keys %keys;
376     return grep /^\Q$word\E/, @o_conf;
377 }
378
379
380 package ####::###### #hide from indexer
381     CPAN::Config;
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
385 # documented
386
387 # that's why I added the CPAN::Config class with autoload and
388 # deprecated warning
389
390 use strict;
391 use vars qw($AUTOLOAD $VERSION);
392 $VERSION = sprintf "%.2f", substr(q$Rev: 431 $,4)/100;
393
394 # formerly CPAN::HandleConfig was known as CPAN::Config
395 sub AUTOLOAD {
396   my($l) = $AUTOLOAD;
397   $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
398   $l =~ s/.*:://;
399   CPAN::HandleConfig->$l(@_);
400 }
401
402 1;
403
404 __END__
405 # Local Variables:
406 # mode: cperl
407 # cperl-indent-level: 2
408 # End: