Index/Loader.pm

filter: Statementstmt 90.4% 85 / 94 Branchbran 63.8% 23 / 36 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 20 / 22 Podpod 100% 9 / 9 totaltotal 81.2% 139 / 171 CC 21 SCAR 12.9
CC
21
Cov
81%
load_packages
8.1
load_authors
5.8
_load_authors
4.1
CRAP 3.6
↑ Summary Index/Package.pm »
1 package CPAN::Index::Loader;
2  
3 =pod
4  
5 =head1 NAME
6  
7 CPAN::Index::Loader - Populates the CPAN index SQLite database
8  
9 =head1 DESCRIPTION
10  
11 This package implements all the functionality required to download
12 the CPAN index data, parse it, and populate the SQLite database
13 file.
14  
15 Because it involves loading a number of otherwise unneeded modules,
16 this package is B<not> loaded by default with the rest of
17 L<CPAN::Index>, but may be loaded on-demand if needed.
18  
19 =head1 METHODS
20  
21 =cut
22  
23 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use IO::Zlib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Params::Util qw{ _INSTANCE _HANDLE };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Email::Address ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use CPAN::Cache ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 $VERSION = '0.01';
34 }
35  
36  
37  
38  
39  
40 #####################################################################
41 # Constructor and Accessors
42  
43 =pod
44  
45 =head2 new
46  
47 my $loader = CPAN::Index::Loader->new(
48 remote_uri => 'http://search.cpan.org/CPAN',
49 local_dir => '/tmp/cpanindex',
50 );
51  
52 =cut
53  
54 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
55 2 my $class = shift;
56 2 my $self = bless { @_ }, $class;
57  
58 # Create the cache object
59 2 ▶ unless ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->cache) 2 0 2
60 2 my @params = ();
61 $self->{cache} = CPAN::Cache->new(
62 remote_uri => delete($self->{remote_uri}),
63 local_dir => delete($self->{local_dir}),
64 trace => $self->{trace},
65 verbose => $self->{verbose},
66 2 );
67 }
68  
69 2 $self;
70 }
71  
72 =pod
73  
74 =head2 cache
75  
76 The C<cache> accessor returns a L<CPAN::Cache> object that represents the
77 CPAN cache.
78  
79 =cut
80  
81 8 ▶sub cache {
Subroutine cache: called CC=1 SCAR=0.0
Pod documented
82 8 $_[0]->{cache};
83 }
84  
85 =pod
86  
87 =head2 remote_uri
88  
89 The C<remote_uri> accessor return a L<URI> object for the location of the
90 CPAN mirror.
91  
92 =cut
93  
94 0 ▶sub remote_uri {
Subroutine remote_uri: not called CC=1 SCAR=6.9
Pod documented
95 0 $_[0]->cache->remote_uri;
96 }
97  
98 =pod
99  
100 =head2 local_dir
101  
102 The C<local_dir> accessor returns the filesystem path for the root directory
103 of the local CPAN file cache.
104  
105 =cut
106  
107 0 ▶sub local_dir {
Subroutine local_dir: not called CC=1 SCAR=6.9
Pod documented
108 0 $_[0]->cache->local_dir;
109 }
110  
111 =pod
112  
113 =head2 local_file
114  
115 my $path = $loader->local_file('01mailrc.txt.gz');
116  
117 The C<local_file> method takes the name of a file in the CPAN and returns
118 the local path to the file.
119  
120 Returns a path string, or throws an exception on error.
121  
122 =cut
123  
124 6 ▶sub local_file {
Subroutine local_file: called CC=1 SCAR=0.0
Pod documented
125 6 $_[0]->cache->file($_[1])->path;
126 }
127  
128 =pod
129  
130 =head2 local_handle
131  
132 my $path = $loader->local_handle('01mailrc.txt.gz');
133  
134 The C<local_handle> method takes the name of a file in the CPAN and returns
135 an L<IO::Handle> to the file.
136  
137 Returns an L<IO::Handle>, most likely an L<IO::Handle>, or throws an
138 exception on error.
139  
140 =cut
141  
142 4 ▶sub local_handle {
Subroutine local_handle: called CC=2 SCAR=6.9
Pod documented
143 4 my $self = shift;
144 4 my $file = $self->local_file(shift);
145 4 ▶ $file =~ /\.gz$/
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\.gz$/ ? : 1 3 4
146 ? IO::Zlib->new( $file, 'rb' ) # [r]ead [b]inary file
147 : IO::File->new( $file );
148 }
149  
150  
151  
152  
153  
154 #####################################################################
155 # Main Methods
156  
157 =pod
158  
159 =head2 load_index
160  
161 The C<load_index> takes a single param of the schema to load, locates
162 the three main index files based on the C<local_dir> path, and then
163 loads the index from those files.
164  
165 Returns the total number of records added.
166  
167 =cut
168  
169 1 ▶sub load_index {
Subroutine load_index: called CC=3 SCAR=12.0
Pod documented
170 1 my $self = shift;
171 1 my $schema = shift;
172 1 my $created = 0;
173  
174 # Load the files
175 1 ▶ $created += $self->load_authors(
Condition33%1/3
$self->local_handle('authors/01mailrc.txt') || $self->local_handle('authors/01mailrc.txt.gz')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->local_handle('authors/01mailrc.txt') || $self->local_handle('authors/01mailrc.txt.gz')
$self->local_handle('authors/01mailrc.txt') $self->local_handle('authors/01mailrc.txt.gz')
Truth table33%1/3
A || B
A= $self->local_handle('authors/01mailrc.txt')B= $self->local_handle('authors/01mailrc.txt.gz')
ABresult
1X1
011
000
176 $schema,
177 $self->local_handle('authors/01mailrc.txt') ||
178 $self->local_handle('authors/01mailrc.txt.gz'),
179 );
180 1 ▶ $created += $self->load_packages(
Condition33%1/3
$self->local_handle('modules/02packages.details.txt') || $self->local_handle('modules/02packages.details.txt.gz')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->local_handle('modules/02packages.details.txt') || $self->local_handle('modules/02packages.details.txt.gz')
$self->local_handle('modules/02packages.details.txt') $self->local_handle('modules/02packages.details.txt.gz')
Truth table33%1/3
A || B
A= $self->local_handle('modules/02packages.details.txt')B= $self->local_handle('modules/02packages.details.txt.gz')
ABresult
1X1
011
000
181 $schema,
182 $self->local_handle('modules/02packages.details.txt') ||
183 $self->local_handle('modules/02packages.details.txt.gz'),
184 );
185  
186 # Return the total
187 1 $created;
188 }
189  
190  
191  
192  
193  
194 #####################################################################
195 # Parsing Methods
196  
197 =pod
198  
199 =head2 load_authors
200  
201 CPAN::Index::Loader->load_authors( $schema, $handle );
202  
203 The C<load_authors> method populates the C<package> table from the CPAN
204 F<01mailrc.txt.gz> file.
205  
206 The C<author> table in the SQLite database should already be empty
207 B<before> this method is called.
208  
209 Returns the number of authors added to the database, or throws an
210 exception on error.
211  
212 =cut
213  
214 2 ▶sub load_authors {
Subroutine load_authors: called CC=5 SCAR=17.6
Pod documented
215 2 my $self = shift;
216 2 ▶ my $schema = _INSTANCE(shift, 'DBIx::Class::Schema')
Branch: 1 branch
BranchTrueFalseTotal
my $schema = &_INSTANCE(shift(), "DBIx::Class::Schema") or Carp::croak("Did not provide a DBIx::Class::Schema param") 2 0 2
217 or Carp::croak("Did not provide a DBIx::Class::Schema param");
218 2 ▶ my $handle = _HANDLE(shift)
Branch: 1 branch
BranchTrueFalseTotal
my $handle = _HANDLE(shift()) or Carp::croak("Did not provide a file handle param") 2 0 2
219 or Carp::croak("Did not provide a file handle param");
220  
221 # Wrap the actual method in a DBIx::Class transaction
222 2 my $created = 0;
223
224 2 my $rs = eval {
225 2 ▶ $schema->txn_do( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 2 $created = $self->_load_authors( $schema, $handle );
227 2 } );
228 };
229 2 ▶ if ( $@ =~ /Rollback failed/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /Rollback failed/) { } 0 2 2
elsif ($@) { } 0 2 2
230 0 Carp::croak("Rollback failed, database may be corrupt");
231 } elsif ( $@ ) {
232 0 Carp::croak("Database error while loading authors: $@");
233 }
234  
235 2 $created;
236 }
237
238 2 ▶sub _load_authors {
Subroutine _load_authors: called CC=4 SCAR=14.2
239 2 my ($self, $schema, $handle) = @_;
240  
241 # Every email address should be different, so disable
242 # Email::Address caching so we don't waste a bunch of memory.
243 2 local $Email::Address::NOCACHE = 1;
244  
245 # Process the author records
246 2 my $created = 0;
247 2 ▶ while ( my $line = $handle->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $handle->getline) 18 2 20
248 # Parse the line
249 18 ▶ unless ( $line =~ /^alias\s+(\S+)\s+\"(.+)\"[\012\015]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^alias\s+(\S+)\s+\"(.+)\"[\012\015]+$/) 0 18 18
250 0 Carp::croak("Invalid 01mailrc.txt.gz line '$line'");
251 }
252 18 my $id = $1;
253 18 my $email = $2;
254  
255 # Parse the full email address to seperate the parts
256 18 my @found = Email::Address->parse($email);
257 18 ▶ unless ( @found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@found) 0 18 18
258 # Invalid email or something that Email::Address can't handle.
259 # Use a default name and address for now.
260 0 @found = Email::Address->parse( "$id <$id\@cpan.org>" );
261 }
262  
263 # Some CPAN users have multiple addresses, for example
264 # A. PREM ANAND <prem_and@rediffmail.com,prem@ncbs.res.in>
265 # When this happens, we'll just take the first one.
266  
267 # Create the record
268 18 $schema->resultset('Author')->create( {
269 id => $id,
270 name => $found[0]->name,
271 email => $found[0]->address,
272 } );
273 18 $created++;
274  
275 # Debugging
276 #if ( $Test::More::VERSION ) {
277 # Test::More::diag("$created...");
278 #}
279 }
280  
281 2 $created;
282 }
283  
284 =pod
285  
286 =head2 load_packages
287  
288 CPAN::Index::Loader->load_packages( $schema, $handle );
289  
290 The C<load_packages> method populates the C<package> table from the CPAN
291 F<02packages.details.txt.gz> file.
292  
293 The C<package> table in the SQLite database should already be empty
294 B<before> this method is called.
295  
296 Returns the number of packages added to the database, or throws an
297 exception on error.
298  
299 =cut
300  
301 2 ▶sub load_packages {
Subroutine load_packages: called CC=7 SCAR=20.9
Pod documented
302 2 my $self = shift;
303 2 ▶ my $schema = _INSTANCE(shift, 'DBIx::Class::Schema')
Branch: 1 branch
BranchTrueFalseTotal
my $schema = &_INSTANCE(shift(), "DBIx::Class::Schema") or Carp::croak("Did not provide a DBIx::Class::Schema param") 2 0 2
304 or Carp::croak("Did not provide a DBIx::Class::Schema param");
305 2 ▶ my $handle = _HANDLE(shift)
Branch: 1 branch
BranchTrueFalseTotal
my $handle = _HANDLE(shift()) or Carp::croak("Did not provide a file handle param") 2 0 2
306 or Carp::croak("Did not provide a file handle param");
307  
308 # Advance past the header, to the first blank line
309 2 ▶ while ( my $line = $handle->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $handle->getline) 18 0 18
310 18 ▶ last if $line !~ /[^\s\012\015]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /[^\s\012\015]/ 2 16 18
311 }
312  
313 # Wrap the database method in a DBIx::Class transaction
314 2 my $created;
315 2 my $rs = eval {
316 2 ▶ $schema->txn_do( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
317 2 $created = $self->_load_packages( $schema, $handle );
318 2 } );
319 };
320 2 ▶ if ( $@ =~ /Rollback failed/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /Rollback failed/) { } 0 2 2
elsif ($@) { } 0 2 2
321 0 Carp::croak("Rollback failed, database may be corrupt");
322 } elsif ( $@ ) {
323 0 Carp::croak("Database error while loading packages: $@");
324 }
325  
326 2 $created;
327 }
328  
329 2 ▶sub _load_packages {
Subroutine _load_packages: called CC=4 SCAR=13.9
330 2 my ($self, $schema, $handle) = @_;
331  
332 # Process the author records
333 2 my $created = 0;
334 2 ▶ while ( my $line = $handle->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $handle->getline) 18 2 20
335 18 ▶ unless ( $line =~ /^(\S+)\s+(\S+)\s+(.+?)[\012\015]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^(\S+)\s+(\S+)\s+(.+?)[\012\015]+$/) 0 18 18
336 0 Carp::croak("Invalid 02packages.details.txt.gz line '$line'");
337 }
338 18 my $name = $1;
339 18 ▶ my $version = $2 eq 'undef' ? undef : $2;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq 'undef' ? : 4 14 18
340 18 my $path = $3;
341  
342 # Create the record
343 18 $schema->resultset('Package')->create( {
344 name => $name,
345 version => $version,
346 path => $path,
347 } );
348 18 $created++;
349  
350 # Debugging
351 #if ( $Test::More::VERSION ) {
352 # Test::More::diag("$created...");
353 #}
354 }
355  
356 2 $created;
357 }
358  
359 5 1;
360  
361 =pod
362  
363 =head1 SUPPORT
364  
365 Bugs should be reported via the CPAN bug tracker
366  
367 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CPAN-Index>
368  
369 For other issues, contact the author.
370  
371 =head1 AUTHOR
372  
373 Adam Kennedy E<lt>cpan@ali.asE<gt>
374  
375 Parts based on various modules by Leon Brocard E<lt>acme@cpan.orgE<gt>
376  
377 =head1 SEE ALSO
378  
379 Related: L<CPAN::Index>, L<CPAN>
380  
381 Based on: L<Parse::CPAN::Authors>, L<Parse::CPAN::Packages>
382  
383 =head1 COPYRIGHT
384  
385 Copyright (c) 2006 Adam Kennedy.
386  
387 This program is free software; you can redistribute
388 it and/or modify it under the same terms as Perl itself.
389  
390 The full text of the license can be found in the
391 LICENSE file included with this module.
392  
393 =cut
↑ Summary Index/Package.pm »