blib/lib/CPAN/Mini/Visit.pm

filter: Statementstmt 89.6% 112 / 125 Branchbran 66.1% 41 / 62 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 78.6% 177 / 225 CC 38 SCAR 30.6
CC
38
Cov
79%
run
37.9
new
16.3
_minicpan
2.0
CRAP 21.4
↑ Summary
1 package CPAN::Mini::Visit;
2  
3 =pod
4  
5 =head1 NAME
6  
7 CPAN::Mini::Visit - A generalised API version of David Golden's visitcpan
8  
9 =head1 SYNOPSIS
10  
11 CPAN::Mini::Visit->new(
12 minicpan => '/minicpan',
13 acme => 0,
14 author => 'ADAMK',
15 warnings => 1,
16 random => 1,
17 callback => sub {
18 print "# counter: $_[0]->{counter}\n";
19 print "# archive: $_[0]->{archive}\n";
20 print "# tempdir: $_[0]->{tempdir}\n";
21 print "# dist: $_[0]->{dist}\n";
22 print "# author: $_[0]->{author}\n";
23 }
24 )->run;
25
26 # counter: 1234
27 # archive: /minicpan/authors/id/A/AD/ADAMK/Config-Tiny-1.00.tar.gz
28 # tempdir: /tmp/1a4YRmFAJ3/Config-Tiny-1.00
29 # dist: ADAMK/Config-Tiny-1.00.tar.gz
30 # author: ADAMK
31  
32 =head1 DESCRIPTION
33  
34 L<CPAN::Mini::Extract> has been relatively successful at allowing processes
35 to run across the contents (or a subset of the contents) of an entire
36 L<minicpan> checkout.
37  
38 However it has become evident that while it is useful (and theoretically
39 optimal from a processing point of view) to maintain an expanded minicpan
40 checkout the sheer size of an expanded minicpan is such that it becomes
41 an undo burdon to manage, move, copy or even delete a directory tree with
42 hundreds of thousands of file totalling in the high single gigabytes in size.
43  
44 Annoyed by this, David Golden created L<visitcpan> which takes an alternative
45 approach of sequentially expanding the tarball of each distribution into a
46 temporary directory, do the processing on that distribution, and then delete
47 the temporary directory before moving on to the next directory.
48  
49 This method results in a longer computation time, but with the benefit of
50 dramatically reduced system overhead, greater adaptability, and allow for
51 easy ad-hoc computations.
52  
53 This improvement in flexibility turns out to be worth the extra computation
54 time in almost all cases.
55  
56 B<CPAN::Mini::Visit> is a simplified and generalised API-based version of
57 David Golden's L<visitcpan> script.
58  
59 It implements only the process of discovering, iterating and expanding
60 archives, before handing off control to an arbitrary callback function
61 provided to the constructor.
62  
63 =cut
64  
65 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69 4 ▶use File::Spec 0.80 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
70 4 ▶use File::Temp 0.21 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4 ▶use File::pushd 1.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 4 ▶use File::chmod 0.31 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 ▶use File::Find::Rule 0.27 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 4 ▶use Archive::Extract 0.32 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75 4 ▶use CPAN::Mini 0.576 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
76 4 ▶use Params::Util 1.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 4 our $VERSION = '0.12_01';
79 4 $VERSION = eval $VERSION;
80  
81 4 use Object::Tiny 1.06 qw{
82 minicpan
83 authors
84 callback
85 skip
86 acme
87 author
88 ignore
89 random
90 warnings
91 prefer_bin
92 4 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 =pod
95  
96 =head2 new
97  
98 Takes a variety of parameters and creates a new visitor object.
99  
100 The C<minicpan> param should be the root directory of a L<CPAN::Mini>
101 download.
102  
103 The C<callback> param should be a C<CODE> reference that will be called
104 for each visit. The first parameter passed to the callback will be a C<HASH>
105 reference containing the tarball location in the C<archive> key, the location
106 of the temporary directory in the C<tempdir> key, the canonical CPAN
107 distribution name in the C<dist> key, and the author id in the C<author> key.
108  
109 The optional C<skip> param should be a C<CODE> reference
110 that will be called for each visit before extracting dist. The first
111 parameter passed to the callback will be a C<HASH> reference with C<archive>,
112 C<dist> and C<author> keys. Callback should return 1 if dist should be skipped
113 and 0 otherwise.
114  
115 The C<acme> param (true by default) can be set to false to exclude any
116 distributions that contain the string "Acme", allowing the visit to ignore
117 any of the joke modules.
118  
119 The C<author> param can be provided to limit the visit to only the modules
120 owned by a specific author.
121  
122 The C<random> param will cause the archives to be processed in random order
123 if enabled. If not, the archives will be processed in alphabetical order.
124  
125 The C<warnings> param will turn on L<Archive::Extract> warnings if enabled,
126 or disable warnings otherwise.
127  
128 The C<prefer_bin> param will tell L<Archive::Extract> to use binary extract
129 instead of CPAN module extract wherever possible. By default, it will use
130 module-based extract.
131  
132 Returns a B<CPAN::Mini::Visit> object, or throws an exception on error.
133  
134 =cut
135  
136 sub new {
137 7 ▶ my $class = shift;
Subroutine new: called CC=14 SCAR=27.9
Pod documented
138 7 my $self = bless { @_ }, $class;
139  
140 # Normalise
141 7 ▶ $self->{random} = $self->random ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->random ? : 1 6 7
142 7 ▶ $self->{prefer_bin} = $self->prefer_bin ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->prefer_bin ? : 0 7 7
143 7 ▶ $self->{warnings} = 0 unless $self->{warnings};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"warnings"} 7 0 7
144  
145 # Check params
146 7 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_HASH(scalar $self->minicpan) or defined &Params::Util::_STRING(scalar $self->minicpan) and -d $self->minicpan) 0 7 7
Condition50%3/6
&Params::Util::_HASH(scalar $self->minicpan) or defined &Params::Util::_STRING(scalar $self->minicpan) and -d $self->minicpan
l!l&&r!l&&!r
430
defined &Params::Util::_STRING(scalar $self->minicpan) and -d $self->minicpan
!ll&&!rl&&r
003
MC/DC0%0/3
&Params::Util::_HASH(scalar $self->minicpan) or defined &Params::Util::_STRING(scalar $self->minicpan) and -d $self->minicpan
&Params::Util::_HASH(scalar $self->minicpan) defined &Params::Util::_STRING(scalar $self->minicpan) -d $self->minicpan
Truth table50%2/4
A or B and C
A= &Params::Util::_HASH(scalar $self->minicpan)B= defined &Params::Util::_STRING(scalar $self->minicpan)C= -d $self->minicpan
ABCresult
1XX1
0111
00X0
0100
147 Params::Util::_HASH($self->minicpan)
148 or (
149 defined Params::Util::_STRING($self->minicpan)
150 and
151 -d $self->minicpan
152 )
153 ) {
154 0 Carp::croak("Missing or invalid 'minicpan' param");
155 }
156 7 ▶ unless ( Params::Util::_CODELIKE($self->callback) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_CODELIKE(scalar $self->callback)) 0 7 7
157 0 Carp::croak("Missing or invalid 'callback' param");
158 }
159 7 ▶ if ( defined $self->ignore ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->ignore) { } 3 4 7
160 3 ▶ unless ( Params::Util::_ARRAYLIKE($self->ignore) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_ARRAYLIKE(scalar $self->ignore)) 0 3 3
161 0 Carp::croak("Invalid 'ignore' param");
162 }
163 # Clone the array so we can prepend more things
164 3 $self->{ignore} = [ @{ $self->ignore } ];
165 } else {
166 4 $self->{ignore} = [];
167 }
168  
169 # Apply the optional author setting
170 7 my $author = Params::Util::_STRING($self->author);
171 7 ▶ if ( defined $author ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $author) 2 5 7
172 2 unshift @{$self->ignore}, sub {
173 8 ▶ $_[0]->{author} ne $author;
Subroutine __ANON__: called CC=1 SCAR=0.0
174 2 };
175 }
176  
177 # Clean and apply the acme setting
178 7 ▶ $self->{acme} = 1 unless defined $self->{acme};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"acme"} 3 4 7
179 7 $self->{acme} = !! $self->{acme};
180 7 ▶ unless ( $self->{acme} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"acme"}) 3 4 7
181 3 unshift @{$self->ignore}, qr/\bAcme\b/;
182 }
183  
184 # Derive the authors directory
185 7 $self->{authors} = File::Spec->catdir( $self->_minicpan, 'authors', 'id' );
186 7 ▶ unless ( -d $self->authors ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->authors) 0 7 7
187 0 Carp::croak("Authors directory '$self->{authors}' does not exist");
188 }
189  
190 7 return $self;
191 }
192  
193 =pod
194  
195 =head2 run
196  
197 The C<run> method executes the visit process, taking no parameters and
198 returning true.
199  
200 Because the object contains no state information, you may call the C<run>
201 method multiple times for a single visit object with no ill effects.
202  
203 =cut
204  
205 sub run {
206 7 ▶ my $self = shift;
Subroutine run: called CC=24 SCAR=36.3
Pod documented
207  
208 # If we've been passed a HASH minicpan param,
209 # do an update_mirror first, before the regular run.
210 7 ▶ if ( Params::Util::_HASH($self->minicpan) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Params::Util::_HASH(scalar $self->minicpan)) 4 3 7
211 4 CPAN::Mini->update_mirror(%{$self->minicpan});
212 }
213  
214 # Search for the files
215 7 my $find = File::Find::Rule->name('*.tar.gz', '*.tgz', '*.zip', '*.bz2')->file->relative;
216 7 my @files = sort $find->in( $self->authors );
217  
218 # Randomise if applicable
219 7 ▶ if ( $self->random ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->random) 1 6 7
220 1 @files = sort { rand() <=> rand() } @files;
221 }
222  
223 # Extract the archive
224 7 my $counter = 0;
225 7 foreach my $path ( @files ) {
226 # Derive the main file properties
227 28 my $archive = File::Spec->catfile( $self->authors, $path );
228 28 my $dist = $path;
229 28 ▶ $dist =~ s|^[A-Z]/[A-Z][A-Z]/|| or die "Bad distpath for $path";
Branch: 1 branch
BranchTrueFalseTotal
$dist =~ s[^[A-Z]/[A-Z][A-Z]/][] or die "Bad distpath for $path" 0 28 28
230 28 ▶ unless ( $dist =~ /^([A-Z]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dist =~ /^([A-Z]+)/) 0 28 28
231 0 die "Bad author for $path";
232 }
233 28 my $author = "$1";
234  
235 # Apply the ignore filters
236 28 my $skip = 0;
237 28 foreach my $filter ( @{$self->ignore} ) {
238 32 ▶ if ( defined Params::Util::_STRING($filter) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Params::Util::_STRING($filter)) 0 32 32
239 0 $filter = quotemeta $filter;
240 0 $filter = qr/$filter/;
241 }
242 32 ▶ if ( Params::Util::_REGEX($filter) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&Params::Util::_REGEX($filter)) { } 20 12 32
elsif (&Params::Util::_CODELIKE($filter)) { } 12 0 12
243 20 ▶ $skip = 1 if $dist =~ $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /$filter/ 3 17 20
244 } elsif ( Params::Util::_CODELIKE($filter) ) {
245 12 ▶ $skip = 1 if $filter->( {
Branch: 1 branch
BranchTrueFalseTotal
if &$filter({"counter", $counter, "archive", $archive, "dist", $dist, "author", $author}) 5 7 12
246 counter => $counter,
247 archive => $archive,
248 dist => $dist,
249 author => $author,
250 } );
251 } else {
252 0 Carp::croak("Missing or invalid filter");
253 }
254 }
255 28 ▶ next if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 8 20 28
256  
257 # Explicitly ignore some damaging distributions
258 # if we are using Perl extraction
259 20 ▶ unless ( $self->prefer_bin ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->prefer_bin) 20 0 20
260 20 ▶ next if $dist =~ /\bHarvey-\d/;
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /\bHarvey-\d/ 0 20 20
261 20 ▶ next if $dist =~ /\bText-SenseClusters\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /\bText-SenseClusters\b/ 0 20 20
262 20 ▶ next if $dist =~ /\bBio-Affymetrix\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /\bBio-Affymetrix\b/ 0 20 20
263 20 ▶ next if $dist =~ /\bAlien-MeCab\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $dist =~ /\bAlien-MeCab\b/ 0 20 20
264 }
265  
266 # Extract the archive
267 20 local $Archive::Extract::WARN = !! ($self->warnings > 1);
268 20 local $Archive::Extract::PREFER_BIN = $self->prefer_bin;
269 20 my $extract = Archive::Extract->new( archive => $archive );
270 20 my $tmpdir = File::Temp->newdir;
271 20 my $ok = 0;
272 SCOPE: {
273 20 my $pushd1 = File::pushd::pushd( File::Spec->curdir );
274 20 $ok = eval {
275 20 $extract->extract( to => $tmpdir );
276 };
277 }
278 20 ▶ if ( $@ or not $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $ok) 0 20 20
Condition33%1/3
$@ or not $ok
l!l&&r!l&&!r
0020
MC/DC0%0/2
$@ or not $ok
$@ not $ok
Truth table33%1/3
A or not(B)
A= $@B= not $ok
ABresult
1X1
011
000
279 0 ▶ if ( $self->warnings > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->warnings > 1) { } 0 0 0
elsif ($self->warnings) { } 0 0 0
280 0 warn("Failed to extract '$archive': $@");
281 } elsif ( $self->warnings ) {
282 0 print " Failed: $dist\n";
283 }
284 0 next;
285 }
286  
287 # If using bin tools, do an additional check for
288 # damaged tarballs with non-executable directories (on unix)
289 20 my $extracted = $extract->extract_path;
290 20 ▶ unless ( -r $extracted and -x $extracted ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $extracted and -x $extracted) 0 20 20
Condition33%1/3
-r $extracted and -x $extracted
!ll&&!rl&&r
0020
MC/DC0%0/2
-r $extracted and -x $extracted
-r $extracted -x $extracted
Truth table33%1/3
A and B
A= -r $extractedB= -x $extracted
ABresult
0X0
100
111
291 # Handle special case where we have screwed up
292 # permissions on the extract directory.
293 # Just assume we have permissions for that.
294 0 File::chmod::chmod( 0755, $extracted );
295 }
296  
297 # Change into the directory
298 20 my $pushd2 = File::pushd::pushd( $extracted );
299  
300 # Invoke the callback
301 20 $self->callback->( {
302 counter => ++$counter,
303 archive => $archive,
304 dist => $dist,
305 author => $author,
306 tempdir => $extracted,
307 } );
308 }
309  
310 7 return 1;
311 }
312  
313  
314  
315  
316  
317 ######################################################################
318 # Support Methods
319  
320 sub _minicpan {
321 7 ▶ my $self = shift;
Subroutine _minicpan: called CC=2 SCAR=6.9
322 return Params::Util::_HASH($self->minicpan)
323 ? $self->minicpan->{local}
324 7 ▶ : $self->minicpan;
Branch: 1 branch
BranchTrueFalseTotal
&Params::Util::_HASH(scalar $self->minicpan) ? : 4 3 7
325 }
326  
327 4 1;
328  
329 =pod
330  
331 =head1 SUPPORT
332  
333 Bugs should be reported via the CPAN bug tracker at
334  
335 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPAN-Mini-Visit>
336  
337 For other issues, contact the author.
338  
339 =head1 AUTHOR
340  
341 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
342  
343 =head1 COPYRIGHT
344  
345 Copyright 2009 - 2011 Adam Kennedy.
346  
347 This program is free software; you can redistribute
348 it and/or modify it under the same terms as Perl itself.
349  
350 The full text of the license can be found in the
351 LICENSE file included with this module.
352  
353 =cut
↑ Summary