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