blib/lib/ORDB/CPANMeta/Generator.pm

filter: Statementstmt 83.1% 133 / 160 Branchbran 47.5% 19 / 40 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.4% 27 / 28 Podpod 57.1% 4 / 7 totaltotal 76.6% 184 / 240 CC 27 SCAR 21.2
CC
27
Cov
75%
run
21.9
__ANON__
11.3
new
5.2
CRAP 8.3
↑ Summary
1 package ORDB::CPANMeta::Generator;
2  
3 =pod
4  
5 =head1 NAME
6  
7 ORDB::CPANMeta::Generator - Generator for the CPAN Meta database
8  
9 =head1 DESCRIPTION
10  
11 This is the module that is used to generate the "CPAN Meta" database.
12  
13 For more information, and to access this database as a consumer, see
14 the L<ORDB::CPANMeta> module.
15  
16 The bulk of the work done in this module is actually achieved with:
17  
18 L<CPAN::Mini> - Fetching the index and dist tarballs
19  
20 L<CPAN::Mini::Visit> - Expanding and processing the tarballs
21  
22 L<Xtract> - Preparing the SQLite database for distribution
23  
24 =head1 METHODS
25  
26 =cut
27  
28 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use File::Spec 3.29 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use File::Path 2.07 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use File::Remove 1.42 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use File::HomeDir 0.86 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use File::Basename 0 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Module::CoreList 2.46 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use Parse::CPAN::Meta 1.4200 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Params::Util 1.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use Getopt::Long 2.34 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use DBI 1.609 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use CPAN::Meta 2.112621 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use CPAN::Mini 0.576 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use CPAN::Mini::Visit 1.14 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use Xtract::Publish 0.12 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 2 our $VERSION = '0.12';
47  
48 2 ▶use Object::Tiny 1.06 qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
49 minicpan
50 sqlite
51 publish
52 visit
53 trace
54 delta
55 prefer_bin
56 warnings
57 dbh
58 2 };
59  
60  
61  
62  
63  
64 ######################################################################
65 # Constructor and Accessors
66  
67 =pod
68  
69 =head2 new
70  
71 The C<new> constructor creates a new processor/generator.
72  
73 =cut
74  
75 1 ▶sub new {
Subroutine new: called CC=4 SCAR=16.4
Pod documented
76 1 my $self = shift->SUPER::new(@_);
77  
78 # Set the default path to the database
79 1 ▶ unless ( defined $self->sqlite ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->sqlite) 0 1 1
80 0 ▶ $self->{sqlite} = File::Spec->catdir(
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
81 File::HomeDir->my_data,
82 ($^O eq 'MSWin32' ? 'Perl' : '.perl'),
83 'ORDB-CPANMeta-Generator',
84 'metadb.sqlite',
85 );
86 }
87  
88 # Set the default path to the publishing location
89 1 ▶ unless ( exists $self->{publish} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"publish"}) 1 0 1
90 1 $self->{publish} = 'cpanmeta';
91 }
92  
93 1 return $self;
94 }
95  
96 =pod
97  
98 =head2 dir
99  
100 The C<dir> method returns the directory that the SQLite
101 database will be written into.
102  
103 =cut
104  
105 2 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.0
Pod documented
106 2 File::Basename::dirname($_[0]->sqlite);
107 }
108  
109 =pod
110  
111 =head2 dsn
112  
113 The C<dsn> method returns the L<DBI> DSN that is used to connect
114 to the generated database.
115  
116 =cut
117  
118 2 ▶sub dsn {
Subroutine dsn: called CC=1 SCAR=0.0
Pod documented
119 2 "DBI:SQLite:" . $_[0]->sqlite
120 }
121  
122  
123  
124  
125  
126 ######################################################################
127 # Main Methods
128  
129 =pod
130  
131 =head2 run
132  
133 The C<run> method executes the process that will produce and fill the
134 final database.
135  
136 =cut
137  
138 1 ▶sub run {
Subroutine run: called CC=13 SCAR=30.9
Pod documented
139 1 my $self = shift;
140  
141 # Normalise
142 1 ▶ $self->{prefer_bin} = $self->prefer_bin ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->prefer_bin ? : 0 1 1
143  
144 # Create the output directory
145 1 File::Path::make_path($self->dir);
146 1 ▶ unless ( -d $self->dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->dir) 0 1 1
147 0 Carp::croak("Failed to create '" . $self->dir . "'");
148 }
149  
150 # Clear the database if it already exists
151 1 ▶ unless ( $self->delta ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->delta) 1 0 1
152 1 ▶ if ( -f $self->sqlite ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->sqlite) 0 1 1
153 0 File::Remove::remove($self->sqlite);
154 }
155 1 ▶ if ( -f $self->sqlite ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->sqlite) 0 1 1
156 0 Carp::croak("Failed to clear " . $self->sqlite);
157 }
158 }
159  
160 # Update the minicpan if needed
161 1 ▶ if ( Params::Util::_HASH($self->minicpan) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Params::Util::_HASH(scalar $self->minicpan)) 0 1 1
162 CPAN::Mini->update_mirror(
163 trace => $self->trace,
164 no_conn_cache => 1,
165 0 %{$self->minicpan},
166 );
167 0 $self->{minicpan} = $self->minicpan->{local};
168 }
169  
170 # Connect to the database
171 1 my $dbh = DBI->connect($self->dsn);
172 1 ▶ unless ( $dbh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 1 1
173 0 Carp::croak("connect: \$DBI::errstr");
174 }
175  
176 # Create the tables
177 1 $dbh->do(<<'END_SQL');
178 CREATE TABLE IF NOT EXISTS meta_distribution (
179 release TEXT NOT NULL,
180 meta INTEGER,
181 meta_name TEXT,
182 meta_version TEXT,
183 meta_abstract TEXT,
184 meta_generated TEXT,
185 meta_from TEXT,
186 meta_license TEXT
187 );
188 END_SQL
189  
190 1 $dbh->do(<<'END_SQL');
191 CREATE TABLE IF NOT EXISTS meta_dependency (
192 release TEXT NOT NULL,
193 module TEXT NOT NULL,
194 version TEXT NULL,
195 phase TEXT NOT NULL,
196 core REAL NULL
197 )
198 END_SQL
199  
200 ### NOTE: This does nothing right now but will later.
201 # Build the index of seen archives.
202 # While building the index, remove entries
203 # that are no longer in the minicpan.
204 1 my $ignore = undef;
205 1 ▶ if ( $self->delta ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->delta) 0 1 1
206 0 $dbh->begin_work;
207 0 my %seen = ();
208 0 my $dists = $dbh->selectcol_arrayref(
209 'SELECT DISTINCT release FROM meta_distribution'
210 );
211 0 foreach my $dist ( @$dists ) {
212 0 my $one = substr($dist, 0, 1);
213 0 my $two = substr($dist, 0, 2);
214 0 my $path = File::Spec->catfile(
215 $self->minicpan,
216 'authors', 'id',
217 $one, $two,
218 split /\//, $dist,
219 );
220 0 ▶ if ( -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 0 0 0
221 # Add to the ignore list
222 0 $seen{$dist} = 1;
223 0 next;
224 }
225  
226 # Clear the release from the database
227 $dbh->do(
228 0 'DELETE FROM meta_distribution WHERE release = ?',
229 {}, $dist,
230 );
231 }
232 $dbh->do(
233 0 'DELETE FROM meta_dependency WHERE release NOT IN '
234 . '( SELECT release FROM meta_distribution )',
235 );
236 0 $dbh->commit;
237  
238 # NOW we need to start ignoring something
239 $ignore = [
240 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 $seen{ $_[0]->{dist} }
242 0 }
243 0 ];
244 }
245  
246 # Clear indexes for speed
247 1 $self->drop_indexes( $dbh );
248  
249 # Run the visitor to generate the database
250 1 $dbh->begin_work;
251 1 my @meta_dist = ();
252 1 my @meta_deps = ();
253 my $visitor = CPAN::Mini::Visit->new(
254 acme => 1,
255 warnings => $self->warnings,
256 minicpan => $self->minicpan,
257 # This does nothing now but will later
258 ignore => $ignore,
259 prefer_bin => $self->prefer_bin,
260 4 ▶ callback => sub {
Subroutine __ANON__: called CC=10 SCAR=24.2
261 4 ▶ print STDERR "$_[0]->{dist}\n" if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 4 4
262 4 my $the = shift;
263 4 my $meta = undef;
264 4 my @deps = ();
265 my $dist = {
266 release => $the->{dist},
267 4 meta => 0,
268 };
269 my $yaml_file = File::Spec->catfile(
270 4 $the->{tempdir}, 'META.yml',
271 );
272 my $json_file = File::Spec->catfile(
273 4 $the->{tempdir}, 'META.json',
274 );
275 4 ▶ if ( -f $json_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $json_file) { } 0 4 4
elsif (-f $yaml_file) { } 4 0 4
276 0 $meta = eval {
277 0 CPAN::Meta->load_file($json_file)
278 };
279 } elsif ( -f $yaml_file ) {
280 4 $meta = eval {
281 4 CPAN::Meta->load_file($yaml_file)
282 };
283 }
284 4 ▶ unless ( $@ or not defined $meta ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@ or not defined $meta) 4 0 4
Condition33%1/3
$@ or not defined $meta
l!l&&r!l&&!r
004
MC/DC0%0/2
$@ or not defined $meta
$@ not defined $meta
Truth table33%1/3
A or not(B)
A= $@B= not defined $meta
ABresult
1X1
011
000
285 4 $dist->{meta} = 1;
286 4 $dist->{meta_name} = $meta->name;
287 4 $dist->{meta_version} = $meta->version;
288 4 $dist->{meta_abstract} = $meta->abstract;
289 4 $dist->{meta_generated} = $meta->generated_by;
290 4 $dist->{meta_generated} =~ s/,.+//;
291 4 $dist->{meta_license} = join ', ', $meta->licenses;
292 4 $dist->{meta_from} = undef;
293  
294 # Fetch the dependency blocks
295 4 my $core = $meta->effective_prereqs;
296 4 foreach my $when ( qw{ configure build test runtime } ) {
297 16 my $requires = $core->requirements_for($when, 'requires');
298 16 my $hash = $requires->as_string_hash;
299 16 push @deps, map { +{
300 release => $the->{dist},
301 phase => $when,
302 module => $_,
303 7 version => $hash->{$_},
304 } } sort keys %$hash;
305 }
306 }
307 $dbh->do(
308 'INSERT INTO meta_distribution VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )', {},
309 $dist->{release},
310 $dist->{meta},
311 $dist->{meta_name},
312 $dist->{meta_version},
313 $dist->{meta_abstract},
314 $dist->{meta_generated},
315 $dist->{meta_from},
316 $dist->{meta_license},
317 4 );
318 $dbh->do(
319 'INSERT INTO meta_dependency VALUES ( ?, ?, ?, ?, ? )', {},
320 $_->{release},
321 $_->{module},
322 $_->{version},
323 $_->{phase},
324 $_->{module} eq 'perl'
325 ? $_->{version}
326 : scalar Module::CoreList->first_release(
327 $_->{module}, $_->{version},
328 ),
329 4 ▶ ) foreach @deps;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'module'} eq 'perl' ? : 1 6 7
330 4 ▶ unless ( $the->{counter} % 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($the->{"counter"} % 100) 0 4 4
331 0 $dbh->commit;
332 0 $dbh->begin_work;
333 }
334 },
335 1 );
336 1 $visitor->run;
337 1 $dbh->commit;
338  
339 # Generate the indexes
340 1 $self->create_indexes( $dbh );
341  
342 # Clean and optimise the database
343 1 $dbh->do('PRAGMA user_version = 10');
344 1 $dbh->do('VACUUM');
345 1 $dbh->do('ANALYZE main');
346  
347 # Publish the database to the current directory
348 1 ▶ if ( defined $self->publish ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->publish) 1 0 1
349 1 ▶ print STDERR "Publishing the generated database...\n" if $self->trace;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 0 1 1
350 1 Xtract::Publish->new(
351 from => $self->sqlite,
352 sqlite => $self->publish,
353 trace => $self->trace,
354 raw => 0,
355 gz => 1,
356 bz2 => 1,
357 lz => 1,
358 )->run;
359 }
360  
361 1 return 1;
362 }
363  
364  
365  
366  
367  
368 ######################################################################
369 # Index Management
370  
371 2 ▶use constant INDEX => (
Subroutine BEGIN: called CC=1 SCAR=0.0
372 [ 'meta_distribution', 'release' ],
373 [ 'meta_dependency', 'release' ],
374 [ 'meta_dependency', 'phase' ],
375 [ 'meta_dependency', 'module' ],
376 2 );
377  
378 1 ▶sub drop_indexes {
Subroutine drop_indexes: called CC=2 SCAR=6.9
Pod undocumented
379 1 my $self = shift;
380 1 my $dbh = shift;
381 1 foreach my $i ( INDEX ) {
382 4 $dbh->do("DROP INDEX IF EXISTS $i->[0]__$i->[1]");
383 }
384 1 return 1;
385 }
386  
387 1 ▶sub create_indexes {
Subroutine create_indexes: called CC=2 SCAR=6.9
Pod undocumented
388 1 my $self = shift;
389 1 my $dbh = shift;
390 1 foreach my $i ( INDEX ) {
391 4 $self->create_index( $dbh, @$i );
392 }
393 1 return 1;
394 }
395  
396 4 ▶sub create_index {
Subroutine create_index: called CC=1 SCAR=0.0
Pod undocumented
397 4 $_[1]->do("CREATE INDEX IF NOT EXISTS $_[2]__$_[3] on $_[2] ( $_[3] )");
398 }
399  
400 2 1;
401  
402 =pod
403  
404 =head1 SUPPORT
405  
406 Bugs should be reported via the CPAN bug tracker at
407  
408 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ORDB-CPANMeta-Generator>
409  
410 For other issues, contact the author.
411  
412 =head1 AUTHOR
413  
414 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
415  
416 =head1 COPYRIGHT
417  
418 Copyright 2009 - 2012 Adam Kennedy.
419  
420 This program is free software; you can redistribute
421 it and/or modify it under the same terms as Perl itself.
422  
423 The full text of the license can be found in the
424 LICENSE file included with this module.
425  
426 =cut
↑ Summary