Upgrade to CPAN-1.83_59
[p5sagit/p5-mst-13.2.git] / lib / CPAN / HandleConfig.pm
CommitLineData
e82b9348 1package CPAN::HandleConfig;
2use strict;
1a43333d 3use vars qw(%can %keys $dot_cpan $VERSION);
4
ca79d794 5$VERSION = sprintf "%.2f", substr(q$Rev: 469 $,4)/100;
e82b9348 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
9ddc4ed0 15 cache_metadata commandnumber_in_prompt cpan_home curl
e82b9348 16 dontload_hash
ca79d794 17 ftp ftp_passive ftp_proxy
e82b9348 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
34sub 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;
9ddc4ed0 40 $DB::single = 1;
e82b9348 41 if($can{$o}) {
9ddc4ed0 42 $self->$o(args => \@args);
e82b9348 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;
ca79d794 82 } elsif ($o =~ /_hash$/) {
83 push @args, "" if @args % 2;
84 $CPAN::Config->{$o} = { @args };
85 } else {
e82b9348 86 $CPAN::Config->{$o} = $args[0] if defined $args[0];
87 $self->prettyprint($o);
88 }
89 }
90}
91
92sub prettyprint {
93 my($self,$k) = @_;
94 my $v = $CPAN::Config->{$k};
95 if (ref $v) {
9ddc4ed0 96 my(@report);
97 if (ref $v eq "ARRAY") {
98 @report = map {"\t[$_]\n"} @$v;
99 } else {
100 @report = map { sprintf("\t%-18s => %s\n",
101 map { "[$_]" } $_,
102 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
103 )} keys %$v;
104 }
e82b9348 105 $CPAN::Frontend->myprint(
106 join(
107 "",
108 sprintf(
109 " %-18s\n",
110 $k
111 ),
9ddc4ed0 112 @report
e82b9348 113 )
114 );
115 } elsif (defined $v) {
116 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
117 } else {
118 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
119 }
120}
121
122sub commit {
9ddc4ed0 123 my($self,@args) = @_;
124 my $configpm;
125 if (@args) {
126 if ($args[0] eq "args") {
127 # we have not signed that contract
128 } else {
129 $configpm = $args[0];
130 }
131 }
e82b9348 132 unless (defined $configpm){
133 $configpm ||= $INC{"CPAN/MyConfig.pm"};
134 $configpm ||= $INC{"CPAN/Config.pm"};
135 $configpm || Carp::confess(q{
136CPAN::Config::commit called without an argument.
137Please specify a filename where to save the configuration or try
138"o conf init" to have an interactive course through configing.
139});
140 }
141 my($mode);
142 if (-f $configpm) {
143 $mode = (stat $configpm)[2];
144 if ($mode && ! -w _) {
145 Carp::confess("$configpm is not writable");
146 }
147 }
148
149 my $msg;
150 $msg = <<EOF unless $configpm =~ /MyConfig/;
151
152# This is CPAN.pm's systemwide configuration file. This file provides
153# defaults for users, and the values can be changed in a per-user
154# configuration file. The user-config file is being looked for as
155# ~/.cpan/CPAN/MyConfig.pm.
156
157EOF
158 $msg ||= "\n";
159 my($fh) = FileHandle->new;
160 rename $configpm, "$configpm~" if -f $configpm;
161 open $fh, ">$configpm" or
162 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
163 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
164 foreach (sort keys %$CPAN::Config) {
165 $fh->print(
166 " '$_' => ",
ca79d794 167 $self->neatvalue($CPAN::Config->{$_}),
e82b9348 168 ",\n"
169 );
170 }
171
172 $fh->print("};\n1;\n__END__\n");
173 close $fh;
174
175 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
176 #chmod $mode, $configpm;
177###why was that so? $self->defaults;
9ddc4ed0 178 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
e82b9348 179 1;
180}
181
ca79d794 182# stolen from MakeMaker; not taking the original because it is buggy;
183# bugreport will have to say: keys of hashes remain unquoted and can
184# produce syntax errors
185sub neatvalue {
186 my($self, $v) = @_;
187 return "undef" unless defined $v;
188 my($t) = ref $v;
189 return "q[$v]" unless $t;
190 if ($t eq 'ARRAY') {
191 my(@m, @neat);
192 push @m, "[";
193 foreach my $elem (@$v) {
194 push @neat, "q[$elem]";
195 }
196 push @m, join ", ", @neat;
197 push @m, "]";
198 return join "", @m;
199 }
200 return "$v" unless $t eq 'HASH';
201 my(@m, $key, $val);
202 while (($key,$val) = each %$v){
203 last unless defined $key; # cautious programming in case (undef,undef) is true
204 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
205 }
206 return "{ ".join(', ',@m)." }";
207}
208
e82b9348 209sub defaults {
210 my($self) = @_;
c9869e1c 211 my $done;
212 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
213 CPAN::Shell->reload_this($config) and $done++;
214 last if $done;
215 }
e82b9348 216 1;
217}
218
219sub init {
9ddc4ed0 220 my($self,@args) = @_;
e82b9348 221 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
222 # have the least
223 # important
224 # variable
225 # undefined
9ddc4ed0 226 $self->load(@args);
e82b9348 227 1;
228}
229
230# This is a piece of repeated code that is abstracted here for
231# maintainability. RMB
232#
233sub _configpmtest {
234 my($configpmdir, $configpmtest) = @_;
235 if (-w $configpmtest) {
236 return $configpmtest;
237 } elsif (-w $configpmdir) {
238 #_#_# following code dumped core on me with 5.003_11, a.k.
239 my $configpm_bak = "$configpmtest.bak";
240 unlink $configpm_bak if -f $configpm_bak;
241 if( -f $configpmtest ) {
242 if( rename $configpmtest, $configpm_bak ) {
243 $CPAN::Frontend->mywarn(<<END);
244Old configuration file $configpmtest
245 moved to $configpm_bak
246END
247 }
248 }
249 my $fh = FileHandle->new;
250 if ($fh->open(">$configpmtest")) {
251 $fh->print("1;\n");
252 return $configpmtest;
253 } else {
254 # Should never happen
255 Carp::confess("Cannot open >$configpmtest");
256 }
257 } else { return }
258}
259
260sub load {
261 my($self, %args) = @_;
262 $CPAN::Be_Silent++ if $args{be_silent};
263
264 my(@miss);
265 use Carp;
9ddc4ed0 266 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
267 eval {require CPAN::Config;}; # not everybody has one
268 }
e82b9348 269 unless ($dot_cpan++){
270 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
9ddc4ed0 271 eval {require CPAN::MyConfig;}; # override system wide settings
e82b9348 272 shift @INC;
273 }
274 return unless @miss = $self->missing_config_data;
275
276 require CPAN::FirstTime;
277 my($configpm,$fh,$redo,$theycalled);
278 $redo ||= "";
279 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
280 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
281 $configpm = $INC{"CPAN/Config.pm"};
282 $redo++;
283 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
284 $configpm = $INC{"CPAN/MyConfig.pm"};
285 $redo++;
286 } else {
287 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
288 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
289 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
c9869e1c 290 my $inc_key;
e82b9348 291 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
c9869e1c 292 $configpm = _configpmtest($configpmdir,$configpmtest);
293 $inc_key = "CPAN/Config.pm";
e82b9348 294 }
295 unless ($configpm) {
296 $configpmdir = File::Spec->catdir($ENV{HOME},".cpan","CPAN");
297 File::Path::mkpath($configpmdir);
298 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
c9869e1c 299 $configpm = _configpmtest($configpmdir,$configpmtest);
300 $inc_key = "CPAN/MyConfig.pm";
e82b9348 301 }
c9869e1c 302 if ($configpm) {
303 $INC{$inc_key} = $configpm;
304 } else {
305 my $text = qq{WARNING: CPAN.pm is unable to } .
306 qq{create a configuration file.};
307 output($text, 'confess');
308 }
309
e82b9348 310 }
311 local($") = ", ";
312 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
0cf35e6a 313Sorry, we have to rerun the configuration dialog for CPAN.pm due to
314the following indispensable but missing parameters:
e82b9348 315
316@miss
317END
318 $CPAN::Frontend->myprint(qq{
319$configpm initialized.
320});
321
322 sleep 2;
323 CPAN::FirstTime::init($configpm, %args);
324}
325
326sub missing_config_data {
327 my(@miss);
328 for (
0cf35e6a 329 "build_cache",
330 "build_dir",
331 "cache_metadata",
332 "cpan_home",
333 "ftp_proxy",
334 "gzip",
335 "http_proxy",
336 "index_expire",
337 "inhibit_startup_message",
338 "keep_source_where",
339 "make",
340 "make_arg",
341 "make_install_arg",
342 "makepl_arg",
343 "mbuild_arg",
344 "mbuild_install_arg",
345 "mbuild_install_build_command",
346 "mbuildpl_arg",
347 "no_proxy",
e82b9348 348 "pager",
e82b9348 349 "prerequisites_policy",
0cf35e6a 350 "scan_cache",
351 "tar",
352 "unzip",
353 "urllist",
e82b9348 354 ) {
355 push @miss, $_ unless defined $CPAN::Config->{$_};
356 }
357 return @miss;
358}
359
e82b9348 360sub help {
361 $CPAN::Frontend->myprint(q[
362Known options:
363 defaults reload default config values from disk
364 commit commit session changes to disk
365 init go through a dialog to set all parameters
366
367You may edit key values in the follow fashion (the "o" is a literal
368letter o):
369
370 o conf build_cache 15
371
372 o conf build_dir "/foo/bar"
373
374 o conf urllist shift
375
376 o conf urllist unshift ftp://ftp.foo.bar/
377
378]);
379 undef; #don't reprint CPAN::Config
380}
381
382sub cpl {
383 my($word,$line,$pos) = @_;
384 $word ||= "";
385 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
386 my(@words) = split " ", substr($line,0,$pos+1);
387 if (
388 defined($words[2])
389 and
390 (
391 $words[2] =~ /list$/ && @words == 3
392 ||
393 $words[2] =~ /list$/ && @words == 4 && length($word)
394 )
395 ) {
396 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
397 } elsif (@words >= 4) {
398 return ();
399 }
400 my %seen;
401 my(@o_conf) = sort grep { !$seen{$_}++ }
402 keys %can,
403 keys %$CPAN::Config,
404 keys %keys;
405 return grep /^\Q$word\E/, @o_conf;
406}
407
9ddc4ed0 408
409package ####::###### #hide from indexer
410 CPAN::Config;
411# note: J. Nick Koston wrote me that they are using
412# CPAN::Config->commit although undocumented. I suggested
413# CPAN::Shell->o("conf","commit") even when ugly it is at least
414# documented
415
416# that's why I added the CPAN::Config class with autoload and
417# deprecated warning
418
419use strict;
420use vars qw($AUTOLOAD $VERSION);
ca79d794 421$VERSION = sprintf "%.2f", substr(q$Rev: 469 $,4)/100;
9ddc4ed0 422
423# formerly CPAN::HandleConfig was known as CPAN::Config
424sub AUTOLOAD {
425 my($l) = $AUTOLOAD;
426 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
427 $l =~ s/.*:://;
428 CPAN::HandleConfig->$l(@_);
429}
430
e82b9348 4311;
0cf35e6a 432
433__END__
434# Local Variables:
435# mode: cperl
ca79d794 436# cperl-indent-level: 4
0cf35e6a 437# End: