po: Update German man pages translation
[dpkg.git] / scripts / Dpkg / Deps / Simple.pm
blob57b76bb9234bb053acf2cb47ecb76fab851ce646
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>
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.
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.
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/>.
22 =encoding utf8
24 =head1 NAME
26 Dpkg::Deps::Simple - represents a single dependency statement
28 =head1 DESCRIPTION
30 This class represents a single dependency statement.
31 It has several interesting properties:
33 =over 4
35 =item package
37 The package name (can be undef if the dependency has not been initialized
38 or if the simplification of the dependency lead to its removal).
40 =item relation
42 The relational operator: "=", "<<", "<=", ">=" or ">>". It can be
43 undefined if the dependency had no version restriction. In that case the
44 following field is also undefined.
46 =item version
48 The version.
50 =item arches
52 The list of architectures where this dependency is applicable. It is
53 undefined when there's no restriction, otherwise it is an
54 array ref. It can contain an exclusion list, in that case each
55 architecture is prefixed with an exclamation mark.
57 =item archqual
59 The arch qualifier of the dependency (can be undef if there is none).
60 In the dependency "python:any (>= 2.6)", the arch qualifier is "any".
62 =item restrictions
64 The restrictions formula for this dependency. It is undefined when there
65 is no restriction formula. Otherwise it is an array ref.
67 =back
69 =cut
71 package Dpkg::Deps::Simple 1.02;
73 use strict;
74 use warnings;
76 use Carp;
78 use Dpkg::Arch qw(debarch_is_concerned debarch_list_parse);
79 use Dpkg::BuildProfiles qw(parse_build_profiles evaluate_restriction_formula);
80 use Dpkg::Version;
81 use Dpkg::ErrorHandling;
82 use Dpkg::Gettext;
84 use parent qw(Dpkg::Interface::Storable);
86 =head1 METHODS
88 =over 4
90 =item $dep = Dpkg::Deps::Simple->new([$dep[, %opts]]);
92 Creates a new object.
94 Options:
96 =over
98 =item B<host_arch>
100 Sets the host architecture.
102 =item B<build_arch>
104 Sets the build architecture.
106 =item B<build_dep>
108 Specifies whether the parser should consider it a build dependency.
109 Defaults to 0.
111 =item B<tests_dep>
113 Specifies whether the parser should consider it a tests dependency.
114 Defaults to 0.
116 This option implicitly (and forcibly) enables C<build_dep> because test
117 dependencies are based on build dependencies (since dpkg 1.22.1).
119 =back
121 =cut
123 sub new {
124 my ($this, $arg, %opts) = @_;
125 my $class = ref($this) || $this;
126 my $self = {};
128 bless $self, $class;
129 $self->reset();
130 $self->{host_arch} = $opts{host_arch};
131 $self->{build_arch} = $opts{build_arch};
132 $self->{build_dep} = $opts{build_dep} // 0;
133 $self->{tests_dep} = $opts{tests_dep} // 0;
134 if ($self->{tests_dep}) {
135 $self->{build_dep} = 1;
138 $self->parse_string($arg) if defined $arg;
139 return $self;
142 =item $dep->reset()
144 Clears any dependency information stored in $dep so that $dep->is_empty()
145 returns true.
147 =cut
149 sub reset {
150 my $self = shift;
152 $self->{package} = undef;
153 $self->{relation} = undef;
154 $self->{version} = undef;
155 $self->{arches} = undef;
156 $self->{archqual} = undef;
157 $self->{restrictions} = undef;
160 =item $dep->parse_string($dep_string)
162 Parses the dependency string and modifies internal properties to match the
163 parsed dependency.
165 =cut
167 sub parse_string {
168 my ($self, $dep) = @_;
170 my $pkgname_re;
171 if ($self->{tests_dep}) {
172 $pkgname_re = qr/[\@a-zA-Z0-9][\@a-zA-Z0-9+.-]*/;
173 } else {
174 $pkgname_re = qr/[a-zA-Z0-9][a-zA-Z0-9+.-]*/;
177 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
178 return if not $dep =~
179 m{^\s* # skip leading whitespace
180 ($pkgname_re) # package name
181 (?: # start of optional part
182 : # colon for architecture
183 ([a-zA-Z0-9][a-zA-Z0-9-]*) # architecture name
184 )? # end of optional part
185 (?: # start of optional part
186 \s* \( # open parenthesis for version part
187 \s* (<<|<=|=|>=|>>|[<>]) # relation part
188 \s* ([^\)\s]+) # do not attempt to parse version
189 \s* \) # closing parenthesis
190 )? # end of optional part
191 (?: # start of optional architecture
192 \s* \[ # open bracket for architecture
193 \s* ([^\]]+) # don't parse architectures now
194 \s* \] # closing bracket
195 )? # end of optional architecture
197 (?: # start of optional restriction
198 \s* < # open bracket for restriction
199 \s* [^>]+ # do not parse restrictions now
200 \s* > # closing bracket
202 )? # end of optional restriction
203 \s*$ # trailing spaces at end
205 if (defined $2) {
206 return if $2 eq 'native' and not $self->{build_dep};
207 $self->{archqual} = $2;
209 $self->{package} = $1;
210 $self->{relation} = version_normalize_relation($3) if defined $3;
211 if (defined $4) {
212 $self->{version} = Dpkg::Version->new($4);
214 if (defined $5) {
215 $self->{arches} = [ debarch_list_parse($5) ];
217 if (defined $6) {
218 $self->{restrictions} = [ parse_build_profiles($6) ];
222 =item $dep->parse($fh, $desc)
224 Parse a dependency line from a filehandle.
226 =cut
228 sub parse {
229 my ($self, $fh, $desc) = @_;
231 my $line = <$fh>;
232 chomp $line;
233 return $self->parse_string($line);
236 =item $dep->load($filename)
238 Parse a dependency line from $filename.
240 =item $dep->output([$fh])
242 =item "$dep"
244 Returns a string representing the dependency. If $fh is set, it prints
245 the string to the filehandle.
247 =cut
249 sub output {
250 my ($self, $fh) = @_;
252 my $res = $self->{package};
253 if (defined $self->{archqual}) {
254 $res .= ':' . $self->{archqual};
256 if (defined $self->{relation}) {
257 $res .= ' (' . $self->{relation} . ' ' . $self->{version} . ')';
259 if (defined $self->{arches}) {
260 $res .= ' [' . join(' ', @{$self->{arches}}) . ']';
262 if (defined $self->{restrictions}) {
263 for my $restrlist (@{$self->{restrictions}}) {
264 $res .= ' <' . join(' ', @{$restrlist}) . '>';
267 if (defined $fh) {
268 print { $fh } $res;
270 return $res;
273 =item $dep->save($filename)
275 Save the dependency into the given $filename.
277 =cut
279 # _arch_is_superset(\@p, \@q)
281 # Returns true if the arch list @p is a superset of arch list @q.
282 # The arguments can also be undef in case there's no explicit architecture
283 # restriction.
284 sub _arch_is_superset {
285 my ($p, $q) = @_;
286 my $p_arch_neg = defined $p and $p->[0] =~ /^!/;
287 my $q_arch_neg = defined $q and $q->[0] =~ /^!/;
289 if (not defined $p) {
290 # If "p" has no arches, it is a superset of q and we should fall through
291 # to the version check.
292 return 1;
293 } elsif (not defined $q) {
294 # If q has no arches, it is a superset of p and there are no useful
295 # implications.
296 return 0;
297 } elsif (not $p_arch_neg and not $q_arch_neg) {
298 # Both have arches. If neither are negated, we know nothing useful
299 # unless q is a subset of p.
301 my %p_arches = map { $_ => 1 } @{$p};
302 my $subset = 1;
303 for my $arch (@{$q}) {
304 $subset = 0 unless $p_arches{$arch};
306 return 0 unless $subset;
307 } elsif ($p_arch_neg and $q_arch_neg) {
308 # If both are negated, we know nothing useful unless p is a subset of
309 # q (and therefore has fewer things excluded, and therefore is more
310 # general).
312 my %q_arches = map { $_ => 1 } @{$q};
313 my $subset = 1;
314 for my $arch (@{$p}) {
315 $subset = 0 unless $q_arches{$arch};
317 return 0 unless $subset;
318 } elsif (not $p_arch_neg and $q_arch_neg) {
319 # If q is negated and p isn't, we'd need to know the full list of
320 # arches to know if there's any relationship, so bail.
321 return 0;
322 } elsif ($p_arch_neg and not $q_arch_neg) {
323 # If p is negated and q isn't, q is a subset of p if none of the
324 # negated arches in p are present in q.
326 my %q_arches = map { $_ => 1 } @{$q};
327 my $subset = 1;
328 for my $arch (@{$p}) {
329 $subset = 0 if $q_arches{substr($arch, 1)};
331 return 0 unless $subset;
333 return 1;
336 # _arch_qualifier_implies($p, $q)
338 # Returns true if the arch qualifier $p and $q are compatible with the
339 # implication $p -> $q, false otherwise. $p/$q can be undef/"any"/"native"
340 # or an architecture string.
342 # Because we are handling dependencies in isolation, and the full context
343 # of the implications are only known when doing dependency resolution at
344 # run-time, we can only assert that they are implied if they are equal.
346 # For example dependencies with different arch-qualifiers cannot be simplified
347 # as these depend on the state of Multi-Arch field in the package depended on.
348 sub _arch_qualifier_implies {
349 my ($p, $q) = @_;
351 return $p eq $q if defined $p and defined $q;
352 return 1 if not defined $p and not defined $q;
353 return 0;
356 # _restrictions_imply($p, $q)
358 # Returns true if the restrictions $p and $q are compatible with the
359 # implication $p -> $q, false otherwise.
360 # NOTE: We don't try to be very clever here, so we may conservatively
361 # return false when there is an implication.
362 sub _restrictions_imply {
363 my ($p, $q) = @_;
365 if (not defined $p) {
366 return 1;
367 } elsif (not defined $q) {
368 return 0;
369 } else {
370 # Check whether set difference is empty.
371 my %restr;
373 for my $restrlist (@{$q}) {
374 my $reststr = join ' ', sort @{$restrlist};
375 $restr{$reststr} = 1;
377 for my $restrlist (@{$p}) {
378 my $reststr = join ' ', sort @{$restrlist};
379 delete $restr{$reststr};
382 return keys %restr == 0;
386 =item $dep->implies($other_dep)
388 Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
389 NOT($other_dep). Returns undef when there is no implication. $dep and
390 $other_dep do not need to be of the same type.
392 =cut
394 sub implies {
395 my ($self, $o) = @_;
397 if ($o->isa('Dpkg::Deps::Simple')) {
398 # An implication is only possible on the same package
399 return if $self->{package} ne $o->{package};
401 # Our architecture set must be a superset of the architectures for
402 # o, otherwise we can't conclude anything.
403 return unless _arch_is_superset($self->{arches}, $o->{arches});
405 # The arch qualifier must not forbid an implication
406 return unless _arch_qualifier_implies($self->{archqual},
407 $o->{archqual});
409 # Our restrictions must imply the restrictions for o
410 return unless _restrictions_imply($self->{restrictions},
411 $o->{restrictions});
413 # If o has no version clause, then our dependency is stronger
414 return 1 if not defined $o->{relation};
415 # If o has a version clause, we must also have one, otherwise there
416 # can't be an implication
417 return if not defined $self->{relation};
419 return Dpkg::Deps::deps_eval_implication($self->{relation},
420 $self->{version}, $o->{relation}, $o->{version});
421 } elsif ($o->isa('Dpkg::Deps::AND')) {
422 # TRUE: Need to imply all individual elements
423 # FALSE: Need to NOT imply at least one individual element
424 my $res = 1;
425 foreach my $dep ($o->get_deps()) {
426 my $implication = $self->implies($dep);
427 unless (defined $implication and $implication == 1) {
428 $res = $implication;
429 last if defined $res;
432 return $res;
433 } elsif ($o->isa('Dpkg::Deps::OR')) {
434 # TRUE: Need to imply at least one individual element
435 # FALSE: Need to not apply all individual elements
436 # UNDEF: The rest
437 my $res = undef;
438 foreach my $dep ($o->get_deps()) {
439 my $implication = $self->implies($dep);
440 if (defined $implication) {
441 if (not defined $res) {
442 $res = $implication;
443 } elsif ($implication) {
444 $res = 1;
445 } else {
446 $res = 0;
448 last if defined $res and $res == 1;
451 return $res;
452 } else {
453 croak 'Dpkg::Deps::Simple cannot evaluate implication with a ' .
454 ref($o);
458 =item $dep->get_deps()
460 Returns a list of sub-dependencies, which for this object it means it
461 returns itself.
463 =cut
465 sub get_deps {
466 my $self = shift;
468 return $self;
471 =item $dep->sort()
473 This method is a no-op for this object.
475 =cut
477 sub sort {
478 # Nothing to sort
481 =item $dep->arch_is_concerned($arch)
483 Returns true if the dependency applies to the indicated architecture.
485 =cut
487 sub arch_is_concerned {
488 my ($self, $host_arch) = @_;
490 return 0 if not defined $self->{package}; # Empty dep
491 return 1 if not defined $self->{arches}; # Dep without arch spec
493 return debarch_is_concerned($host_arch, @{$self->{arches}});
496 =item $dep->reduce_arch($arch)
498 Simplifies the dependency to contain only information relevant to the given
499 architecture. This object can be left empty after this operation. This trims
500 off the architecture restriction list of these objects.
502 =cut
504 sub reduce_arch {
505 my ($self, $host_arch) = @_;
507 if (not $self->arch_is_concerned($host_arch)) {
508 $self->reset();
509 } else {
510 $self->{arches} = undef;
514 =item $dep->has_arch_restriction()
516 Returns the package name if the dependency applies only to a subset of
517 architectures.
519 =cut
521 sub has_arch_restriction {
522 my $self = shift;
524 if (defined $self->{arches}) {
525 return $self->{package};
526 } else {
527 return ();
531 =item $dep->profile_is_concerned()
533 Returns true if the dependency applies to the indicated profile.
535 =cut
537 sub profile_is_concerned {
538 my ($self, $build_profiles) = @_;
540 return 0 if not defined $self->{package}; # Empty dep
541 return 1 if not defined $self->{restrictions}; # Dep without restrictions
542 return evaluate_restriction_formula($self->{restrictions}, $build_profiles);
545 =item $dep->reduce_profiles()
547 Simplifies the dependency to contain only information relevant to the given
548 profile. This object can be left empty after this operation. This trims off
549 the profile restriction list of this object.
551 =cut
553 sub reduce_profiles {
554 my ($self, $build_profiles) = @_;
556 if (not $self->profile_is_concerned($build_profiles)) {
557 $self->reset();
558 } else {
559 $self->{restrictions} = undef;
563 =item $dep->get_evaluation($facts)
565 Evaluates the dependency given a list of installed packages and a list of
566 virtual packages provided. These lists are part of the
567 L<Dpkg::Deps::KnownFacts> object given as parameters.
569 Returns 1 when it's true, 0 when it's false, undef when some information
570 is lacking to conclude.
572 =cut
574 sub get_evaluation {
575 my ($self, $facts) = @_;
577 return if not defined $self->{package};
578 return $facts->evaluate_simple_dep($self);
581 =item $dep->simplify_deps($facts, @assumed_deps)
583 Simplifies the dependency as much as possible given the list of facts (see
584 class L<Dpkg::Deps::KnownFacts>) and a list of other dependencies that are
585 known to be true.
587 =cut
589 sub simplify_deps {
590 my ($self, $facts) = @_;
592 my $eval = $self->get_evaluation($facts);
593 $self->reset() if defined $eval and $eval == 1;
596 =item $dep->is_empty()
598 Returns true if the dependency is empty and doesn't contain any useful
599 information. This is true when the object has not yet been initialized.
601 =cut
603 sub is_empty {
604 my $self = shift;
606 return not defined $self->{package};
609 =item $dep->merge_union($other_dep)
611 Returns true if $dep could be modified to represent the union of both
612 dependencies. Otherwise returns false.
614 =cut
616 sub merge_union {
617 my ($self, $o) = @_;
619 return 0 if not $o->isa('Dpkg::Deps::Simple');
620 return 0 if $self->is_empty() or $o->is_empty();
621 return 0 if $self->{package} ne $o->{package};
622 return 0 if defined $self->{arches} or defined $o->{arches};
624 if (not defined $o->{relation} and defined $self->{relation}) {
625 # Union is the non-versioned dependency
626 $self->{relation} = undef;
627 $self->{version} = undef;
628 return 1;
631 my $implication = $self->implies($o);
632 my $rev_implication = $o->implies($self);
633 if (defined $implication) {
634 if ($implication) {
635 $self->{relation} = $o->{relation};
636 $self->{version} = $o->{version};
637 return 1;
638 } else {
639 return 0;
642 if (defined $rev_implication) {
643 if ($rev_implication) {
644 # Already merged...
645 return 1;
646 } else {
647 return 0;
650 return 0;
653 =back
655 =head1 CHANGES
657 =head2 Version 1.02 (dpkg 1.17.10)
659 New methods: Add $dep->profile_is_concerned() and $dep->reduce_profiles().
661 =head2 Version 1.01 (dpkg 1.16.1)
663 New method: Add $dep->reset().
665 New property: recognizes the arch qualifier "any" and stores it in the
666 "archqual" property when present.
668 =head2 Version 1.00 (dpkg 1.15.6)
670 Mark the module as public.
672 =cut