SQLite/Index.pm

filter: Statementstmt 80.3% 139 / 173 Branchbran 48.7% 38 / 78 Conditioncond 47.0% 8 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 95.2% 20 / 21 Podpod 37.5% 3 / 8 totaltotal 68.0% 209 / 307 CC 47 SCAR 29.6
CC
47
Cov
69%
index
53.1
fetch_cpan_indices
15.1
download_index
14.1
CRAP 19.3
« SQLite/DBI/Search.pm ↑ Summary SQLite.pm »
1 # $Id: Index.pm 84 2020-05-31 06:29:34Z stro $
2  
3 package CPAN::SQLite::Index;
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.219';
8  
9 6 ▶use English qw/-no_match_vars/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use CPAN::SQLite::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use CPAN::SQLite::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use CPAN::SQLite::Populate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use CPAN::SQLite::DBI qw($tables);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 ▶unless ($ENV{CPAN_SQLITE_NO_LOG_FILES}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"CPAN_SQLITE_NO_LOG_FILES"}) 6 0 6
23 6 $ENV{CPAN_SQLITE_DEBUG} = 1;
24 }
25  
26 6 our ($oldout);
27 6 my $log_file = 'cpan_sqlite_log.' . time;
28  
29 # This is usually already defined in real life, but tests need it to be set
30 6 ▶$CPAN::FrontEnd ||= "CPAN::Shell";
Condition100%2/2
$CPAN::FrontEnd ||= "CPAN::Shell"
l!l
33
MC/DC100%1/1
$CPAN::FrontEnd ||= "CPAN::Shell"
$CPAN::FrontEnd
Truth table100%2/2
A
A= $CPAN::FrontEnd
Aresult
11
00
31  
32 sub new {
33 4 ▶ my ($class, %args) = @_;
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
34 4 ▶ if ($args{setup} and $args{reindex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"setup"} and $args{"reindex"}) 0 4 4
Condition66%2/3
$args{"setup"} and $args{"reindex"}
!ll&&!rl&&r
220
MC/DC0%0/2
$args{"setup"} and $args{"reindex"}
$args{"setup"} $args{"reindex"}
Truth table66%2/3
A and B
A= $args{"setup"}B= $args{"reindex"}
ABresult
0X0
100
111
35 0 die "Reindexing must be done on an exisiting database";
36 }
37  
38 4 my $self = { index => undef, state => undef, %args };
39  
40 4 return bless $self, $class;
41 }
42  
43 sub download_index {
44 2 ▶ my $self = shift;
Subroutine download_index: called CC=10 SCAR=26.4
Pod undocumented
45  
46 2 ▶ if ($ENV{'CPAN_SQLITE_DOWNLOAD'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"CPAN_SQLITE_DOWNLOAD"}) 1 1 2
47 1 ▶ $ENV{'CPAN_SQLITE_DOWNLOAD_URL'} = 'http://cpansqlite.trouchelle.com/' unless $ENV{'CPAN_SQLITE_DOWNLOAD_URL'};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"CPAN_SQLITE_DOWNLOAD_URL"} 0 1 1
48 }
49  
50 2 ▶ return 0 unless $ENV{'CPAN_SQLITE_DOWNLOAD_URL'};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"CPAN_SQLITE_DOWNLOAD_URL"} 1 1 2
51  
52 1 $CPAN::FrontEnd->myprint("Downloading the compiled index db ... ");
53  
54 1 ▶ if (my $response =
Branch: 1 branch
BranchTrueFalseTotal
if (my $response = "HTTP::Tiny"->new->mirror($ENV{"CPAN_SQLITE_DOWNLOAD_URL"}, catfile($self->{"db_dir"}, $self->{"db_name"}))) 1 0 1
55 HTTP::Tiny->new->mirror($ENV{'CPAN_SQLITE_DOWNLOAD_URL'} => catfile($self->{'db_dir'}, $self->{'db_name'})))
56 {
57 1 ▶ if ($response->{'success'} and $response->{'status'} and $response->{'status'} eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"} and $response->{"status"} and $response->{"status"} eq 200) 1 0 1
Condition33%2/6
$response->{"success"} and $response->{"status"} and $response->{"status"} eq 200
!ll&&!rl&&r
001
$response->{"success"} and $response->{"status"}
!ll&&!rl&&r
001
MC/DC0%0/3
$response->{"success"} and $response->{"status"} and $response->{"status"} eq 200
$response->{"success"} $response->{"status"} $response->{"status"} eq 200
Truth table25%1/4
A and B and C
A= $response->{"success"}B= $response->{"status"}C= $response->{"status"} eq 200
ABCresult
0XX0
10X0
1100
1111
58 1 ▶ if (my $type = $response->{'headers'}->{'content-type'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $response->{"headers"}{"content-type"}) 1 0 1
59 1 ▶ if ($type eq 'application/x-sqlite3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "application/x-sqlite3") 1 0 1
60 1 return 1;
61 }
62 }
63 }
64 }
65  
66 0 $CPAN::FrontEnd->mywarn('Cannot download the compiled index db');
67 0 return 0;
68 }
69  
70 sub index {
71 1 ▶ my $self = shift;
Subroutine index: called CC=17 SCAR=39.7
Pod documented
72 1 my $setup = $self->{'setup'};
73  
74 1 ▶ if ($setup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setup) 1 0 1
75 1 my $db_name = catfile($self->{'db_dir'}, $self->{db_name});
76 1 ▶ if (-f $db_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $db_name) 0 1 1
77 0 $CPAN::FrontEnd->myprint("Removing existing $db_name ... ");
78 0 ▶ if (unlink $db_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $db_name) { } 0 0 0
79 0 $CPAN::FrontEnd->myprint("Done.\n");
80 } else {
81 0 $CPAN::FrontEnd->mywarn("Failed: $!\n");
82 }
83 }
84 }
85  
86 1 my $log = catfile($self->{'log_dir'}, $log_file);
87  
88 1 ▶ unless ($ENV{'CPAN_SQLITE_NO_LOG_FILES'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"CPAN_SQLITE_NO_LOG_FILES"}) 1 0 1
89 1 $oldout = error_fh($log);
90 }
91  
92 1 my $log_cleanup = $ENV{'CPAN_SQLITE_LOG_FILES_CLEANUP'};
93 1 ▶ $log_cleanup = 30 unless defined $log_cleanup;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $log_cleanup 1 0 1
94 1 ▶ if ($log_cleanup and $log_cleanup =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_cleanup and $log_cleanup =~ /^\d+$/) 1 0 1
Condition33%1/3
$log_cleanup and $log_cleanup =~ /^\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
$log_cleanup and $log_cleanup =~ /^\d+$/
$log_cleanup $log_cleanup =~ /^\d+$/
Truth table33%1/3
A and B
A= $log_cleanupB= $log_cleanup =~ /^\d+$/
ABresult
0X0
100
111
95 1 ▶ if (opendir(my $DIR, $self->{'log_dir'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $DIR, $self->{"log_dir"}) 1 0 1
96 1 my @files = grep { /cpan_sqlite_log\./ } readdir $DIR;
97 1 closedir $DIR;
98  
99 1 @files = grep { -C $_ > $log_cleanup } map { catfile($self->{'log_dir'}, $_) } @files;
100  
101 1 ▶ if (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 0 1 1
102 0 $CPAN::FrontEnd->myprint('Cleaning old log files ... ');
103 0 unlink @files;
104 0 $CPAN::FrontEnd->myprint("Done.\n");
105 }
106 }
107 }
108  
109 1 ▶ if ($self->download_index()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->download_index) 0 1 1
110 0 return 1;
111 }
112  
113 1 ▶ if ($self->{'update_indices'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"update_indices"}) 0 1 1
114 0 $CPAN::FrontEnd->myprint('Fetching index files ... ');
115 0 ▶ if ($self->fetch_cpan_indices()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fetch_cpan_indices) { } 0 0 0
116 0 $CPAN::FrontEnd->myprint("Done.\n");
117 } else {
118 0 $CPAN::FrontEnd->mywarn("Failed\n");
119 0 return;
120 }
121 }
122  
123 1 $CPAN::FrontEnd->myprint('Gathering information from index files ... ');
124 1 ▶ if ($self->fetch_info()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fetch_info) { } 1 0 1
125 1 $CPAN::FrontEnd->myprint("Done.\n");
126 } else {
127 0 $CPAN::FrontEnd->mywarn("Failed\n");
128 0 return;
129 }
130  
131 1 ▶ unless ($setup) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($setup) 0 1 1
132 0 $CPAN::FrontEnd->myprint('Obtaining current state of database ... ');
133 0 ▶ if ($self->state()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state) { } 0 0 0
134 0 $CPAN::FrontEnd->myprint("Done.\n");
135 } else {
136 0 $CPAN::FrontEnd->mywarn("Failed\n");
137 0 return;
138 }
139 }
140  
141 1 $CPAN::FrontEnd->myprint('Populating database tables ... ');
142 1 ▶ if ($self->populate()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->populate) { } 1 0 1
143 1 $CPAN::FrontEnd->myprint("Done.\n");
144 } else {
145 0 $CPAN::FrontEnd->mywarn("Failed\n");
146 0 return;
147 }
148  
149 1 return 1;
150 }
151  
152 sub fetch_cpan_indices {
153 1 ▶ my $self = shift;
Subroutine fetch_cpan_indices: called CC=12 SCAR=27.2
Pod undocumented
154  
155 1 my $CPAN = $self->{CPAN};
156 1 my $indices = {
157 '01mailrc.txt.gz' => 'authors',
158 '02packages.details.txt.gz' => 'modules',
159 };
160  
161 1 foreach my $index (keys %$indices) {
162 2 my $file = catfile($CPAN, $indices->{$index}, $index);
163 2 ▶ next if (-e $file and -M $file < 1);
Branch: 1 branch
BranchTrueFalseTotal
if -e $file and -M $file < 1 0 2 2
Condition33%1/3
-e $file and -M $file < 1
!ll&&!rl&&r
200
MC/DC0%0/2
-e $file and -M $file < 1
-e $file -M $file < 1
Truth table33%1/3
A and B
A= -e $fileB= -M $file < 1
ABresult
0X0
100
111
164 2 my $dir = dirname($file);
165 2 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 2 0 2
166 2 ▶ mkpath($dir, 0, oct(755)) or die "Cannot mkpath $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkpath($dir, 0, 493) or die "Cannot mkpath ${dir}: $!" 0 2 2
167 }
168 2 my @urllist = @{ $self->{urllist} };
169 2 foreach my $cpan (@urllist) {
170 2 my $from = join '/', ($cpan, $indices->{$index}, $index);
171 2 ▶ if (my $response = HTTP::Tiny->new->get($from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $response = "HTTP::Tiny"->new->get($from)) 2 0 2
172 2 ▶ if ($response->{'success'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 2 0 2
173 2 ▶ if (open(my $FILE, '>', $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $FILE, ">", $file) 2 0 2
174 2 binmode $FILE;
175 2 print $FILE $response->{'content'};
176 2 ▶ if (close($FILE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (close $FILE) 2 0 2
177 2 next;
178 }
179 }
180 }
181 }
182 }
183 2 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 2 2
184 0 $CPAN::FrontEnd->mywarn("Cannot retrieve '$file'");
185 0 return;
186 }
187 }
188 1 return 1;
189 }
190  
191 sub fetch_info {
192 2 ▶ my $self = shift;
Subroutine fetch_info: called CC=3 SCAR=11.0
Pod undocumented
193 2 my %wanted = map { $_ => $self->{$_} } qw(CPAN ignore keep_source_where);
194 2 my $info = CPAN::SQLite::Info->new(%wanted);
195 2 ▶ $info->fetch_info() or return;
Branch: 1 branch
BranchTrueFalseTotal
$info->fetch_info or return 0 2 2
196 2 my @tables = qw(dists mods auths info);
197 2 my $index;
198 2 foreach my $table (@tables) {
199 8 my $class = __PACKAGE__ . '::' . $table;
200 8 my $this = { info => $info->{$table} };
201 8 $index->{$table} = bless $this, $class;
202 }
203 2 $self->{index} = $index;
204 2 return 1;
205 }
206  
207 sub state {
208 0 ▶ my $self = shift;
Subroutine state: not called CC=2 SCAR=17.9
Pod documented
209  
210 0 my %wanted = map { $_ => $self->{$_} } qw(db_name index setup reindex db_dir);
211 0 my $state = CPAN::SQLite::State->new(%wanted);
212 0 ▶ $state->state() or return;
Branch: 1 branch
BranchTrueFalseTotal
$state->state or return 0 0 0
213 0 $self->{state} = $state;
214 0 return 1;
215 }
216  
217 sub populate {
218 2 ▶ my $self = shift;
Subroutine populate: called CC=2 SCAR=7.0
Pod documented
219 2 my %wanted = map { $_ => $self->{$_} } qw(db_name index setup state db_dir);
220 2 my $db = CPAN::SQLite::Populate->new(%wanted);
221 2 ▶ $db->populate() or return;
Branch: 1 branch
BranchTrueFalseTotal
$db->populate or return 0 2 2
222 2 return 1;
223 }
224  
225 sub error_fh {
226 1 ▶ my $file = shift;
Subroutine error_fh: called CC=3 SCAR=11.1
Pod undocumented
227 1 ▶ open(my $tmp, '>', $file) or die "Cannot open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $tmp, ">", $file or die "Cannot open ${file}: $!" 0 1 1
228 1 close $tmp;
229  
230 # Should be open(my $oldout, '>&', \*STDOUT); but it fails on 5.6.2
231 1 open(my $oldout, '>&STDOUT');
232 1 ▶ open(STDOUT, '>', $file) or die "Cannot tie STDOUT to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $file or die "Cannot tie STDOUT to ${file}: $!" 0 1 1
233 1 select STDOUT;
234 1 $| = 1;
235 1 return $oldout;
236 }
237  
238 sub DESTROY {
239 4 ▶ unless ($ENV{CPAN_SQLITE_NO_LOG_FILES}) {
Subroutine DESTROY: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"CPAN_SQLITE_NO_LOG_FILES"}) 4 0 4
240 4 close STDOUT;
241 4 ▶ open(STDOUT, '>&', $oldout) if $oldout;
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::SQLite::Index::oldout 1 3 4
242 }
243 4 return;
244 }
245  
246 6 1;
247  
248 =head1 NAME
249  
250 CPAN::SQLite::Index - set up or update database tables.
251  
252 =head1 VERSION
253  
254 version 0.219
255  
256 =head1 SYNOPSIS
257  
258 my $index = CPAN::SQLite::Index->new(setup => 1);
259 $index->index();
260  
261 =head1 DESCRIPTION
262  
263 This is the main module used to set up or update the
264 database tables used to store information from the
265 CPAN and ppm indices. The creation of the object
266  
267 my $index = CPAN::SQLite::Index->new(%args);
268  
269 accepts two possible arguments:
270  
271 =over 3
272  
273 =item * setup =E<gt> 1
274  
275 This (optional) argument specifies that the database is being set up.
276 Any existing tables will be dropped.
277  
278 =item * reindex =E<gt> value
279  
280 This (optional) argument specifies distribution names that
281 one would like to reindex in an existing database. These may
282 be specified as either a scalar, for a single distribution,
283 or as an array reference for a list of distributions.
284  
285 =back
286  
287 =head1 DETAILS
288  
289 Calling
290  
291 $index->index();
292  
293 will start the indexing procedure. Various messages
294 detailing the progress will written to I<STDOUT>,
295 which by default will be captured into a file
296 F<cpan_sqlite_log.dddddddddd>, where the extension
297 is the C<time> that the method was invoked. Error messages
298 are not captured, and will appear in I<STDERR>.
299  
300 The steps of the indexing procedure are as follows.
301  
302 =over 4
303  
304 =item * download existing pre-compiled index (optional)
305  
306 If CPAN_SQLITE_DOWNLOAD or CPAN_SQLITE_DOWNLOAD_URL variables are set, an
307 already existing and up-to-date cpandb.sql file will be downloaded from
308 either specified URL or http://cpansqlite.trouchelle.com/ where it's
309 updated every hour. This greatly increases performance and decreases CPU
310 and memory consumption during the indexing process but if your CPAN
311 mirror is out-of-sync or you're using DarkPAN, it obviously wouldn't
312 work. It also wouldn't work without an internet connection.
313  
314 See L<WWW::CPAN::SQLite> if you want to setup your own service for
315 pre-compiling the database.
316  
317 If neither variable is set, this step is skipped.
318  
319 =item * fetch index data
320  
321 The necessary CPAN index files
322 F<$CPAN/authors/01mailrc.txt.gz> and
323 F<$CPAN/modules/02packages.details.txt.gz> will be fetched
324 from the CPAN mirror specified by the C<$cpan> variable
325 at the beginning of L<CPAN::SQLite::Index>. If you are
326 using this option, it is recommended to use the
327 same CPAN mirror with subsequent updates, to ensure consistency
328 of the database. As well, the information on the locations
329 of the CPAN mirrors used for Template-Toolkit and GeoIP
330 is written.
331  
332 =item * get index information
333  
334 Information from the CPAN indices is extracted through
335 L<CPAN::SQLite::Info>.
336  
337 =item * get state information
338  
339 Unless the C<setup> argument within the C<new>
340 method of L<CPAN::SQLite::Index> is specified,
341 this will get information on the state of the database
342 through L<CPAN::SQLite::State>.
343 A comparison is then made between this information
344 and that gathered from the CPAN indices, and if there's
345 a discrepancy in some items, those items are marked
346 for either insertion, updating, or deletion, as appropriate.
347  
348 =item * populate the database
349  
350 At this stage the gathered information is used to populate
351 the database, through L<CPAN::SQLite::Populate>,
352 either inserting new items, updating
353 existing ones, or deleting obsolete items.
354  
355 =back
356  
357 =head1 SEE ALSO
358  
359 L<CPAN::SQLite::Info>, L<CPAN::SQLite::State>,
360 L<CPAN::SQLite::Populate>,
361 and L<CPAN::SQLite::Util>.
362 Development takes place on the CPAN-SQLite project
363 at L<http://sourceforge.net/projects/cpan-search/>.
364  
365 =head1 AUTHORS
366  
367 Randy Kobes (passed away on September 18, 2010)
368  
369 Serguei Trouchelle E<lt>stro@cpan.orgE<gt>
370  
371 =head1 COPYRIGHT
372  
373 Copyright 2006 by Randy Kobes E<lt>r.kobes@uwinnipeg.caE<gt>.
374  
375 Copyright 2011 by Serguei Trouchelle E<lt>stro@cpan.orgE<gt>.
376  
377 Use and redistribution are under the same terms as Perl itself.
378  
379 =cut
« SQLite/DBI/Search.pm ↑ Summary SQLite.pm »