Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

671 lines
20 KiB

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