File Coverage

blib/lib/Dpkg/Deps/AND.pm
Criterion Covered Total %
statement 51 62 82.2
branch 19 26 73.0
condition 9 12 75.0
subroutine 6 7 85.7
pod 4 4 100.0
total 89 111 80.1


line stmt bran cond sub pod time code
1             # Copyright © 1998 Richard Braakman
2             # Copyright © 1999 Darren Benham
3             # Copyright © 2000 Sean 'Shaleh' Perry
4             # Copyright © 2004 Frank Lichtenheld
5             # Copyright © 2006 Russ Allbery
6             # Copyright © 2007-2009 Raphaël Hertzog
7             # Copyright © 2008-2009, 2012-2014 Guillem Jover
8             #
9             # This program is free software; you may redistribute it and/or modify
10             # it under the terms of the GNU General Public License as published by
11             # the Free Software Foundation; either version 2 of the License, or
12             # (at your option) any later version.
13             #
14             # This is distributed in the hope that it will be useful,
15             # but WITHOUT ANY WARRANTY; without even the implied warranty of
16             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17             # GNU General Public License for more details.
18             #
19             # You should have received a copy of the GNU General Public License
20             # along with this program. If not, see .
21              
22             package Dpkg::Deps::AND;
23              
24             =encoding utf8
25              
26             =head1 NAME
27              
28             Dpkg::Deps::AND - list of AND dependencies
29              
30             =head1 DESCRIPTION
31              
32             This class represents a list of dependencies that must be met at the same
33             time. It inherits from Dpkg::Deps::Multiple.
34              
35             =cut
36              
37 1     1   8 use strict;
  1         2  
  1         31  
38 1     1   6 use warnings;
  1         1  
  1         41  
39              
40             our $VERSION = '1.00';
41              
42 1     1   6 use parent qw(Dpkg::Deps::Multiple);
  1         1  
  1         5  
43              
44             =head1 METHODS
45              
46             =over 4
47              
48             =item $dep->output([$fh])
49              
50             The output method uses ", " to join the list of sub-dependencies.
51              
52             =cut
53              
54             sub output {
55 45     45 1 195 my ($self, $fh) = @_;
56              
57             my $res = join(', ', map {
58 118         233 $_->output()
59             } grep {
60 45         117 not $_->is_empty()
  118         227  
61             } $self->get_deps());
62              
63 45 50       113 if (defined $fh) {
64 0         0 print { $fh } $res;
  0         0  
65             }
66 45         250 return $res;
67             }
68              
69             =item $dep->implies($other_dep)
70              
71             Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
72             NOT($other_dep). Returns undef when there's no implication. $dep and
73             $other_dep do not need to be of the same type.
74              
75             =cut
76              
77             sub implies {
78 23     23 1 63 my ($self, $o) = @_;
79              
80             # If any individual member can imply $o or NOT $o, we're fine
81 23         61 foreach my $dep ($self->get_deps()) {
82 34         78 my $implication = $dep->implies($o);
83 34 100 100     116 return 1 if defined $implication and $implication == 1;
84 28 100 66     69 return 0 if defined $implication and $implication == 0;
85             }
86              
87             # If o is an AND, we might have an implication, if we find an
88             # implication within us for each predicate in o
89 16 100       53 if ($o->isa('Dpkg::Deps::AND')) {
90 9         39 my $subset = 1;
91 9         23 foreach my $odep ($o->get_deps()) {
92 20         27 my $found = 0;
93 20         37 foreach my $dep ($self->get_deps()) {
94 67 100       120 $found = 1 if $dep->implies($odep);
95             }
96 20 100       43 $subset = 0 if not $found;
97             }
98 9 100       21 return 1 if $subset;
99             }
100 15         60 return;
101             }
102              
103             =item $dep->get_evaluation($facts)
104              
105             Evaluates the dependency given a list of installed packages and a list of
106             virtual packages provided. These lists are part of the Dpkg::Deps::KnownFacts
107             object given as parameters.
108              
109             Returns 1 when it's true, 0 when it's false, undef when some information
110             is lacking to conclude.
111              
112             =cut
113              
114             sub get_evaluation {
115 0     0 1 0 my ($self, $facts) = @_;
116              
117             # Return 1 only if all members evaluates to true
118             # Return 0 if at least one member evaluates to false
119             # Return undef otherwise
120 0         0 my $result = 1;
121 0         0 foreach my $dep ($self->get_deps()) {
122 0         0 my $eval = $dep->get_evaluation($facts);
123 0 0       0 if (not defined $eval) {
    0          
    0          
124 0         0 $result = undef;
125             } elsif ($eval == 0) {
126 0         0 $result = 0;
127 0         0 last;
128             } elsif ($eval == 1) {
129             # Still possible
130             }
131             }
132 0         0 return $result;
133             }
134              
135             =item $dep->simplify_deps($facts, @assumed_deps)
136              
137             Simplifies the dependency as much as possible given the list of facts (see
138             object Dpkg::Deps::KnownFacts) and a list of other dependencies that are
139             known to be true.
140              
141             =cut
142              
143             sub simplify_deps {
144 23     23 1 110 my ($self, $facts, @knowndeps) = @_;
145 23         36 my @new;
146              
147             WHILELOOP:
148 23         33 while (@{$self->{list}}) {
  65         144  
149 42         59 my $dep = shift @{$self->{list}};
  42         65  
150 42         102 my $eval = $dep->get_evaluation($facts);
151 42 100 66     156 next if defined $eval and $eval == 1;
152 31         58 foreach my $odep (@knowndeps, @new) {
153 18 100       42 next WHILELOOP if $odep->implies($dep);
154             }
155             # When a dependency is implied by another dependency that
156             # follows, then invert them
157             # "a | b, c, a" becomes "a, c" and not "c, a"
158 30         43 my $i = 0;
159 30         38 foreach my $odep (@{$self->{list}}) {
  30         57  
160 27 100 66     75 if (defined $odep and $odep->implies($dep)) {
161 4         6 splice @{$self->{list}}, $i, 1;
  4         11  
162 4         8 unshift @{$self->{list}}, $odep;
  4         9  
163 4         20 next WHILELOOP;
164             }
165 23         41 $i++;
166             }
167 26         45 push @new, $dep;
168             }
169 23         72 $self->{list} = [ @new ];
170             }
171              
172             =back
173              
174             =head1 CHANGES
175              
176             =head2 Version 1.00 (dpkg 1.15.6)
177              
178             Mark the module as public.
179              
180             =cut
181              
182             1;