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