BackPAN/Index.pm

filter: Statementstmt 87.4% 118 / 135 Branchbran 88.2% 30 / 34 Conditioncond 73.5% 25 / 34 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 77.7% 21 / 27 Podpod 84.6% 11 / 13 totaltotal 82.0% 215 / 262 CC 31 SCAR 21.6
CC
31
Cov
85%
_populate_database
19.1
_update_database
9.5
_database_is_empty
3.1
CRAP 8.6
↑ Summary Parse/BACKPAN/Packages.pm »
1 package BackPAN::Index;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '0.42';
7  
8 10 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use CPAN::DistnameInfo 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use BackPAN::Index::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use BackPAN::Index::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 with 'BackPAN::Index::Role::Log', 'BackPAN::Index::Role::HasCache';
15  
16 10 has update =>
17 is => 'ro',
18 isa => 'Bool',
19 default => 0;
20  
21 10 has cache_ttl =>
22 is => 'ro',
23 isa => 'Int',
24 default => 60 * 60;
25  
26 10 has releases_only_from_authors =>
27 is => 'ro',
28 isa => 'Bool',
29 default => 1;
30  
31 10 has backpan_index_url =>
32 is => 'ro',
33 isa => 'URI',
34 coerce => 1,
35 builder => 'default_backpan_index_url';
36  
37 10 ▶sub default_backpan_index_url {
Subroutine default_backpan_index_url: called CC=1 SCAR=0.0
Pod undocumented
38 10 return "http://gitpan.integra.net/backpan-index.gz";
39 }
40  
41 has backpan_index =>
42 is => 'ro',
43 isa => 'BackPAN::Index::IndexFile',
44 lazy => 1,
45 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 10 my $self = shift;
47  
48 10 require BackPAN::Index::IndexFile;
49 10 return BackPAN::Index::IndexFile->new(
50 cache => $self->cache,
51 index_url => $self->backpan_index_url
52 );
53 10 };
54  
55 10 has cache_dir =>
56 is => 'ro',
57 isa => 'Str'
58 ;
59  
60 has '+cache' =>
61 is => 'rw',
62 required => 0,
63 lazy => 1,
64 10 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
65 10 my $self = shift;
66  
67 10 my %cache_opts;
68 10 $cache_opts{ttl} = $self->cache_ttl;
69 10 ▶ $cache_opts{directory} = $self->cache_dir if $self->cache_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cache_dir 10 0 10
70 10 $cache_opts{enabled} = !$self->update;
71  
72 10 require App::Cache;
73 10 return App::Cache->new( \%cache_opts );
74 }
75 10 ;
76  
77 has db =>
78 is => 'ro',
79 isa => 'BackPAN::Index::Database',
80 handles => [qw(schema)],
81 lazy => 1,
82 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 10 my $self = shift;
84  
85 10 require BackPAN::Index::Database;
86 10 return BackPAN::Index::Database->new(
87 cache => $self->cache
88 );
89 10 };
90  
91  
92 10 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
93 10 my $self = shift;
94  
95 10 $self->_update_database();
96  
97 10 return $self;
98 }
99  
100 10 ▶sub _update_database {
Subroutine _update_database: called CC=9 SCAR=22.5
101 10 my $self = shift;
102  
103 # Delay loading it into memory until we need it
104 10 ▶ $self->backpan_index->get_index if $self->backpan_index->should_index_be_updated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->backpan_index->should_index_be_updated 1 9 10
105  
106 10 ▶ my $should_update_db =
Condition66%4/6
$self->update || $self->db->should_update_db || $self->_index_archive_newer_than_db
l!l&&r!l&&!r
109
$self->update or $self->db->should_update_db
l!l&&r!l&&!r
109
MC/DC33%1/3
$self->update || $self->db->should_update_db || $self->_index_archive_newer_than_db
$self->update $self->db->should_update_db $self->_index_archive_newer_than_db
Truth table50%2/4
A or B || C
A= $self->updateB= $self->db->should_update_dbC= $self->_index_archive_newer_than_db
ABCresult
1XX1
01X1
0011
0000
107 $self->update ||
108 $self->db->should_update_db ||
109 $self->_index_archive_newer_than_db;
110  
111 10 my $db_file = $self->db->db_file;
112 10 ▶ unlink $db_file if -e $db_file and $should_update_db;
Branch: 1 branch
BranchTrueFalseTotal
if -e $db_file and $should_update_db 0 10 10
Condition66%2/3
-e $db_file and $should_update_db
!ll&&!rl&&r
190
MC/DC0%0/2
-e $db_file and $should_update_db
-e $db_file $should_update_db
Truth table66%2/3
A and B
A= -e $db_fileB= $should_update_db
ABresult
0X0
100
111
113  
114 10 ▶ $self->db->create_tables if $should_update_db;
Branch: 1 branch
BranchTrueFalseTotal
if $should_update_db 1 9 10
115  
116 10 ▶ $self->_populate_database if $should_update_db || $self->_database_is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $should_update_db or $self->_database_is_empty 1 9 10
Condition66%2/3
$should_update_db or $self->_database_is_empty
l!l&&r!l&&!r
109
MC/DC50%1/2
$should_update_db or $self->_database_is_empty
$should_update_db $self->_database_is_empty
Truth table66%2/3
A or B
A= $should_update_dbB= $self->_database_is_empty
ABresult
1X1
011
000
117  
118 10 return;
119 }
120  
121 9 ▶sub _index_archive_newer_than_db {
Subroutine _index_archive_newer_than_db: called CC=1 SCAR=0.0
122 9 my $self = shift;
123  
124 9 return $self->db->db_mtime < $self->backpan_index->index_archive_mtime;
125 }
126  
127 1 ▶sub _populate_database {
Subroutine _populate_database: called CC=19 SCAR=29.5
128 1 my $self = shift;
129  
130 1 my $dbh = $self->db->dbh;
131  
132 1 $self->_log("Populating database...");
133 1 $dbh->begin_work;
134  
135 # Get it out of the hot loop.
136 1 my $only_authors = $self->releases_only_from_authors;
137  
138 1 my $insert_file_sth = $dbh->prepare(q[
139 INSERT INTO files
140 (path, date, size)
141 VALUES (?, ?, ? )
142 ]);
143  
144 1 my $insert_release_sth = $dbh->prepare(q[
145 INSERT INTO releases
146 (path, dist, version, date, size, maturity, cpanid, distvname)
147 VALUES (?, ?, ?, ?, ?, ?, ?, ? )
148 ]);
149  
150 1 my $insert_dist_sth = $dbh->prepare(q[
151 INSERT INTO dists
152 (name, num_releases,
153 first_release, first_date, first_author,
154 latest_release, latest_date, latest_author)
155 VALUES (?, ?,
156 ?, ?, ?,
157 ?, ?, ?)
158 ]);
159  
160 1 my %dists;
161 my %files;
162 1 open my $fh, $self->backpan_index->index_file;
163 1 ▶ while( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 276149 1 276150
164 276149 chomp $line;
165 276149 my ( $path, $date, $size, @junk ) = split ' ', $line;
166  
167 276149 ▶ if( $files{$path}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files{$path}++) 2 276147 276149
168 2 $self->_log("Duplicate file $path in index, ignoring");
169 2 next;
170 }
171  
172 276147 ▶ if( !defined $path or !defined $date or !defined $size or @junk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $path && defined $date && defined $size or @junk) 1 276146 276147
Condition55%5/9
not defined $path && defined $date && defined $size or @junk
l!l&&r!l&&!r
10276146
defined $path && defined $date && defined $size
!ll&&!rl&&r
01276146
defined $path and defined $date
!ll&&!rl&&r
00276147
MC/DC25%1/4
not defined $path && defined $date && defined $size or @junk
defined $path defined $date defined $size @junk
Truth table40%2/5
not(A and B && C) or D
A= defined $pathB= defined $dateC= defined $sizeD= @junk
ABCDresult
0XXX1
10XX1
110X1
11111
11100
173 1 $self->_log("Bad data read at line $.: $line");
174 1 next;
175 }
176  
177 276146 ▶ next unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 300 275846 276146
178 275846 ▶ next if $only_authors and $path !~ m{^authors/};
Branch: 1 branch
BranchTrueFalseTotal
if $only_authors and not $path =~ m[^authors/] 343 275503 275846
Condition66%2/3
$only_authors and not $path =~ m[^authors/]
!ll&&!rl&&r
0275503343
MC/DC50%1/2
$only_authors and not $path =~ m[^authors/]
$only_authors not $path =~ m[^authors/]
Truth table66%2/3
A and not(B)
A= $only_authorsB= not $path =~ m[^authors/]
ABresult
0X0
100
111
179  
180 275503 $insert_file_sth->execute($path, $date, $size);
181  
182 275503 ▶ next if $path =~ /\.(readme|meta)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.(readme|meta)$/ 152789 122714 275503
183  
184 122714 my $i = CPAN::DistnameInfo->new( $path );
185  
186 122714 my $dist = $i->dist;
187 122714 ▶ next unless $i->dist;
Branch: 1 branch
BranchTrueFalseTotal
unless $i->dist 4085 118629 122714
188  
189 118629 ▶ $insert_release_sth->execute(
Condition100%2/2
$i->version || ''
l!l
118184445
MC/DC100%1/1
$i->version || ''
$i->version
Truth table100%2/2
A
A= $i->version
Aresult
11
00
190 $path,
191 $dist,
192 $i->version || '',
193 $date,
194 $size,
195 $i->maturity,
196 $i->cpanid,
197 $i->distvname,
198 );
199  
200  
201 # Update aggregate data about dists
202 118629 ▶ my $distdata = ($dists{$dist} ||= { name => $dist });
Condition100%2/2
$dists{$dist} ||= {"name", $dist}
l!l
9671421915
MC/DC100%1/1
$dists{$dist} ||= {"name", $dist}
$dists{$dist}
Truth table100%2/2
A
A= $dists{$dist}
Aresult
11
00
203  
204 118629 ▶ if( !defined $distdata->{first_release} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $distdata->{"first_release"} or $date < $distdata->{"first_date"}) 23944 94685 118629
Condition100%3/3
not defined $distdata->{"first_release"} or $date < $distdata->{"first_date"}
l!l&&r!l&&!r
21915202994685
MC/DC100%2/2
not defined $distdata->{"first_release"} or $date < $distdata->{"first_date"}
not defined $distdata->{"first_release"} $date < $distdata->{"first_date"}
Truth table100%3/3
not(A) or B
A= not defined $distdata->{"first_release"}B= $date < $distdata->{"first_date"}
ABresult
1X1
011
000
205 $date < $distdata->{first_date} )
206 {
207 23944 $distdata->{first_release} = $path;
208 23944 $distdata->{first_author} = $i->cpanid;
209 23944 $distdata->{first_date} = $date;
210 }
211  
212 118629 ▶ if( !defined $distdata->{latest_release} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $distdata->{"latest_release"} or $date > $distdata->{"latest_date"}) 105746 12883 118629
Condition100%3/3
not defined $distdata->{"latest_release"} or $date > $distdata->{"latest_date"}
l!l&&r!l&&!r
219158383112883
MC/DC100%2/2
not defined $distdata->{"latest_release"} or $date > $distdata->{"latest_date"}
not defined $distdata->{"latest_release"} $date > $distdata->{"latest_date"}
Truth table100%3/3
not(A) or B
A= not defined $distdata->{"latest_release"}B= $date > $distdata->{"latest_date"}
ABresult
1X1
011
000
213 $date > $distdata->{latest_date} )
214 {
215 105746 $distdata->{latest_release} = $path;
216 105746 $distdata->{latest_author} = $i->cpanid;
217 105746 $distdata->{latest_date} = $date;
218 }
219  
220 118629 $distdata->{num_releases}++;
221 }
222  
223 1 for my $dist (values %dists) {
224 $insert_dist_sth->execute(
225 21915 @{$dist}
226 21915 {qw(name num_releases
227 first_release first_date first_author
228 latest_release latest_date latest_author
229 )}
230 );
231 }
232  
233 # Add indexes after inserting so as not to slow down the inserts
234 1 $self->db->create_indexes;
235  
236 1 $dbh->commit;
237  
238 1 $self->_log("Done.");
239  
240 1 return;
241 }
242  
243  
244 9 ▶sub _database_is_empty {
Subroutine _database_is_empty: called CC=3 SCAR=11.4
245 9 my $self = shift;
246  
247 9 ▶ return 1 unless $self->files->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->files->count 0 9 9
248 9 ▶ return 1 unless $self->releases->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->releases->count 0 9 9
249 9 return 0;
250 }
251  
252  
253 1 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod undocumented
254 1 my($self, $path) = @_;
255 1 return $self->files->single({ path => $path });
256 }
257  
258 14 ▶sub files {
Subroutine files: called CC=1 SCAR=0.0
Pod documented
259 14 my $self = shift;
260 14 return $self->schema->resultset('File');
261 }
262  
263  
264 2 ▶sub dist {
Subroutine dist: called CC=1 SCAR=0.0
Pod documented
265 2 my($self, $dist) = @_;
266  
267 2 return $self->dists->single({ name => $dist });
268 }
269  
270  
271 11 ▶sub releases {
Subroutine releases: called CC=2 SCAR=6.9
Pod documented
272 11 my($self, $dist) = @_;
273  
274 11 ▶ return $self->schema->resultset("Release") unless defined $dist;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dist 9 2 11
275 2 return $self->schema->resultset("Release")->search({ dist => $dist });
276 }
277  
278  
279 0 ▶sub release {
Subroutine release: not called CC=1 SCAR=6.9
Pod documented
280 0 my($self, $dist, $version) = @_;
281  
282 0 return $self->releases($dist)->single({ version => $version });
283 }
284  
285  
286 5 ▶sub dists {
Subroutine dists: called CC=1 SCAR=0.0
Pod documented
287 5 my $self = shift;
288  
289 5 return $self->schema->resultset("Dist");
290 }
291  
292  
293 =head1 NAME
294  
295 BackPAN::Index - An interface to the BackPAN index
296  
297 =head1 SYNOPSIS
298  
299 use BackPAN::Index;
300 my $backpan = BackPAN::Index->new;
301  
302 # These are all DBIx::Class::ResultSet's
303 my $files = $backpan->files;
304 my $dists = $backpan->dists;
305 my $releases = $backpan->releases("Acme-Pony");
306  
307 # Use DBIx::Class::ResultSet methods on them
308 my $release = $releases->single({ version => '1.23' });
309  
310 my $dist = $backpan->dist("Test-Simple");
311 my $releases = $dist->releases;
312  
313 =head1 DESCRIPTION
314  
315 This downloads, caches and parses the BackPAN index into a local
316 database for efficient querying.
317  
318 Its a pretty thin wrapper around DBIx::Class returning
319 L<DBIx::Class::ResultSet> objects which makes it efficient and
320 flexible.
321  
322 The Comprehensive Perl Archive Network (CPAN) is a very useful
323 collection of Perl code. However, in order to keep CPAN relatively
324 small, authors of modules can delete older versions of modules to only
325 let CPAN have the latest version of a module. BackPAN is where these
326 deleted modules are backed up. It's more like a full CPAN mirror, only
327 without the deletions. This module provides an index of BackPAN and
328 some handy methods.
329  
330 =head1 METHODS
331  
332 =head2 new
333  
334 my $backpan = BackPAN::Index->new(\%options);
335  
336 Create a new object representing the BackPAN index.
337  
338 It will, if necessary, download the BackPAN index and compile it into
339 a database for efficient storage. Initial creation is slow, but it
340 will be cached.
341  
342 new() takes some options
343  
344 =head3 update
345  
346 Because it is rather large, BackPAN::Index caches a copy of the
347 BackPAN index and builds a local database to speed access. This flag
348 controls if the local index is updated.
349  
350 If true, forces an update of the BACKPAN index.
351  
352 If false, the index will never be updated even if the cache is
353 expired. It will always create a new index if one does not exist.
354  
355 By default the index is cached and checked for updates according to
356 C<<$backpan->cache_ttl>>.
357  
358 =head3 cache_ttl
359  
360 How many seconds before checking for an updated index.
361  
362 Defaults to an hour.
363  
364 =head3 debug
365  
366 If true, debug messages will be printed.
367  
368 Defaults to false.
369  
370 =head3 releases_only_from_authors
371  
372 If true, only files in the C<authors> directory will be considered as
373 releases. If false any file in the index may be considered for a
374 release.
375  
376 Defaults to true.
377  
378 =head3 cache_dir
379  
380 Location of the cache directory.
381  
382 Defaults to whatever L<App::Cache> does.
383  
384 =head3 backpan_index_url
385  
386 URL to the BackPAN index.
387  
388 Defaults to a sensible location.
389  
390  
391 =head2 files
392  
393 my $files = $backpan->files;
394  
395 Returns a ResultSet representing all the files on BackPAN.
396  
397 =head2 files_by
398  
399 my $files = $backpan->files_by($cpanid);
400 my @files = $backpan->files_by($cpanid);
401  
402 Returns all the files by a given $cpanid.
403  
404 Returns either a list of BackPAN::Index::Files or a ResultSet.
405  
406 =cut
407  
408 0 ▶sub files_by {
Subroutine files_by: not called CC=1 SCAR=6.9
Pod documented
409 0 my $self = shift;
410 0 my $cpanid = shift;
411  
412 0 return $self->files->search({ "releases.cpanid" => $cpanid }, { join => "releases" });
413 }
414  
415 =head2 dists
416  
417 my $dists = $backpan->dists;
418  
419 Returns a ResultSet representing all the distributions on BackPAN.
420  
421 =head2 dist
422  
423 my $dists = $backpan->dist($dist_name);
424  
425 Returns a single BackPAN::Index::Dist object for $dist_name.
426  
427 =head2 dists_by
428  
429 my $dists = $backpan->dists_by($cpanid);
430 my @dists = $backpan->dists_by($cpanid);
431  
432 Returns the dists which contain at least one release by the given
433 $cpanid.
434  
435 Returns either a ResultSet or a list of the Dists.
436  
437 =cut
438  
439 0 ▶sub dists_by {
Subroutine dists_by: not called CC=1 SCAR=6.9
Pod documented
440 0 my $self = shift;
441 0 my $cpanid = shift;
442  
443 0 return $self->dists->search({ "releases.cpanid" => $cpanid }, { join => "releases", distinct => 1 });
444 }
445  
446  
447 =head2 dists_changed_since
448  
449 my $dists = $backpan->dists_changed_since($time);
450  
451 Returns a ResultSet of distributions which have had releases at or after after $time.
452  
453 =cut
454  
455 0 ▶sub dists_changed_since {
Subroutine dists_changed_since: not called CC=1 SCAR=6.9
Pod documented
456 0 my $self = shift;
457 0 my $time = shift;
458  
459 0 return $self->dists->search( latest_date => \">= $time" );
460 }
461  
462 =head2 releases
463  
464 my $all_releases = $backpan->releases();
465 my $dist_releases = $backpan->releases($dist_name);
466  
467 Returns a ResultSet representing all the releases on BackPAN. If a
468 $dist_name is given it returns the releases of just one distribution.
469  
470 =head2 release
471  
472 my $release = $backpan->release($dist_name, $version);
473  
474 Returns a single BackPAN::Index::Release object for the given
475 $dist_name and $version.
476  
477 =head2 releases_by
478  
479 my $releases = $backpan->releases_by($cpanid);
480 my @releases = $backpan->releases_by($cpanid);
481  
482 Returns all the releases of a single author.
483  
484 Returns either a list of Releases or a ResultSet representing those releases.
485  
486 =cut
487  
488 0 ▶sub releases_by {
Subroutine releases_by: not called CC=1 SCAR=6.9
Pod documented
489 0 my $self = shift;
490 0 my $cpanid = shift;
491  
492 0 return $self->releases->search({ cpanid => $cpanid });
493 }
494  
495  
496 =head2 releases_since
497  
498 my $releases = $backpan->releases_since($time);
499  
500 Returns a ResultSet of releases which were released at or after $time.
501  
502 =cut
503  
504 0 ▶sub releases_since {
Subroutine releases_since: not called CC=1 SCAR=6.9
Pod documented
505 0 my $self = shift;
506 0 my $time = shift;
507  
508 0 return $self->releases->search( date => \">= $time" );
509 }
510  
511  
512 =head1 EXAMPLES
513  
514 The real power of BackPAN::Index comes from L<DBIx::Class::ResultSet>.
515 Its very flexible and very powerful but not always obvious how to get
516 it to do things. Here's some examples.
517  
518 # How many files are on BackPAN?
519 my $count = $backpan->files->count;
520  
521 # How big is BackPAN?
522 my $size = $backpan->files->get_column("size")->sum;
523  
524 # What are the names of all the distributions?
525 my @names = $backpan->dists->get_column("name")->all;
526  
527 # What path contains this release?
528 my $path = $backpan->release("Acme-Pony", 1.01)->path;
529  
530 # Get all the releases of Moose ordered by version
531 my @releases = $backpan->dist("Moose")->releases
532 ->search(undef, { order_by => "version" });
533  
534 =head1 AUTHOR
535  
536 Michael G Schwern <schwern@pobox.com>
537  
538 =head1 COPYRIGHT
539  
540 Copyright 2009, Michael G Schwern
541  
542 =head1 LICENSE
543  
544 This module is free software; you can redistribute it or modify it under
545 the same terms as Perl itself.
546  
547 =head1 SEE ALSO
548  
549 L<DBIx::Class::ResultSet>, L<BackPAN::Index::File>,
550 L<BackPAN::Index::Release>, L<BackPAN::Index::Dist>
551  
552 Repository: L<http://github.com/acme/parse-backpan-packages>
553 Bugs: L<http://rt.cpan.org/Public/Dist/Display.html?Name=Parse-BACKPAN-Packages>
554  
555 =cut
556  
557 10 1;
↑ Summary Parse/BACKPAN/Packages.pm »