File Coverage

File:Dpkg/Deps/AND.pm
Coverage:80.2%

linestmtbrancondsubpodtimecode
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 <hertzog@debian.org>
7# Copyright © 2008-2009, 2012-2014 Guillem Jover <guillem@debian.org>
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 <https://www.gnu.org/licenses/>.
21
22package Dpkg::Deps::AND;
23
24=encoding utf8
25
26 - 35
=head1 NAME

Dpkg::Deps::AND - list of AND dependencies

=head1 DESCRIPTION

This class represents a list of dependencies that must be met at the same
time. It inherits from Dpkg::Deps::Multiple.

=cut
36
37
1
1
1
2
1
15
use strict;
38
1
1
1
2
1
24
use warnings;
39
40our $VERSION = '1.00';
41
42
1
1
1
1
1
1
use parent qw(Dpkg::Deps::Multiple);
43
44 - 52
=head1 METHODS

=over 4

=item $dep->output([$fh])

The output method uses ", " to join the list of sub-dependencies.

=cut
53
54sub output {
55
45
1
29
    my ($self, $fh) = @_;
56
57    my $res = join(', ', map {
58
118
81
        $_->output()
59    } grep {
60
45
118
44
75
        not $_->is_empty()
61    } $self->get_deps());
62
63
45
41
    if (defined $fh) {
64
0
0
0
0
        print { $fh } $res;
65    }
66
45
60
    return $res;
67}
68
69 - 75
=item $dep->implies($other_dep)

Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
NOT($other_dep). Returns undef when there's no implication. $dep and
$other_dep do not need to be of the same type.

=cut
76
77sub implies {
78
23
1
16
    my ($self, $o) = @_;
79
80    # If any individual member can imply $o or NOT $o, we're fine
81
23
17
    foreach my $dep ($self->get_deps()) {
82
34
28
        my $implication = $dep->implies($o);
83
34
37
        return 1 if defined $implication and $implication == 1;
84
28
20
        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
21
    if ($o->isa('Dpkg::Deps::AND')) {
90
9
6
        my $subset = 1;
91
9
6
        foreach my $odep ($o->get_deps()) {
92
20
10
            my $found = 0;
93
20
12
            foreach my $dep ($self->get_deps()) {
94
67
39
                $found = 1 if $dep->implies($odep);
95            }
96
20
16
            $subset = 0 if not $found;
97        }
98
9
6
        return 1 if $subset;
99    }
100
15
17
    return;
101}
102
103 - 112
=item $dep->get_evaluation($facts)

Evaluates the dependency given a list of installed packages and a list of
virtual packages provided. These lists are part of the Dpkg::Deps::KnownFacts
object given as parameters.

Returns 1 when it's true, 0 when it's false, undef when some information
is lacking to conclude.

=cut
113
114sub get_evaluation {
115
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
        if (not defined $eval) {
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 - 141
=item $dep->simplify_deps($facts, @assumed_deps)

Simplifies the dependency as much as possible given the list of facts (see
object Dpkg::Deps::KnownFacts) and a list of other dependencies that are
known to be true.

=cut
142
143sub simplify_deps {
144
23
1
20
    my ($self, $facts, @knowndeps) = @_;
145
23
11
    my @new;
146
147WHILELOOP:
148
23
65
13
60
    while (@{$self->{list}}) {
149
42
42
19
26
        my $dep = shift @{$self->{list}};
150
42
37
        my $eval = $dep->get_evaluation($facts);
151
42
64
        next if defined $eval and $eval == 1;
152
31
21
        foreach my $odep (@knowndeps, @new) {
153
18
16
            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
15
        my $i = 0;
159
30
30
14
23
        foreach my $odep (@{$self->{list}}) {
160
27
28
            if (defined $odep and $odep->implies($dep)) {
161
4
4
3
4
                splice @{$self->{list}}, $i, 1;
162
4
4
1
3
                unshift @{$self->{list}}, $odep;
163
4
7
                next WHILELOOP;
164            }
165
23
12
            $i++;
166        }
167
26
13
        push @new, $dep;
168    }
169
23
29
    $self->{list} = [ @new ];
170}
171
172=back
173
174 - 180
=head1 CHANGES

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut
181
1821;