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 ();
17 use File::Basename ();
20 use vars qw($VERSION);
21 $VERSION = substr q$Revision: 1.60_01 $, 10;
25 CPAN::FirstTime - Utility for CPAN::Config file Initialization
29 CPAN::FirstTime::init()
33 The init routine asks a few questions and writes a CPAN::Config
34 file. Nothing special.
42 unless ($CPAN::VERSION) {
45 eval {require CPAN::Config;};
59 CPAN is the world-wide archive of perl resources. It consists of about
60 100 sites that all replicate the same contents all around the globe.
61 Many countries have at least one CPAN site already. The resources
62 found on CPAN are easily accessible with the CPAN.pm module. If you
63 want to use CPAN.pm, you have to configure it properly.
65 If you do not want to enter a dialog now, you can answer 'no' to this
66 question and I\'ll try to autoconfigure. (Note: you can revisit this
67 dialog anytime later by typing 'o conf init' at the cpan prompt.)
71 my $manual_conf = prompt("Are you ready for manual configuration?", "yes");
74 if ($manual_conf =~ /^y/i) {
78 $CPAN::Config->{urllist} ||= [];
81 # prototype should match that of &MakeMaker::prompt
82 *_real_prompt = sub ($;$) {
84 my($ret) = defined $a ? $a : "";
85 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} || File::Spec->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 unless (File::Spec->file_name_is_absolute($ans)) {
121 my $cwd = Cwd::cwd();
122 my $absans = File::Spec->catdir($cwd,$ans);
123 warn "The path '$ans' is not an absolute path. Please specify an absolute path\n";
127 eval { File::Path::mkpath($ans); }; # dies if it can't
129 warn "Couldn't create directory $ans.
133 if (-d $ans && -w _) {
136 warn "Couldn't find directory $ans
137 or directory is not writable. Please retry.\n";
140 $CPAN::Config->{cpan_home} = $ans;
144 If you want, I can keep the source files after a build in the cpan
145 home directory. If you choose so then future builds will take the
146 files from there. If you don\'t want to keep them, answer 0 to the
151 $CPAN::Config->{keep_source_where} = File::Spec->catdir($CPAN::Config->{cpan_home},"sources");
152 $CPAN::Config->{build_dir} = File::Spec->catdir($CPAN::Config->{cpan_home},"build");
155 # Cache size, Index expire
160 How big should the disk cache be for keeping the build directories
161 with all the intermediate files\?
165 $default = $CPAN::Config->{build_cache} || 10;
166 $ans = prompt("Cache size for build directory (in MB)?", $default);
167 $CPAN::Config->{build_cache} = $ans;
169 # XXX This the time when we refetch the index files (in days)
170 $CPAN::Config->{'index_expire'} = 1;
174 By default, each time the CPAN module is started, cache scanning
175 is performed to keep the cache size in sync. To prevent from this,
176 disable the cache scanning with 'never'.
180 $default = $CPAN::Config->{scan_cache} || 'atstart';
182 $ans = prompt("Perform cache scanning (atstart or never)?", $default);
183 } while ($ans ne 'atstart' && $ans ne 'never');
184 $CPAN::Config->{scan_cache} = $ans;
191 To considerably speed up the initial CPAN shell startup, it is
192 possible to use Storable to create a cache of metadata. If Storable
193 is not available, the normal index mechanism will be used.
197 defined($default = $CPAN::Config->{cache_metadata}) or $default = 1;
199 $ans = prompt("Cache metadata (yes/no)?", ($default ? 'yes' : 'no'));
200 } while ($ans !~ /^[yn]/i);
201 $CPAN::Config->{cache_metadata} = ($ans =~ /^y/i ? 1 : 0);
208 The next option deals with the charset your terminal supports. In
209 general CPAN is English speaking territory, thus the charset does not
210 matter much, but some of the aliens out there who upload their
211 software to CPAN bear names that are outside the ASCII range. If your
212 terminal supports UTF-8, you say no to the next question, if it
213 supports ISO-8859-1 (also known as LATIN1) then you say yes, and if it
214 supports neither nor, your answer does not matter, you will not be
215 able to read the names of some authors anyway. If you answer no, names
216 will be output in UTF-8.
220 defined($default = $CPAN::Config->{term_is_latin}) or $default = 1;
222 $ans = prompt("Your terminal expects ISO-8859-1 (yes/no)?",
223 ($default ? 'yes' : 'no'));
224 } while ($ans !~ /^[yn]/i);
225 $CPAN::Config->{term_is_latin} = ($ans =~ /^y/i ? 1 : 0);
228 # save history in file histfile
232 If you have one of the readline packages (Term::ReadLine::Perl,
233 Term::ReadLine::Gnu, possibly others) installed, the interactive CPAN
234 shell will have history support. The next two questions deal with the
235 filename of the history file and with its size. If you do not want to
236 set this variable, please hit SPACE RETURN to the following question.
240 defined($default = $CPAN::Config->{histfile}) or
241 $default = File::Spec->catfile($CPAN::Config->{cpan_home},"histfile");
242 $ans = prompt("File to save your history?", $default);
243 $CPAN::Config->{histfile} = $ans;
245 if ($CPAN::Config->{histfile}) {
246 defined($default = $CPAN::Config->{histsize}) or $default = 100;
247 $ans = prompt("Number of lines to save?", $default);
248 $CPAN::Config->{histsize} = $ans;
252 # prerequisites_policy
253 # Do we follow PREREQ_PM?
257 The CPAN module can detect when a module that which you are trying to
258 build depends on prerequisites. If this happens, it can build the
259 prerequisites for you automatically ('follow'), ask you for
260 confirmation ('ask'), or just ignore them ('ignore'). Please set your
261 policy to one of the three values.
265 $default = $CPAN::Config->{prerequisites_policy} || 'ask';
268 prompt("Policy on building prerequisites (follow, ask or ignore)?",
270 } while ($ans ne 'follow' && $ans ne 'ask' && $ans ne 'ignore');
271 $CPAN::Config->{prerequisites_policy} = $ans;
279 The CPAN module will need a few external programs to work properly.
280 Please correct me, if I guess the wrong path for a program. Don\'t
281 panic if you do not have some of them, just press ENTER for those. To
282 disable the use of a download program, you can type a space followed
288 local $^W if $^O eq 'MacOS';
289 my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
290 local $^W = $old_warn;
292 for $progname (qw/gzip tar unzip make
293 curl lynx wget ncftpget ncftp ftp
296 if ($^O eq 'MacOS') {
297 $CPAN::Config->{$progname} = 'not_here';
300 my $progcall = $progname;
301 # we don't need ncftp if we have ncftpget
302 next if $progname eq "ncftp" && $CPAN::Config->{ncftpget} gt " ";
303 my $path = $CPAN::Config->{$progname}
304 || $Config::Config{$progname}
306 if (File::Spec->file_name_is_absolute($path)) {
307 # testing existence is not good enough, some have these exe
310 # warn "Warning: configured $path does not exist\n" unless -e $path;
317 $progcall = $Config::Config{$progname} if $Config::Config{$progname};
320 $path ||= find_exe($progcall,[@path]);
321 warn "Warning: $progcall not found in PATH\n" unless
322 $path; # not -e $path, because find_exe already checked that
323 $ans = prompt("Where is your $progname program?",$path) || $path;
324 $CPAN::Config->{$progname} = $ans;
326 my $path = $CPAN::Config->{'pager'} ||
327 $ENV{PAGER} || find_exe("less",[@path]) ||
328 find_exe("more",[@path]) || ($^O eq 'MacOS' ? $ENV{EDITOR} : 0 )
330 $ans = prompt("What is your favorite pager program?",$path);
331 $CPAN::Config->{'pager'} = $ans;
332 $path = $CPAN::Config->{'shell'};
333 if (File::Spec->file_name_is_absolute($path)) {
334 warn "Warning: configured $path does not exist\n" unless -e $path;
337 $path ||= $ENV{SHELL};
338 if ($^O eq 'MacOS') {
339 $CPAN::Config->{'shell'} = 'not_here';
341 $path =~ s,\\,/,g if $^O eq 'os2'; # Cosmetic only
342 $ans = prompt("What is your favorite shell?",$path);
343 $CPAN::Config->{'shell'} = $ans;
347 # Arguments to make etc.
352 Every Makefile.PL is run by perl in a separate process. Likewise we
353 run \'make\' and \'make install\' in processes. If you have any
354 parameters \(e.g. PREFIX, LIB, UNINST or the like\) you want to pass
355 to the calls, please specify them here.
357 If you don\'t understand this question, just press ENTER.
361 $default = $CPAN::Config->{makepl_arg} || "";
362 $CPAN::Config->{makepl_arg} =
363 prompt("Parameters for the 'perl Makefile.PL' command?
364 Typical frequently used settings:
366 PREFIX=~/perl non-root users (please see manual for more hints)
368 Your choice: ",$default);
369 $default = $CPAN::Config->{make_arg} || "";
370 $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command?
371 Typical frequently used setting:
373 -j3 dual processor system
375 Your choice: ",$default);
377 $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || "";
378 $CPAN::Config->{make_install_arg} =
379 prompt("Parameters for the 'make install' command?
380 Typical frequently used setting:
382 UNINST=1 to always uninstall potentially conflicting files
384 Your choice: ",$default);
392 Sometimes you may wish to leave the processes run by CPAN alone
393 without caring about them. As sometimes the Makefile.PL contains
394 question you\'re expected to answer, you can set a timer that will
395 kill a 'perl Makefile.PL' process after the specified time in seconds.
397 If you set this value to 0, these processes will wait forever. This is
398 the default and recommended setting.
402 $default = $CPAN::Config->{inactivity_timeout} || 0;
403 $CPAN::Config->{inactivity_timeout} =
404 prompt("Timeout for inactivity during Makefile.PL?",$default);
410 If you\'re accessing the net via proxies, you can specify them in the
411 CPAN configuration or via environment variables. The variable in
412 the \$CPAN::Config takes precedence.
416 for (qw/ftp_proxy http_proxy no_proxy/) {
417 $default = $CPAN::Config->{$_} || $ENV{$_};
418 $CPAN::Config->{$_} = prompt("Your $_?",$default);
421 if ($CPAN::Config->{ftp_proxy} ||
422 $CPAN::Config->{http_proxy}) {
423 $default = $CPAN::Config->{proxy_user} || $CPAN::LWP::UserAgent::USER;
426 If your proxy is an authenticating proxy, you can store your username
427 permanently. If you do not want that, just press RETURN. You will then
428 be asked for your username in every future session.
431 if ($CPAN::Config->{proxy_user} = prompt("Your proxy user id?",$default)) {
434 Your password for the authenticating proxy can also be stored
435 permanently on disk. If this violates your security policy, just press
436 RETURN. You will then be asked for the password in every future
441 if ($CPAN::META->has_inst("Term::ReadKey")) {
442 Term::ReadKey::ReadMode("noecho");
446 Warning: Term::ReadKey seems not to be available, your password will
447 be echoed to the terminal!
451 $CPAN::Config->{proxy_pass} = prompt_no_strip("Your proxy password?");
452 if ($CPAN::META->has_inst("Term::ReadKey")) {
453 Term::ReadKey::ReadMode("restore");
455 $CPAN::Frontend->myprint("\n\n");
463 conf_sites() unless $fastread;
465 # We don't ask that now, it will be noticed in time, won't it?
466 $CPAN::Config->{'inhibit_startup_message'} = 0;
467 $CPAN::Config->{'getcwd'} = 'cwd';
470 CPAN::Config->commit($configpm);
474 my $m = 'MIRRORED.BY';
475 my $mby = File::Spec->catfile($CPAN::Config->{keep_source_where},$m);
476 File::Path::mkpath(File::Basename::dirname($mby));
477 if (-f $mby && -f $m && -M $m < -M $mby) {
479 File::Copy::copy($m,$mby) or die "Could not update $mby: $!";
483 my $overwrite_local = 0;
484 if ($mby && -f $mby && -M _ <= 60 && -s _ > 0) {
485 my $mtime = localtime((stat _)[9]);
486 my $prompt = qq{Found $mby as of $mtime
488 I\'d use that as a database of CPAN sites. If that is OK for you,
489 please answer 'y', but if you want me to get a new database now,
490 please answer 'n' to the following question.
492 Shall I use the local database in $mby?};
493 my $ans = prompt($prompt,"y");
494 $overwrite_local = 1 unless $ans =~ /^y/i;
497 if ($overwrite_local) {
498 print qq{Trying to overwrite $mby
500 $mby = CPAN::FTP->localize($m,$mby,3);
501 $overwrite_local = 0;
502 } elsif ( ! -f $mby ){
503 print qq{You have no $mby
504 I\'m trying to fetch one
506 $mby = CPAN::FTP->localize($m,$mby,3);
507 } elsif (-M $mby > 60 && $loopcount == 0) {
508 print qq{Your $mby is older than 60 days,
509 I\'m trying to fetch one
511 $mby = CPAN::FTP->localize($m,$mby,3);
513 } elsif (-s $mby == 0) {
514 print qq{You have an empty $mby,
515 I\'m trying to fetch one
517 $mby = CPAN::FTP->localize($m,$mby,3);
522 read_mirrored_by($mby);
529 #warn "in find_exe exe[$exe] path[@$path]";
531 my $abs = File::Spec->catfile($dir,$exe);
532 if (($abs = MM->maybe_command($abs))) {
539 my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_;
547 # display, at most, 15 items at a time
548 my $limit = $#{ $items } - $pos;
549 $limit = 15 if $limit > 15;
551 # show the next $limit items, get the new position
552 $pos = display_some($items, $limit, $pos);
553 $pos = 0 if $pos >= @$items;
555 my $num = prompt($prompt,$default);
557 @nums = split (' ', $num);
558 my $i = scalar @$items;
559 (warn "invalid items entered, try again\n"), next
560 if grep (/\D/ || $_ < 1 || $_ > $i, @nums);
561 if ($require_nonempty) {
562 (warn "$empty_warning\n");
566 # a blank line continues...
575 my ($items, $limit, $pos) = @_;
578 my @displayable = @$items[$pos .. ($pos + $limit)];
579 for my $item (@displayable) {
580 printf "(%d) %s\n", ++$pos, $item;
582 printf("%d more items, hit SPACE RETURN to show them\n",
589 sub read_mirrored_by {
590 my $local = shift or return;
591 my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location);
592 my $fh = FileHandle->new;
593 $fh->open($local) or die "Couldn't open $local: $!";
596 ($host) = /^([\w\.\-]+)/ unless defined $host;
597 next unless defined $host;
598 next unless /\s+dst_(dst|location)/;
599 /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and
600 ($continent, $country) = @location[-1,-2];
601 $continent =~ s/\s\(.*//;
602 $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude
603 /dst_dst\s+=\s+\"([^\"]+)/ and $dst = $1;
604 next unless $host && $dst && $continent && $country;
605 $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst);
607 $dst=$continent=$country="";
610 $CPAN::Config->{urllist} ||= [];
612 if (@previous_urls = @{$CPAN::Config->{urllist}}) {
613 $CPAN::Config->{urllist} = [];
618 Now we need to know where your favorite CPAN sites are located. Push
619 a few sites onto the array (just in case the first on the array won\'t
620 work). If you are mirroring CPAN to your local workstation, specify a
623 First, pick a nearby continent and country (you can pick several of
624 each, separated by spaces, or none if you just want to keep your
625 existing selections). Then, you will be presented with a list of URLs
626 of CPAN mirrors in the countries you selected, along with previously
627 selected URLs. Select some of those URLs, or just keep the old list.
628 Finally, you will be prompted for any extra URLs -- file:, ftp:, or
629 http: -- that host a CPAN mirror.
633 my (@cont, $cont, %cont, @countries, @urls, %seen);
634 my $no_previous_warn =
635 "Sorry! since you don't have any existing picks, you must make a\n" .
636 "geographic selection.";
637 @cont = picklist([sort keys %all],
638 "Select your continent (or several nearby continents)",
644 foreach $cont (@cont) {
645 my @c = sort keys %{$all{$cont}};
646 @cont{@c} = map ($cont, 0..$#c);
647 @c = map ("$_ ($cont)", @c) if @cont > 1;
648 push (@countries, @c);
652 @countries = picklist (\@countries,
653 "Select your country (or several nearby countries)",
657 %seen = map (($_ => 1), @previous_urls);
658 # hmmm, should take list of defaults from CPAN::Config->{'urllist'}...
659 foreach $country (@countries) {
660 (my $bare_country = $country) =~ s/ \(.*\)//;
661 my @u = sort keys %{$all{$cont{$bare_country}}{$bare_country}};
662 @u = grep (! $seen{$_}, @u);
663 @u = map ("$_ ($bare_country)", @u)
668 push (@urls, map ("$_ (previous pick)", @previous_urls));
669 my $prompt = "Select as many URLs as you like (by number),
670 put them on one line, separated by blanks, e.g. '1 4 5'";
671 if (@previous_urls) {
672 $default = join (' ', ((scalar @urls) - (scalar @previous_urls) + 1) ..
674 $prompt .= "\n(or just hit RETURN to keep your previous picks)";
677 @urls = picklist (\@urls, $prompt, $default);
678 foreach (@urls) { s/ \(.*\)//; }
679 push @{$CPAN::Config->{urllist}}, @urls;
683 my %seen = map (($_ => 1), @{$CPAN::Config->{urllist}});
686 my $prompt = "Enter another URL or RETURN to quit:";
688 $prompt = qq{CPAN.pm needs at least one URL where it can fetch CPAN files from.
690 Please enter your CPAN site:};
692 $ans = prompt ($prompt, "");
695 $ans =~ s|/?\z|/|; # has to end with one slash
696 $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file:
697 if ($ans =~ /^\w+:\/./) {
698 push @urls, $ans unless $seen{$ans}++;
700 printf(qq{"%s" doesn\'t look like an URL at first sight.
701 I\'ll ignore it for now.
702 You can add it to your %s
703 later if you\'re sure it\'s right.\n},
705 $INC{'CPAN/MyConfig.pm'} || $INC{'CPAN/Config.pm'} || "configuration file",
709 } while $ans || !%seen;
711 push @{$CPAN::Config->{urllist}}, @urls;
712 # xxx delete or comment these out when you're happy that it works
713 print "New set of picks:\n";
714 map { print " $_\n" } @{$CPAN::Config->{urllist}};
719 $_[0] =~ s/^\s+//; # no leading spaces
720 $_[0] =~ s/\s+\z//; # no trailing spaces
725 my $ans = _real_prompt(@_);
733 sub prompt_no_strip ($;$) {
734 return _real_prompt(@_);
738 *_real_prompt = \*ExtUtils::MakeMaker::prompt;