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