[PAUSE] CPAN Upload: A/AN/ANDK/CPAN-1.83_55.tar.gz
[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
9ddc4ed0 5$VERSION = sprintf "%.2f", substr(q$Rev: 423 $,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
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
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;
82 } else {
83 $CPAN::Config->{$o} = $args[0] if defined $args[0];
84 $self->prettyprint($o);
85 }
86 }
87}
88
89sub prettyprint {
90 my($self,$k) = @_;
91 my $v = $CPAN::Config->{$k};
92 if (ref $v) {
9ddc4ed0 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 }
e82b9348 102 $CPAN::Frontend->myprint(
103 join(
104 "",
105 sprintf(
106 " %-18s\n",
107 $k
108 ),
9ddc4ed0 109 @report
e82b9348 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
119sub commit {
9ddc4ed0 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 }
e82b9348 129 unless (defined $configpm){
130 $configpm ||= $INC{"CPAN/MyConfig.pm"};
131 $configpm ||= $INC{"CPAN/Config.pm"};
132 $configpm || Carp::confess(q{
133CPAN::Config::commit called without an argument.
134Please 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
154EOF
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;
9ddc4ed0 175 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
e82b9348 176 1;
177}
178
179*default = \&defaults;
180sub defaults {
181 my($self) = @_;
182 $self->unload;
183 $self->load;
184 1;
185}
186
187sub init {
9ddc4ed0 188 my($self,@args) = @_;
e82b9348 189 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
190 # have the least
191 # important
192 # variable
193 # undefined
9ddc4ed0 194 $self->load(@args);
e82b9348 195 1;
196}
197
198# This is a piece of repeated code that is abstracted here for
199# maintainability. RMB
200#
201sub _configpmtest {
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);
212Old configuration file $configpmtest
213 moved to $configpm_bak
214END
215 }
216 }
217 my $fh = FileHandle->new;
218 if ($fh->open(">$configpmtest")) {
219 $fh->print("1;\n");
220 return $configpmtest;
221 } else {
222 # Should never happen
223 Carp::confess("Cannot open >$configpmtest");
224 }
225 } else { return }
226}
227
228sub load {
229 my($self, %args) = @_;
230 $CPAN::Be_Silent++ if $args{be_silent};
231
232 my(@miss);
233 use Carp;
9ddc4ed0 234 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
235 eval {require CPAN::Config;}; # not everybody has one
236 }
e82b9348 237 unless ($dot_cpan++){
238 unshift @INC, File::Spec->catdir($ENV{HOME},".cpan");
9ddc4ed0 239 eval {require CPAN::MyConfig;}; # override system wide settings
e82b9348 240 shift @INC;
241 }
242 return unless @miss = $self->missing_config_data;
243
244 require CPAN::FirstTime;
245 my($configpm,$fh,$redo,$theycalled);
246 $redo ||= "";
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"};
250 $redo++;
251 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
252 $configpm = $INC{"CPAN/MyConfig.pm"};
253 $redo++;
254 } else {
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);
260 }
261 unless ($configpm) {
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);
266 unless ($configpm) {
267 my $text = qq{WARNING: CPAN.pm is unable to } .
268 qq{create a configuration file.};
269 output($text, 'confess');
270 }
271 }
272 }
273 local($") = ", ";
274 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
0cf35e6a 275Sorry, we have to rerun the configuration dialog for CPAN.pm due to
276the following indispensable but missing parameters:
e82b9348 277
278@miss
279END
280 $CPAN::Frontend->myprint(qq{
281$configpm initialized.
282});
283
284 sleep 2;
285 CPAN::FirstTime::init($configpm, %args);
286}
287
288sub missing_config_data {
289 my(@miss);
290 for (
0cf35e6a 291 "build_cache",
292 "build_dir",
293 "cache_metadata",
294 "cpan_home",
295 "ftp_proxy",
296 "gzip",
297 "http_proxy",
298 "index_expire",
299 "inhibit_startup_message",
300 "keep_source_where",
301 "make",
302 "make_arg",
303 "make_install_arg",
304 "makepl_arg",
305 "mbuild_arg",
306 "mbuild_install_arg",
307 "mbuild_install_build_command",
308 "mbuildpl_arg",
309 "no_proxy",
e82b9348 310 "pager",
e82b9348 311 "prerequisites_policy",
0cf35e6a 312 "scan_cache",
313 "tar",
314 "unzip",
315 "urllist",
e82b9348 316 ) {
317 push @miss, $_ unless defined $CPAN::Config->{$_};
318 }
319 return @miss;
320}
321
322sub unload {
323 delete $INC{'CPAN/MyConfig.pm'};
324 delete $INC{'CPAN/Config.pm'};
325}
326
327sub help {
328 $CPAN::Frontend->myprint(q[
329Known options:
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
333
334You may edit key values in the follow fashion (the "o" is a literal
335letter o):
336
337 o conf build_cache 15
338
339 o conf build_dir "/foo/bar"
340
341 o conf urllist shift
342
343 o conf urllist unshift ftp://ftp.foo.bar/
344
345]);
346 undef; #don't reprint CPAN::Config
347}
348
349sub cpl {
350 my($word,$line,$pos) = @_;
351 $word ||= "";
352 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
353 my(@words) = split " ", substr($line,0,$pos+1);
354 if (
355 defined($words[2])
356 and
357 (
358 $words[2] =~ /list$/ && @words == 3
359 ||
360 $words[2] =~ /list$/ && @words == 4 && length($word)
361 )
362 ) {
363 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
364 } elsif (@words >= 4) {
365 return ();
366 }
367 my %seen;
368 my(@o_conf) = sort grep { !$seen{$_}++ }
369 keys %can,
370 keys %$CPAN::Config,
371 keys %keys;
372 return grep /^\Q$word\E/, @o_conf;
373}
374
9ddc4ed0 375
376package ####::###### #hide from indexer
377 CPAN::Config;
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
381# documented
382
383# that's why I added the CPAN::Config class with autoload and
384# deprecated warning
385
386use strict;
387use vars qw($AUTOLOAD $VERSION);
388$VERSION = sprintf "%.2f", substr(q$Rev: 423 $,4)/100;
389
390# formerly CPAN::HandleConfig was known as CPAN::Config
391sub AUTOLOAD {
392 my($l) = $AUTOLOAD;
393 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
394 $l =~ s/.*:://;
395 CPAN::HandleConfig->$l(@_);
396}
397
e82b9348 3981;
0cf35e6a 399
400__END__
401# Local Variables:
402# mode: cperl
403# cperl-indent-level: 2
404# End: