blib/lib/PAR/Repository/Query.pm

filter: Statementstmt 13.0% 13 / 100 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 50.0% 4 / 8 Podpod 100% 4 / 4 totaltotal 9.0% 21 / 233 CC 48 SCAR 55.9
CC
48
Cov
6%
query_module
306.0
query_script_hash
306.0
query_dist
272.0
CRAP 268.4
↑ Summary
1 package PAR::Repository::Query;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.14';
10  
11 =head1 NAME
12  
13 PAR::Repository::Query - Implements repository queries
14  
15 =head1 SYNOPSIS
16  
17 use PAR::Repository;
18 # or:
19 use PAR::Repository::Client;
20  
21 =head1 DESCRIPTION
22  
23 This module is for internal use by L<PAR::Repository> or
24 L<PAR::Repository::Client> only. Both modules inherit from this.
25 C<PAR::Repository::Query> implements a unified query interface for
26 both the server- and client-side components of PAR repositories.
27  
28 If you decide to inherit from this class (for whatever reason),
29 you should provide at least two methods: C<modules_dbm> which returns
30 a L<DBM::Deep> object representing the modules DBM file.
31 (See L<PAR::Repository::DBM> for details.) And C<scripts_dbm> which is
32 the equivalent for the scripts DBM file.
33  
34 =head2 EXPORT
35  
36 None. But the methods are callable on C<PAR::Repository> and
37 C<PAR::Repository::Client> objects.
38  
39 =head1 METHODS
40  
41 Following is a list of class and instance methods.
42 (Instance methods until otherwise mentioned.)
43  
44 There is no C<PAR::Repository::Query> object.
45  
46 =cut
47  
48 =head2 query_module
49  
50 Polls the repository for modules matching certain criteria.
51 Takes named arguments. Either a C<regex> or a C<name> parameter
52 must be present but not both.
53  
54 Returns a reference to an array containing alternating distribution
55 file names and module versions. This method returns the following
56 structure
57  
58 [ 'Foo-Bar-0.01-any_arch-5.8.7.par', '0.01', ... ]
59  
60 that means the module was found in the distribution
61 F<Foo-Bar-0.01-any_arch-5.8.7.par> and the copy in that file has version
62 0.01.
63  
64 Parameters:
65  
66 =over 2
67  
68 =item B<name>
69  
70 The name of the module to look for. This is used for an exact match.
71 If you want to find C<Foo> in C<Foo::Bar>, use the C<regex> parameter.
72 Only one of C<name> and C<regex> may be specified.
73  
74 =item B<regex>
75  
76 Same as C<name>, but interpreted as a regular expression.
77 Only one of C<name> and C<regex> may be specified.
78  
79 =item B<arch>
80  
81 Can be used to reduce the number of matches to a specific architecture.
82 Always interpreted as a regular expression.
83  
84 =back
85  
86 =cut
87  
88 0 ▶sub query_module {
Subroutine query_module: not called CC=17 SCAR=57.2
Pod documented
89 0 my $self = shift;
90 # $self->verbose(2, "Entering query_module()");
91 0 ▶ croak("query_module() called with uneven number of arguments.")
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
92 if @_ % 2;
93 0 my %args = @_;
94
95 0 my $name = $args{name};
96 0 my $regex = $args{regex};
97  
98 0 ▶ if (defined $name and defined $regex) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $name and defined $regex) { } 0 0 0
elsif (not defined $name || defined $regex) { } 0 0 0
elsif (defined $name) { } 0 0 0
Condition0%0/3
defined $name and defined $regex
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and defined $regex
defined $name defined $regex
Truth table0%0/3
A and B
A= defined $nameB= defined $regex
ABresult
0X0
100
111
99 0 croak("query_module() accepts only one of 'name' and 'regex' parameters.");
100 }
101 ▶ elsif (not defined $name and not defined $regex) {
Condition0%0/3
defined $name || defined $regex
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $name || defined $regex
defined $name defined $regex
Truth table0%0/3
A || B
A= defined $nameB= defined $regex
ABresult
1X1
011
000
102 0 croak("query_module() needs one of 'name' and 'regex' parameters.");
103 }
104 elsif (defined $name) {
105 0 $regex = qr/^\Q$name\E$/;
106 }
107 else { # regex defined
108 0 ▶ $regex = qr/$regex/ if not ref($regex) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $regex eq "Regexp" 0 0 0
109 }
110  
111 0 ▶ my ($modh, $modfile) = $self->modules_dbm
Branch: 1 branch
BranchTrueFalseTotal
() = $self->modules_dbm or die "Could not get modules DBM." 0 0 0
112 or die("Could not get modules DBM.");
113  
114 0 my @modules;
115
116 0 my $arch_regex = $args{arch};
117 0 ▶ $arch_regex = qr/$arch_regex/
Branch: 1 branch
BranchTrueFalseTotal
if defined $arch_regex and not ref $arch_regex eq "Regexp" 0 0 0
Condition0%0/3
defined $arch_regex and not ref $arch_regex eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arch_regex and not ref $arch_regex eq "Regexp"
defined $arch_regex not ref $arch_regex eq "Regexp"
Truth table0%0/3
A and not(B)
A= defined $arch_regexB= not ref $arch_regex eq "Regexp"
ABresult
0X0
100
111
118 if defined $arch_regex and not ref($arch_regex) eq 'Regexp';
119
120 # iterate over all modules in the mod_dbm hash
121 0 ▶ while (my ($mod_name, $dists) = each(%$modh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$modh) 0 0 0
122 # skip non-matching
123 0 ▶ next if $mod_name !~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_name =~ /$regex/ 0 0 0
124
125 0 ▶ if (defined $arch_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arch_regex) { } 0 0 0
126 0 ▶ while (my ($distname, $version) = each(%$dists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dists) 0 0 0
127 0 (undef, undef, my $arch, undef)
128 = PAR::Dist::parse_dist_name($distname);
129 0 ▶ next if $arch !~ $arch_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $arch =~ /$arch_regex/ 0 0 0
130 0 push @modules, [$distname, $version];
131 }
132 }
133 else {
134 0 ▶ while (my ($distname, $version) = each(%$dists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dists) 0 0 0
135 0 push @modules, [$distname, $version];
136 }
137 }
138 }
139
140 0 my %seen;
141 # sort return list alphabetically
142 return [
143 0 map { @$_ }
144 0 sort { $a->[0] cmp $b->[0] }
145 0 grep { not $seen{$_->[0] . '|' . $_->[1]}++ }
146 @modules
147 ];
148 }
149  
150 =head2 query_script
151  
152 Note: Usually, you probably want to use C<query_script_hash()>
153 instead. The usage of both methods is very similar (and described
154 right below), but the data structure returned differes somewhat.
155  
156 Polls the repository for scripts matching certain criteria.
157 Takes named arguments. Either a C<regex> or a C<name> parameter
158 must be present but not both.
159  
160 Returns a reference to an array containing alternating distribution
161 file names and script versions. This method returns the following
162 structure
163  
164 [ 'Foo-Bar-0.01-any_arch-5.8.7.par', '0.01', ... ]
165  
166 that means the script was found in the distribution
167 F<Foo-Bar-0.01-any_arch-5.8.7.par> and the copy in that file has version
168 0.01.
169  
170 Parameters:
171  
172 =over 2
173  
174 =item B<name>
175  
176 The name of the script to look for. This is used for an exact match.
177 If you want to find C<foo> in C<foobar>, use the C<regex> parameter.
178 Only one of C<name> and C<regex> may be specified.
179  
180 =item B<regex>
181  
182 Same as C<name>, but interpreted as a regular expression.
183 Only one of C<name> and C<regex> may be specified.
184  
185 =item B<arch>
186  
187 Can be used to reduce the number of matches to a specific architecture.
188 Always interpreted as a regular expression.
189  
190 =back
191  
192 =cut
193  
194 # FIXME: factor out common code from query_script and query_module!
195 0 ▶sub query_script {
Subroutine query_script: not called CC=1 SCAR=6.9
Pod documented
196 0 my $self = shift;
197 # $self->verbose(2, "Entering query_script()");
198  
199 0 my $scripts = $self->query_script_hash(@_);
200  
201 0 my %seen;
202 # sort return list alphabetically
203 return [
204 0 map { @$_ }
205 0 sort { $a->[0] cmp $b->[0] }
206 0 grep { not $seen{$_->[0] . '|' . $_->[1]}++ }
207 map {
208 0 my $scripthash = $scripts->{$_};
209 0 map { [$_, $scripthash->{$_}] } keys %$scripthash;
210 }
211 keys %$scripts
212 ];
213 }
214  
215  
216 =head2 query_script_hash
217  
218 Works exactly the same as C<query_script> except it returns
219 a different resulting structure which includes the matching
220 script's name:
221  
222 { 'fooscript' => { 'Foo-Bar-0.01-any_arch-5.8.7.par' => '0.01', ... }, ... }
223  
224 that means the script C<fooscript> was found in the distribution
225 F<Foo-Bar-0.01-any_arch-5.8.7.par> and the copy in that file has version
226 0.01.
227  
228 Parameters are the same as for C<query_script>
229  
230 =cut
231  
232 # FIXME: factor out common code from query_script_hash and query_module!
233 0 ▶sub query_script_hash {
Subroutine query_script_hash: not called CC=17 SCAR=57.2
Pod documented
234 0 my $self = shift;
235 # $self->verbose(2, "Entering query_script_hash()");
236 0 ▶ croak("query_script() or query_script_hash() called with uneven number of arguments.")
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
237 if @_ % 2;
238 0 my %args = @_;
239
240 0 my $name = $args{name};
241 0 my $regex = $args{regex};
242  
243 0 ▶ if (defined $name and defined $regex) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $name and defined $regex) { } 0 0 0
elsif (not defined $name || defined $regex) { } 0 0 0
elsif (defined $name) { } 0 0 0
Condition0%0/3
defined $name and defined $regex
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and defined $regex
defined $name defined $regex
Truth table0%0/3
A and B
A= defined $nameB= defined $regex
ABresult
0X0
100
111
244 0 croak("query_script() or query_script_hash() accepts only one of 'name' and 'regex' parameters.");
245 }
246 ▶ elsif (not defined $name and not defined $regex) {
Condition0%0/3
defined $name || defined $regex
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $name || defined $regex
defined $name defined $regex
Truth table0%0/3
A || B
A= defined $nameB= defined $regex
ABresult
1X1
011
000
247 0 croak("query_script() or query_script_hash() needs one of 'name' and 'regex' parameters.");
248 }
249 elsif (defined $name) {
250 0 $regex = qr/^\Q$name\E$/;
251 }
252 else { # regex defined
253 0 ▶ $regex = qr/$regex/ if not ref($regex) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $regex eq "Regexp" 0 0 0
254 }
255  
256 0 ▶ my ($scrh, $scrfile) = $self->scripts_dbm
Branch: 1 branch
BranchTrueFalseTotal
() = $self->scripts_dbm or die "Could not get scripts DBM." 0 0 0
257 or die("Could not get scripts DBM.");
258  
259 0 my %scripts;
260
261 0 my $arch_regex = $args{arch};
262 0 ▶ $arch_regex = qr/$arch_regex/
Branch: 1 branch
BranchTrueFalseTotal
if defined $arch_regex and not ref $arch_regex eq "Regexp" 0 0 0
Condition0%0/3
defined $arch_regex and not ref $arch_regex eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arch_regex and not ref $arch_regex eq "Regexp"
defined $arch_regex not ref $arch_regex eq "Regexp"
Truth table0%0/3
A and not(B)
A= defined $arch_regexB= not ref $arch_regex eq "Regexp"
ABresult
0X0
100
111
263 if defined $arch_regex and not ref($arch_regex) eq 'Regexp';
264
265 # iterate over all scripts in the scripts hash
266 0 ▶ while (my ($scr_name, $dists) = each(%$scrh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$scrh) 0 0 0
267 # skip non-matching
268 0 ▶ next if $scr_name !~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $scr_name =~ /$regex/ 0 0 0
269
270 0 ▶ while (my ($distname, $version) = each(%$dists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dists) 0 0 0
271 0 ▶ if (defined $arch_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arch_regex) 0 0 0
272 0 (undef, undef, my $arch, undef)
273 = PAR::Dist::parse_dist_name($distname);
274 0 ▶ next if $arch !~ $arch_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $arch =~ /$arch_regex/ 0 0 0
275 }
276 0 ▶ $scripts{$scr_name} = {} if not exists $scripts{$scr_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $scripts{$scr_name} 0 0 0
277 0 $scripts{$scr_name}{$distname} = $version; # distname => version
278 }
279 }
280  
281 0 return \%scripts;
282 }
283  
284  
285  
286 =head2 query_dist
287  
288 Polls the repository for distributions matching certain criteria.
289 Takes named arguments. Either a C<regex> or a C<name> parameter
290 must be present but not both.
291  
292 Returns a reference to an array containing alternating distribution
293 file names and hash references. The hashes contain module names
294 and associated versions in the distribution.
295 This method returns the following structure
296  
297 [
298 'Foo-Bar-0.01-any_arch-5.8.7.par',
299 {Foo::Bar => '0.01', Foo::Bar::Baz => '0.02'},
300 ...
301 ]
302  
303 that means the distribution F<Foo-Bar-0.01-any_arch-5.8.7.par> matched and
304 that distribution contains the modules C<Foo::Bar> and C<Foo::Bar::Baz>
305 with versions 0.01 and 0.02 respectively.
306  
307 Parameters:
308  
309 =over 2
310  
311 =item B<name>
312  
313 The name of the distribution to look for. This is used for an exact match.
314 If you want to find C<Foo> in C<Foo-Bar-0.01-any_arch-5.8.8.par>,
315 use the C<regex> parameter.
316 Only one of C<name> and C<regex> may be specified.
317  
318 =item B<regex>
319  
320 Same as C<name>, but interpreted as a regular expression.
321 Only one of C<name> and C<regex> may be specified.
322  
323 =item B<arch>
324  
325 Can be used to reduce the number of matches to a specific architecture.
326 Always interpreted as a regular expression.
327  
328 =back
329  
330 =cut
331  
332 0 ▶sub query_dist {
Subroutine query_dist: not called CC=16 SCAR=56.1
Pod documented
333 0 my $self = shift;
334 # $self->verbose(2, "Entering query_dist()");
335 0 ▶ croak("query_dist() called with uneven number of arguments.")
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
336 if @_ % 2;
337 0 my %args = @_;
338
339 0 my $name = $args{name};
340 0 my $regex = $args{regex};
341  
342 0 ▶ if (defined $name and defined $regex) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $name and defined $regex) { } 0 0 0
elsif (not defined $name || defined $regex) { } 0 0 0
elsif (defined $name) { } 0 0 0
Condition0%0/3
defined $name and defined $regex
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and defined $regex
defined $name defined $regex
Truth table0%0/3
A and B
A= defined $nameB= defined $regex
ABresult
0X0
100
111
343 0 croak("query_dist() accepts only one of 'name' and 'regex' parameters.");
344 }
345 ▶ elsif (not defined $name and not defined $regex) {
Condition0%0/3
defined $name || defined $regex
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $name || defined $regex
defined $name defined $regex
Truth table0%0/3
A || B
A= defined $nameB= defined $regex
ABresult
1X1
011
000
346 0 croak("query_dist() needs one of 'name' and 'regex' parameters.");
347 }
348 elsif (defined $name) {
349 0 $regex = qr/^\Q$name\E$/;
350 }
351 else { # regex defined
352 0 ▶ $regex = qr/$regex/ if not ref($regex) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $regex eq "Regexp" 0 0 0
353 }
354  
355 0 ▶ my ($modh, $modfile) = $self->modules_dbm
Branch: 1 branch
BranchTrueFalseTotal
() = $self->modules_dbm or die "Could not get modules DBM." 0 0 0
356 or die("Could not get modules DBM.");
357  
358 0 my %dists;
359
360 0 my $arch_regex = $args{arch};
361 0 ▶ $arch_regex = qr/$arch_regex/
Branch: 1 branch
BranchTrueFalseTotal
if defined $arch_regex and not ref $arch_regex eq "Regexp" 0 0 0
Condition0%0/3
defined $arch_regex and not ref $arch_regex eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arch_regex and not ref $arch_regex eq "Regexp"
defined $arch_regex not ref $arch_regex eq "Regexp"
Truth table0%0/3
A and not(B)
A= defined $arch_regexB= not ref $arch_regex eq "Regexp"
ABresult
0X0
100
111
362 if defined $arch_regex and not ref($arch_regex) eq 'Regexp';
363
364 # iterate over all modules in the mod_dbm hash
365 0 ▶ while (my ($mod_name, $this_dists) = each(%$modh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$modh) 0 0 0
366 # get the distributions for the module
367 0 my $this_dists = $modh->{$mod_name};
368
369 0 ▶ while (my ($dist_name, $dist) = each(%$this_dists)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$this_dists) 0 0 0
370 # skip non-matching
371 0 ▶ next if $dist_name !~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $dist_name =~ /$regex/ 0 0 0
372
373 # skip non-matching archs
374 0 ▶ if (defined $arch_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arch_regex) 0 0 0
375 0 (undef, undef, my $arch, undef)
376 = PAR::Dist::parse_dist_name($dist_name);
377 0 ▶ next if $arch !~ $arch_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $arch =~ /$arch_regex/ 0 0 0
378 }
379
380 0 $dists{$dist_name}{$mod_name} = $dist;
381 }
382 }
383
384 # sort return list alphabetically
385 return [
386 0 map { @$_ }
387 0 sort { $a->[0] cmp $b->[0] }
388 0 map { [$_, $dists{$_}] }
389 keys %dists
390 ];
391 }
392  
393  
394  
395 1 1;
396 __END__
↑ Summary