Upgrade to CPANPLUS-0.82
[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             check_install( module => 'Module::Signature', version => '0.06' )
362             and ( can_run('gpg') || 
363                   check_install(module => 'Crypt::OpenPGP')
364             );
365         } ? 1 : 0;
366
367 =item skiptest
368
369 A boolean indicating whether or not to skip tests when installing modules.
370 Defaults to 'false'.
371
372 =cut
373
374         $Conf->{'conf'}->{'skiptest'} = 0;
375
376 =item storable
377
378 A boolean indicating whether or not to use C<Storable> to write compiled
379 source file information to disk. This makes for faster startup and look
380 up times, but takes extra diskspace. Defaults to 'true' if you have 
381 C<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
390 Digit indicating the time before a fetch request times out (in seconds).
391 Defaults to 300.
392
393 =cut
394
395         $Conf->{'conf'}->{'timeout'} = 300;
396
397 =item verbose
398
399 A boolean indicating whether or not C<CPANPLUS> runs in verbose mode.
400 Defaults to 'true' if you have the environment variable 
401 C<PERL5_CPANPLUS_VERBOSE> set to true, 'false' otherwise.
402
403 It is recommended you run with verbose enabled, but it is disabled
404 for historical reasons.
405
406 =cut
407
408         $Conf->{'conf'}->{'verbose'} = $ENV{PERL5_CPANPLUS_VERBOSE} || 0;
409
410 =item write_install_log
411
412 A boolean indicating whether or not to write install logs after installing
413 a 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
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
428
429 =over 4
430
431 =item editor
432
433 A 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
445 A string holding the path to your C<make> binary. Looks for the C<make>
446 program 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
455 A 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
463         ### no one uses this feature anyway, and it's only working for EU::MM
464         ### and not for module::build
465         #'perl'      => '',
466
467 =item shell
468
469 A 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
480 A string holding the path to your C<sudo> binary if your install path
481 requires super user permissions. Looks for C<sudo> in your path, or 
482 remains 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
503 A string holding the path to the C<cpanp-run-perl> utility bundled
504 with CPANPLUS, which is used to enable autoflushing in spawned processes.
505
506 =cut
507
508         ### perlwrapper that allows us to turn on autoflushing                        
509         $Conf->{'program'}->{'perlwrapper'} = sub { 
510             my $name = 'cpanp-run-perl';
511
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             };
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
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;
565
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                 ### prefer anything that's found in the path paralel to your $^X
572                 for my $dir (File::Spec->rel2abs( dirname($^X) ),
573                              split(/\Q$Config::Config{path_sep}\E/, $ENV{PATH}),
574                              File::Spec->curdir, 
575                 ) {             
576                     $maybe = File::Spec->catfile( $dir, $bin );
577                     $path = $maybe and last BIN if -f $maybe;
578                 }
579             }          
580                 
581             ### we should have a $path by now ideally, if so return it
582             return $path if defined $path;
583             
584             ### if not, warn about it and give sensible default.
585             ### XXX try to be a no-op instead then.. 
586             ### cross your fingers...
587             ### pass '-P' to perl: "run program through C 
588             ### preprocessor before compilation"
589             ### XXX using -P actually changes the way some Makefile.PLs
590             ### are executed, so don't do that... --kane
591             error(loc(
592                 "Could not find the '%1' binary in your path".
593                 "--this may be a problem.\n".
594                 "Please locate this program and set ".
595                 "your '%2' config entry to its path.\n".
596                 "From the default shell, you can do this by typing:\n\n".
597                 "  %3\n".
598                 "  %4\n",
599                 $name, 'perlwrapper', 
600                 's program perlwrapper FULL_PATH_TO_CPANP_RUN_PERL',
601                 's save'
602              ));                                        
603              return '';
604         }->();
605         
606 =back
607
608 =cut
609
610 sub new {
611     my $class   = shift;
612     my $obj     = $class->SUPER::new;
613
614     $obj->mk_accessors( keys %$Conf );
615
616     for my $acc ( keys %$Conf ) {
617         my $subobj = Object::Accessor->new;
618         $subobj->mk_accessors( keys %{$Conf->{$acc}} );
619
620         ### read in all the settings from the sub accessors;
621         for my $subacc ( $subobj->ls_accessors ) {
622             $subobj->$subacc( $Conf->{$acc}->{$subacc} );
623         }
624
625         ### now store it in the parent object
626         $obj->$acc( $subobj );
627     }
628     
629     $obj->_clean_up_paths;
630     
631     ### shut up IPC::Cmd warning about not findin IPC::Run on win32
632     $IPC::Cmd::WARN = 0;
633     
634     return $obj;
635 }
636
637 sub _clean_up_paths {
638     my $self = shift;
639
640     ### clean up paths if we are on win32
641     if( $^O eq 'MSWin32' ) {
642         for my $pgm ( $self->program->ls_accessors ) {
643             my $path = $self->program->$pgm;
644
645             ### paths with whitespace needs to be shortened
646             ### for shell outs.
647             if ($path and $path =~ /\s+/) {
648                 my($prog, $args);
649
650                 ### patch from Steve Hay, 13nd of June 2007
651                 ### msg-id: <467012A4.6060705@uk.radan.com>
652                 ### windows directories are not allowed to end with 
653                 ### a space, so any occurrence of '\w\s+/\w+' means
654                 ### we're dealing with arguments, not directory
655                 ### names.
656                 if ($path =~ /^(.*?)(\s+\/.*$)/) {
657                     ($prog, $args) = ($1, $2);
658                 
659                 ### otherwise, there are no arguments
660                 } else {
661                     ($prog, $args) = ($path, '');
662                 }
663                 
664                 $prog = Win32::GetShortPathName( $prog );
665                 $self->program->$pgm( $prog . $args );
666             }
667         }
668     }
669
670     return 1;
671 }
672
673 1;
674
675 =pod
676
677 =head1 BUG REPORTS
678
679 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
680
681 =head1 AUTHOR
682
683 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
684
685 =head1 COPYRIGHT
686
687 The CPAN++ interface (of which this module is a part of) is copyright (c) 
688 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
689
690 This library is free software; you may redistribute and/or modify it 
691 under the same terms as Perl itself.
692
693 =head1 SEE ALSO
694
695 L<CPANPLUS::Backend>, L<CPANPLUS::Configure::Setup>, L<CPANPLUS::Configure>
696
697 =cut
698
699 # Local variables:
700 # c-indentation-style: bsd
701 # c-basic-offset: 4
702 # indent-tabs-mode: nil
703 # End:
704 # vim: expandtab shiftwidth=4: