blib/lib/Module/MetaInfo/AutoGuess.pm

filter: Statementstmt 81.8% 135 / 165 Branchbran 56.3% 53 / 94 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 82.3% 14 / 17 Podpod 100% 4 / 4 totaltotal 72.6% 207 / 285 CC 50 SCAR 29.2
CC
50
Cov
72%
_check_doc_file_for_desc
72.0
_process_file_names
20.9
_check_perl_prog_for_desc
13.4
CRAP 18.5
« Progcopy.PL ↑ Summary blib/lib/Module/MetaInfo.pm »
1 package Module::MetaInfo::AutoGuess;
2 2 $VERSION = "0.01";
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Module::MetaInfo::_Extractor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 @ISA= qw(Module::MetaInfo::_Extractor);
14  
15 =head1 NAME
16  
17 Module::MetaInfo::AutoGuess - Guess meta information from perl modules
18  
19 =head1 USAGE
20  
21 use Module::MetaInfo::AutoGuess;
22 $mod=new Module::MetaInfo::AutoGuess(perl-module-file.tar.gz);
23 $desc=$mod->description();
24  
25 =head1 DESCRIPTION
26  
27 This module provides functions for guessing meta information from old
28 perl modules which have no explicit meta information storage. The aim
29 is to provide a transition mechnism through which meta information can
30 be supported for the majority of perl modules without any extra work
31 from the module maintainers.
32  
33 =head1 FUNCTIONS
34  
35 The meta information which should be generated can be worked out from
36 the needs of packaging systems such as RPM (RedHat Package Manager:
37 for RedHat Linux and related Linux distributions), DPKG (Debian
38 Packager - for Debian GNU/Linux).
39  
40 =head2 description
41  
42 This function tries to get a description for the module. It does this
43 by searching for files which might have description information then
44 looking in each one in order (from the most likely to the least -
45 heuristic guessing) until it finds something which seems to be a
46 reasonable description.
47  
48 The description returned should be treated as plain text. In the
49 current version however, it may contain unconverted POD directives.
50 In future these will probably be converted to text. Possibly some
51 options should be given about the kind of text to be produced?
52  
53 =head2 docs
54  
55 This function returns an array (or reference to an array in a scalar
56 context) which contains all of the files in the perl module which are
57 thought to be documentation.
58  
59 =head1 UNIMPLEMENTED FUNCTIONS
60  
61 Currently there are no dependency related functions (requires /
62 provides / suggests). The first two of these can be taken from
63 programs included in RPM > 3.0.4 if they are needed. Please indicate
64 that you need this to the author. There isn't a function to return a
65 module summary. This would be a one line summary of the function.
66 Probably best would be to take this from the CPAN modules.txt file.
67  
68 =cut
69  
70 # sub ProcessFileNames
71 # looks through a list of candidate files names and orders them
72 # according to desirability then cuts off those that look likely
73 # to do more harm than good.
74  
75 # N.B. function call to here is done a bit wierdly...
76  
77 5 ▶sub _process_file_names {
Subroutine _process_file_names: called CC=17 SCAR=30.4
78 5 my ($self, $doclist) = @_;
79 5 ▶ die "function miscall" unless (ref $self && (ref $doclist eq "ARRAY"));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and ref $doclist eq "ARRAY" 0 5 5
Condition33%1/3
ref $self and ref $doclist eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/2
ref $self and ref $doclist eq "ARRAY"
ref $self ref $doclist eq "ARRAY"
Truth table33%1/3
A and B
A= ref $selfB= ref $doclist eq "ARRAY"
ABresult
0X0
100
111
80  
81 print STDERR "Sorting different perl file possibilities\n"
82 5 ▶ if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 4 5
83  
84 5 local $::simplename=$self->{package_name};
85 5 local ($::A, $::B);
86 5 $::simplename =~ s,[-/ ],_,g;
87 5 $::simplename =~ tr/[A-Z]/[a-z]/;
88  
89 #Ordering Heuristic
90 #
91 #best: the description in the module named the same as the package
92 #
93 #next: documentation files
94 #
95 #next: files named as package
96 #finally: prefer .pod to .pm to .pl
97 #
98 #N.B. sort high to low not low to high
99  
100 my @sort_list = sort {
101 5 local $::res=0;
102 8 $::A = $a;
103 8 $::B = $b;
104 8 $::A =~ s,[-/ ],_,g;
105 8 $::A =~ tr/[A-Z]/[a-z]/;
106 8 $::B =~ s,[-/ ],_,g;
107 8 $::B =~ tr/[A-Z]/[a-z]/;
108  
109 #bundles seem a bad place to look from our limited experience
110 #this might be better as an exception on the next rule??
111 8 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = -($main::B =~ /(^|_)bundle_/ <=> $main::A =~ /(^|_)bundle_/) 0 8 8
112 if ( $::res = - (($::B =~ m/(^|_)bundle_/ )
113 <=> ($::A =~ m/(^|_)bundle_/ )) ) ;
114 8 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = $main::B =~ /$main::simplename.(pm|pod|pod)/ <=> $main::A =~ /$main::simplename.(pm|pod|pod)/ 0 8 8
115 if ( $::res = (($::B =~ m/$::simplename.(pm|pod|pod)/ )
116 <=> ($::A =~ m/$::simplename.(pm|pod|pod)/ )) ) ;
117 8 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = $main::B =~ /^readme/ <=> $main::A =~ /^readme/ 0 8 8
118 if ( $::res = (($::B =~ m/^readme/ )
119 <=> ($::A =~ m/^readme/ )) ) ;
120 8 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = $main::B =~ /.pod$/ <=> $main::A =~ /.pod$/ 0 8 8
121 if ( $::res = (($::B =~ m/.pod$/ )
122 <=> ($::A =~ m/.pod$/ )) ) ;
123 8 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = $main::B =~ /.pm$/ <=> $main::A =~ /.pm$/ 3 5 8
124 if ( $::res = (($::B =~ m/.pm$/ )
125 <=> ($::A =~ m/.pm$/ )) ) ;
126 5 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = $main::B =~ /.pl$/ <=> $main::A =~ /.pl$/ 0 5 5
127 if ( $::res = (($::B =~ m/.pl$/ )
128 <=> ($::A =~ m/.pl$/ )) ) ;
129 5 ▶ return $::res
Branch: 1 branch
BranchTrueFalseTotal
if $main::res = $main::B =~ /$main::simplename/ <=> $main::A =~ /$main::simplename/ 0 5 5
130 if ( $::res = (($::B =~ m/$::simplename/ )
131 <=> ($::A =~ m/$::simplename/ )) ) ;
132 5 return length $::B <=> length $::A;
133 } @$doclist;
134  
135 print STDERR "Checking which fies could really be used\n"
136 5 ▶ if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 4 5
137 5 my $useful=0; #assume first always good
138 CASE: {
139 5 ▶ $#sort_list == 1 && do {
Branch: 1 branch
BranchTrueFalseTotal
$#sort_list == 1 and do { $useful = 1; last CASE } 2 3 5
140 2 $useful=1;
141 2 last CASE;
142 };
143 3 while (1) {
144 9 ▶ $useful==$#sort_list and last CASE;
Branch: 1 branch
BranchTrueFalseTotal
$useful == $#sort_list and last CASE 3 6 9
145 #non perl files in the list must be there for some reason
146 6 ▶ ($sort_list[$useful+1] =~ m/\.p(od|m|l)$/) or do {$useful++; next};
Branch: 1 branch
BranchTrueFalseTotal
$sort_list[$useful + 1] =~ /\.p(od|m|l)$/ or do { ++$useful; next } 0 6 6
147 0 my $cmp_name=$sort_list[$useful+1];
148 0 $cmp_name =~ s,[-/ ],_,g;
149 0 $cmp_name =~ tr/[A-Z]/[a-z]/;
150 #perl files should look something like the package name???
151 0 ▶ ($cmp_name =~ m/$::simplename/) && do {$useful++; next};
Branch: 1 branch
BranchTrueFalseTotal
$cmp_name =~ /$main::simplename/ and do { ++$useful; next } 0 0 0
152 0 last CASE;
153 }
154 }
155 5 $#sort_list = $useful;
156  
157 print STDERR "Description file list is as follows:\n " ,
158 5 ▶ join ("\n ", @sort_list), "\n" if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 4 5
159  
160 #FIXME: ref return would be more efficient
161 5 return \@sort_list;
162 }
163  
164  
165 # sub _check_perl_prog_for_desc
166  
167 # given a documentation file, see if we can extract a description from it
168  
169 0 ▶sub _check_doc_file_for_desc {
Subroutine _check_doc_file_for_desc: not called CC=8 SCAR=42.8
170 0 my $self=shift;
171 0 my $filename=shift;
172 0 my $fh = Symbol::gensym();
173 print STDERR "Try to use $filename as description\n"
174 0 ▶ if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 0 0 0
175 0 ▶ open($fh, "<$filename") || die "Failed to open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<$filename" or die "Failed to open ${filename}: $!" 0 0 0
176 0 my $desc;
177 0 my $linecount=1;
178 0 ▶ LINE: while ( my $line=<$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
179 0 $desc .= $line;
180 0 $linecount++;
181 0 ▶ $linecount > 30 && last LINE;
Branch: 1 branch
BranchTrueFalseTotal
$linecount > 30 and last LINE 0 0 0
182 }
183 0 ▶ close($fh) or die "Failed to close $filename $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close $filename $!" 0 0 0
184 #FIXME: quality check
185 0 ▶ $linecount > 2 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$linecount > 2 or return undef 0 0 0
186 0 ▶ return $desc if ( $desc );
Branch: 1 branch
BranchTrueFalseTotal
if $desc 0 0 0
187 }
188  
189  
190 # sub _check_perl_prog_for_desc
191  
192 # given a valid perl program see if there is a valid description in it.
193  
194 3 ▶sub _check_perl_prog_for_desc {
Subroutine _check_perl_prog_for_desc: called CC=12 SCAR=25.9
195 3 my $self=shift;
196 3 my $filename=shift;
197 3 my $desc="";
198 3 my $fh = Symbol::gensym();
199 print STDERR "Try to use $filename as description\n"
200 3 ▶ if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 2 3
201 3 ▶ open($fh, $filename) || die "Failed to open $filename: $!";;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $filename or die "Failed to open ${filename}: $!" 3 0 3
202  
203 3 my $linecount=1;
204 3 ▶ LINE: while (my $line=<$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 81 0 81
205 81 ▶ ($line =~ m/^=head1[\t ]+DESCRIPTION/) and do {
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^=head1[\t ]+DESCRIPTION/ and do { while (defined($line = readline $fh)) { $line =~ /^=(head1)|(cut)/ and last LINE; $desc .= $line; ++$linecount; $linecount > 30 and last LINE; } } 3 78 81
206 3 ▶ while ( $line=<$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 71 0 71
207 71 ▶ ($line =~ m/^=(head1)|(cut)/) and last LINE;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^=(head1)|(cut)/ and last LINE 1 70 71
208 70 $desc .= $line;
209 70 $linecount++;
210 70 ▶ $linecount > 30 && last LINE;
Branch: 1 branch
BranchTrueFalseTotal
$linecount > 30 and last LINE 2 68 70
211 }
212 };
213 #tests to see if the descripiton is good enough
214 #FIXME: mentions package name?
215 }
216 3 ▶ close($fh) or die "Failed to close $filename $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Failed to close $filename $!" 3 0 3
217 3 ▶ ( $desc =~ m/(....\n.*){3}/m ) and do {
Branch: 1 branch
BranchTrueFalseTotal
$desc =~ /(....\n.*){3}/m and do { $desc =~ s/^\s*\n//; $desc = 'This package contains the perl module ' . $self->{"package_name"} . ".\n\n" . $desc; print STDERR "Found description in $filename\n" if ${$self->{"_verbose"};}; return $desc } 3 0 3
218 #Often descriptions don't say the name of the module and
219 #furthermore they always assume that we know they are a perl
220 #module so put in a little header.
221 3 $desc =~ s/^\s*\n//;
222 $desc="This package contains the perl module " .
223 3 $self->{package_name} . ".\n\n" . $desc;
224 3 ▶ print STDERR "Found description in $filename\n" if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 2 3
225 3 return $desc;
226 };
227 0 ▶ print STDERR "No description found in $filename\n" if ${$self->{_verbose}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 0 0 0
228 0 return undef;
229 }
230  
231  
232  
233 #=head1 $self->_check_files_for_desc()
234 #
235 #this function looks at a files in a list and for each in order identifies
236 #if it has content that could be used as a module description.
237 #
238 #=cut
239  
240 5 ▶sub _check_files_for_desc {
Subroutine _check_files_for_desc: called CC=7 SCAR=21.5
241  
242 5 my $doc_list=&_process_file_names;
243  
244 5 my $self = shift;
245 5 my $desc;
246  
247 5 FILE: foreach my $filename ( @$doc_list ){
248 -e $filename or
249 7 ▶ do {print STDERR "no $filename file\n" if ${$self->{'_verbose'}};
Branch: 2 branches
BranchTrueFalseTotal
-e $filename or do { print STDERR "no $filename file\n" if ${$self->{"_verbose"};}; next FILE } 3 4 7
if ${$self->{"_verbose"};} 0 4 4
250 4 next FILE};
251 3 ▶ $filename =~ m/\.p(od|m|l)$/ && do {
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ /\.p(od|m|l)$/ and do { $desc = $self->_check_perl_prog_for_desc($filename); $desc and last FILE; next FILE } 3 0 3
252 3 $desc=$self->_check_perl_prog_for_desc($filename);
253 3 ▶ $desc && last FILE;
Branch: 1 branch
BranchTrueFalseTotal
$desc and last FILE 3 0 3
254 0 next FILE;
255 };
256 0 $desc=$self->_check_doc_file_for_desc($filename);
257 0 ▶ last FILE if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 0 0 0
258 }
259 5 return $desc;
260 }
261  
262 =head2 description
263  
264 This function finds and returns a description of the perl module.
265  
266 In the current implementation we use a set of wierd heuristics to
267 guess what is the best description available.
268  
269 When creating an rpm, for example, it's a good idea to proceed the
270 description with something to the effect of:
271  
272 this rpm contains the perl module XXX
273  
274 where XXX is the name you are using for the perl module.
275  
276 =cut
277  
278 5 ▶sub description {
Subroutine description: called CC=5 SCAR=16.1
Pod documented
279 5 my $self=shift;
280 5 ▶ croak "$self must be a reference" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 5 5
281 5 ▶ $self->setup() unless $self->{setup};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"setup"} 5 0 5
282 5 my $desc = "";
283 5 ▶ print STDERR "Hunting for files in distribution\n" if ${$self->{'_verbose'}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 4 5
284  
285 #Files for use for a description. Names are relative to package
286 #base. Are there more names which work good? BLURB? INTRO?
287  
288 my (@doc_list) = ( $self->{expand_dir} ."/". "README" ,
289 5 $self->{expand_dir} ."/". "DESCRIPTION" );
290  
291 #we just use absolute paths
292 # my $dirpref = $self->{expand_dir};
293  
294 58 ▶ my $handler=sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
295 58 ▶ m/\.p(od|m|l)$/ or return;
Branch: 1 branch
BranchTrueFalseTotal
/\.p(od|m|l)$/ or return 3 55 58
296 3 my $name=$File::Find::name;
297 # $name =~ s/^$dirpref//;
298 3 push @doc_list, $name;
299 5 };
300 5 &File::Find::find($handler, $self->{expand_dir});
301  
302 5 $desc=$self->_check_files_for_desc(\@doc_list);
303  
304 5 ▶ unless ( $desc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($desc) 2 3 5
305 warn "Failed to generate any description for "
306 2 . $self->{package_name} . ".\n";
307 2 return undef;
308 }
309  
310 #FIXME: what's the best way to clean up whitespace? Is it needed at all?
311 #bear in mind that both perl descriptions and rpm special case
312 #indentation with white space to mean something like \verbatim
313  
314 3 $desc=~s/^[\t ]*//mg; #space at the start of lines
315 3 $desc=~s/[\t ]*$//mg; #space at the end of lines
316 3 $desc=~s/^[_\W]*//s ; #blank punctuation lines at the start
317 3 $desc=~s/\s*$//; #blank lines at the end.
318  
319 3 return $desc;
320 }
321  
322 =head2 doc_files
323  
324 We give a list of files or directories which it is good to treat as
325 documentation and include within any binary distribution.
326  
327 We like to include usage documentation, copyrights and release
328 information. Probably we don't care too much about implementation
329 documentation. Right now we just doo fairly simple file name guessing
330 in the top level directory of the distribution.
331  
332 doc_files returns a list of files to a list context and a reference to
333 an array of files to a scalar context.
334  
335 =cut
336  
337 2 my $docre='(?x) (^README)
338 |(^C((?i)OPY((ING)|(RIGHT))))|(LICENSE$)
339 |(^doc(s|u.*)?)
340 |(^FAQ)
341 |(^(?i)notes$)
342 |(^(?i)todo$)
343 |(^(Changes)|(NEWS)$)
344 |((?i)examples?)';
345  
346 2 ▶sub doc_files() {
Subroutine doc_files: called CC=5 SCAR=16.3
Pod documented
347 2 my $self = shift;
348 2 ▶ $self->setup() unless $self->{setup};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"setup"} 0 2 2
349 2 my $old_dir = Cwd::cwd();
350 2 my @docs = ();
351 2 my $return="";
352 opendir (BASEDIR , $self->{expand_dir})
353 2 ▶ || die "can't open package main directory $self->{expand_dir} $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir BASEDIR, $self->{"expand_dir"} or die qq[can't open package main directory $self->{"expand_dir"} $!] 2 0 2
354 2 my @files=readdir (BASEDIR);
355 2 @docs= grep {m/$docre/i} @files;
356 print STDERR "Found the following documentation files\n" ,
357 2 ▶ join (" " , @docs ), "\n" if ${$self->{'_verbose'}};
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->{"_verbose"};} 1 1 2
358 2 ▶ return wantarray ? @docs : \@docs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
359 }
360  
361 =head2 requires / provides
362  
363 These functions would try to guess which perl modules are needed to
364 run this one / which perl libraries this module provides.. They
365 aren't implmeneted yet.
366  
367 Currently we haven't distinguished pre-requisite modules needed to run
368 the module from ones needed merely to install it.
369  
370 There is code in perl.prov and perl.req in the lib directory of RPM
371 (the RedHat Package Manager) which can determine this information,
372 however, it requires the module to have been build correctly and
373 installed in a temporary directory hierarchy.
374  
375 =cut
376  
377 0 ▶sub requires {
Subroutine requires: not called CC=1 SCAR=6.9
Pod documented
378 0 die "Module::Metainfo::requires() isn't yet implemented";
379 }
380  
381 0 ▶sub provides {
Subroutine provides: not called CC=1 SCAR=6.9
Pod documented
382 0 die "Module::Metainfo::provides() isn't yet implemented";
383 }
384  
385 =head1 FUTURE FUNCTIONS
386  
387 There are a number of other things which should be implemented. These
388 can be guessed from looking at the possible meta-information which can
389 be stored in the RPM or DPG formats, for example. Examples include
390  
391 =over 4
392  
393 =item *
394  
395 copyright - GPL / as perl / redistributable / etc.
396  
397 =item *
398  
399 application area - Database / Internet / WWW / HTTP etc.
400  
401 =item *
402  
403 suggests - related applications
404  
405 =back
406  
407 In many cases this data is generated currently by package building
408 tools simply by using a fixed string. The function should do better
409 than that in almost all cases or else it is't worth having...
410  
411 =head1 FUTURE DEVELOPMENT
412  
413 Incorporate a mechanism for deliberately storing meta information
414 inside perl modules, e.g. by adding a directory structure inside. I
415 already have a prototype for this included into makerpm.
416  
417 =head1 COPYRIGHT
418  
419 You may distribute under the terms of either the GNU General
420 Public License or the Artistic License, as specified in the
421 Perl README.
422  
423 =head1 AUTHOR
424  
425 Michael De La Rue.
426  
427 =head1 SEE ALSO
428  
429 L<pm-metainfo>
430  
431 =cut
432  
433 2 42;
« Progcopy.PL ↑ Summary blib/lib/Module/MetaInfo.pm »