File: | Dpkg/Deps/OR.pm |
Coverage: | 67.7% |
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 <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 | |||||||
22 | package Dpkg::Deps::OR; | ||||||
23 | |||||||
24 | =encoding utf8 | ||||||
25 | |||||||
26 - 35 | =head1 NAME Dpkg::Deps::OR - list of OR dependencies =head1 DESCRIPTION This class represents a list of dependencies of which only one must be met for the dependency to be true. It inherits from Dpkg::Deps::Multiple. =cut | ||||||
36 | |||||||
37 | 1 1 1 | 2 0 14 | use strict; | ||||
38 | 1 1 1 | 2 0 24 | use warnings; | ||||
39 | |||||||
40 | our $VERSION = '1.00'; | ||||||
41 | |||||||
42 | 1 1 1 | 15 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 | |||||||
54 | sub output { | ||||||
55 | 14 | 1 | 12 | my ($self, $fh) = @_; | |||
56 | |||||||
57 | my $res = join(' | ', map { | ||||||
58 | 31 | 22 | $_->output() | ||||
59 | } grep { | ||||||
60 | 14 31 | 11 28 | not $_->is_empty() | ||||
61 | } $self->get_deps()); | ||||||
62 | |||||||
63 | 14 | 13 | if (defined $fh) { | ||||
64 | 0 0 | 0 0 | print { $fh } $res; | ||||
65 | } | ||||||
66 | 14 | 13 | 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 | |||||||
77 | sub implies { | ||||||
78 | 4 | 1 | 3 | my ($self, $o) = @_; | |||
79 | |||||||
80 | # Special case for AND with a single member, replace it by its member | ||||||
81 | 4 | 7 | if ($o->isa('Dpkg::Deps::AND')) { | ||||
82 | 2 | 2 | my @subdeps = $o->get_deps(); | ||||
83 | 2 | 2 | if (scalar(@subdeps) == 1) { | ||||
84 | 2 | 1 | $o = $subdeps[0]; | ||||
85 | } | ||||||
86 | } | ||||||
87 | |||||||
88 | # In general, an OR dependency can't imply anything except if each | ||||||
89 | # of its member implies a member in the other OR dependency | ||||||
90 | 4 | 4 | if ($o->isa('Dpkg::Deps::OR')) { | ||||
91 | 3 | 3 | my $subset = 1; | ||||
92 | 3 | 2 | foreach my $dep ($self->get_deps()) { | ||||
93 | 19 | 8 | my $found = 0; | ||||
94 | 19 | 17 | foreach my $odep ($o->get_deps()) { | ||||
95 | 72 | 44 | $found = 1 if $dep->implies($odep); | ||||
96 | } | ||||||
97 | 19 | 14 | $subset = 0 if not $found; | ||||
98 | } | ||||||
99 | 3 | 4 | return 1 if $subset; | ||||
100 | } | ||||||
101 | 3 | 3 | return; | ||||
102 | } | ||||||
103 | |||||||
104 - 113 | =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 | ||||||
114 | |||||||
115 | sub get_evaluation { | ||||||
116 | 12 | 1 | 9 | my ($self, $facts) = @_; | |||
117 | |||||||
118 | # Returns false if all members evaluates to 0 | ||||||
119 | # Returns true if at least one member evaluates to true | ||||||
120 | # Returns undef otherwise | ||||||
121 | 12 | 5 | my $result = 0; | ||||
122 | 12 | 10 | foreach my $dep ($self->get_deps()) { | ||||
123 | 26 | 21 | my $eval = $dep->get_evaluation($facts); | ||||
124 | 26 | 30 | if (not defined $eval) { | ||||
125 | 0 | 0 | $result = undef; | ||||
126 | } elsif ($eval == 1) { | ||||||
127 | 3 | 0 | $result = 1; | ||||
128 | 3 | 3 | last; | ||||
129 | } elsif ($eval == 0) { | ||||||
130 | # Still possible to have a false evaluation | ||||||
131 | } | ||||||
132 | } | ||||||
133 | 12 | 10 | return $result; | ||||
134 | } | ||||||
135 | |||||||
136 - 142 | =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 | ||||||
143 | |||||||
144 | sub simplify_deps { | ||||||
145 | 0 | 1 | my ($self, $facts) = @_; | ||||
146 | 0 | my @new; | |||||
147 | |||||||
148 | WHILELOOP: | ||||||
149 | 0 0 | while (@{$self->{list}}) { | |||||
150 | 0 0 | my $dep = shift @{$self->{list}}; | |||||
151 | 0 | my $eval = $dep->get_evaluation($facts); | |||||
152 | 0 | if (defined $eval and $eval == 1) { | |||||
153 | 0 | $self->{list} = []; | |||||
154 | 0 | return; | |||||
155 | } | ||||||
156 | 0 0 | foreach my $odep (@new, @{$self->{list}}) { | |||||
157 | 0 | next WHILELOOP if $odep->implies($dep); | |||||
158 | } | ||||||
159 | 0 | push @new, $dep; | |||||
160 | } | ||||||
161 | 0 | $self->{list} = [ @new ]; | |||||
162 | } | ||||||
163 | |||||||
164 | =back | ||||||
165 | |||||||
166 - 172 | =head1 CHANGES =head2 Version 1.00 (dpkg 1.15.6) Mark the module as public. =cut | ||||||
173 | |||||||
174 | 1; |