Introduce the man[24-8] variables, from Andy Dougherty.
[p5sagit/p5-mst-13.2.git] / lib / CPAN / FirstTime.pm
CommitLineData
5f05dabc 1package CPAN::Mirrored::By;
2
3sub new {
4 my($self,@arg) = @_;
5 bless [@arg], $self;
6}
da199366 7sub continent { shift->[0] }
8sub country { shift->[1] }
5f05dabc 9sub url { shift->[2] }
10
11package CPAN::FirstTime;
12
13use strict;
14use ExtUtils::MakeMaker qw(prompt);
05454584 15use FileHandle ();
09d9d230 16use File::Basename ();
05454584 17use File::Path ();
5f05dabc 18use vars qw($VERSION);
6d29edf5 19$VERSION = substr q$Revision: 1.44 $, 10;
5f05dabc 20
21=head1 NAME
22
23CPAN::FirstTime - Utility for CPAN::Config file Initialization
24
25=head1 SYNOPSIS
26
27CPAN::FirstTime::init()
28
29=head1 DESCRIPTION
30
31The init routine asks a few questions and writes a CPAN::Config
32file. Nothing special.
33
34=cut
35
36
37sub init {
38 my($configpm) = @_;
39 use Config;
f610777f 40 unless ($CPAN::VERSION) {
41 require CPAN::Nox;
42 }
5f05dabc 43 eval {require CPAN::Config;};
44 $CPAN::Config ||= {};
da199366 45 local($/) = "\n";
46 local($\) = "";
13bc20ff 47 local($|) = 1;
da199366 48
5f05dabc 49 my($ans,$default,$local,$cont,$url,$expected_size);
f610777f 50
da199366 51 #
52 # Files, directories
53 #
54
2e2b7522 55 print qq[
09d9d230 56
57CPAN is the world-wide archive of perl resources. It consists of about
58100 sites that all replicate the same contents all around the globe.
59Many countries have at least one CPAN site already. The resources
60found on CPAN are easily accessible with the CPAN.pm module. If you
61want to use CPAN.pm, you have to configure it properly.
62
63If you do not want to enter a dialog now, you can answer 'no' to this
64question and I\'ll try to autoconfigure. (Note: you can revisit this
65dialog anytime later by typing 'o conf init' at the cpan prompt.)
66
2e2b7522 67];
09d9d230 68
69 my $manual_conf =
70 ExtUtils::MakeMaker::prompt("Are you ready for manual configuration?",
71 "yes");
72 my $fastread;
73 {
74 local $^W;
75 if ($manual_conf =~ /^\s*y/i) {
76 $fastread = 0;
77 *prompt = \&ExtUtils::MakeMaker::prompt;
78 } else {
79 $fastread = 1;
36263cb3 80 $CPAN::Config->{urllist} ||= [];
c9d9b473 81 # prototype should match that of &MakeMaker::prompt
82 *prompt = sub ($;$) {
09d9d230 83 my($q,$a) = @_;
84 my($ret) = defined $a ? $a : "";
85 printf qq{%s [%s]\n\n}, $q, $ret;
86 $ret;
87 };
88 }
89 }
90 print qq{
91
92The following questions are intended to help you with the
93configuration. The CPAN module needs a directory of its own to cache
94important index files and maybe keep a temporary mirror of CPAN files.
95This may be a site-wide directory or a personal directory.
96
5f05dabc 97};
98
99 my $cpan_home = $CPAN::Config->{cpan_home} || MM->catdir($ENV{HOME}, ".cpan");
100 if (-d $cpan_home) {
101 print qq{
102
103I see you already have a directory
104 $cpan_home
105Shall we use it as the general CPAN build and cache directory?
106
107};
108 } else {
109 print qq{
110
111First of all, I\'d like to create this directory. Where?
112
113};
114 }
115
116 $default = $cpan_home;
05454584 117 while ($ans = prompt("CPAN build and cache directory?",$default)) {
36263cb3 118 eval { File::Path::mkpath($ans); }; # dies if it can't
119 if ($@) {
120 warn "Couldn't create directory $ans.
121Please retry.\n";
122 next;
123 }
124 if (-d $ans && -w _) {
125 last;
126 } else {
127 warn "Couldn't find directory $ans
10b2abe6 128 or directory is not writable. Please retry.\n";
36263cb3 129 }
10b2abe6 130 }
5f05dabc 131 $CPAN::Config->{cpan_home} = $ans;
f610777f 132
5f05dabc 133 print qq{
134
135If you want, I can keep the source files after a build in the cpan
136home directory. If you choose so then future builds will take the
137files from there. If you don\'t want to keep them, answer 0 to the
138next question.
139
140};
141
142 $CPAN::Config->{keep_source_where} = MM->catdir($CPAN::Config->{cpan_home},"sources");
143 $CPAN::Config->{build_dir} = MM->catdir($CPAN::Config->{cpan_home},"build");
144
da199366 145 #
146 # Cache size, Index expire
147 #
148
5f05dabc 149 print qq{
150
151How big should the disk cache be for keeping the build directories
de34a54b 152with all the intermediate files\?
5f05dabc 153
154};
155
156 $default = $CPAN::Config->{build_cache} || 10;
157 $ans = prompt("Cache size for build directory (in MB)?", $default);
158 $CPAN::Config->{build_cache} = $ans;
159
160 # XXX This the time when we refetch the index files (in days)
161 $CPAN::Config->{'index_expire'} = 1;
162
f610777f 163 print qq{
164
165By default, each time the CPAN module is started, cache scanning
166is performed to keep the cache size in sync. To prevent from this,
167disable the cache scanning with 'never'.
168
169};
170
171 $default = $CPAN::Config->{scan_cache} || 'atstart';
172 do {
173 $ans = prompt("Perform cache scanning (atstart or never)?", $default);
174 } while ($ans ne 'atstart' && $ans ne 'never');
175 $CPAN::Config->{scan_cache} = $ans;
176
5e05dca5 177 print qq{
178
179To speed up the initial CPAN shell startup, it is possible to use
c4d24d4c 180Storable to create an cache of metadata. If Storable is not available,
181the normal index mechanism will be used. This feature is still
6d29edf5 182considered experimental and not recommended for production use.
5e05dca5 183
184};
185
c4d24d4c 186 defined($default = $CPAN::Config->{cache_metadata}) or $default = 0;
5e05dca5 187 do {
188 $ans = prompt("Cache metadata (yes/no)?", ($default ? 'yes' : 'no'));
189 } while ($ans !~ /^\s*[yn]/i);
190 $CPAN::Config->{cache_metadata} = ($ans =~ /^\s*y/i ? 1 : 0);
191
f610777f 192 #
193 # prerequisites_policy
194 # Do we follow PREREQ_PM?
195 #
196 print qq{
197
198The CPAN module can detect when a module that which you are trying to
199build depends on prerequisites. If this happens, it can build the
200prerequisites for you automatically ('follow'), ask you for
201confirmation ('ask'), or just ignore them ('ignore'). Please set your
202policy to one of the three values.
203
204};
205
de34a54b 206 $default = $CPAN::Config->{prerequisites_policy} || 'ask';
f610777f 207 do {
f14b5cec 208 $ans =
209 prompt("Policy on building prerequisites (follow, ask or ignore)?",
210 $default);
f610777f 211 } while ($ans ne 'follow' && $ans ne 'ask' && $ans ne 'ignore');
212 $CPAN::Config->{prerequisites_policy} = $ans;
213
da199366 214 #
215 # External programs
216 #
217
5f05dabc 218 print qq{
219
220The CPAN module will need a few external programs to work
221properly. Please correct me, if I guess the wrong path for a program.
05454584 222Don\'t panic if you do not have some of them, just press ENTER for
223those.
5f05dabc 224
225};
226
f14b5cec 227 my $old_warn = $^W;
228 local $^W if $^O eq 'MacOS';
55e314ee 229 my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
f14b5cec 230 local $^W = $old_warn;
09d9d230 231 my $progname;
2e2b7522 232 for $progname (qw/gzip tar unzip make lynx ncftpget ncftp ftp/){
f14b5cec 233 if ($^O eq 'MacOS') {
234 $CPAN::Config->{$progname} = 'not_here';
235 next;
236 }
09d9d230 237 my $progcall = $progname;
2e2b7522 238 # we don't need ncftp if we have ncftpget
239 next if $progname eq "ncftp" && $CPAN::Config->{ncftpget} gt " ";
240 my $path = $CPAN::Config->{$progname}
241 || $Config::Config{$progname}
242 || "";
243 if (MM->file_name_is_absolute($path)) {
244 # testing existence is not good enough, some have these exe
245 # extensions
246
247 # warn "Warning: configured $path does not exist\n" unless -e $path;
248 # $path = "";
249 } else {
250 $path = '';
251 }
252 unless ($path) {
253 # e.g. make -> nmake
254 $progcall = $Config::Config{$progname} if $Config::Config{$progname};
255 }
09d9d230 256
2e2b7522 257 $path ||= find_exe($progcall,[@path]);
258 warn "Warning: $progcall not found in PATH\n" unless
259 $path; # not -e $path, because find_exe already checked that
260 $ans = prompt("Where is your $progname program?",$path) || $path;
261 $CPAN::Config->{$progname} = $ans;
5f05dabc 262 }
263 my $path = $CPAN::Config->{'pager'} ||
264 $ENV{PAGER} || find_exe("less",[@path]) ||
f14b5cec 265 find_exe("more",[@path]) || ($^O eq 'MacOS' ? $ENV{EDITOR} : 0 )
266 || "more";
55e314ee 267 $ans = prompt("What is your favorite pager program?",$path);
5f05dabc 268 $CPAN::Config->{'pager'} = $ans;
55e314ee 269 $path = $CPAN::Config->{'shell'};
270 if (MM->file_name_is_absolute($path)) {
271 warn "Warning: configured $path does not exist\n" unless -e $path;
272 $path = "";
273 }
274 $path ||= $ENV{SHELL};
f14b5cec 275 if ($^O eq 'MacOS') {
276 $CPAN::Config->{'shell'} = 'not_here';
277 } else {
278 $path =~ s,\\,/,g if $^O eq 'os2'; # Cosmetic only
279 $ans = prompt("What is your favorite shell?",$path);
280 $CPAN::Config->{'shell'} = $ans;
281 }
da199366 282
283 #
284 # Arguments to make etc.
285 #
286
5f05dabc 287 print qq{
288
da199366 289Every Makefile.PL is run by perl in a separate process. Likewise we
5f05dabc 290run \'make\' and \'make install\' in processes. If you have any parameters
291\(e.g. PREFIX, INSTALLPRIVLIB, UNINST or the like\) you want to pass to
292the calls, please specify them here.
293
05454584 294If you don\'t understand this question, just press ENTER.
295
5f05dabc 296};
297
298 $default = $CPAN::Config->{makepl_arg} || "";
299 $CPAN::Config->{makepl_arg} =
300 prompt("Parameters for the 'perl Makefile.PL' command?",$default);
301 $default = $CPAN::Config->{make_arg} || "";
302 $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command?",$default);
303
304 $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || "";
305 $CPAN::Config->{make_install_arg} =
306 prompt("Parameters for the 'make install' command?",$default);
307
da199366 308 #
309 # Alarm period
310 #
311
10b2abe6 312 print qq{
313
314Sometimes you may wish to leave the processes run by CPAN alone
315without caring about them. As sometimes the Makefile.PL contains
316question you\'re expected to answer, you can set a timer that will
317kill a 'perl Makefile.PL' process after the specified time in seconds.
318
e50380aa 319If you set this value to 0, these processes will wait forever. This is
320the default and recommended setting.
10b2abe6 321
322};
323
324 $default = $CPAN::Config->{inactivity_timeout} || 0;
325 $CPAN::Config->{inactivity_timeout} =
09d9d230 326 prompt("Timeout for inactivity during Makefile.PL?",$default);
10b2abe6 327
09d9d230 328 # Proxies
da199366 329
09d9d230 330 print qq{
10b2abe6 331
09d9d230 332If you\'re accessing the net via proxies, you can specify them in the
333CPAN configuration or via environment variables. The variable in
334the \$CPAN::Config takes precedence.
5f05dabc 335
05454584 336};
09d9d230 337
338 for (qw/ftp_proxy http_proxy no_proxy/) {
339 $default = $CPAN::Config->{$_} || $ENV{$_};
340 $CPAN::Config->{$_} = prompt("Your $_?",$default);
5f05dabc 341 }
342
09d9d230 343 #
344 # MIRRORED.BY
345 #
346
347 conf_sites() unless $fastread;
348
d4fd5c69 349 unless (@{$CPAN::Config->{'wait_list'}||[]}) {
350 print qq{
da199366 351
05454584 352WAIT support is available as a Plugin. You need the CPAN::WAIT module
353to actually use it. But we need to know your favorite WAIT server. If
354you don\'t know a WAIT server near you, just press ENTER.
355
356};
d4fd5c69 357 $default = "wait://ls6.informatik.uni-dortmund.de:1404";
358 $ans = prompt("Your favorite WAIT server?\n ",$default);
359 push @{$CPAN::Config->{'wait_list'}}, $ans;
360 }
05454584 361
e50380aa 362 # We don't ask that now, it will be noticed in time, won't it?
5f05dabc 363 $CPAN::Config->{'inhibit_startup_message'} = 0;
e50380aa 364 $CPAN::Config->{'getcwd'} = 'cwd';
5f05dabc 365
366 print "\n\n";
367 CPAN::Config->commit($configpm);
368}
369
09d9d230 370sub conf_sites {
371 my $m = 'MIRRORED.BY';
372 my $mby = MM->catfile($CPAN::Config->{keep_source_where},$m);
373 File::Path::mkpath(File::Basename::dirname($mby));
374 if (-f $mby && -f $m && -M $m < -M $mby) {
375 require File::Copy;
376 File::Copy::copy($m,$mby) or die "Could not update $mby: $!";
377 }
911a92db 378 my $loopcount = 0;
de34a54b 379 local $^T = time;
380 while ($mby) {
36263cb3 381 if ( ! -f $mby ){
382 print qq{You have no $mby
09d9d230 383 I\'m trying to fetch one
384};
36263cb3 385 $mby = CPAN::FTP->localize($m,$mby,3);
911a92db 386 } elsif (-M $mby > 60 && $loopcount == 0) {
387 print qq{Your $mby is older than 60 days,
09d9d230 388 I\'m trying to fetch one
389};
36263cb3 390 $mby = CPAN::FTP->localize($m,$mby,3);
911a92db 391 $loopcount++;
36263cb3 392 } elsif (-s $mby == 0) {
393 print qq{You have an empty $mby,
394 I\'m trying to fetch one
395};
396 $mby = CPAN::FTP->localize($m,$mby,3);
397 } else {
398 last;
399 }
09d9d230 400 }
401 read_mirrored_by($mby);
de34a54b 402 bring_your_own();
09d9d230 403}
404
5f05dabc 405sub find_exe {
406 my($exe,$path) = @_;
55e314ee 407 my($dir);
408 #warn "in find_exe exe[$exe] path[@$path]";
5f05dabc 409 for $dir (@$path) {
55e314ee 410 my $abs = MM->catfile($dir,$exe);
13bc20ff 411 if (($abs = MM->maybe_command($abs))) {
5f05dabc 412 return $abs;
413 }
414 }
415}
416
f610777f 417sub picklist {
418 my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_;
419 $default ||= '';
420
421 my ($item, $i);
422 for $item (@$items) {
423 printf "(%d) %s\n", ++$i, $item;
424 }
425
426 my @nums;
427 while (1) {
428 my $num = prompt($prompt,$default);
429 @nums = split (' ', $num);
430 (warn "invalid items entered, try again\n"), next
431 if grep (/\D/ || $_ < 1 || $_ > $i, @nums);
432 if ($require_nonempty) {
433 (warn "$empty_warning\n"), next
434 unless @nums;
435 }
436 last;
437 }
438 print "\n";
439 for (@nums) { $_-- }
440 @{$items}[@nums];
441}
442
5f05dabc 443sub read_mirrored_by {
de34a54b 444 my $local = shift or return;
5f05dabc 445 my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location);
05454584 446 my $fh = FileHandle->new;
447 $fh->open($local) or die "Couldn't open $local: $!";
f14b5cec 448 local $/ = "\012";
05454584 449 while (<$fh>) {
5f05dabc 450 ($host) = /^([\w\.\-]+)/ unless defined $host;
451 next unless defined $host;
452 next unless /\s+dst_(dst|location)/;
453 /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and
454 ($continent, $country) = @location[-1,-2];
455 $continent =~ s/\s\(.*//;
f610777f 456 $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude
5f05dabc 457 /dst_dst\s+=\s+\"([^\"]+)/ and $dst = $1;
458 next unless $host && $dst && $continent && $country;
459 $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst);
460 undef $host;
461 $dst=$continent=$country="";
462 }
05454584 463 $fh->close;
5f05dabc 464 $CPAN::Config->{urllist} ||= [];
f610777f 465 my(@previous_urls);
466 if (@previous_urls = @{$CPAN::Config->{urllist}}) {
5f05dabc 467 $CPAN::Config->{urllist} = [];
5f05dabc 468 }
f610777f 469
5f05dabc 470 print qq{
471
f610777f 472Now we need to know where your favorite CPAN sites are located. Push
5f05dabc 473a few sites onto the array (just in case the first on the array won\'t
474work). If you are mirroring CPAN to your local workstation, specify a
475file: URL.
476
f610777f 477First, pick a nearby continent and country (you can pick several of
478each, separated by spaces, or none if you just want to keep your
479existing selections). Then, you will be presented with a list of URLs
480of CPAN mirrors in the countries you selected, along with previously
481selected URLs. Select some of those URLs, or just keep the old list.
482Finally, you will be prompted for any extra URLs -- file:, ftp:, or
483http: -- that host a CPAN mirror.
5f05dabc 484
485};
486
f610777f 487 my (@cont, $cont, %cont, @countries, @urls, %seen);
488 my $no_previous_warn =
489 "Sorry! since you don't have any existing picks, you must make a\n" .
490 "geographic selection.";
491 @cont = picklist([sort keys %all],
492 "Select your continent (or several nearby continents)",
493 '',
494 ! @previous_urls,
495 $no_previous_warn);
496
497
498 foreach $cont (@cont) {
499 my @c = sort keys %{$all{$cont}};
500 @cont{@c} = map ($cont, 0..$#c);
501 @c = map ("$_ ($cont)", @c) if @cont > 1;
502 push (@countries, @c);
5f05dabc 503 }
f610777f 504
505 if (@countries) {
506 @countries = picklist (\@countries,
507 "Select your country (or several nearby countries)",
508 '',
509 ! @previous_urls,
510 $no_previous_warn);
511 %seen = map (($_ => 1), @previous_urls);
512 # hmmm, should take list of defaults from CPAN::Config->{'urllist'}...
513 foreach $country (@countries) {
514 (my $bare_country = $country) =~ s/ \(.*\)//;
515 my @u = sort keys %{$all{$cont{$bare_country}}{$bare_country}};
516 @u = grep (! $seen{$_}, @u);
517 @u = map ("$_ ($bare_country)", @u)
518 if @countries > 1;
519 push (@urls, @u);
520 }
521 }
522 push (@urls, map ("$_ (previous pick)", @previous_urls));
523 my $prompt = "Select as many URLs as you like";
524 if (@previous_urls) {
525 $default = join (' ', ((scalar @urls) - (scalar @previous_urls) + 1) ..
526 (scalar @urls));
527 $prompt .= "\n(or just hit RETURN to keep your previous picks)";
528 }
529
530 @urls = picklist (\@urls, $prompt, $default);
531 foreach (@urls) { s/ \(.*\)//; }
de34a54b 532 push @{$CPAN::Config->{urllist}}, @urls;
533}
f610777f 534
de34a54b 535sub bring_your_own {
536 my %seen = map (($_ => 1), @{$CPAN::Config->{urllist}});
537 my($ans,@urls);
f610777f 538 do {
de34a54b 539 my $prompt = "Enter another URL or RETURN to quit:";
540 unless (%seen) {
541 $prompt = qq{CPAN.pm needs at least one URL where it can fetch CPAN files from.
542
543Please enter your CPAN site:};
544 }
545 $ans = prompt ($prompt, "");
f610777f 546
547 if ($ans) {
de34a54b 548 $ans =~ s|/?\z|/|; # has to end with one slash
f610777f 549 $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file:
550 if ($ans =~ /^\w+:\/./) {
de34a54b 551 push @urls, $ans unless $seen{$ans}++;
552 } else {
f610777f 553 print qq{"$ans" doesn\'t look like an URL at first sight.
554I\'ll ignore it for now. You can add it to $INC{'CPAN/MyConfig.pm'}
555later if you\'re sure it\'s right.\n};
556 }
557 }
de34a54b 558 } while $ans || !%seen;
f610777f 559
560 push @{$CPAN::Config->{urllist}}, @urls;
561 # xxx delete or comment these out when you're happy that it works
562 print "New set of picks:\n";
563 map { print " $_\n" } @{$CPAN::Config->{urllist}};
5f05dabc 564}
565
5661;