1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
2 package CPAN::Mirrored::By;
8 sub continent { shift->[0] }
9 sub country { shift->[1] }
10 sub url { shift->[2] }
12 package CPAN::FirstTime;
15 use ExtUtils::MakeMaker qw(prompt);
17 use File::Basename ();
19 use vars qw($VERSION);
20 $VERSION = substr q$Revision: 1.51 $, 10;
24 CPAN::FirstTime - Utility for CPAN::Config file Initialization
28 CPAN::FirstTime::init()
32 The init routine asks a few questions and writes a CPAN::Config
33 file. Nothing special.
41 unless ($CPAN::VERSION) {
44 eval {require CPAN::Config;};
50 my($ans,$default,$local,$cont,$url,$expected_size);
58 CPAN is the world-wide archive of perl resources. It consists of about
59 100 sites that all replicate the same contents all around the globe.
60 Many countries have at least one CPAN site already. The resources
61 found on CPAN are easily accessible with the CPAN.pm module. If you
62 want to use CPAN.pm, you have to configure it properly.
64 If you do not want to enter a dialog now, you can answer 'no' to this
65 question and I\'ll try to autoconfigure. (Note: you can revisit this
66 dialog anytime later by typing 'o conf init' at the cpan prompt.)
71 ExtUtils::MakeMaker::prompt("Are you ready for manual configuration?",
76 if ($manual_conf =~ /^\s*y/i) {
78 *prompt = \&ExtUtils::MakeMaker::prompt;
81 $CPAN::Config->{urllist} ||= [];
82 # prototype should match that of &MakeMaker::prompt
85 my($ret) = defined $a ? $a : "";
86 printf qq{%s [%s]\n\n}, $q, $ret;
93 The following questions are intended to help you with the
94 configuration. The CPAN module needs a directory of its own to cache
95 important index files and maybe keep a temporary mirror of CPAN files.
96 This may be a site-wide directory or a personal directory.
100 my $cpan_home = $CPAN::Config->{cpan_home} || MM->catdir($ENV{HOME}, ".cpan");
104 I see you already have a directory
106 Shall we use it as the general CPAN build and cache directory?
112 First of all, I\'d like to create this directory. Where?
117 $default = $cpan_home;
118 while ($ans = prompt("CPAN build and cache directory?",$default)) {
119 eval { File::Path::mkpath($ans); }; # dies if it can't
121 warn "Couldn't create directory $ans.
125 if (-d $ans && -w _) {
128 warn "Couldn't find directory $ans
129 or directory is not writable. Please retry.\n";
132 $CPAN::Config->{cpan_home} = $ans;
136 If you want, I can keep the source files after a build in the cpan
137 home directory. If you choose so then future builds will take the
138 files from there. If you don\'t want to keep them, answer 0 to the
143 $CPAN::Config->{keep_source_where} = MM->catdir($CPAN::Config->{cpan_home},"sources");
144 $CPAN::Config->{build_dir} = MM->catdir($CPAN::Config->{cpan_home},"build");
147 # Cache size, Index expire
152 How big should the disk cache be for keeping the build directories
153 with all the intermediate files\?
157 $default = $CPAN::Config->{build_cache} || 10;
158 $ans = prompt("Cache size for build directory (in MB)?", $default);
159 $CPAN::Config->{build_cache} = $ans;
161 # XXX This the time when we refetch the index files (in days)
162 $CPAN::Config->{'index_expire'} = 1;
166 By default, each time the CPAN module is started, cache scanning
167 is performed to keep the cache size in sync. To prevent from this,
168 disable the cache scanning with 'never'.
172 $default = $CPAN::Config->{scan_cache} || 'atstart';
174 $ans = prompt("Perform cache scanning (atstart or never)?", $default);
175 } while ($ans ne 'atstart' && $ans ne 'never');
176 $CPAN::Config->{scan_cache} = $ans;
183 To considerably speed up the initial CPAN shell startup, it is
184 possible to use Storable to create a cache of metadata. If Storable
185 is not available, the normal index mechanism will be used.
189 defined($default = $CPAN::Config->{cache_metadata}) or $default = 1;
191 $ans = prompt("Cache metadata (yes/no)?", ($default ? 'yes' : 'no'));
192 } while ($ans !~ /^\s*[yn]/i);
193 $CPAN::Config->{cache_metadata} = ($ans =~ /^\s*y/i ? 1 : 0);
200 The next option deals with the charset your terminal supports. In
201 general CPAN is English speaking territory, thus the charset does not
202 matter much, but some of the aliens out there who upload their
203 software to CPAN bear names that are outside the ASCII range. If your
204 terminal supports UTF-8, you say no to the next question, if it
205 supports ISO-8859-1 (also known as LATIN1) then you say yes, and if it
206 supports neither nor, your answer does not matter, you will not be
207 able to read the names of some authors anyway. If you answer no, nmes
208 will be output in UTF-8.
212 defined($default = $CPAN::Config->{term_is_latin}) or $default = 1;
214 $ans = prompt("Your terminal expects ISO-8859-1 (yes/no)?",
215 ($default ? 'yes' : 'no'));
216 } while ($ans !~ /^\s*[yn]/i);
217 $CPAN::Config->{term_is_latin} = ($ans =~ /^\s*y/i ? 1 : 0);
220 # prerequisites_policy
221 # Do we follow PREREQ_PM?
225 The CPAN module can detect when a module that which you are trying to
226 build depends on prerequisites. If this happens, it can build the
227 prerequisites for you automatically ('follow'), ask you for
228 confirmation ('ask'), or just ignore them ('ignore'). Please set your
229 policy to one of the three values.
233 $default = $CPAN::Config->{prerequisites_policy} || 'ask';
236 prompt("Policy on building prerequisites (follow, ask or ignore)?",
238 } while ($ans ne 'follow' && $ans ne 'ask' && $ans ne 'ignore');
239 $CPAN::Config->{prerequisites_policy} = $ans;
247 The CPAN module will need a few external programs to work properly.
248 Please correct me, if I guess the wrong path for a program. Don\'t
249 panic if you do not have some of them, just press ENTER for those. To
250 disable the use of a download program, you can type a space followed
256 local $^W if $^O eq 'MacOS';
257 my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
258 local $^W = $old_warn;
260 for $progname (qw/gzip tar unzip make lynx wget ncftpget ncftp ftp/){
261 if ($^O eq 'MacOS') {
262 $CPAN::Config->{$progname} = 'not_here';
265 my $progcall = $progname;
266 # we don't need ncftp if we have ncftpget
267 next if $progname eq "ncftp" && $CPAN::Config->{ncftpget} gt " ";
268 my $path = $CPAN::Config->{$progname}
269 || $Config::Config{$progname}
271 if (MM->file_name_is_absolute($path)) {
272 # testing existence is not good enough, some have these exe
275 # warn "Warning: configured $path does not exist\n" unless -e $path;
282 $progcall = $Config::Config{$progname} if $Config::Config{$progname};
285 $path ||= find_exe($progcall,[@path]);
286 warn "Warning: $progcall not found in PATH\n" unless
287 $path; # not -e $path, because find_exe already checked that
288 $ans = prompt("Where is your $progname program?",$path) || $path;
289 $CPAN::Config->{$progname} = $ans;
291 my $path = $CPAN::Config->{'pager'} ||
292 $ENV{PAGER} || find_exe("less",[@path]) ||
293 find_exe("more",[@path]) || ($^O eq 'MacOS' ? $ENV{EDITOR} : 0 )
295 $ans = prompt("What is your favorite pager program?",$path);
296 $CPAN::Config->{'pager'} = $ans;
297 $path = $CPAN::Config->{'shell'};
298 if (MM->file_name_is_absolute($path)) {
299 warn "Warning: configured $path does not exist\n" unless -e $path;
302 $path ||= $ENV{SHELL};
303 if ($^O eq 'MacOS') {
304 $CPAN::Config->{'shell'} = 'not_here';
306 $path =~ s,\\,/,g if $^O eq 'os2'; # Cosmetic only
307 $ans = prompt("What is your favorite shell?",$path);
308 $CPAN::Config->{'shell'} = $ans;
312 # Arguments to make etc.
317 Every Makefile.PL is run by perl in a separate process. Likewise we
318 run \'make\' and \'make install\' in processes. If you have any
319 parameters \(e.g. PREFIX, LIB, UNINST or the like\) you want to pass
320 to the calls, please specify them here.
322 If you don\'t understand this question, just press ENTER.
326 $default = $CPAN::Config->{makepl_arg} || "";
327 $CPAN::Config->{makepl_arg} =
328 prompt("Parameters for the 'perl Makefile.PL' command?
329 Typical frequently used settings:
331 POLLUTE=1 increasing backwards compatibility
332 LIB=~/perl non-root users (please see manual for more hints)
334 Your choice: ",$default);
335 $default = $CPAN::Config->{make_arg} || "";
336 $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command?
337 Typical frequently used setting:
339 -j3 dual processor system
341 Your choice: ",$default);
343 $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || "";
344 $CPAN::Config->{make_install_arg} =
345 prompt("Parameters for the 'make install' command?
346 Typical frequently used setting:
348 UNINST=1 to always uninstall potentially conflicting files
350 Your choice: ",$default);
358 Sometimes you may wish to leave the processes run by CPAN alone
359 without caring about them. As sometimes the Makefile.PL contains
360 question you\'re expected to answer, you can set a timer that will
361 kill a 'perl Makefile.PL' process after the specified time in seconds.
363 If you set this value to 0, these processes will wait forever. This is
364 the default and recommended setting.
368 $default = $CPAN::Config->{inactivity_timeout} || 0;
369 $CPAN::Config->{inactivity_timeout} =
370 prompt("Timeout for inactivity during Makefile.PL?",$default);
376 If you\'re accessing the net via proxies, you can specify them in the
377 CPAN configuration or via environment variables. The variable in
378 the \$CPAN::Config takes precedence.
382 for (qw/ftp_proxy http_proxy no_proxy/) {
383 $default = $CPAN::Config->{$_} || $ENV{$_};
384 $CPAN::Config->{$_} = prompt("Your $_?",$default);
391 conf_sites() unless $fastread;
393 unless (@{$CPAN::Config->{'wait_list'}||[]}) {
396 WAIT support is available as a Plugin. You need the CPAN::WAIT module
397 to actually use it. But we need to know your favorite WAIT server. If
398 you don\'t know a WAIT server near you, just press ENTER.
401 $default = "wait://ls6.informatik.uni-dortmund.de:1404";
402 $ans = prompt("Your favorite WAIT server?\n ",$default);
403 push @{$CPAN::Config->{'wait_list'}}, $ans;
406 # We don't ask that now, it will be noticed in time, won't it?
407 $CPAN::Config->{'inhibit_startup_message'} = 0;
408 $CPAN::Config->{'getcwd'} = 'cwd';
411 CPAN::Config->commit($configpm);
415 my $m = 'MIRRORED.BY';
416 my $mby = MM->catfile($CPAN::Config->{keep_source_where},$m);
417 File::Path::mkpath(File::Basename::dirname($mby));
418 if (-f $mby && -f $m && -M $m < -M $mby) {
420 File::Copy::copy($m,$mby) or die "Could not update $mby: $!";
424 my $overwrite_local = 0;
425 if ($mby && -f $mby && -M _ <= 60 && -s _ > 0) {
426 my $mtime = localtime((stat _)[9]);
427 my $prompt = qq{Found $mby as of $mtime
429 I\'d use that as a database of CPAN sites. If that is OK for you,
430 please answer 'y', but if you want me to get a new database now,
431 please answer 'n' to the following question.
433 Shall I use the local database in $mby?};
434 my $ans = prompt($prompt,"y");
435 $overwrite_local = 1 unless $ans =~ /^y/i;
438 if ($overwrite_local) {
439 print qq{Trying to overwrite $mby
441 $mby = CPAN::FTP->localize($m,$mby,3);
442 $overwrite_local = 0;
443 } elsif ( ! -f $mby ){
444 print qq{You have no $mby
445 I\'m trying to fetch one
447 $mby = CPAN::FTP->localize($m,$mby,3);
448 } elsif (-M $mby > 60 && $loopcount == 0) {
449 print qq{Your $mby is older than 60 days,
450 I\'m trying to fetch one
452 $mby = CPAN::FTP->localize($m,$mby,3);
454 } elsif (-s $mby == 0) {
455 print qq{You have an empty $mby,
456 I\'m trying to fetch one
458 $mby = CPAN::FTP->localize($m,$mby,3);
463 read_mirrored_by($mby);
470 #warn "in find_exe exe[$exe] path[@$path]";
472 my $abs = MM->catfile($dir,$exe);
473 if (($abs = MM->maybe_command($abs))) {
480 my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_;
484 for $item (@$items) {
485 printf "(%d) %s\n", ++$i, $item;
490 my $num = prompt($prompt,$default);
491 @nums = split (' ', $num);
492 (warn "invalid items entered, try again\n"), next
493 if grep (/\D/ || $_ < 1 || $_ > $i, @nums);
494 if ($require_nonempty) {
495 (warn "$empty_warning\n"), next
505 sub read_mirrored_by {
506 my $local = shift or return;
507 my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location);
508 my $fh = FileHandle->new;
509 $fh->open($local) or die "Couldn't open $local: $!";
512 ($host) = /^([\w\.\-]+)/ unless defined $host;
513 next unless defined $host;
514 next unless /\s+dst_(dst|location)/;
515 /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and
516 ($continent, $country) = @location[-1,-2];
517 $continent =~ s/\s\(.*//;
518 $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude
519 /dst_dst\s+=\s+\"([^\"]+)/ and $dst = $1;
520 next unless $host && $dst && $continent && $country;
521 $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst);
523 $dst=$continent=$country="";
526 $CPAN::Config->{urllist} ||= [];
528 if (@previous_urls = @{$CPAN::Config->{urllist}}) {
529 $CPAN::Config->{urllist} = [];
534 Now we need to know where your favorite CPAN sites are located. Push
535 a few sites onto the array (just in case the first on the array won\'t
536 work). If you are mirroring CPAN to your local workstation, specify a
539 First, pick a nearby continent and country (you can pick several of
540 each, separated by spaces, or none if you just want to keep your
541 existing selections). Then, you will be presented with a list of URLs
542 of CPAN mirrors in the countries you selected, along with previously
543 selected URLs. Select some of those URLs, or just keep the old list.
544 Finally, you will be prompted for any extra URLs -- file:, ftp:, or
545 http: -- that host a CPAN mirror.
549 my (@cont, $cont, %cont, @countries, @urls, %seen);
550 my $no_previous_warn =
551 "Sorry! since you don't have any existing picks, you must make a\n" .
552 "geographic selection.";
553 @cont = picklist([sort keys %all],
554 "Select your continent (or several nearby continents)",
560 foreach $cont (@cont) {
561 my @c = sort keys %{$all{$cont}};
562 @cont{@c} = map ($cont, 0..$#c);
563 @c = map ("$_ ($cont)", @c) if @cont > 1;
564 push (@countries, @c);
568 @countries = picklist (\@countries,
569 "Select your country (or several nearby countries)",
573 %seen = map (($_ => 1), @previous_urls);
574 # hmmm, should take list of defaults from CPAN::Config->{'urllist'}...
575 foreach $country (@countries) {
576 (my $bare_country = $country) =~ s/ \(.*\)//;
577 my @u = sort keys %{$all{$cont{$bare_country}}{$bare_country}};
578 @u = grep (! $seen{$_}, @u);
579 @u = map ("$_ ($bare_country)", @u)
584 push (@urls, map ("$_ (previous pick)", @previous_urls));
585 my $prompt = "Select as many URLs as you like,
586 put them on one line, separated by blanks";
587 if (@previous_urls) {
588 $default = join (' ', ((scalar @urls) - (scalar @previous_urls) + 1) ..
590 $prompt .= "\n(or just hit RETURN to keep your previous picks)";
593 @urls = picklist (\@urls, $prompt, $default);
594 foreach (@urls) { s/ \(.*\)//; }
595 push @{$CPAN::Config->{urllist}}, @urls;
599 my %seen = map (($_ => 1), @{$CPAN::Config->{urllist}});
602 my $prompt = "Enter another URL or RETURN to quit:";
604 $prompt = qq{CPAN.pm needs at least one URL where it can fetch CPAN files from.
606 Please enter your CPAN site:};
608 $ans = prompt ($prompt, "");
611 $ans =~ s|/?\z|/|; # has to end with one slash
612 $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file:
613 if ($ans =~ /^\w+:\/./) {
614 push @urls, $ans unless $seen{$ans}++;
616 printf(qq{"%s" doesn\'t look like an URL at first sight.
617 I\'ll ignore it for now.
618 You can add it to your %s
619 later if you\'re sure it\'s right.\n},
621 $INC{'CPAN/MyConfig.pm'} || $INC{'CPAN/Config.pm'} || "configuration file",
625 } while $ans || !%seen;
627 push @{$CPAN::Config->{urllist}}, @urls;
628 # xxx delete or comment these out when you're happy that it works
629 print "New set of picks:\n";
630 map { print " $_\n" } @{$CPAN::Config->{urllist}};