Dpkg/Index.pm

filter: Statementstmt 44.6% 63 / 141 Branchbran 30.7% 16 / 52 Conditioncond 7.6% 2 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 41.6% 15 / 36 Podpod 100% 14 / 14 totaltotal 38.4% 110 / 286 CC 41 SCAR 41.5
CC
41
Cov
37%
set_options
211.9
get_keys
22.6
merge
20.0
CRAP 63.2
« Dpkg/BuildDriver/DebianRules.pm ↑ Summary Dpkg/IPC.pm »
1 # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2012-2017 Guillem Jover <guillem@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Index - generic index of control information
22  
23 =head1 DESCRIPTION
24  
25 This class represent a set of L<Dpkg::Control> objects.
26  
27 =cut
28  
29 package Dpkg::Index 3.00;
30  
31 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 4 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use Dpkg::Control;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 4 ▶use parent qw(Dpkg::Interface::Storable);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 use overload
40 0 ▶ '@{}' => sub { return $_[0]->{order} },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 4 fallback => 1;
42  
43 =head1 METHODS
44  
45 =over 4
46  
47 =item $index = Dpkg::Index->new(%opts)
48  
49 Creates a new empty index. See set_options() for more details.
50  
51 =cut
52  
53 39 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
54 39 my ($this, %opts) = @_;
55 39 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0390
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
56  
57 my $self = {
58 items => {},
59 order => [],
60 unique_tuple_key => 1,
61 0 ▶ get_key_func => sub { return $_[0]->{Package} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 39 type => CTRL_UNKNOWN,
63 item_opts => {},
64 };
65 39 bless $self, $class;
66 39 $self->set_options(%opts);
67 39 ▶ if (exists $opts{load}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"load"}) 0 39 39
68 0 $self->load($opts{load});
69 }
70  
71 39 return $self;
72 }
73  
74 =item $index->set_options(%opts)
75  
76 Change the value of some options.
77  
78 Options:
79  
80 =over
81  
82 =item B<type>
83  
84 Set the deb822 control type, used to setup default values for other options.
85  
86 =item B<item_opts>
87  
88 Set a hash reference that will be passed to the item constructor in
89 the $index->new_item() method.
90 The index can only contain one item with a given key.
91  
92 =item B<unique_tuple_key>
93  
94 Set a boolean requesting whether the default key should be the unique tuple
95 (default to true).
96  
97 =item B<get_key_func>
98  
99 Set a function returning a key for the item passed in parameters.
100  
101 The default depends on the type:
102  
103 =over
104  
105 =item *
106  
107 for CTRL_TMPL_SRC, it is the Source field;
108  
109 =item *
110  
111 for CTRL_REPO_SRC and CTRL_DSC it is the Package and Version fields
112 (concatenated with "_") when B<unique_tuple_key> is true (the default), or
113 otherwise the Package field;
114  
115 =item *
116  
117 for CTRL_TMPL_PKG it is the Package field;
118  
119 =item *
120  
121 for CTRL_REPO_PKG and CTRL_DEB it is the Package, Version and
122 Architecture fields (concatenated with "_") when B<unique_tuple_key> is
123 true (the default) or otherwise the Package field;
124  
125 =item *
126  
127 for CTRL_CHANGELOG it is the Source and the Version fields (concatenated
128 with an intermediary "_");
129  
130 =item *
131  
132 for CTRL_TESTS is an integer index (0-based) corresponding to the Tests or
133 Test-Command field stanza;
134  
135 =item *
136  
137 for CTRL_FILE_CHANGES it is the Source, Version and Architecture fields
138 (concatenated with "_");
139  
140 =item *
141  
142 for CTRL_FILE_VENDOR it is the Vendor field;
143  
144 =item *
145  
146 for CTRL_FILE_STATUS it is the Package and Architecture fields (concatenated
147 with "_");
148  
149 =item *
150  
151 otherwise it is the Package field by default.
152  
153 =back
154  
155 =back
156  
157 =cut
158  
159 39 ▶sub set_options {
Subroutine set_options: called CC=21 SCAR=53.6
Pod documented
160 39 my ($self, %opts) = @_;
161  
162 # Default values based on type.
163 39 ▶ if (exists $opts{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"type"}) 39 0 39
164 39 my $t = $opts{type};
165 39 ▶ if ($t == CTRL_TMPL_PKG) {
Branch: 12 branches
BranchTrueFalseTotal
if ($t == 2) { } 0 39 39
elsif ($t == 1) { } 0 39 39
elsif ($t == 512) { } 36 3 39
elsif ($t == 2048) { } 0 3 3
elsif ($t == 4096) { } 0 3 3
elsif ($t == 8192) { } 0 3 3
elsif ($t == 16384) { } 3 0 3
elsif ($t == 4 or $t == 16) { } 0 0 0
elsif ($t == 8 or $t == 32) { } 0 0 0
elsif ($t == 64) { } 0 0 0
elsif ($t == 128) { } 0 0 0
elsif ($t == 256) { } 0 0 0
166 0 ▶ $self->{get_key_func} = sub { return $_[0]->{Package}; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 } elsif ($t == CTRL_TMPL_SRC) {
168 0 ▶ $self->{get_key_func} = sub { return $_[0]->{Source}; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 } elsif ($t == CTRL_CHANGELOG) {
170 498 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
171 498 return $_[0]->{Source} . '_' . $_[0]->{Version};
172 36 };
173 } elsif ($t == CTRL_COPYRIGHT_HEADER) {
174 # This is a bit pointless, because the value will almost always
175 # be the same, but guarantees that we use a known field.
176 0 ▶ $self->{get_key_func} = sub { return $_[0]->{Format}; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 } elsif ($t == CTRL_COPYRIGHT_FILES) {
178 0 ▶ $self->{get_key_func} = sub { return $_[0]->{Files}; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 } elsif ($t == CTRL_COPYRIGHT_LICENSE) {
180 0 ▶ $self->{get_key_func} = sub { return $_[0]->{License}; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 } elsif ($t == CTRL_TESTS) {
182 9 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 9 return scalar @{$self->{order}};
184 3 };
185 ▶ } elsif ($t == CTRL_REPO_SRC or $t == CTRL_DSC) {
Condition0%0/3
$t == 4 or $t == 16
l!l&&r!l&&!r
000
MC/DC0%0/2
$t == 4 or $t == 16
$t == 4 $t == 16
Truth table0%0/3
A or B
A= $t == 4B= $t == 16
ABresult
1X1
011
000
186 0 ▶ if ($opts{unique_tuple_key} // $self->{unique_tuple_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'unique_tuple_key'} // $self->{'unique_tuple_key'}) { } 0 0 0
Condition0%0/3
$opts{"unique_tuple_key"} // $self->{"unique_tuple_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"unique_tuple_key"} // $self->{"unique_tuple_key"}
$opts{"unique_tuple_key"} $self->{"unique_tuple_key"}
Truth table0%0/3
A // B
A= $opts{"unique_tuple_key"}B= $self->{"unique_tuple_key"}
ABresult
1X1
011
000
187 0 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 return $_[0]->{Package} . '_' . $_[0]->{Version};
189 0 };
190 } else {
191 0 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
192 0 return $_[0]->{Package};
193 0 };
194 }
195 ▶ } elsif ($t == CTRL_REPO_PKG or $t == CTRL_DEB) {
Condition0%0/3
$t == 8 or $t == 32
l!l&&r!l&&!r
000
MC/DC0%0/2
$t == 8 or $t == 32
$t == 8 $t == 32
Truth table0%0/3
A or B
A= $t == 8B= $t == 32
ABresult
1X1
011
000
196 0 ▶ if ($opts{unique_tuple_key} // $self->{unique_tuple_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'unique_tuple_key'} // $self->{'unique_tuple_key'}) { } 0 0 0
Condition0%0/3
$opts{"unique_tuple_key"} // $self->{"unique_tuple_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"unique_tuple_key"} // $self->{"unique_tuple_key"}
$opts{"unique_tuple_key"} $self->{"unique_tuple_key"}
Truth table0%0/3
A // B
A= $opts{"unique_tuple_key"}B= $self->{"unique_tuple_key"}
ABresult
1X1
011
000
197 0 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 return $_[0]->{Package} . '_' . $_[0]->{Version} . '_' .
199 0 $_[0]->{Architecture};
200 0 };
201 } else {
202 0 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 return $_[0]->{Package};
204 0 };
205 }
206 } elsif ($t == CTRL_FILE_CHANGES) {
207 0 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 return $_[0]->{Source} . '_' . $_[0]->{Version} . '_' .
209 0 $_[0]->{Architecture};
210 0 };
211 } elsif ($t == CTRL_FILE_VENDOR) {
212 0 ▶ $self->{get_key_func} = sub { return $_[0]->{Vendor}; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 } elsif ($t == CTRL_FILE_STATUS) {
214 0 ▶ $self->{get_key_func} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 return $_[0]->{Package} . '_' . $_[0]->{Architecture};
216 0 };
217 }
218 }
219  
220 # Options set by the user override default values.
221 39 $self->{$_} = $opts{$_} foreach keys %opts;
222 }
223  
224 =item $type = $index->get_type()
225  
226 Returns the type of control information stored. See the type parameter
227 set during new().
228  
229 =cut
230  
231 0 ▶sub get_type {
Subroutine get_type: not called CC=1 SCAR=6.9
Pod documented
232 0 my $self = shift;
233 0 return $self->{type};
234 }
235  
236 =item $index->add($item, [$key])
237  
238 Add a new item in the index. If the $key parameter is omitted, the key
239 will be generated with the get_key_func function (see set_options() for
240 details).
241  
242 =cut
243  
244 507 ▶sub add {
Subroutine add: called CC=3 SCAR=11.6
Pod documented
245 507 my ($self, $item, $key) = @_;
246  
247 507 ▶ $key //= $self->{get_key_func}($item);
Condition33%1/3
$key //= $self->{"get_key_func"}->($item)
l!l&&r!l&&!r
05070
MC/DC0%0/2
$key //= $self->{"get_key_func"}->($item)
$key $self->{"get_key_func"}->($item)
Truth table33%1/3
A //= B
A= $keyB= $self->{"get_key_func"}->($item)
ABresult
1X1
011
000
248 507 ▶ if (not exists $self->{items}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"items"}{$key}) 507 0 507
249 507 push @{$self->{order}}, $key;
250 }
251 507 $self->{items}{$key} = $item;
252 }
253  
254 =item $count = $index->parse($fh, $desc)
255  
256 Reads the filehandle and creates all items parsed. When called multiple
257 times, the parsed stanzas are accumulated.
258  
259 Returns the number of items parsed.
260  
261 =cut
262  
263 3 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
264 3 my ($self, $fh, $desc) = @_;
265 3 my $item = $self->new_item();
266 3 my $i = 0;
267 3 ▶ while ($item->parse($fh, $desc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->parse($fh, $desc)) 9 1 10
268 9 $self->add($item);
269 9 $item = $self->new_item();
270 9 $i++;
271 }
272 1 return $i;
273 }
274  
275 =item $count = $index->load($file)
276  
277 Reads the file and creates all items parsed. Returns the number of items
278 parsed. Handles compressed files transparently based on their extensions.
279  
280 =item $item = $index->new_item()
281  
282 Creates a new item. Mainly useful for derived objects that would want
283 to override this method to return something else than a L<Dpkg::Control>
284 object.
285  
286 =cut
287  
288 0 ▶sub new_item {
Subroutine new_item: not called CC=1 SCAR=6.9
Pod documented
289 0 my $self = shift;
290  
291 return Dpkg::Control->new(
292 0 %{$self->{item_opts}},
293 type => $self->{type},
294 0 );
295 }
296  
297 =item $item = $index->get_by_key($key)
298  
299 Returns the item identified by $key or undef.
300  
301 =cut
302  
303 15 ▶sub get_by_key {
Subroutine get_by_key: called CC=2 SCAR=8.1
Pod documented
304 15 my ($self, $key) = @_;
305 15 ▶ return $self->{items}{$key} if exists $self->{items}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"items"}{$key} 15 0 15
306 0 return;
307 }
308  
309 =item @keys = $index->get_keys(%criteria)
310  
311 Returns the keys of items that matches all the criteria. The key of the
312 %criteria hash is a field name and the value is either a regex that needs
313 to match the field value, or a reference to a function that must return
314 true and that receives the field value as single parameter, or a scalar
315 that must be equal to the field value.
316  
317 =cut
318  
319 7 ▶sub get_keys {
Subroutine get_keys: called CC=6 SCAR=31.2
Pod documented
320 7 my ($self, %crit) = @_;
321 7 my @selected = @{$self->{order}};
322  
323 # Search criteria.
324 7 foreach my $s_crit (keys %crit) {
325 0 ▶ if (ref($crit{$s_crit}) eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $crit{$s_crit} eq 'Regexp') { } 0 0 0
elsif (ref $crit{$s_crit} eq 'CODE') { } 0 0 0
326 @selected = grep {
327 0 exists $self->{items}{$_}{$s_crit} and
328 0 ▶ $self->{items}{$_}{$s_crit} =~ $crit{$s_crit}
Condition0%0/3
exists $self->{'items'}{$_}{$s_crit} && $self->{'items'}{$_}{$s_crit} =~ /$crit{$s_crit}/u
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{'items'}{$_}{$s_crit} && $self->{'items'}{$_}{$s_crit} =~ /$crit{$s_crit}/u
exists $self->{'items'}{$_}{$s_crit} $self->{'items'}{$_}{$s_crit} =~ /$crit{$s_crit}/u
Truth table0%0/3
A && B
A= exists $self->{'items'}{$_}{$s_crit}B= $self->{'items'}{$_}{$s_crit} =~ /$crit{$s_crit}/u
ABresult
0X0
100
111
329 } @selected;
330 } elsif (ref($crit{$s_crit}) eq 'CODE') {
331 @selected = grep {
332 0 $crit{$s_crit}->($self->{items}{$_}{$s_crit});
333 } @selected;
334 } else {
335 @selected = grep {
336 0 exists $self->{items}{$_}{$s_crit} and
337 0 ▶ $self->{items}{$_}{$s_crit} eq $crit{$s_crit}
Condition0%0/3
exists $self->{'items'}{$_}{$s_crit} && $self->{'items'}{$_}{$s_crit} eq $crit{$s_crit}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{'items'}{$_}{$s_crit} && $self->{'items'}{$_}{$s_crit} eq $crit{$s_crit}
exists $self->{'items'}{$_}{$s_crit} $self->{'items'}{$_}{$s_crit} eq $crit{$s_crit}
Truth table0%0/3
A && B
A= exists $self->{'items'}{$_}{$s_crit}B= $self->{'items'}{$_}{$s_crit} eq $crit{$s_crit}
ABresult
0X0
100
111
338 } @selected;
339 }
340 }
341 7 return @selected;
342 }
343  
344 =item @items = $index->get(%criteria)
345  
346 Returns all the items that matches all the criteria.
347  
348 =cut
349  
350 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
351 0 my ($self, %crit) = @_;
352 0 return map { $self->{items}{$_} } $self->get_keys(%crit);
353 }
354  
355 =item $item = $index->remove_by_key($key)
356  
357 Remove the item identified by the given key.
358  
359 Returns the removed item.
360  
361 =cut
362  
363 0 ▶sub remove_by_key {
Subroutine remove_by_key: not called CC=1 SCAR=6.9
Pod documented
364 0 my ($self, $key) = @_;
365 0 @{$self->{order}} = grep { $_ ne $key } @{$self->{order}};
366 0 return delete $self->{items}{$key};
367 }
368  
369 =item @items = $index->remove(%criteria)
370  
371 Returns and removes all the items that matches all the criteria.
372  
373 =cut
374  
375 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
376 0 my ($self, %crit) = @_;
377 0 my @keys = $self->get_keys(%crit);
378 0 my (%keys, @ret);
379 0 foreach my $key (@keys) {
380 0 $keys{$key} = 1;
381 0 ▶ push @ret, $self->{items}{$key} if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
382 0 delete $self->{items}{$key};
383 }
384 0 @{$self->{order}} = grep { not exists $keys{$_} } @{$self->{order}};
385 0 return @ret;
386 }
387  
388 =item $index->merge($other_index, %opts)
389  
390 Merge the entries of the other index. While merging, the keys of the merged
391 index are used, they are not re-computed. It's your responsibility to ensure
392 that they have been computed with the same function.
393  
394 Options:
395  
396 =over
397  
398 =item B<keep_keys>
399  
400 Set whether to re-compute the keys from the other index.
401 Defaults to 1.
402  
403 =back
404  
405 =cut
406  
407 0 ▶sub merge {
Subroutine merge: not called CC=4 SCAR=30.0
Pod documented
408 0 my ($self, $other, %opts) = @_;
409 0 ▶ $opts{keep_keys} //= 1;
Condition0%0/2
$opts{"keep_keys"} //= 1
l!l
00
MC/DC0%0/1
$opts{"keep_keys"} //= 1
$opts{"keep_keys"}
Truth table0%0/2
A
A= $opts{"keep_keys"}
Aresult
11
00
410 0 foreach my $key ($other->get_keys()) {
411 0 ▶ $self->add($other->get_by_key($key), $opts{keep_keys} ? $key : undef);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'keep_keys'} ? : 0 0 0
412 }
413 }
414  
415 =item $index->sort(\&sortfunc)
416  
417 Sort the index with the given sort function. If no function is given, an
418 alphabetic sort is done based on the keys. The sort function receives the
419 items themselves as parameters and not the keys.
420  
421 =cut
422  
423 0 ▶sub sort {
Subroutine sort: not called CC=2 SCAR=17.9
Pod documented
424 0 my ($self, $func) = @_;
425 0 ▶ if (defined $func) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func) { } 0 0 0
426 0 @{$self->{order}} = sort {
427 0 $func->($self->{items}{$a}, $self->{items}{$b})
428 0 } @{$self->{order}};
429 } else {
430 0 @{$self->{order}} = sort @{$self->{order}};
431 }
432 }
433  
434 =item $string = "$index"
435  
436 =item $string = $index->output([$fh])
437  
438 Get a string representation of the index. The L<Dpkg::Control> objects are
439 output in the order which they have been read or added except if the order
440 have been changed with sort().
441  
442 Print the string representation of the index to a filehandle if $fh has
443 been passed.
444  
445 =cut
446  
447 7 ▶sub output {
Subroutine output: called CC=4 SCAR=15.0
Pod documented
448 7 my ($self, $fh) = @_;
449 7 my $str = '';
450 7 foreach my $key ($self->get_keys()) {
451 15 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 0 15 15
452 0 print { $fh } $self->get_by_key($key) . "\n";
453 }
454 15 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 15 0 15
455 15 $str .= $self->get_by_key($key) . "\n";
456 }
457 }
458 7 return $str;
459 }
460  
461 =item $index->save($file)
462  
463 Writes the content of the index in a file. Auto-compresses files
464 based on their extensions.
465  
466 =back
467  
468 =head1 CHANGES
469  
470 =head2 Version 3.00 (dpkg 1.21.2)
471  
472 Change behavior: The CTRL_TESTS key now defaults to a stanza index.
473  
474 =head2 Version 2.01 (dpkg 1.20.6)
475  
476 New option: Add new "item_opts" option.
477  
478 =head2 Version 2.00 (dpkg 1.20.0)
479  
480 Change behavior: The "unique_tuple_key" option now defaults to true.
481  
482 =head2 Version 1.01 (dpkg 1.19.0)
483  
484 New option: Add new "unique_tuple_key" option to $index->set_options() to set
485 better default "get_key_func" options, which will become the default behavior
486 in 1.20.x.
487  
488 =head2 Version 1.00 (dpkg 1.15.6)
489  
490 Mark the module as public.
491  
492 =cut
493  
494 4 1;
« Dpkg/BuildDriver/DebianRules.pm ↑ Summary Dpkg/IPC.pm »