Source code of Windows XP (NT5)
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.

528 lines
15 KiB

  1. package CPAN::Mirrored::By;
  2. sub new {
  3. my($self,@arg) = @_;
  4. bless [@arg], $self;
  5. }
  6. sub continent { shift->[0] }
  7. sub country { shift->[1] }
  8. sub url { shift->[2] }
  9. package CPAN::FirstTime;
  10. use strict;
  11. use ExtUtils::MakeMaker qw(prompt);
  12. use FileHandle ();
  13. use File::Basename ();
  14. use File::Path ();
  15. use vars qw($VERSION);
  16. $VERSION = substr q$Revision: 1.36 $, 10;
  17. =head1 NAME
  18. CPAN::FirstTime - Utility for CPAN::Config file Initialization
  19. =head1 SYNOPSIS
  20. CPAN::FirstTime::init()
  21. =head1 DESCRIPTION
  22. The init routine asks a few questions and writes a CPAN::Config
  23. file. Nothing special.
  24. =cut
  25. sub init {
  26. my($configpm) = @_;
  27. use Config;
  28. unless ($CPAN::VERSION) {
  29. require CPAN::Nox;
  30. }
  31. eval {require CPAN::Config;};
  32. $CPAN::Config ||= {};
  33. local($/) = "\n";
  34. local($\) = "";
  35. local($|) = 1;
  36. my($ans,$default,$local,$cont,$url,$expected_size);
  37. #
  38. # Files, directories
  39. #
  40. print qq[
  41. CPAN is the world-wide archive of perl resources. It consists of about
  42. 100 sites that all replicate the same contents all around the globe.
  43. Many countries have at least one CPAN site already. The resources
  44. found on CPAN are easily accessible with the CPAN.pm module. If you
  45. want to use CPAN.pm, you have to configure it properly.
  46. If you do not want to enter a dialog now, you can answer 'no' to this
  47. question and I\'ll try to autoconfigure. (Note: you can revisit this
  48. dialog anytime later by typing 'o conf init' at the cpan prompt.)
  49. ];
  50. my $manual_conf =
  51. ExtUtils::MakeMaker::prompt("Are you ready for manual configuration?",
  52. "yes");
  53. my $fastread;
  54. {
  55. local $^W;
  56. if ($manual_conf =~ /^\s*y/i) {
  57. $fastread = 0;
  58. *prompt = \&ExtUtils::MakeMaker::prompt;
  59. } else {
  60. $fastread = 1;
  61. *prompt = sub {
  62. my($q,$a) = @_;
  63. my($ret) = defined $a ? $a : "";
  64. printf qq{%s [%s]\n\n}, $q, $ret;
  65. $ret;
  66. };
  67. }
  68. }
  69. print qq{
  70. The following questions are intended to help you with the
  71. configuration. The CPAN module needs a directory of its own to cache
  72. important index files and maybe keep a temporary mirror of CPAN files.
  73. This may be a site-wide directory or a personal directory.
  74. };
  75. my $cpan_home = $CPAN::Config->{cpan_home} || MM->catdir($ENV{HOME}, ".cpan");
  76. if (-d $cpan_home) {
  77. print qq{
  78. I see you already have a directory
  79. $cpan_home
  80. Shall we use it as the general CPAN build and cache directory?
  81. };
  82. } else {
  83. print qq{
  84. First of all, I\'d like to create this directory. Where?
  85. };
  86. }
  87. $default = $cpan_home;
  88. while ($ans = prompt("CPAN build and cache directory?",$default)) {
  89. eval { File::Path::mkpath($ans); }; # dies if it can't
  90. if ($@) {
  91. warn "Couldn't create directory $ans.
  92. Please retry.\n";
  93. next;
  94. }
  95. if (-d $ans && -w _) {
  96. last;
  97. } else {
  98. warn "Couldn't find directory $ans
  99. or directory is not writable. Please retry.\n";
  100. }
  101. }
  102. $CPAN::Config->{cpan_home} = $ans;
  103. print qq{
  104. If you want, I can keep the source files after a build in the cpan
  105. home directory. If you choose so then future builds will take the
  106. files from there. If you don\'t want to keep them, answer 0 to the
  107. next question.
  108. };
  109. $CPAN::Config->{keep_source_where} = MM->catdir($CPAN::Config->{cpan_home},"sources");
  110. $CPAN::Config->{build_dir} = MM->catdir($CPAN::Config->{cpan_home},"build");
  111. #
  112. # Cache size, Index expire
  113. #
  114. print qq{
  115. How big should the disk cache be for keeping the build directories
  116. with all the intermediate files?
  117. };
  118. $default = $CPAN::Config->{build_cache} || 10;
  119. $ans = prompt("Cache size for build directory (in MB)?", $default);
  120. $CPAN::Config->{build_cache} = $ans;
  121. # XXX This the time when we refetch the index files (in days)
  122. $CPAN::Config->{'index_expire'} = 1;
  123. print qq{
  124. By default, each time the CPAN module is started, cache scanning
  125. is performed to keep the cache size in sync. To prevent from this,
  126. disable the cache scanning with 'never'.
  127. };
  128. $default = $CPAN::Config->{scan_cache} || 'atstart';
  129. do {
  130. $ans = prompt("Perform cache scanning (atstart or never)?", $default);
  131. } while ($ans ne 'atstart' && $ans ne 'never');
  132. $CPAN::Config->{scan_cache} = $ans;
  133. #
  134. # prerequisites_policy
  135. # Do we follow PREREQ_PM?
  136. #
  137. print qq{
  138. The CPAN module can detect when a module that which you are trying to
  139. build depends on prerequisites. If this happens, it can build the
  140. prerequisites for you automatically ('follow'), ask you for
  141. confirmation ('ask'), or just ignore them ('ignore'). Please set your
  142. policy to one of the three values.
  143. };
  144. $default = $CPAN::Config->{prerequisites_policy} || 'follow';
  145. do {
  146. $ans =
  147. prompt("Policy on building prerequisites (follow, ask or ignore)?",
  148. $default);
  149. } while ($ans ne 'follow' && $ans ne 'ask' && $ans ne 'ignore');
  150. $CPAN::Config->{prerequisites_policy} = $ans;
  151. #
  152. # External programs
  153. #
  154. print qq{
  155. The CPAN module will need a few external programs to work
  156. properly. Please correct me, if I guess the wrong path for a program.
  157. Don\'t panic if you do not have some of them, just press ENTER for
  158. those.
  159. };
  160. my $old_warn = $^W;
  161. local $^W if $^O eq 'MacOS';
  162. my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
  163. local $^W = $old_warn;
  164. my $progname;
  165. for $progname (qw/gzip tar unzip make lynx ncftpget ncftp ftp/){
  166. if ($^O eq 'MacOS') {
  167. $CPAN::Config->{$progname} = 'not_here';
  168. next;
  169. }
  170. my $progcall = $progname;
  171. # we don't need ncftp if we have ncftpget
  172. next if $progname eq "ncftp" && $CPAN::Config->{ncftpget} gt " ";
  173. my $path = $CPAN::Config->{$progname}
  174. || $Config::Config{$progname}
  175. || "";
  176. if (MM->file_name_is_absolute($path)) {
  177. # testing existence is not good enough, some have these exe
  178. # extensions
  179. # warn "Warning: configured $path does not exist\n" unless -e $path;
  180. # $path = "";
  181. } else {
  182. $path = '';
  183. }
  184. unless ($path) {
  185. # e.g. make -> nmake
  186. $progcall = $Config::Config{$progname} if $Config::Config{$progname};
  187. }
  188. $path ||= find_exe($progcall,[@path]);
  189. warn "Warning: $progcall not found in PATH\n" unless
  190. $path; # not -e $path, because find_exe already checked that
  191. $ans = prompt("Where is your $progname program?",$path) || $path;
  192. $CPAN::Config->{$progname} = $ans;
  193. }
  194. my $path = $CPAN::Config->{'pager'} ||
  195. $ENV{PAGER} || find_exe("less",[@path]) ||
  196. find_exe("more",[@path]) || ($^O eq 'MacOS' ? $ENV{EDITOR} : 0 )
  197. || "more";
  198. $ans = prompt("What is your favorite pager program?",$path);
  199. $CPAN::Config->{'pager'} = $ans;
  200. $path = $CPAN::Config->{'shell'};
  201. if (MM->file_name_is_absolute($path)) {
  202. warn "Warning: configured $path does not exist\n" unless -e $path;
  203. $path = "";
  204. }
  205. $path ||= $ENV{SHELL};
  206. if ($^O eq 'MacOS') {
  207. $CPAN::Config->{'shell'} = 'not_here';
  208. } else {
  209. $path =~ s,\\,/,g if $^O eq 'os2'; # Cosmetic only
  210. $ans = prompt("What is your favorite shell?",$path);
  211. $CPAN::Config->{'shell'} = $ans;
  212. }
  213. #
  214. # Arguments to make etc.
  215. #
  216. print qq{
  217. Every Makefile.PL is run by perl in a separate process. Likewise we
  218. run \'make\' and \'make install\' in processes. If you have any parameters
  219. \(e.g. PREFIX, INSTALLPRIVLIB, UNINST or the like\) you want to pass to
  220. the calls, please specify them here.
  221. If you don\'t understand this question, just press ENTER.
  222. };
  223. $default = $CPAN::Config->{makepl_arg} || "";
  224. $CPAN::Config->{makepl_arg} =
  225. prompt("Parameters for the 'perl Makefile.PL' command?",$default);
  226. $default = $CPAN::Config->{make_arg} || "";
  227. $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command?",$default);
  228. $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || "";
  229. $CPAN::Config->{make_install_arg} =
  230. prompt("Parameters for the 'make install' command?",$default);
  231. #
  232. # Alarm period
  233. #
  234. print qq{
  235. Sometimes you may wish to leave the processes run by CPAN alone
  236. without caring about them. As sometimes the Makefile.PL contains
  237. question you\'re expected to answer, you can set a timer that will
  238. kill a 'perl Makefile.PL' process after the specified time in seconds.
  239. If you set this value to 0, these processes will wait forever. This is
  240. the default and recommended setting.
  241. };
  242. $default = $CPAN::Config->{inactivity_timeout} || 0;
  243. $CPAN::Config->{inactivity_timeout} =
  244. prompt("Timeout for inactivity during Makefile.PL?",$default);
  245. # Proxies
  246. print qq{
  247. If you\'re accessing the net via proxies, you can specify them in the
  248. CPAN configuration or via environment variables. The variable in
  249. the \$CPAN::Config takes precedence.
  250. };
  251. for (qw/ftp_proxy http_proxy no_proxy/) {
  252. $default = $CPAN::Config->{$_} || $ENV{$_};
  253. $CPAN::Config->{$_} = prompt("Your $_?",$default);
  254. }
  255. #
  256. # MIRRORED.BY
  257. #
  258. conf_sites() unless $fastread;
  259. unless (@{$CPAN::Config->{'wait_list'}||[]}) {
  260. print qq{
  261. WAIT support is available as a Plugin. You need the CPAN::WAIT module
  262. to actually use it. But we need to know your favorite WAIT server. If
  263. you don\'t know a WAIT server near you, just press ENTER.
  264. };
  265. $default = "wait://ls6.informatik.uni-dortmund.de:1404";
  266. $ans = prompt("Your favorite WAIT server?\n ",$default);
  267. push @{$CPAN::Config->{'wait_list'}}, $ans;
  268. }
  269. # We don't ask that now, it will be noticed in time, won't it?
  270. $CPAN::Config->{'inhibit_startup_message'} = 0;
  271. $CPAN::Config->{'getcwd'} = 'cwd';
  272. print "\n\n";
  273. CPAN::Config->commit($configpm);
  274. }
  275. sub conf_sites {
  276. my $m = 'MIRRORED.BY';
  277. my $mby = MM->catfile($CPAN::Config->{keep_source_where},$m);
  278. File::Path::mkpath(File::Basename::dirname($mby));
  279. if (-f $mby && -f $m && -M $m < -M $mby) {
  280. require File::Copy;
  281. File::Copy::copy($m,$mby) or die "Could not update $mby: $!";
  282. }
  283. if ( ! -f $mby ){
  284. print qq{You have no $mby
  285. I\'m trying to fetch one
  286. };
  287. $mby = CPAN::FTP->localize($m,$mby,3);
  288. } elsif (-M $mby > 30 ) {
  289. print qq{Your $mby is older than 30 days,
  290. I\'m trying to fetch one
  291. };
  292. $mby = CPAN::FTP->localize($m,$mby,3);
  293. }
  294. read_mirrored_by($mby);
  295. }
  296. sub find_exe {
  297. my($exe,$path) = @_;
  298. my($dir);
  299. #warn "in find_exe exe[$exe] path[@$path]";
  300. for $dir (@$path) {
  301. my $abs = MM->catfile($dir,$exe);
  302. if (($abs = MM->maybe_command($abs))) {
  303. return $abs;
  304. }
  305. }
  306. }
  307. sub picklist {
  308. my($items,$prompt,$default,$require_nonempty,$empty_warning)=@_;
  309. $default ||= '';
  310. my ($item, $i);
  311. for $item (@$items) {
  312. printf "(%d) %s\n", ++$i, $item;
  313. }
  314. my @nums;
  315. while (1) {
  316. my $num = prompt($prompt,$default);
  317. @nums = split (' ', $num);
  318. (warn "invalid items entered, try again\n"), next
  319. if grep (/\D/ || $_ < 1 || $_ > $i, @nums);
  320. if ($require_nonempty) {
  321. (warn "$empty_warning\n"), next
  322. unless @nums;
  323. }
  324. last;
  325. }
  326. print "\n";
  327. for (@nums) { $_-- }
  328. @{$items}[@nums];
  329. }
  330. sub read_mirrored_by {
  331. my($local) = @_;
  332. my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location);
  333. my $fh = FileHandle->new;
  334. $fh->open($local) or die "Couldn't open $local: $!";
  335. local $/ = "\012";
  336. while (<$fh>) {
  337. ($host) = /^([\w\.\-]+)/ unless defined $host;
  338. next unless defined $host;
  339. next unless /\s+dst_(dst|location)/;
  340. /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and
  341. ($continent, $country) = @location[-1,-2];
  342. $continent =~ s/\s\(.*//;
  343. $continent =~ s/\W+$//; # if Jarkko doesn't know latitude/longitude
  344. /dst_dst\s+=\s+\"([^\"]+)/ and $dst = $1;
  345. next unless $host && $dst && $continent && $country;
  346. $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst);
  347. undef $host;
  348. $dst=$continent=$country="";
  349. }
  350. $fh->close;
  351. $CPAN::Config->{urllist} ||= [];
  352. my(@previous_urls);
  353. if (@previous_urls = @{$CPAN::Config->{urllist}}) {
  354. $CPAN::Config->{urllist} = [];
  355. }
  356. print qq{
  357. Now we need to know where your favorite CPAN sites are located. Push
  358. a few sites onto the array (just in case the first on the array won\'t
  359. work). If you are mirroring CPAN to your local workstation, specify a
  360. file: URL.
  361. First, pick a nearby continent and country (you can pick several of
  362. each, separated by spaces, or none if you just want to keep your
  363. existing selections). Then, you will be presented with a list of URLs
  364. of CPAN mirrors in the countries you selected, along with previously
  365. selected URLs. Select some of those URLs, or just keep the old list.
  366. Finally, you will be prompted for any extra URLs -- file:, ftp:, or
  367. http: -- that host a CPAN mirror.
  368. };
  369. my (@cont, $cont, %cont, @countries, @urls, %seen);
  370. my $no_previous_warn =
  371. "Sorry! since you don't have any existing picks, you must make a\n" .
  372. "geographic selection.";
  373. @cont = picklist([sort keys %all],
  374. "Select your continent (or several nearby continents)",
  375. '',
  376. ! @previous_urls,
  377. $no_previous_warn);
  378. foreach $cont (@cont) {
  379. my @c = sort keys %{$all{$cont}};
  380. @cont{@c} = map ($cont, 0..$#c);
  381. @c = map ("$_ ($cont)", @c) if @cont > 1;
  382. push (@countries, @c);
  383. }
  384. if (@countries) {
  385. @countries = picklist (\@countries,
  386. "Select your country (or several nearby countries)",
  387. '',
  388. ! @previous_urls,
  389. $no_previous_warn);
  390. %seen = map (($_ => 1), @previous_urls);
  391. # hmmm, should take list of defaults from CPAN::Config->{'urllist'}...
  392. foreach $country (@countries) {
  393. (my $bare_country = $country) =~ s/ \(.*\)//;
  394. my @u = sort keys %{$all{$cont{$bare_country}}{$bare_country}};
  395. @u = grep (! $seen{$_}, @u);
  396. @u = map ("$_ ($bare_country)", @u)
  397. if @countries > 1;
  398. push (@urls, @u);
  399. }
  400. }
  401. push (@urls, map ("$_ (previous pick)", @previous_urls));
  402. my $prompt = "Select as many URLs as you like";
  403. if (@previous_urls) {
  404. $default = join (' ', ((scalar @urls) - (scalar @previous_urls) + 1) ..
  405. (scalar @urls));
  406. $prompt .= "\n(or just hit RETURN to keep your previous picks)";
  407. }
  408. @urls = picklist (\@urls, $prompt, $default);
  409. foreach (@urls) { s/ \(.*\)//; }
  410. %seen = map (($_ => 1), @urls);
  411. do {
  412. $ans = prompt ("Enter another URL or RETURN to quit:", "");
  413. if ($ans) {
  414. $ans =~ s|/?$|/|; # has to end with one slash
  415. $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file:
  416. if ($ans =~ /^\w+:\/./) {
  417. push @urls, $ans
  418. unless $seen{$ans};
  419. }
  420. else {
  421. print qq{"$ans" doesn\'t look like an URL at first sight.
  422. I\'ll ignore it for now. You can add it to $INC{'CPAN/MyConfig.pm'}
  423. later if you\'re sure it\'s right.\n};
  424. }
  425. }
  426. } while $ans;
  427. push @{$CPAN::Config->{urllist}}, @urls;
  428. # xxx delete or comment these out when you're happy that it works
  429. print "New set of picks:\n";
  430. map { print " $_\n" } @{$CPAN::Config->{urllist}};
  431. }
  432. 1;