Commit | Line | Data |
8d97e4a1 |
1 | # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*- |
5f05dabc |
2 | package CPAN::Mirrored::By; |
3 | |
4 | sub new { |
5 | my($self,@arg) = @_; |
6 | bless [@arg], $self; |
7 | } |
da199366 |
8 | sub continent { shift->[0] } |
9 | sub country { shift->[1] } |
5f05dabc |
10 | sub url { shift->[2] } |
11 | |
12 | package CPAN::FirstTime; |
13 | |
14 | use strict; |
15 | use ExtUtils::MakeMaker qw(prompt); |
05454584 |
16 | use FileHandle (); |
09d9d230 |
17 | use File::Basename (); |
05454584 |
18 | use File::Path (); |
5de3f0da |
19 | use File::Spec; |
5f05dabc |
20 | use vars qw($VERSION); |
5fc0f0f6 |
21 | $VERSION = substr q$Revision: 1.58 $, 10; |
5f05dabc |
22 | |
23 | =head1 NAME |
24 | |
25 | CPAN::FirstTime - Utility for CPAN::Config file Initialization |
26 | |
27 | =head1 SYNOPSIS |
28 | |
29 | CPAN::FirstTime::init() |
30 | |
31 | =head1 DESCRIPTION |
32 | |
33 | The init routine asks a few questions and writes a CPAN::Config |
34 | file. Nothing special. |
35 | |
36 | =cut |
37 | |
38 | |
39 | sub init { |
40 | my($configpm) = @_; |
41 | use Config; |
f610777f |
42 | unless ($CPAN::VERSION) { |
43 | require CPAN::Nox; |
44 | } |
5f05dabc |
45 | eval {require CPAN::Config;}; |
46 | $CPAN::Config ||= {}; |
da199366 |
47 | local($/) = "\n"; |
48 | local($\) = ""; |
13bc20ff |
49 | local($|) = 1; |
da199366 |
50 | |
5fc0f0f6 |
51 | my($ans,$default); |
f610777f |
52 | |
da199366 |
53 | # |
54 | # Files, directories |
55 | # |
56 | |
2e2b7522 |
57 | print qq[ |
09d9d230 |
58 | |
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. |
64 | |
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.) |
68 | |
2e2b7522 |
69 | ]; |
09d9d230 |
70 | |
71 | my $manual_conf = |
72 | ExtUtils::MakeMaker::prompt("Are you ready for manual configuration?", |
73 | "yes"); |
74 | my $fastread; |
75 | { |
76 | local $^W; |
77 | if ($manual_conf =~ /^\s*y/i) { |
78 | $fastread = 0; |
79 | *prompt = \&ExtUtils::MakeMaker::prompt; |
80 | } else { |
81 | $fastread = 1; |
36263cb3 |
82 | $CPAN::Config->{urllist} ||= []; |
c9d9b473 |
83 | # prototype should match that of &MakeMaker::prompt |
84 | *prompt = sub ($;$) { |
09d9d230 |
85 | my($q,$a) = @_; |
86 | my($ret) = defined $a ? $a : ""; |
87 | printf qq{%s [%s]\n\n}, $q, $ret; |
88 | $ret; |
89 | }; |
90 | } |
91 | } |
92 | print qq{ |
93 | |
94 | The following questions are intended to help you with the |
95 | configuration. The CPAN module needs a directory of its own to cache |
96 | important index files and maybe keep a temporary mirror of CPAN files. |
97 | This may be a site-wide directory or a personal directory. |
98 | |
5f05dabc |
99 | }; |
100 | |
5de3f0da |
101 | my $cpan_home = $CPAN::Config->{cpan_home} || File::Spec->catdir($ENV{HOME}, ".cpan"); |
5f05dabc |
102 | if (-d $cpan_home) { |
103 | print qq{ |
104 | |
105 | I see you already have a directory |
106 | $cpan_home |
107 | Shall we use it as the general CPAN build and cache directory? |
108 | |
109 | }; |
110 | } else { |
111 | print qq{ |
112 | |
113 | First of all, I\'d like to create this directory. Where? |
114 | |
115 | }; |
116 | } |
117 | |
118 | $default = $cpan_home; |
05454584 |
119 | while ($ans = prompt("CPAN build and cache directory?",$default)) { |
5fc0f0f6 |
120 | unless (File::Spec->file_name_is_absolute($ans)) { |
121 | require Cwd; |
122 | my $cwd = Cwd::cwd(); |
123 | my $absans = File::Spec->catdir($cwd,$ans); |
124 | warn "The path '$ans' is not an absolute path. Please specify an absolute path\n"; |
125 | $default = $absans; |
126 | next; |
127 | } |
36263cb3 |
128 | eval { File::Path::mkpath($ans); }; # dies if it can't |
129 | if ($@) { |
130 | warn "Couldn't create directory $ans. |
131 | Please retry.\n"; |
132 | next; |
133 | } |
134 | if (-d $ans && -w _) { |
135 | last; |
136 | } else { |
137 | warn "Couldn't find directory $ans |
10b2abe6 |
138 | or directory is not writable. Please retry.\n"; |
36263cb3 |
139 | } |
10b2abe6 |
140 | } |
5f05dabc |
141 | $CPAN::Config->{cpan_home} = $ans; |
f610777f |
142 | |
5f05dabc |
143 | print qq{ |
144 | |
145 | If you want, I can keep the source files after a build in the cpan |
146 | home directory. If you choose so then future builds will take the |
147 | files from there. If you don\'t want to keep them, answer 0 to the |
148 | next question. |
149 | |
150 | }; |
151 | |
5de3f0da |
152 | $CPAN::Config->{keep_source_where} = File::Spec->catdir($CPAN::Config->{cpan_home},"sources"); |
153 | $CPAN::Config->{build_dir} = File::Spec->catdir($CPAN::Config->{cpan_home},"build"); |
5f05dabc |
154 | |
da199366 |
155 | # |
156 | # Cache size, Index expire |
157 | # |
158 | |
5f05dabc |
159 | print qq{ |
160 | |
161 | How big should the disk cache be for keeping the build directories |
de34a54b |
162 | with all the intermediate files\? |
5f05dabc |
163 | |
164 | }; |
165 | |
166 | $default = $CPAN::Config->{build_cache} || 10; |
167 | $ans = prompt("Cache size for build directory (in MB)?", $default); |
168 | $CPAN::Config->{build_cache} = $ans; |
169 | |
170 | # XXX This the time when we refetch the index files (in days) |
171 | $CPAN::Config->{'index_expire'} = 1; |
172 | |
f610777f |
173 | print qq{ |
174 | |
175 | By default, each time the CPAN module is started, cache scanning |
176 | is performed to keep the cache size in sync. To prevent from this, |
177 | disable the cache scanning with 'never'. |
178 | |
179 | }; |
180 | |
181 | $default = $CPAN::Config->{scan_cache} || 'atstart'; |
182 | do { |
183 | $ans = prompt("Perform cache scanning (atstart or never)?", $default); |
184 | } while ($ans ne 'atstart' && $ans ne 'never'); |
185 | $CPAN::Config->{scan_cache} = $ans; |
186 | |
9d61fa1d |
187 | # |
188 | # cache_metadata |
189 | # |
5e05dca5 |
190 | print qq{ |
191 | |
5a5fac02 |
192 | To considerably speed up the initial CPAN shell startup, it is |
193 | possible to use Storable to create a cache of metadata. If Storable |
194 | is not available, the normal index mechanism will be used. |
5e05dca5 |
195 | |
196 | }; |
197 | |
5a5fac02 |
198 | defined($default = $CPAN::Config->{cache_metadata}) or $default = 1; |
5e05dca5 |
199 | do { |
200 | $ans = prompt("Cache metadata (yes/no)?", ($default ? 'yes' : 'no')); |
201 | } while ($ans !~ /^\s*[yn]/i); |
202 | $CPAN::Config->{cache_metadata} = ($ans =~ /^\s*y/i ? 1 : 0); |
203 | |
f610777f |
204 | # |
9d61fa1d |
205 | # term_is_latin |
206 | # |
207 | print qq{ |
208 | |
209 | The next option deals with the charset your terminal supports. In |
210 | general CPAN is English speaking territory, thus the charset does not |
211 | matter much, but some of the aliens out there who upload their |
212 | software to CPAN bear names that are outside the ASCII range. If your |
213 | terminal supports UTF-8, you say no to the next question, if it |
214 | supports ISO-8859-1 (also known as LATIN1) then you say yes, and if it |
215 | supports neither nor, your answer does not matter, you will not be |
c049f953 |
216 | able to read the names of some authors anyway. If you answer no, names |
9d61fa1d |
217 | will be output in UTF-8. |
218 | |
219 | }; |
220 | |
221 | defined($default = $CPAN::Config->{term_is_latin}) or $default = 1; |
222 | do { |
223 | $ans = prompt("Your terminal expects ISO-8859-1 (yes/no)?", |
224 | ($default ? 'yes' : 'no')); |
225 | } while ($ans !~ /^\s*[yn]/i); |
226 | $CPAN::Config->{term_is_latin} = ($ans =~ /^\s*y/i ? 1 : 0); |
227 | |
228 | # |
5fc0f0f6 |
229 | # save history in file histfile |
230 | # |
231 | print qq{ |
232 | |
233 | If you have one of the readline packages (Term::ReadLine::Perl, |
234 | Term::ReadLine::Gnu, possibly others) installed, the interactive CPAN |
235 | shell will have history support. The next two questions deal with the |
236 | filename of the history file and with its size. If you do not want to |
237 | set this variable, please hit SPACE RETURN to the following question. |
238 | |
239 | }; |
240 | |
241 | defined($default = $CPAN::Config->{histfile}) or |
242 | $default = File::Spec->catfile($CPAN::Config->{cpan_home},"histfile"); |
243 | $ans = prompt("File to save your history?", $default); |
244 | $ans =~ s/^\s+//; |
245 | $ans =~ s/\s+\z//; |
246 | $CPAN::Config->{histfile} = $ans; |
247 | |
248 | if ($CPAN::Config->{histfile}) { |
249 | defined($default = $CPAN::Config->{histsize}) or $default = 100; |
250 | $ans = prompt("Number of lines to save?", $default); |
251 | $CPAN::Config->{histsize} = $ans; |
252 | } |
253 | |
254 | # |
f610777f |
255 | # prerequisites_policy |
256 | # Do we follow PREREQ_PM? |
257 | # |
258 | print qq{ |
259 | |
260 | The CPAN module can detect when a module that which you are trying to |
261 | build depends on prerequisites. If this happens, it can build the |
262 | prerequisites for you automatically ('follow'), ask you for |
263 | confirmation ('ask'), or just ignore them ('ignore'). Please set your |
264 | policy to one of the three values. |
265 | |
266 | }; |
267 | |
de34a54b |
268 | $default = $CPAN::Config->{prerequisites_policy} || 'ask'; |
f610777f |
269 | do { |
f14b5cec |
270 | $ans = |
271 | prompt("Policy on building prerequisites (follow, ask or ignore)?", |
272 | $default); |
f610777f |
273 | } while ($ans ne 'follow' && $ans ne 'ask' && $ans ne 'ignore'); |
274 | $CPAN::Config->{prerequisites_policy} = $ans; |
275 | |
da199366 |
276 | # |
277 | # External programs |
278 | # |
279 | |
5f05dabc |
280 | print qq{ |
281 | |
9d61fa1d |
282 | The CPAN module will need a few external programs to work properly. |
283 | Please correct me, if I guess the wrong path for a program. Don\'t |
284 | panic if you do not have some of them, just press ENTER for those. To |
285 | disable the use of a download program, you can type a space followed |
286 | by ENTER. |
5f05dabc |
287 | |
288 | }; |
289 | |
f14b5cec |
290 | my $old_warn = $^W; |
291 | local $^W if $^O eq 'MacOS'; |
55e314ee |
292 | my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'}; |
f14b5cec |
293 | local $^W = $old_warn; |
09d9d230 |
294 | my $progname; |
9d61fa1d |
295 | for $progname (qw/gzip tar unzip make lynx wget ncftpget ncftp ftp/){ |
f14b5cec |
296 | if ($^O eq 'MacOS') { |
297 | $CPAN::Config->{$progname} = 'not_here'; |
298 | next; |
299 | } |
09d9d230 |
300 | my $progcall = $progname; |
2e2b7522 |
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} |
305 | || ""; |
5de3f0da |
306 | if (File::Spec->file_name_is_absolute($path)) { |
2e2b7522 |
307 | # testing existence is not good enough, some have these exe |
308 | # extensions |
309 | |
310 | # warn "Warning: configured $path does not exist\n" unless -e $path; |
311 | # $path = ""; |
312 | } else { |
313 | $path = ''; |
314 | } |
315 | unless ($path) { |
316 | # e.g. make -> nmake |
317 | $progcall = $Config::Config{$progname} if $Config::Config{$progname}; |
318 | } |
09d9d230 |
319 | |
2e2b7522 |
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; |
5f05dabc |
325 | } |
326 | my $path = $CPAN::Config->{'pager'} || |
327 | $ENV{PAGER} || find_exe("less",[@path]) || |
f14b5cec |
328 | find_exe("more",[@path]) || ($^O eq 'MacOS' ? $ENV{EDITOR} : 0 ) |
329 | || "more"; |
55e314ee |
330 | $ans = prompt("What is your favorite pager program?",$path); |
5f05dabc |
331 | $CPAN::Config->{'pager'} = $ans; |
55e314ee |
332 | $path = $CPAN::Config->{'shell'}; |
5de3f0da |
333 | if (File::Spec->file_name_is_absolute($path)) { |
55e314ee |
334 | warn "Warning: configured $path does not exist\n" unless -e $path; |
335 | $path = ""; |
336 | } |
337 | $path ||= $ENV{SHELL}; |
f14b5cec |
338 | if ($^O eq 'MacOS') { |
339 | $CPAN::Config->{'shell'} = 'not_here'; |
340 | } else { |
341 | $path =~ s,\\,/,g if $^O eq 'os2'; # Cosmetic only |
342 | $ans = prompt("What is your favorite shell?",$path); |
343 | $CPAN::Config->{'shell'} = $ans; |
344 | } |
da199366 |
345 | |
346 | # |
347 | # Arguments to make etc. |
348 | # |
349 | |
5f05dabc |
350 | print qq{ |
351 | |
da199366 |
352 | Every Makefile.PL is run by perl in a separate process. Likewise we |
8d97e4a1 |
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. |
5f05dabc |
356 | |
05454584 |
357 | If you don\'t understand this question, just press ENTER. |
358 | |
5f05dabc |
359 | }; |
360 | |
361 | $default = $CPAN::Config->{makepl_arg} || ""; |
362 | $CPAN::Config->{makepl_arg} = |
8d97e4a1 |
363 | prompt("Parameters for the 'perl Makefile.PL' command? |
364 | Typical frequently used settings: |
365 | |
366 | POLLUTE=1 increasing backwards compatibility |
367 | LIB=~/perl non-root users (please see manual for more hints) |
368 | |
369 | Your choice: ",$default); |
5f05dabc |
370 | $default = $CPAN::Config->{make_arg} || ""; |
8d97e4a1 |
371 | $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command? |
372 | Typical frequently used setting: |
373 | |
374 | -j3 dual processor system |
375 | |
376 | Your choice: ",$default); |
5f05dabc |
377 | |
378 | $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || ""; |
379 | $CPAN::Config->{make_install_arg} = |
8d97e4a1 |
380 | prompt("Parameters for the 'make install' command? |
381 | Typical frequently used setting: |
382 | |
383 | UNINST=1 to always uninstall potentially conflicting files |
384 | |
385 | Your choice: ",$default); |
5f05dabc |
386 | |
da199366 |
387 | # |
388 | # Alarm period |
389 | # |
390 | |
10b2abe6 |
391 | print qq{ |
392 | |
393 | Sometimes you may wish to leave the processes run by CPAN alone |
394 | without caring about them. As sometimes the Makefile.PL contains |
395 | question you\'re expected to answer, you can set a timer that will |
396 | kill a 'perl Makefile.PL' process after the specified time in seconds. |
397 | |
e50380aa |
398 | If you set this value to 0, these processes will wait forever. This is |
399 | the default and recommended setting. |
10b2abe6 |
400 | |
401 | }; |
402 | |
403 | $default = $CPAN::Config->{inactivity_timeout} || 0; |
404 | $CPAN::Config->{inactivity_timeout} = |
09d9d230 |
405 | prompt("Timeout for inactivity during Makefile.PL?",$default); |
10b2abe6 |
406 | |
09d9d230 |
407 | # Proxies |
da199366 |
408 | |
09d9d230 |
409 | print qq{ |
10b2abe6 |
410 | |
09d9d230 |
411 | If you\'re accessing the net via proxies, you can specify them in the |
412 | CPAN configuration or via environment variables. The variable in |
413 | the \$CPAN::Config takes precedence. |
5f05dabc |
414 | |
05454584 |
415 | }; |
09d9d230 |
416 | |
417 | for (qw/ftp_proxy http_proxy no_proxy/) { |
418 | $default = $CPAN::Config->{$_} || $ENV{$_}; |
419 | $CPAN::Config->{$_} = prompt("Your $_?",$default); |
5f05dabc |
420 | } |
421 | |
c049f953 |
422 | if ($CPAN::Config->{ftp_proxy} || |
423 | $CPAN::Config->{http_proxy}) { |
424 | $default = $CPAN::Config->{proxy_user} || $CPAN::LWP::UserAgent::USER; |
425 | print qq{ |
426 | |
427 | If your proxy is an authenticating proxy, you can store your username |
428 | permanently. If you do not want that, just press RETURN. You will then |
429 | be asked for your username in every future session. |
430 | |
431 | }; |
432 | if ($CPAN::Config->{proxy_user} = prompt("Your proxy user id?",$default)) { |
433 | print qq{ |
434 | |
435 | Your password for the authenticating proxy can also be stored |
436 | permanently on disk. If this violates your security policy, just press |
437 | RETURN. You will then be asked for the password in every future |
438 | session. |
439 | |
440 | }; |
441 | |
442 | if ($CPAN::META->has_inst("Term::ReadKey")) { |
443 | Term::ReadKey::ReadMode("noecho"); |
444 | } else { |
445 | print qq{ |
446 | |
447 | Warning: Term::ReadKey seems not to be available, your password will |
448 | be echoed to the terminal! |
449 | |
450 | }; |
451 | } |
452 | $CPAN::Config->{proxy_pass} = prompt("Your proxy password?"); |
453 | if ($CPAN::META->has_inst("Term::ReadKey")) { |
454 | Term::ReadKey::ReadMode("restore"); |
455 | } |
456 | $CPAN::Frontend->myprint("\n\n"); |
457 | } |
458 | } |
459 | |
09d9d230 |
460 | # |
461 | # MIRRORED.BY |
462 | # |
463 | |
464 | conf_sites() unless $fastread; |
465 | |
d4fd5c69 |
466 | unless (@{$CPAN::Config->{'wait_list'}||[]}) { |
467 | print qq{ |
da199366 |
468 | |
05454584 |
469 | WAIT support is available as a Plugin. You need the CPAN::WAIT module |
470 | to actually use it. But we need to know your favorite WAIT server. If |
471 | you don\'t know a WAIT server near you, just press ENTER. |
472 | |
473 | }; |
0362b508 |
474 | $default = "wait://ls6-www.informatik.uni-dortmund.de:1404"; |
d4fd5c69 |
475 | $ans = prompt("Your favorite WAIT server?\n ",$default); |
476 | push @{$CPAN::Config->{'wait_list'}}, $ans; |
477 | } |
05454584 |
478 | |
e50380aa |
479 | # We don't ask that now, it will be noticed in time, won't it? |
5f05dabc |
480 | $CPAN::Config->{'inhibit_startup_message'} = 0; |
e50380aa |
481 | $CPAN::Config->{'getcwd'} = 'cwd'; |
5f05dabc |
482 | |
483 | print "\n\n"; |
484 | CPAN::Config->commit($configpm); |
485 | } |
486 | |
09d9d230 |
487 | sub conf_sites { |
488 | my $m = 'MIRRORED.BY'; |
5de3f0da |
489 | my $mby = File::Spec->catfile($CPAN::Config->{keep_source_where},$m); |
09d9d230 |
490 | File::Path::mkpath(File::Basename::dirname($mby)); |
491 | if (-f $mby && -f $m && -M $m < -M $mby) { |
492 | require File::Copy; |
493 | File::Copy::copy($m,$mby) or die "Could not update $mby: $!"; |
494 | } |
911a92db |
495 | my $loopcount = 0; |
de34a54b |
496 | local $^T = time; |
d8773709 |
497 | my $overwrite_local = 0; |
498 | if ($mby && -f $mby && -M _ <= 60 && -s _ > 0) { |
499 | my $mtime = localtime((stat _)[9]); |
500 | my $prompt = qq{Found $mby as of $mtime |
501 | |
c049f953 |
502 | I\'d use that as a database of CPAN sites. If that is OK for you, |
503 | please answer 'y', but if you want me to get a new database now, |
504 | please answer 'n' to the following question. |
d8773709 |
505 | |
c049f953 |
506 | Shall I use the local database in $mby?}; |
d8773709 |
507 | my $ans = prompt($prompt,"y"); |
508 | $overwrite_local = 1 unless $ans =~ /^y/i; |
509 | } |
de34a54b |
510 | while ($mby) { |
d8773709 |
511 | if ($overwrite_local) { |
512 | print qq{Trying to overwrite $mby |
513 | }; |
514 | $mby = CPAN::FTP->localize($m,$mby,3); |
515 | $overwrite_local = 0; |
516 | } elsif ( ! -f $mby ){ |
36263cb3 |
517 | print qq{You have no $mby |
09d9d230 |
518 | I\'m trying to fetch one |
519 | }; |
36263cb3 |
520 | $mby = CPAN::FTP->localize($m,$mby,3); |
911a92db |
521 | } elsif (-M $mby > 60 && $loopcount == 0) { |
522 | print qq{Your $mby is older than 60 days, |
09d9d230 |
523 | I\'m trying to fetch one |
524 | }; |
36263cb3 |
525 | $mby = CPAN::FTP->localize($m,$mby,3); |
911a92db |
526 | $loopcount++; |
36263cb3 |
527 | } elsif (-s $mby == 0) { |
528 | print qq{You have an empty $mby, |
529 | I\'m trying to fetch one |
530 | }; |
531 | $mby = CPAN::FTP->localize($m,$mby,3); |
532 | } else { |
533 | last; |
534 | } |
09d9d230 |
535 | } |
536 | read_mirrored_by($mby); |
de34a54b |
537 | bring_your_own(); |
09d9d230 |
538 | } |
539 | |
5f05dabc |
540 | sub find_exe { |
541 | my($exe,$path) = @_; |
55e314ee |
542 | my($dir); |
543 | #warn "in find_exe exe[$exe] path[@$path]"; |
5f05dabc |
544 | for $dir (@$path) { |
5de3f0da |
545 | my $abs = File::Spec->catfile($dir,$exe); |
13bc20ff |
546 | if (($abs = MM->maybe_command($abs))) { |
5f05dabc |
547 | return $abs; |
548 | } |
549 | } |
550 | } |
551 | |
f610777f |
552 | sub picklist { |
553 | my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_; |
554 | $default ||= ''; |
555 | |
5fc0f0f6 |
556 | my $pos = 0; |
f610777f |
557 | |
558 | my @nums; |
559 | while (1) { |
ec385757 |
560 | |
5fc0f0f6 |
561 | # display, at most, 15 items at a time |
562 | my $limit = $#{ $items } - $pos; |
563 | $limit = 15 if $limit > 15; |
564 | |
565 | # show the next $limit items, get the new position |
566 | $pos = display_some($items, $limit, $pos); |
567 | $pos = 0 if $pos >= @$items; |
568 | |
569 | my $num = prompt($prompt,$default); |
570 | |
571 | @nums = split (' ', $num); |
572 | my $i = scalar @$items; |
573 | (warn "invalid items entered, try again\n"), next |
574 | if grep (/\D/ || $_ < 1 || $_ > $i, @nums); |
575 | if ($require_nonempty) { |
576 | (warn "$empty_warning\n"); |
577 | } |
578 | print "\n"; |
579 | |
580 | # a blank line continues... |
581 | next unless @nums; |
582 | last; |
f610777f |
583 | } |
f610777f |
584 | for (@nums) { $_-- } |
585 | @{$items}[@nums]; |
586 | } |
587 | |
ec385757 |
588 | sub display_some { |
589 | my ($items, $limit, $pos) = @_; |
590 | $pos ||= 0; |
591 | |
592 | my @displayable = @$items[$pos .. ($pos + $limit)]; |
593 | for my $item (@displayable) { |
594 | printf "(%d) %s\n", ++$pos, $item; |
595 | } |
5fc0f0f6 |
596 | printf("%d more items, hit SPACE RETURN to show them\n", |
597 | (@$items - $pos) |
598 | ) |
599 | if $pos < @$items; |
ec385757 |
600 | return $pos; |
601 | } |
602 | |
5f05dabc |
603 | sub read_mirrored_by { |
de34a54b |
604 | my $local = shift or return; |
5f05dabc |
605 | my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location); |
05454584 |
606 | my $fh = FileHandle->new; |
607 | $fh->open($local) or die "Couldn't open $local: $!"; |
f14b5cec |
608 | local $/ = "\012"; |
05454584 |
609 | while (<$fh>) { |
5f05dabc |
610 | ($host) = /^([\w\.\-]+)/ unless defined $host; |
611 | next unless defined $host; |
612 | next unless /\s+dst_(dst|location)/; |
613 | /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and |
614 | ($continent, $country) = @location[-1,-2]; |
615 | $continent =~ s/\s\(.*//; |
f610777f |
616 | $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude |
5f05dabc |
617 | /dst_dst\s+=\s+\"([^\"]+)/ and $dst = $1; |
618 | next unless $host && $dst && $continent && $country; |
619 | $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst); |
620 | undef $host; |
621 | $dst=$continent=$country=""; |
622 | } |
05454584 |
623 | $fh->close; |
5f05dabc |
624 | $CPAN::Config->{urllist} ||= []; |
f610777f |
625 | my(@previous_urls); |
626 | if (@previous_urls = @{$CPAN::Config->{urllist}}) { |
5f05dabc |
627 | $CPAN::Config->{urllist} = []; |
5f05dabc |
628 | } |
f610777f |
629 | |
5f05dabc |
630 | print qq{ |
631 | |
f610777f |
632 | Now we need to know where your favorite CPAN sites are located. Push |
5f05dabc |
633 | a few sites onto the array (just in case the first on the array won\'t |
634 | work). If you are mirroring CPAN to your local workstation, specify a |
635 | file: URL. |
636 | |
f610777f |
637 | First, pick a nearby continent and country (you can pick several of |
638 | each, separated by spaces, or none if you just want to keep your |
639 | existing selections). Then, you will be presented with a list of URLs |
640 | of CPAN mirrors in the countries you selected, along with previously |
641 | selected URLs. Select some of those URLs, or just keep the old list. |
642 | Finally, you will be prompted for any extra URLs -- file:, ftp:, or |
643 | http: -- that host a CPAN mirror. |
5f05dabc |
644 | |
645 | }; |
646 | |
f610777f |
647 | my (@cont, $cont, %cont, @countries, @urls, %seen); |
648 | my $no_previous_warn = |
649 | "Sorry! since you don't have any existing picks, you must make a\n" . |
650 | "geographic selection."; |
651 | @cont = picklist([sort keys %all], |
652 | "Select your continent (or several nearby continents)", |
653 | '', |
654 | ! @previous_urls, |
655 | $no_previous_warn); |
656 | |
657 | |
658 | foreach $cont (@cont) { |
659 | my @c = sort keys %{$all{$cont}}; |
660 | @cont{@c} = map ($cont, 0..$#c); |
661 | @c = map ("$_ ($cont)", @c) if @cont > 1; |
662 | push (@countries, @c); |
5f05dabc |
663 | } |
f610777f |
664 | |
665 | if (@countries) { |
666 | @countries = picklist (\@countries, |
667 | "Select your country (or several nearby countries)", |
668 | '', |
669 | ! @previous_urls, |
670 | $no_previous_warn); |
671 | %seen = map (($_ => 1), @previous_urls); |
672 | # hmmm, should take list of defaults from CPAN::Config->{'urllist'}... |
673 | foreach $country (@countries) { |
674 | (my $bare_country = $country) =~ s/ \(.*\)//; |
675 | my @u = sort keys %{$all{$cont{$bare_country}}{$bare_country}}; |
676 | @u = grep (! $seen{$_}, @u); |
677 | @u = map ("$_ ($bare_country)", @u) |
678 | if @countries > 1; |
679 | push (@urls, @u); |
680 | } |
681 | } |
682 | push (@urls, map ("$_ (previous pick)", @previous_urls)); |
5fc0f0f6 |
683 | my $prompt = "Select as many URLs as you like (by number), |
684 | put them on one line, separated by blanks, e.g. '1 4 5'"; |
f610777f |
685 | if (@previous_urls) { |
686 | $default = join (' ', ((scalar @urls) - (scalar @previous_urls) + 1) .. |
687 | (scalar @urls)); |
688 | $prompt .= "\n(or just hit RETURN to keep your previous picks)"; |
689 | } |
690 | |
691 | @urls = picklist (\@urls, $prompt, $default); |
692 | foreach (@urls) { s/ \(.*\)//; } |
de34a54b |
693 | push @{$CPAN::Config->{urllist}}, @urls; |
694 | } |
f610777f |
695 | |
de34a54b |
696 | sub bring_your_own { |
697 | my %seen = map (($_ => 1), @{$CPAN::Config->{urllist}}); |
698 | my($ans,@urls); |
f610777f |
699 | do { |
de34a54b |
700 | my $prompt = "Enter another URL or RETURN to quit:"; |
701 | unless (%seen) { |
702 | $prompt = qq{CPAN.pm needs at least one URL where it can fetch CPAN files from. |
703 | |
704 | Please enter your CPAN site:}; |
705 | } |
706 | $ans = prompt ($prompt, ""); |
f610777f |
707 | |
708 | if ($ans) { |
5fc0f0f6 |
709 | $ans =~ s/^\s+//; # no leading spaces |
710 | $ans =~ s/\s+\z//; # no trailing spaces |
de34a54b |
711 | $ans =~ s|/?\z|/|; # has to end with one slash |
f610777f |
712 | $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file: |
713 | if ($ans =~ /^\w+:\/./) { |
8d97e4a1 |
714 | push @urls, $ans unless $seen{$ans}++; |
de34a54b |
715 | } else { |
8d97e4a1 |
716 | printf(qq{"%s" doesn\'t look like an URL at first sight. |
717 | I\'ll ignore it for now. |
718 | You can add it to your %s |
719 | later if you\'re sure it\'s right.\n}, |
720 | $ans, |
721 | $INC{'CPAN/MyConfig.pm'} || $INC{'CPAN/Config.pm'} || "configuration file", |
722 | ); |
f610777f |
723 | } |
724 | } |
de34a54b |
725 | } while $ans || !%seen; |
f610777f |
726 | |
727 | push @{$CPAN::Config->{urllist}}, @urls; |
728 | # xxx delete or comment these out when you're happy that it works |
729 | print "New set of picks:\n"; |
730 | map { print " $_\n" } @{$CPAN::Config->{urllist}}; |
5f05dabc |
731 | } |
732 | |
733 | 1; |