[perl #43425] local $[: fix scoping during parser error handling.
[p5sagit/p5-mst-13.2.git] / lib / CPANPLUS / Config.pm
CommitLineData
6aaee015 1package CPANPLUS::Config;
2
3use strict;
4use warnings;
5
6use base 'Object::Accessor';
7
8use base 'CPANPLUS::Internals::Utils';
9
10use Config;
11use File::Spec;
12use Module::Load;
13use CPANPLUS;
14use CPANPLUS::Error;
15use CPANPLUS::Internals::Constants;
16
17use File::Basename qw[dirname];
18use IPC::Cmd qw[can_run];
19use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
20use Module::Load::Conditional qw[check_install];
21
622d31ac 22
23=pod
24
25=head1 NAME
26
27CPANPLUS::Config
28
29=head1 DESCRIPTION
30
31This module contains defaults and heuristics for configuration
32information for CPANPLUS. To change any of these values, please
33see the documentation in C<CPANPLUS::Configure>.
34
35Below you'll find a list of configuration types and keys, and
36their meaning.
37
38=head1 CONFIGURATION
39
40=cut
41
42### BAH! you can't have POD interleaved with a hash
43### declaration.. so declare every entry seperatedly :(
6aaee015 44my $Conf = {
45 '_fetch' => {
46 'blacklist' => [ 'ftp' ],
47 },
622d31ac 48
49 ### _source, _build and _mirror are supposed to be static
50 ### no changes should be needed unless pause/cpan changes
51 '_source' => {
52 'hosts' => 'MIRRORED.BY',
53 'auth' => '01mailrc.txt.gz',
54 'stored' => 'sourcefiles',
55 'dslip' => '03modlist.data.gz',
56 'update' => '86400',
57 'mod' => '02packages.details.txt.gz'
58 },
59 '_build' => {
60 'plugins' => 'plugins',
61 'moddir' => 'build',
62 'startdir' => '',
63 'distdir' => 'dist',
64 'autobundle' => 'autobundle',
65 'autobundle_prefix' => 'Snapshot',
66 'autdir' => 'authors',
67 'install_log_dir' => 'install-logs',
68 'sanity_check' => 1,
69 },
70 '_mirror' => {
71 'base' => 'authors/id/',
72 'auth' => 'authors/01mailrc.txt.gz',
73 'dslip' => 'modules/03modlist.data.gz',
74 'mod' => 'modules/02packages.details.txt.gz'
75 },
76};
77
78=head2 Section 'conf'
79
80=over 4
81
82=item hosts
83
84An array ref containing hosts entries to be queried for packages.
85
86An example entry would like this:
87
88 { 'scheme' => 'ftp',
89 'path' => '/pub/CPAN/',
90 'host' => 'ftp.cpan.org'
91 },
92
93=cut
94
95 ### default host list
96 $Conf->{'conf'}->{'hosts'} = [
6aaee015 97 {
98 'scheme' => 'ftp',
99 'path' => '/pub/CPAN/',
100 'host' => 'ftp.cpan.org'
101 },
102 {
103 'scheme' => 'http',
104 'path' => '/',
105 'host' => 'www.cpan.org'
106 },
107 {
108 'scheme' => 'ftp',
109 'path' => '/pub/CPAN/',
110 'host' => 'ftp.nl.uu.net'
111 },
112 {
113 'scheme' => 'ftp',
114 'path' => '/pub/CPAN/',
115 'host' => 'cpan.valueclick.com'
116 },
117 {
118 'scheme' => 'ftp',
119 'path' => '/pub/languages/perl/CPAN/',
120 'host' => 'ftp.funet.fi'
121 }
622d31ac 122 ];
123
124=item allow_build_interactivity
125
126Boolean flag to indicate whether 'perl Makefile.PL' and similar
127are run interactively or not. Defaults to 'true'.
128
129=cut
130
131 $Conf->{'conf'}->{'allow_build_interactivity'} = 1;
132
133=item base
134
135The directory CPANPLUS keeps all it's build and state information in.
136Defaults to ~/.cpanplus.
137
138=cut
139
140 $Conf->{'conf'}->{'base'} = File::Spec->catdir(
141 __PACKAGE__->_home_dir, DOT_CPANPLUS );
142
143=item buildflags
144
145Any flags to be passed to 'perl Build.PL'. See C<perldoc Module::Build>
146for details. Defaults to an empty string.
147
148=cut
149
150 $Conf->{'conf'}->{'buildflags'} = '';
151
152=item cpantest
153
154Boolean flag to indicate whether or not to mail test results of module
155installations to C<http://testers.cpan.org>. Defaults to 'false'.
156
157=cut
158
159 $Conf->{'conf'}->{'cpantest'} = 0;
160
161=item cpantest_mx
162
163String holding an explicit mailserver to use when sending out emails
164for C<http://testers.cpan.org>. An empty string will use your system
165settings. Defaults to an empty string.
166
167=cut
168
169 $Conf->{'conf'}->{'cpantest_mx'} = '';
170
171=item debug
172
173Boolean flag to enable or disable extensive debuggging information.
174Defaults to 'false'.
175
176=cut
177
178 $Conf->{'conf'}->{'debug'} = 0;
179
180=item dist_type
181
182Default distribution type to use when building packages. See C<cpan2dist>
183or C<CPANPLUS::Dist> for details. An empty string will not use any
184package building software. Defaults to an empty string.
185
186=cut
187
188 $Conf->{'conf'}->{'dist_type'} = '';
189
190=item email
191
192Email address to use for anonymous ftp access and as C<from> address
193when sending emails. Defaults to an C<example.com> address.
194
195=cut
196
197 $Conf->{'conf'}->{'email'} = DEFAULT_EMAIL;
198
199=item extractdir
200
201String containing the directory where fetched archives should be
202extracted. An empty string will use a directory under your C<base>
203directory. Defaults to an empty string.
204
205=cut
206
207 $Conf->{'conf'}->{'extractdir'} = '';
208
209=item fetchdir
210
211String containing the directory where fetched archives should be
212stored. An empty string will use a directory under your C<base>
213directory. Defaults to an empty string.
214
215=cut
216
217 $Conf->{'conf'}->{'fetchdir'} = '';
218
219=item flush
220
221Boolean indicating whether build failures, cache dirs etc should
222be flushed after every operation or not. Defaults to 'true'.
223
224=cut
225
226 $Conf->{'conf'}->{'flush'} = 1;
227
228=item force
229
230Boolean indicating whether files should be forcefully overwritten
231if they exist, modules should be installed when they fail tests,
232etc. Defaults to 'false'.
233
234=cut
235
236 $Conf->{'conf'}->{'force'} = 0;
237
238=item lib
239
240An array ref holding directories to be added to C<@INC> when CPANPLUS
241starts up. Defaults to an empty array reference.
242
243=cut
244
245 $Conf->{'conf'}->{'lib'} = [];
246
247=item makeflags
248
249A string holding flags that will be passed to the C<make> program
250when invoked. Defaults to an empty string.
251
252=cut
253
254 $Conf->{'conf'}->{'makeflags'} = '';
255
256=item makemakerflags
257
258A string holding flags that will be passed to C<perl Makefile.PL>
259when invoked. Defaults to an empty string.
260
261=cut
262
263 $Conf->{'conf'}->{'makemakerflags'} = '';
264
265=item md5
266
267A boolean indicating whether or not md5 checks should be done when
268an archive is fetched. Defaults to 'true' if you have C<Digest::MD5>
269installed, 'false' otherwise.
270
271=cut
272
273 $Conf->{'conf'}->{'md5'} = (
274 check_install( module => 'Digest::MD5' ) ? 1 : 0 );
275
276=item no_update
277
278A boolean indicating whether or not C<CPANPLUS>' source files should be
279updated or not. Defaults to 'false'.
280
281=cut
282
283 $Conf->{'conf'}->{'no_update'} = 0;
284
285=item passive
286
287A boolean indicating whether or not to use passive ftp connections.
288Defaults to 'true'.
289
290=cut
291
292 $Conf->{'conf'}->{'passive'} = 1;
293
294=item prefer_bin
295
296A boolean indicating whether or not to prefer command line programs
297over perl modules. Defaults to 'false' unless you do not have
298C<Compress::Zlib> installed (as that would mean we could not extract
299C<.tar.gz> files)
300
301=cut
6aaee015 302 ### if we dont have c::zlib, we'll need to use /bin/tar or we
303 ### can not extract any files. Good time to change the default
622d31ac 304 $Conf->{'conf'}->{'prefer_bin'} =
305 (eval {require Compress::Zlib; 1} ? 0 : 1 );
306
307=item prefer_makefile
308
309A boolean indicating whether or not prefer a C<Makefile.PL> over a
310C<Build.PL> file if both are present. Defaults to 'true'.
311
312=cut
313
314 $Conf->{'conf'}->{'prefer_makefile'} = 1;
315
316=item prereqs
317
318A digit indicating what to do when a package you are installing has a
319prerequisite. Options are:
320
321 0 Do not install
322 1 Install
323 2 Ask
324 3 Ignore (dangerous, install will probably fail!)
325
326The default is to ask.
327
328=cut
329
330 $Conf->{'conf'}->{'prereqs'} = PREREQ_ASK;
331
332=item shell
333
334A string holding the shell class you wish to start up when starting
335C<CPANPLUS> in interactive mode.
336
337Defaults to C<CPANPLUS::Shell::Default>, the default CPANPLUS shell.
338
339=cut
340
341 $Conf->{'conf'}->{'shell'} = 'CPANPLUS::Shell::Default';
342
343=item show_startup_tip
344
345A boolean indicating whether or not to show start up tips in the
346interactive shell. Defaults to 'true'.
347
348=cut
349
350 $Conf->{'conf'}->{'show_startup_tip'} = 1;
351
352=item signature
353
354A boolean indicating whether or not check signatures if packages are
355signed. Defaults to 'true' if you have C<gpg> or C<Crypt::OpenPGP>
356installed, 'false' otherwise.
357
358=cut
359
360 $Conf->{'conf'}->{'signature'} = do {
502c7995 361 check_install( module => 'Module::Signature', version => '0.06' )
362 and ( can_run('gpg') ||
363 check_install(module => 'Crypt::OpenPGP')
364 );
365 } ? 1 : 0;
622d31ac 366
367=item skiptest
368
369A boolean indicating whether or not to skip tests when installing modules.
370Defaults to 'false'.
371
372=cut
373
374 $Conf->{'conf'}->{'skiptest'} = 0;
375
376=item storable
377
378A boolean indicating whether or not to use C<Storable> to write compiled
379source file information to disk. This makes for faster startup and look
380up times, but takes extra diskspace. Defaults to 'true' if you have
381C<Storable> installed and 'false' if you don't.
382
383=cut
384
385 $Conf->{'conf'}->{'storable'} =
386 ( check_install( module => 'Storable' ) ? 1 : 0 );
387
388=item timeout
389
390Digit indicating the time before a fetch request times out (in seconds).
391Defaults to 300.
392
393=cut
394
395 $Conf->{'conf'}->{'timeout'} = 300;
396
397=item verbose
398
399A boolean indicating whether or not C<CPANPLUS> runs in verbose mode.
400Defaults to 'true' if you have the environment variable
401C<PERL5_CPANPLUS_VERBOSE> set to true, 'false' otherwise.
402
403It is recommended you run with verbose enabled, but it is disabled
404for historical reasons.
405
406=cut
407
408 $Conf->{'conf'}->{'verbose'} = $ENV{PERL5_CPANPLUS_VERBOSE} || 0;
409
410=item write_install_log
411
412A boolean indicating whether or not to write install logs after installing
413a module using the interactive shell. Defaults to 'true'.
414
415
416=cut
417
418 $Conf->{'conf'}->{'write_install_logs'} = 1;
419
420=back
421
422=head2 Section 'program'
423
424=cut
425
6aaee015 426 ### Paths get stripped of whitespace on win32 in the constructor
427 ### sudo gets emptied if there's no need for it in the constructor
622d31ac 428
429=over 4
430
431=item editor
432
433A string holding the path to your editor of choice. Defaults to your
434$ENV{EDITOR}, $ENV{VISIUAL}, 'vi' or 'pico' programs, in that order.
435
436=cut
437
438 $Conf->{'program'}->{'editor'} = do {
439 $ENV{'EDITOR'} || $ENV{'VISUAL'} ||
440 can_run('vi') || can_run('pico')
441 };
442
443=item make
444
445A string holding the path to your C<make> binary. Looks for the C<make>
446program used to build perl or failing that, a C<make> in your path.
447
448=cut
449
450 $Conf->{'program'}->{'make'} =
451 can_run($Config{'make'}) || can_run('make');
452
453=item pager
454
455A string holding the path to your pager of choice. Defaults to your
456$ENV{PAGER}, 'less' or 'more' programs, in that order.
457
458=cut
459
460 $Conf->{'program'}->{'pager'} =
461 $ENV{'PAGER'} || can_run('less') || can_run('more');
462
6aaee015 463 ### no one uses this feature anyway, and it's only working for EU::MM
464 ### and not for module::build
465 #'perl' => '',
622d31ac 466
467=item shell
468
469A string holding the path to your login shell of choice. Defaults to your
470$ENV{SHELL} setting, or $ENV{COMSPEC} on Windows.
471
472=cut
473
474 $Conf->{'program'}->{'shell'} = $^O eq 'MSWin32'
475 ? $ENV{COMSPEC}
476 : $ENV{SHELL};
477
478=item sudo
479
480A string holding the path to your C<sudo> binary if your install path
481requires super user permissions. Looks for C<sudo> in your path, or
482remains empty if you do not require super user permissiosn to install.
483
484=cut
485
486 $Conf->{'program'}->{'sudo'} = do {
487 $> # check for all install dirs!
488 # installsiteman3dir is a 5.8'ism.. don't check
489 # it on 5.6.x...
490 ? ( -w $Config{'installsitelib'} &&
491 ( defined $Config{'installsiteman3dir'} &&
492 -w $Config{'installsiteman3dir'}
493 ) &&
494 -w $Config{'installsitebin'}
495 ? undef
496 : can_run('sudo')
497 )
498 : can_run('sudo')
499 };
500
501=item perlwrapper
502
503A string holding the path to the C<cpanp-run-perl> utility bundled
504with CPANPLUS, which is used to enable autoflushing in spawned processes.
505
506=cut
507
6aaee015 508 ### perlwrapper that allows us to turn on autoflushing
622d31ac 509 $Conf->{'program'}->{'perlwrapper'} = sub {
494f1016 510 my $name = 'cpanp-run-perl';
6aaee015 511
494f1016 512 my @bins = do{
513 require Config;
514 my $ver = $Config::Config{version};
515
516 ### if we are running with 'versiononly' enabled,
517 ### all binaries will have the perlversion appended
518 ### ie, cpanp will become cpanp5.9.5
519 ### so prefer the versioned binary in that case
520 $Config::Config{versiononly}
521 ? ($name.$ver, $name)
522 : ($name, $name.$ver);
523 };
502c7995 524
525 ### patch from Steve Hay Fri 29 Jun 2007 14:26:02 GMT+02:00
526 ### Msg-Id: <4684FA5A.7030506@uk.radan.com>
527 ### look for files with a ".bat" extension as well on Win32
528 @bins = map { $_, "$_.bat" } @bins if $^O eq 'MSWin32';
529
494f1016 530 my $path;
531 BIN: for my $bin (@bins) {
532
533 ### parallel to your cpanp/cpanp-boxed
534 my $maybe = File::Spec->rel2abs(
535 File::Spec->catdir( dirname($0), $bin )
536 );
537 $path = $maybe and last BIN if -f $maybe;
538
539 ### parallel to your CPANPLUS.pm:
540 ### $INC{cpanplus}/../bin/cpanp-run-perl
541 $maybe = File::Spec->rel2abs(
542 File::Spec->catdir(
543 dirname($INC{'CPANPLUS.pm'}),
544 '..', # lib dir
545 'bin', # bin dir
546 $bin, # script
547 )
548 );
549 $path = $maybe and last BIN if -f $maybe;
550
551 ### you installed CPANPLUS in a custom prefix,
552 ### so go paralel to /that/. PREFIX=/tmp/cp
553 ### would put cpanp-run-perl in /tmp/cp/bin and
554 ### CPANPLUS.pm in
555 ### /tmp/cp/lib/perl5/site_perl/5.8.8
556 $maybe = File::Spec->rel2abs(
557 File::Spec->catdir(
558 dirname( $INC{'CPANPLUS.pm'} ),
559 '..', '..', '..', '..', # 4x updir
560 'bin', # bin dir
561 $bin, # script
562 )
563 );
564 $path = $maybe and last BIN if -f $maybe;
6aaee015 565
494f1016 566 ### in your path -- take this one last, the
567 ### previous two assume extracted tarballs
568 ### or user installs
569 ### note that we don't use 'can_run' as it's
570 ### not an executable, just a wrapper...
571 for my $dir (split(/\Q$Config::Config{path_sep}\E/, $ENV{PATH}),
572 File::Spec->curdir
573 ) {
574 $maybe = File::Spec->catfile( $dir, $bin );
575 $path = $maybe and last BIN if -f $maybe;
576 }
577 }
578
579 ### we should have a $path by now ideally, if so return it
580 return $path if defined $path;
581
582 ### if not, warn about it and give sensible default.
583 ### XXX try to be a no-op instead then..
584 ### cross your fingers...
585 ### pass '-P' to perl: "run program through C
586 ### preprocessor before compilation"
587 error(loc(
502c7995 588 "Could not find the '%1' binary in your path".
494f1016 589 "--this may be a problem.\n".
590 "Please locate this program and set ".
502c7995 591 "your '%2' config entry to its path.\n".
592 "From the default shell, you can do this by typing:\n\n".
593 " %3\n".
594 " %4\n",
595 $name, 'perlwrapper',
596 's program perlwrapper FULL_PATH_TO_CPANP_RUN_PERL',
597 's save'
494f1016 598 ));
e3b7d412 599 return '';
622d31ac 600 }->();
601
602=back
603
604=cut
6aaee015 605
6aaee015 606sub new {
607 my $class = shift;
608 my $obj = $class->SUPER::new;
609
610 $obj->mk_accessors( keys %$Conf );
611
612 for my $acc ( keys %$Conf ) {
613 my $subobj = Object::Accessor->new;
614 $subobj->mk_accessors( keys %{$Conf->{$acc}} );
615
616 ### read in all the settings from the sub accessors;
617 for my $subacc ( $subobj->ls_accessors ) {
618 $subobj->$subacc( $Conf->{$acc}->{$subacc} );
619 }
620
621 ### now store it in the parent object
622 $obj->$acc( $subobj );
623 }
624
625 $obj->_clean_up_paths;
626
627 ### shut up IPC::Cmd warning about not findin IPC::Run on win32
628 $IPC::Cmd::WARN = 0;
629
630 return $obj;
631}
632
633sub _clean_up_paths {
634 my $self = shift;
635
636 ### clean up paths if we are on win32
637 if( $^O eq 'MSWin32' ) {
638 for my $pgm ( $self->program->ls_accessors ) {
c1ceaa19 639 my $path = $self->program->$pgm;
640
641 ### paths with whitespace needs to be shortened
642 ### for shell outs.
643 if ($path and $path =~ /\s+/) {
644 my($prog, $args);
645
646 ### patch from Steve Hay, 13nd of June 2007
647 ### msg-id: <467012A4.6060705@uk.radan.com>
648 ### windows directories are not allowed to end with
649 ### a space, so any occurrence of '\w\s+/\w+' means
650 ### we're dealing with arguments, not directory
651 ### names.
652 if ($path =~ /^(.*?)(\s+\/.*$)/) {
653 ($prog, $args) = ($1, $2);
654
655 ### otherwise, there are no arguments
656 } else {
657 ($prog, $args) = ($path, '');
658 }
659
660 $prog = Win32::GetShortPathName( $prog );
661 $self->program->$pgm( $prog . $args );
662 }
6aaee015 663 }
664 }
665
666 return 1;
667}
668
6691;
622d31ac 670
671=pod
672
673=head1 BUG REPORTS
674
675Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
676
677=head1 AUTHOR
678
679This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
680
681=head1 COPYRIGHT
682
683The CPAN++ interface (of which this module is a part of) is copyright (c)
6842001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
685
686This library is free software; you may redistribute and/or modify it
687under the same terms as Perl itself.
688
689=head1 SEE ALSO
690
691L<CPANPLUS::Backend>, L<CPANPLUS::Configure::Setup>, L<CPANPLUS::Configure>
692
693=cut
694
695# Local variables:
696# c-indentation-style: bsd
697# c-basic-offset: 4
698# indent-tabs-mode: nil
699# End:
700# vim: expandtab shiftwidth=4: