Commit | Line | Data |
---|---|---|
e82b9348 SP |
1 | package CPAN::HandleConfig; |
2 | use strict; | |
547d3dfd | 3 | use vars qw(%can %keys $loading $VERSION); |
1a43333d | 4 | |
5254b38e | 5 | $VERSION = "5.5"; |
e82b9348 SP |
6 | |
7 | %can = ( | |
4d1321a7 A |
8 | commit => "Commit changes to disk", |
9 | defaults => "Reload defaults from disk", | |
10 | help => "Short help about 'o conf' usage", | |
11 | init => "Interactive setting of all options", | |
e82b9348 SP |
12 | ); |
13 | ||
1e8f9a0a SP |
14 | # Q: where is the "How do I add a new config option" HOWTO? |
15 | # A1: svn diff -r 757:758 # where dagolden added test_report | |
16 | # A2: svn diff -r 985:986 # where andk added yaml_module | |
5254b38e SP |
17 | # A3: 1. add new config option to %keys below |
18 | # 2. add a Pod description in CPAN::FirstTime; it should include a | |
19 | # prompt line; see others for examples | |
20 | # 3. add a "matcher" section in CPAN::FirstTime::init that includes | |
21 | # a prompt function; see others for examples | |
22 | # 4. add config option to documentation section in CPAN.pm | |
23 | ||
6658a91b SP |
24 | %keys = map { $_ => undef } |
25 | ( | |
b72dd56f SP |
26 | "applypatch", |
27 | "auto_commit", | |
6658a91b SP |
28 | "build_cache", |
29 | "build_dir", | |
05bab18e | 30 | "build_dir_reuse", |
6658a91b SP |
31 | "build_requires_install_policy", |
32 | "bzip2", | |
33 | "cache_metadata", | |
34 | "check_sigs", | |
b72dd56f | 35 | "colorize_debug", |
6658a91b SP |
36 | "colorize_output", |
37 | "colorize_print", | |
38 | "colorize_warn", | |
39 | "commandnumber_in_prompt", | |
40 | "commands_quote", | |
5254b38e | 41 | "connect_to_internet_ok", |
6658a91b SP |
42 | "cpan_home", |
43 | "curl", | |
44 | "dontload_hash", # deprecated after 1.83_68 (rev. 581) | |
45 | "dontload_list", | |
46 | "ftp", | |
47 | "ftp_passive", | |
48 | "ftp_proxy", | |
5254b38e SP |
49 | "ftpstats_size", |
50 | "ftpstats_period", | |
6658a91b SP |
51 | "getcwd", |
52 | "gpg", | |
53 | "gzip", | |
5254b38e | 54 | "halt_on_failure", |
6658a91b SP |
55 | "histfile", |
56 | "histsize", | |
57 | "http_proxy", | |
58 | "inactivity_timeout", | |
59 | "index_expire", | |
60 | "inhibit_startup_message", | |
61 | "keep_source_where", | |
547d3dfd | 62 | "load_module_verbosity", |
6658a91b SP |
63 | "lynx", |
64 | "make", | |
65 | "make_arg", | |
66 | "make_install_arg", | |
67 | "make_install_make_command", | |
68 | "makepl_arg", | |
69 | "mbuild_arg", | |
70 | "mbuild_install_arg", | |
71 | "mbuild_install_build_command", | |
72 | "mbuildpl_arg", | |
73 | "ncftp", | |
74 | "ncftpget", | |
75 | "no_proxy", | |
76 | "pager", | |
77 | "password", | |
78 | "patch", | |
5254b38e | 79 | "perl5lib_verbosity", |
6658a91b | 80 | "prefer_installer", |
6658a91b | 81 | "prefs_dir", |
547d3dfd | 82 | "prerequisites_policy", |
6658a91b SP |
83 | "proxy_pass", |
84 | "proxy_user", | |
05bab18e | 85 | "randomize_urllist", |
6658a91b SP |
86 | "scan_cache", |
87 | "shell", | |
547d3dfd | 88 | "show_unparsable_versions", |
6658a91b | 89 | "show_upload_date", |
547d3dfd | 90 | "show_zero_versions", |
6658a91b | 91 | "tar", |
547d3dfd | 92 | "tar_verbosity", |
6658a91b SP |
93 | "term_is_latin", |
94 | "term_ornaments", | |
95 | "test_report", | |
5254b38e | 96 | "trust_test_report_history", |
6658a91b SP |
97 | "unzip", |
98 | "urllist", | |
810a0276 | 99 | "use_sqlite", |
6658a91b SP |
100 | "username", |
101 | "wait_list", | |
102 | "wget", | |
547d3dfd | 103 | "yaml_load_code", |
6658a91b SP |
104 | "yaml_module", |
105 | ); | |
106 | ||
107 | my %prefssupport = map { $_ => 1 } | |
108 | ( | |
109 | "build_requires_install_policy", | |
be34b10d | 110 | "check_sigs", |
6658a91b SP |
111 | "make", |
112 | "make_install_make_command", | |
113 | "prefer_installer", | |
114 | "test_report", | |
115 | ); | |
116 | ||
e82b9348 SP |
117 | # returns true on successful action |
118 | sub edit { | |
119 | my($self,@args) = @_; | |
120 | return unless @args; | |
121 | CPAN->debug("self[$self]args[".join(" | ",@args)."]"); | |
122 | my($o,$str,$func,$args,$key_exists); | |
123 | $o = shift @args; | |
124 | if($can{$o}) { | |
547d3dfd SP |
125 | $self->$o(args => \@args); # o conf init => sub init => sub load |
126 | return 1; | |
e82b9348 SP |
127 | } else { |
128 | CPAN->debug("o[$o]") if $CPAN::DEBUG; | |
129 | unless (exists $keys{$o}) { | |
130 | $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n"); | |
131 | } | |
b72dd56f SP |
132 | my $changed; |
133 | ||
134 | ||
05bab18e SP |
135 | # one day I used randomize_urllist for a boolean, so we must |
136 | # list them explicitly --ak | |
547d3dfd | 137 | if (0) { |
b72dd56f SP |
138 | } elsif ($o =~ /^(wait_list|urllist|dontload_list)$/) { |
139 | ||
140 | # | |
141 | # ARRAYS | |
142 | # | |
143 | ||
547d3dfd SP |
144 | $func = shift @args; |
145 | $func ||= ""; | |
05bab18e | 146 | CPAN->debug("func[$func]args[@args]") if $CPAN::DEBUG; |
547d3dfd SP |
147 | # Let's avoid eval, it's easier to comprehend without. |
148 | if ($func eq "push") { | |
149 | push @{$CPAN::Config->{$o}}, @args; | |
e82b9348 | 150 | $changed = 1; |
547d3dfd SP |
151 | } elsif ($func eq "pop") { |
152 | pop @{$CPAN::Config->{$o}}; | |
e82b9348 | 153 | $changed = 1; |
547d3dfd SP |
154 | } elsif ($func eq "shift") { |
155 | shift @{$CPAN::Config->{$o}}; | |
e82b9348 | 156 | $changed = 1; |
547d3dfd SP |
157 | } elsif ($func eq "unshift") { |
158 | unshift @{$CPAN::Config->{$o}}, @args; | |
e82b9348 | 159 | $changed = 1; |
547d3dfd | 160 | } elsif ($func eq "splice") { |
05bab18e SP |
161 | my $offset = shift @args || 0; |
162 | my $length = shift @args || 0; | |
547d3dfd | 163 | splice @{$CPAN::Config->{$o}}, $offset, $length, @args; # may warn |
e82b9348 | 164 | $changed = 1; |
547d3dfd SP |
165 | } elsif ($func) { |
166 | $CPAN::Config->{$o} = [$func, @args]; | |
e82b9348 | 167 | $changed = 1; |
547d3dfd | 168 | } else { |
e82b9348 | 169 | $self->prettyprint($o); |
547d3dfd | 170 | } |
4d1321a7 A |
171 | if ($changed) { |
172 | if ($o eq "urllist") { | |
173 | # reset the cached values | |
174 | undef $CPAN::FTP::Thesite; | |
175 | undef $CPAN::FTP::Themethod; | |
f20de9f0 | 176 | $CPAN::Index::LAST_TIME = 0; |
4d1321a7 A |
177 | } elsif ($o eq "dontload_list") { |
178 | # empty it, it will be built up again | |
179 | $CPAN::META->{dontload_hash} = {}; | |
180 | } | |
e82b9348 | 181 | } |
ca79d794 | 182 | } elsif ($o =~ /_hash$/) { |
b72dd56f SP |
183 | |
184 | # | |
185 | # HASHES | |
186 | # | |
187 | ||
547d3dfd | 188 | if (@args==1 && $args[0] eq "") { |
6658a91b SP |
189 | @args = (); |
190 | } elsif (@args % 2) { | |
191 | push @args, ""; | |
192 | } | |
ca79d794 | 193 | $CPAN::Config->{$o} = { @args }; |
b72dd56f | 194 | $changed = 1; |
ca79d794 | 195 | } else { |
b72dd56f SP |
196 | |
197 | # | |
198 | # SCALARS | |
199 | # | |
200 | ||
547d3dfd | 201 | if (defined $args[0]) { |
6658a91b SP |
202 | $CPAN::CONFIG_DIRTY = 1; |
203 | $CPAN::Config->{$o} = $args[0]; | |
b72dd56f | 204 | $changed = 1; |
6658a91b | 205 | } |
547d3dfd | 206 | $self->prettyprint($o) |
7d97ad34 | 207 | if exists $keys{$o} or defined $CPAN::Config->{$o}; |
547d3dfd | 208 | } |
b72dd56f SP |
209 | if ($changed) { |
210 | if ($CPAN::Config->{auto_commit}) { | |
211 | $self->commit; | |
212 | } else { | |
213 | $CPAN::CONFIG_DIRTY = 1; | |
214 | $CPAN::Frontend->myprint("Please use 'o conf commit' to ". | |
215 | "make the config permanent!\n\n"); | |
216 | } | |
217 | } | |
e82b9348 SP |
218 | } |
219 | } | |
220 | ||
221 | sub prettyprint { | |
547d3dfd SP |
222 | my($self,$k) = @_; |
223 | my $v = $CPAN::Config->{$k}; | |
224 | if (ref $v) { | |
225 | my(@report); | |
226 | if (ref $v eq "ARRAY") { | |
227 | @report = map {"\t$_ \[$v->[$_]]\n"} 0..$#$v; | |
228 | } else { | |
229 | @report = map | |
230 | { | |
231 | sprintf "\t%-18s => %s\n", | |
232 | "[$_]", | |
233 | defined $v->{$_} ? "[$v->{$_}]" : "undef" | |
234 | } keys %$v; | |
235 | } | |
236 | $CPAN::Frontend->myprint( | |
237 | join( | |
238 | "", | |
239 | sprintf( | |
240 | " %-18s\n", | |
241 | $k | |
242 | ), | |
243 | @report | |
244 | ) | |
245 | ); | |
246 | } elsif (defined $v) { | |
247 | $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v); | |
9ddc4ed0 | 248 | } else { |
547d3dfd | 249 | $CPAN::Frontend->myprint(sprintf " %-18s undef\n", $k); |
9ddc4ed0 | 250 | } |
e82b9348 SP |
251 | } |
252 | ||
253 | sub commit { | |
9ddc4ed0 | 254 | my($self,@args) = @_; |
6658a91b | 255 | CPAN->debug("args[@args]") if $CPAN::DEBUG; |
05bab18e SP |
256 | if ($CPAN::RUN_DEGRADED) { |
257 | $CPAN::Frontend->mydie( | |
258 | "'o conf commit' disabled in ". | |
259 | "degraded mode. Maybe try\n". | |
260 | " !undef \$CPAN::RUN_DEGRADED\n" | |
261 | ); | |
262 | } | |
9ddc4ed0 A |
263 | my $configpm; |
264 | if (@args) { | |
265 | if ($args[0] eq "args") { | |
266 | # we have not signed that contract | |
267 | } else { | |
268 | $configpm = $args[0]; | |
269 | } | |
270 | } | |
547d3dfd SP |
271 | unless (defined $configpm) { |
272 | $configpm ||= $INC{"CPAN/MyConfig.pm"}; | |
273 | $configpm ||= $INC{"CPAN/Config.pm"}; | |
274 | $configpm || Carp::confess(q{ | |
e82b9348 SP |
275 | CPAN::Config::commit called without an argument. |
276 | Please specify a filename where to save the configuration or try | |
277 | "o conf init" to have an interactive course through configing. | |
278 | }); | |
279 | } | |
280 | my($mode); | |
281 | if (-f $configpm) { | |
547d3dfd SP |
282 | $mode = (stat $configpm)[2]; |
283 | if ($mode && ! -w _) { | |
284 | Carp::confess("$configpm is not writable"); | |
285 | } | |
e82b9348 SP |
286 | } |
287 | ||
288 | my $msg; | |
5254b38e | 289 | my $home = home(); |
e82b9348 SP |
290 | $msg = <<EOF unless $configpm =~ /MyConfig/; |
291 | ||
292 | # This is CPAN.pm's systemwide configuration file. This file provides | |
293 | # defaults for users, and the values can be changed in a per-user | |
294 | # configuration file. The user-config file is being looked for as | |
5254b38e | 295 | # $home/.cpan/CPAN/MyConfig.pm. |
e82b9348 SP |
296 | |
297 | EOF | |
298 | $msg ||= "\n"; | |
299 | my($fh) = FileHandle->new; | |
300 | rename $configpm, "$configpm~" if -f $configpm; | |
301 | open $fh, ">$configpm" or | |
302 | $CPAN::Frontend->mydie("Couldn't open >$configpm: $!"); | |
303 | $fh->print(qq[$msg\$CPAN::Config = \{\n]); | |
304 | foreach (sort keys %$CPAN::Config) { | |
44d21104 | 305 | unless (exists $keys{$_}) { |
dc053c64 SP |
306 | # do not drop them: forward compatibility! |
307 | $CPAN::Frontend->mywarn("Unknown config variable '$_'\n"); | |
44d21104 A |
308 | next; |
309 | } | |
547d3dfd SP |
310 | $fh->print( |
311 | " '$_' => ", | |
312 | $self->neatvalue($CPAN::Config->{$_}), | |
313 | ",\n" | |
314 | ); | |
e82b9348 SP |
315 | } |
316 | ||
317 | $fh->print("};\n1;\n__END__\n"); | |
318 | close $fh; | |
319 | ||
320 | #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 ); | |
321 | #chmod $mode, $configpm; | |
322 | ###why was that so? $self->defaults; | |
9ddc4ed0 | 323 | $CPAN::Frontend->myprint("commit: wrote '$configpm'\n"); |
6658a91b | 324 | $CPAN::CONFIG_DIRTY = 0; |
e82b9348 SP |
325 | 1; |
326 | } | |
327 | ||
ca79d794 SP |
328 | # stolen from MakeMaker; not taking the original because it is buggy; |
329 | # bugreport will have to say: keys of hashes remain unquoted and can | |
330 | # produce syntax errors | |
331 | sub neatvalue { | |
332 | my($self, $v) = @_; | |
333 | return "undef" unless defined $v; | |
334 | my($t) = ref $v; | |
547d3dfd | 335 | unless ($t) { |
6658a91b SP |
336 | $v =~ s/\\/\\\\/g; |
337 | return "q[$v]"; | |
338 | } | |
ca79d794 SP |
339 | if ($t eq 'ARRAY') { |
340 | my(@m, @neat); | |
341 | push @m, "["; | |
342 | foreach my $elem (@$v) { | |
343 | push @neat, "q[$elem]"; | |
344 | } | |
345 | push @m, join ", ", @neat; | |
346 | push @m, "]"; | |
347 | return join "", @m; | |
348 | } | |
349 | return "$v" unless $t eq 'HASH'; | |
350 | my(@m, $key, $val); | |
547d3dfd | 351 | while (($key,$val) = each %$v) { |
ca79d794 SP |
352 | last unless defined $key; # cautious programming in case (undef,undef) is true |
353 | push(@m,"q[$key]=>".$self->neatvalue($val)) ; | |
354 | } | |
355 | return "{ ".join(', ',@m)." }"; | |
356 | } | |
357 | ||
e82b9348 SP |
358 | sub defaults { |
359 | my($self) = @_; | |
05bab18e SP |
360 | if ($CPAN::RUN_DEGRADED) { |
361 | $CPAN::Frontend->mydie( | |
362 | "'o conf defaults' disabled in ". | |
363 | "degraded mode. Maybe try\n". | |
364 | " !undef \$CPAN::RUN_DEGRADED\n" | |
365 | ); | |
366 | } | |
c9869e1c SP |
367 | my $done; |
368 | for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) { | |
26844e27 | 369 | if ($INC{$config}) { |
6658a91b | 370 | CPAN->debug("INC{'$config'}[$INC{$config}]") if $CPAN::DEBUG; |
810a0276 | 371 | CPAN::Shell->_reload_this($config,{reloforce => 1}); |
26844e27 SP |
372 | $CPAN::Frontend->myprint("'$INC{$config}' reread\n"); |
373 | last; | |
374 | } | |
c9869e1c | 375 | } |
6658a91b | 376 | $CPAN::CONFIG_DIRTY = 0; |
e82b9348 SP |
377 | 1; |
378 | } | |
379 | ||
ed84aac9 A |
380 | =head2 C<< CLASS->safe_quote ITEM >> |
381 | ||
382 | Quotes an item to become safe against spaces | |
383 | in shell interpolation. An item is enclosed | |
384 | in double quotes if: | |
385 | ||
386 | - the item contains spaces in the middle | |
387 | - the item does not start with a quote | |
388 | ||
389 | This happens to avoid shell interpolation | |
390 | problems when whitespace is present in | |
391 | directory names. | |
392 | ||
393 | This method uses C<commands_quote> to determine | |
394 | the correct quote. If C<commands_quote> is | |
395 | a space, no quoting will take place. | |
396 | ||
397 | ||
398 | if it starts and ends with the same quote character: leave it as it is | |
399 | ||
400 | if it contains no whitespace: leave it as it is | |
401 | ||
402 | if it contains whitespace, then | |
403 | ||
404 | if it contains quotes: better leave it as it is | |
405 | ||
406 | else: quote it with the correct quote type for the box we're on | |
407 | ||
408 | =cut | |
409 | ||
410 | { | |
411 | # Instead of patching the guess, set commands_quote | |
412 | # to the right value | |
413 | my ($quotes,$use_quote) | |
414 | = $^O eq 'MSWin32' | |
415 | ? ('"', '"') | |
547d3dfd | 416 | : (q{"'}, "'") |
ed84aac9 A |
417 | ; |
418 | ||
419 | sub safe_quote { | |
420 | my ($self, $command) = @_; | |
421 | # Set up quote/default quote | |
422 | my $quote = $CPAN::Config->{commands_quote} || $quotes; | |
423 | ||
424 | if ($quote ne ' ' | |
6658a91b | 425 | and defined($command ) |
ed84aac9 A |
426 | and $command =~ /\s/ |
427 | and $command !~ /[$quote]/) { | |
428 | return qq<$use_quote$command$use_quote> | |
429 | } | |
430 | return $command; | |
431 | } | |
432 | } | |
433 | ||
e82b9348 | 434 | sub init { |
9ddc4ed0 | 435 | my($self,@args) = @_; |
547d3dfd SP |
436 | CPAN->debug("self[$self]args[".join(",",@args)."]"); |
437 | $self->load(doit => 1, @args); | |
e82b9348 SP |
438 | 1; |
439 | } | |
440 | ||
441 | # This is a piece of repeated code that is abstracted here for | |
442 | # maintainability. RMB | |
443 | # | |
444 | sub _configpmtest { | |
547d3dfd | 445 | my($configpmdir, $configpmtest) = @_; |
e82b9348 SP |
446 | if (-w $configpmtest) { |
447 | return $configpmtest; | |
448 | } elsif (-w $configpmdir) { | |
449 | #_#_# following code dumped core on me with 5.003_11, a.k. | |
450 | my $configpm_bak = "$configpmtest.bak"; | |
451 | unlink $configpm_bak if -f $configpm_bak; | |
452 | if( -f $configpmtest ) { | |
453 | if( rename $configpmtest, $configpm_bak ) { | |
547d3dfd | 454 | $CPAN::Frontend->mywarn(<<END); |
e82b9348 SP |
455 | Old configuration file $configpmtest |
456 | moved to $configpm_bak | |
457 | END | |
547d3dfd SP |
458 | } |
459 | } | |
460 | my $fh = FileHandle->new; | |
461 | if ($fh->open(">$configpmtest")) { | |
462 | $fh->print("1;\n"); | |
463 | return $configpmtest; | |
464 | } else { | |
465 | # Should never happen | |
466 | Carp::confess("Cannot open >$configpmtest"); | |
467 | } | |
e82b9348 SP |
468 | } else { return } |
469 | } | |
470 | ||
87892b73 RGS |
471 | sub require_myconfig_or_config () { |
472 | return if $INC{"CPAN/MyConfig.pm"}; | |
473 | local @INC = @INC; | |
474 | my $home = home(); | |
475 | unshift @INC, File::Spec->catdir($home,'.cpan'); | |
476 | eval { require CPAN::MyConfig }; | |
ed84aac9 A |
477 | my $err_myconfig = $@; |
478 | if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) { | |
479 | die "Error while requiring CPAN::MyConfig:\n$err_myconfig"; | |
480 | } | |
87892b73 RGS |
481 | unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already |
482 | eval {require CPAN::Config;}; # not everybody has one | |
ed84aac9 A |
483 | my $err_config = $@; |
484 | if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) { | |
485 | die "Error while requiring CPAN::Config:\n$err_config"; | |
486 | } | |
87892b73 RGS |
487 | } |
488 | } | |
489 | ||
490 | sub home () { | |
491 | my $home; | |
5254b38e SP |
492 | # Suppress load messages until we load the config and know whether |
493 | # load messages are desired. Otherwise, it's unexpected and odd | |
494 | # why one load message pops up even when verbosity is turned off. | |
495 | # This means File::HomeDir load messages are never seen, but I | |
496 | # think that's probably OK -- DAGOLDEN | |
497 | ||
498 | # 5.6.2 seemed to segfault localizing a value in a hashref | |
499 | # so do it manually instead | |
500 | my $old_v = $CPAN::Config->{load_module_verbosity}; | |
501 | $CPAN::Config->{load_module_verbosity} = q[none]; | |
87892b73 | 502 | if ($CPAN::META->has_usable("File::HomeDir")) { |
5254b38e SP |
503 | $home = File::HomeDir->can('my_dot_config') |
504 | ? File::HomeDir->my_dot_config | |
505 | : File::HomeDir->my_data; | |
547d3dfd SP |
506 | unless (defined $home) { |
507 | $home = File::HomeDir->my_home | |
508 | } | |
509 | } | |
510 | unless (defined $home) { | |
87892b73 RGS |
511 | $home = $ENV{HOME}; |
512 | } | |
5254b38e | 513 | $CPAN::Config->{load_module_verbosity} = $old_v; |
87892b73 RGS |
514 | $home; |
515 | } | |
516 | ||
e82b9348 SP |
517 | sub load { |
518 | my($self, %args) = @_; | |
547d3dfd SP |
519 | $CPAN::Be_Silent++ if $args{be_silent}; |
520 | my $doit; | |
521 | $doit = delete $args{doit}; | |
e82b9348 | 522 | |
e82b9348 | 523 | use Carp; |
87892b73 | 524 | require_myconfig_or_config; |
547d3dfd SP |
525 | my @miss = $self->missing_config_data; |
526 | return unless $doit || @miss; | |
527 | return if $loading; | |
528 | $loading++; | |
e82b9348 SP |
529 | |
530 | require CPAN::FirstTime; | |
547d3dfd | 531 | my($configpm,$fh,$redo); |
e82b9348 | 532 | $redo ||= ""; |
e82b9348 | 533 | if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) { |
547d3dfd SP |
534 | $configpm = $INC{"CPAN/Config.pm"}; |
535 | $redo++; | |
e82b9348 | 536 | } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) { |
547d3dfd SP |
537 | $configpm = $INC{"CPAN/MyConfig.pm"}; |
538 | $redo++; | |
e82b9348 | 539 | } else { |
547d3dfd SP |
540 | my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"}); |
541 | my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN"); | |
542 | my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm"); | |
c9869e1c | 543 | my $inc_key; |
547d3dfd SP |
544 | if (-d $configpmdir or File::Path::mkpath($configpmdir)) { |
545 | $configpm = _configpmtest($configpmdir,$configpmtest); | |
c9869e1c | 546 | $inc_key = "CPAN/Config.pm"; |
547d3dfd SP |
547 | } |
548 | unless ($configpm) { | |
549 | $configpmdir = File::Spec->catdir(home,".cpan","CPAN"); | |
550 | File::Path::mkpath($configpmdir); | |
551 | $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm"); | |
552 | $configpm = _configpmtest($configpmdir,$configpmtest); | |
c9869e1c | 553 | $inc_key = "CPAN/MyConfig.pm"; |
547d3dfd | 554 | } |
c9869e1c SP |
555 | if ($configpm) { |
556 | $INC{$inc_key} = $configpm; | |
557 | } else { | |
558 | my $text = qq{WARNING: CPAN.pm is unable to } . | |
559 | qq{create a configuration file.}; | |
560 | output($text, 'confess'); | |
561 | } | |
562 | ||
e82b9348 SP |
563 | } |
564 | local($") = ", "; | |
547d3dfd | 565 | if ($redo && !$doit) { |
8962fc49 | 566 | $CPAN::Frontend->myprint(<<END); |
0cf35e6a | 567 | Sorry, we have to rerun the configuration dialog for CPAN.pm due to |
547d3dfd | 568 | some missing parameters... |
e82b9348 | 569 | |
e82b9348 | 570 | END |
26844e27 | 571 | $args{args} = \@miss; |
8962fc49 | 572 | } |
e82b9348 | 573 | CPAN::FirstTime::init($configpm, %args); |
547d3dfd SP |
574 | $loading--; |
575 | return; | |
e82b9348 SP |
576 | } |
577 | ||
b72dd56f SP |
578 | |
579 | # returns mandatory but missing entries in the Config | |
e82b9348 SP |
580 | sub missing_config_data { |
581 | my(@miss); | |
582 | for ( | |
b72dd56f | 583 | "auto_commit", |
0cf35e6a SP |
584 | "build_cache", |
585 | "build_dir", | |
586 | "cache_metadata", | |
587 | "cpan_home", | |
588 | "ftp_proxy", | |
ed84aac9 | 589 | #"gzip", |
0cf35e6a SP |
590 | "http_proxy", |
591 | "index_expire", | |
547d3dfd | 592 | #"inhibit_startup_message", |
0cf35e6a | 593 | "keep_source_where", |
ed84aac9 | 594 | #"make", |
0cf35e6a SP |
595 | "make_arg", |
596 | "make_install_arg", | |
597 | "makepl_arg", | |
598 | "mbuild_arg", | |
599 | "mbuild_install_arg", | |
5254b38e | 600 | ($^O eq "MSWin32" ? "" : "mbuild_install_build_command"), |
0cf35e6a SP |
601 | "mbuildpl_arg", |
602 | "no_proxy", | |
ed84aac9 | 603 | #"pager", |
e82b9348 | 604 | "prerequisites_policy", |
0cf35e6a | 605 | "scan_cache", |
ed84aac9 A |
606 | #"tar", |
607 | #"unzip", | |
0cf35e6a | 608 | "urllist", |
e82b9348 | 609 | ) { |
44d21104 | 610 | next unless exists $keys{$_}; |
547d3dfd | 611 | push @miss, $_ unless defined $CPAN::Config->{$_}; |
e82b9348 SP |
612 | } |
613 | return @miss; | |
614 | } | |
615 | ||
e82b9348 SP |
616 | sub help { |
617 | $CPAN::Frontend->myprint(q[ | |
618 | Known options: | |
e82b9348 | 619 | commit commit session changes to disk |
4d1321a7 A |
620 | defaults reload default config values from disk |
621 | help this help | |
26844e27 | 622 | init enter a dialog to set all or a set of parameters |
e82b9348 | 623 | |
4d1321a7 | 624 | Edit key values as in the following (the "o" is a literal letter o): |
e82b9348 | 625 | o conf build_cache 15 |
e82b9348 | 626 | o conf build_dir "/foo/bar" |
e82b9348 | 627 | o conf urllist shift |
e82b9348 | 628 | o conf urllist unshift ftp://ftp.foo.bar/ |
4d1321a7 | 629 | o conf inhibit_startup_message 1 |
e82b9348 SP |
630 | |
631 | ]); | |
632 | undef; #don't reprint CPAN::Config | |
633 | } | |
634 | ||
635 | sub cpl { | |
636 | my($word,$line,$pos) = @_; | |
637 | $word ||= ""; | |
638 | CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG; | |
639 | my(@words) = split " ", substr($line,0,$pos+1); | |
640 | if ( | |
547d3dfd SP |
641 | defined($words[2]) |
642 | and | |
8962fc49 SP |
643 | $words[2] =~ /list$/ |
644 | and | |
547d3dfd SP |
645 | ( |
646 | @words == 3 | |
647 | || | |
648 | @words == 4 && length($word) | |
649 | ) | |
e82b9348 | 650 | ) { |
547d3dfd | 651 | return grep /^\Q$word\E/, qw(splice shift unshift pop push); |
8962fc49 SP |
652 | } elsif (defined($words[2]) |
653 | and | |
654 | $words[2] eq "init" | |
655 | and | |
656 | ( | |
657 | @words == 3 | |
658 | || | |
26844e27 | 659 | @words >= 4 && length($word) |
8962fc49 | 660 | )) { |
547d3dfd | 661 | return sort grep /^\Q$word\E/, keys %keys; |
e82b9348 | 662 | } elsif (@words >= 4) { |
547d3dfd | 663 | return (); |
e82b9348 SP |
664 | } |
665 | my %seen; | |
666 | my(@o_conf) = sort grep { !$seen{$_}++ } | |
667 | keys %can, | |
668 | keys %$CPAN::Config, | |
669 | keys %keys; | |
670 | return grep /^\Q$word\E/, @o_conf; | |
671 | } | |
672 | ||
6658a91b SP |
673 | sub prefs_lookup { |
674 | my($self,$distro,$what) = @_; | |
be34b10d | 675 | |
6658a91b | 676 | if ($prefssupport{$what}) { |
be34b10d SP |
677 | return $CPAN::Config->{$what} unless |
678 | $distro | |
679 | and $distro->prefs | |
680 | and $distro->prefs->{cpanconfig} | |
681 | and defined $distro->prefs->{cpanconfig}{$what}; | |
682 | return $distro->prefs->{cpanconfig}{$what}; | |
6658a91b | 683 | } else { |
be34b10d SP |
684 | $CPAN::Frontend->mywarn("Warning: $what not yet officially ". |
685 | "supported for distroprefs, doing a normal lookup"); | |
6658a91b SP |
686 | return $CPAN::Config->{$what}; |
687 | } | |
688 | } | |
9ddc4ed0 | 689 | |
9ddc4ed0 | 690 | |
6658a91b SP |
691 | { |
692 | package | |
693 | CPAN::Config; ####::###### #hide from indexer | |
694 | # note: J. Nick Koston wrote me that they are using | |
695 | # CPAN::Config->commit although undocumented. I suggested | |
696 | # CPAN::Shell->o("conf","commit") even when ugly it is at least | |
697 | # documented | |
698 | ||
699 | # that's why I added the CPAN::Config class with autoload and | |
700 | # deprecated warning | |
701 | ||
702 | use strict; | |
703 | use vars qw($AUTOLOAD $VERSION); | |
5254b38e | 704 | $VERSION = "5.5"; |
6658a91b SP |
705 | |
706 | # formerly CPAN::HandleConfig was known as CPAN::Config | |
707 | sub AUTOLOAD { | |
547d3dfd | 708 | my $class = shift; # e.g. in dh-make-perl: CPAN::Config |
6658a91b SP |
709 | my($l) = $AUTOLOAD; |
710 | $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig\n"); | |
711 | $l =~ s/.*:://; | |
712 | CPAN::HandleConfig->$l(@_); | |
713 | } | |
9ddc4ed0 A |
714 | } |
715 | ||
e82b9348 | 716 | 1; |
0cf35e6a SP |
717 | |
718 | __END__ | |
26844e27 SP |
719 | |
720 | =head1 LICENSE | |
721 | ||
722 | This program is free software; you can redistribute it and/or | |
723 | modify it under the same terms as Perl itself. | |
724 | ||
725 | =cut | |
726 | ||
0cf35e6a SP |
727 | # Local Variables: |
728 | # mode: cperl | |
ca79d794 | 729 | # cperl-indent-level: 4 |
0cf35e6a | 730 | # End: |