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