<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright Â© 1998 Richard Braakman
# Copyright Â© 1999 Darren Benham
# Copyright Â© 2000 Sean 'Shaleh' Perry
# Copyright Â© 2004 Frank Lichtenheld
# Copyright Â© 2006 Russ Allbery
# Copyright Â© 2007-2009 RaphaÃ«l Hertzog &lt;hertzog@debian.org&gt;
# Copyright Â© 2008-2009,2012-2014 Guillem Jover &lt;guillem@debian.org&gt;
#
# This program is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.

=encoding utf8

=head1 NAME

Dpkg::Deps - parse and manipulate dependencies of Debian packages

=head1 DESCRIPTION

The Dpkg::Deps module provides classes implementing various types of
dependencies.

The most important function is deps_parse(), it turns a dependency line in
a set of Dpkg::Deps::{Simple,AND,OR,Union} objects depending on the case.

=cut

package Dpkg::Deps 1.07;

use strict;
use warnings;
use feature qw(current_sub);

our @EXPORT = qw(
    deps_concat
    deps_parse
    deps_eval_implication
    deps_iterate
    deps_compare
);

use Carp;
use Exporter qw(import);

use Dpkg::Version;
use Dpkg::Arch qw(get_host_arch get_build_arch debarch_to_debtuple);
use Dpkg::BuildProfiles qw(get_build_profiles);
use Dpkg::ErrorHandling;
use Dpkg::Gettext;
use Dpkg::Deps::Simple;
use Dpkg::Deps::Union;
use Dpkg::Deps::AND;
use Dpkg::Deps::OR;
use Dpkg::Deps::KnownFacts;

=head1 FUNCTIONS

All the deps_* functions are exported by default.

=over 4

=item deps_eval_implication($rel_p, $v_p, $rel_q, $v_q)

($rel_p, $v_p) and ($rel_q, $v_q) express two dependencies as (relation,
version). The relation variable can have the following values that are
exported by L&lt;Dpkg::Version&gt;: REL_EQ, REL_LT, REL_LE, REL_GT, REL_GT.

This functions returns 1 if the "p" dependency implies the "q"
dependency. It returns 0 if the "p" dependency implies that "q" is
not satisfied. It returns undef when there's no implication.

The $v_p and $v_q parameter should be L&lt;Dpkg::Version&gt; objects.

=cut

sub deps_eval_implication {
    my ($rel_p, $v_p, $rel_q, $v_q) = @_;

    # If versions are not valid, we can't decide of any implication
    return unless defined($v_p) and $v_p-&gt;is_valid();
    return unless defined($v_q) and $v_q-&gt;is_valid();

    # q wants an exact version, so p must provide that exact version.  p
    # disproves q if q's version is outside the range enforced by p.
    if ($rel_q eq REL_EQ) {
        if ($rel_p eq REL_LT) {
            return ($v_p &lt;= $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_LE) {
            return ($v_p &lt; $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_GT) {
            return ($v_p &gt;= $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_GE) {
            return ($v_p &gt; $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_EQ) {
            return ($v_p == $v_q);
        }
    }

    # A greater than clause may disprove a less than clause. An equal
    # cause might as well.  Otherwise, if
    # p's clause is &lt;&lt;, &lt;=, or =, the version must be &lt;= q's to imply q.
    if ($rel_q eq REL_LE) {
        if ($rel_p eq REL_GT) {
            return ($v_p &gt;= $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_GE) {
            return ($v_p &gt; $v_q) ? 0 : undef;
	} elsif ($rel_p eq REL_EQ) {
            return ($v_p &lt;= $v_q) ? 1 : 0;
        } else { # &lt;&lt;, &lt;=
            return ($v_p &lt;= $v_q) ? 1 : undef;
        }
    }

    # Similar, but &lt;&lt; is stronger than &lt;= so p's version must be &lt;&lt; q's
    # version if the p relation is &lt;= or =.
    if ($rel_q eq REL_LT) {
        if ($rel_p eq REL_GT or $rel_p eq REL_GE) {
            return ($v_p &gt;= $v_p) ? 0 : undef;
        } elsif ($rel_p eq REL_LT) {
            return ($v_p &lt;= $v_q) ? 1 : undef;
	} elsif ($rel_p eq REL_EQ) {
            return ($v_p &lt; $v_q) ? 1 : 0;
        } else { # &lt;&lt;, &lt;=
            return ($v_p &lt; $v_q) ? 1 : undef;
        }
    }

    # Same logic as above, only inverted.
    if ($rel_q eq REL_GE) {
        if ($rel_p eq REL_LT) {
            return ($v_p &lt;= $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_LE) {
            return ($v_p &lt; $v_q) ? 0 : undef;
	} elsif ($rel_p eq REL_EQ) {
            return ($v_p &gt;= $v_q) ? 1 : 0;
        } else { # &gt;&gt;, &gt;=
            return ($v_p &gt;= $v_q) ? 1 : undef;
        }
    }
    if ($rel_q eq REL_GT) {
        if ($rel_p eq REL_LT or $rel_p eq REL_LE) {
            return ($v_p &lt;= $v_q) ? 0 : undef;
        } elsif ($rel_p eq REL_GT) {
            return ($v_p &gt;= $v_q) ? 1 : undef;
	} elsif ($rel_p eq REL_EQ) {
            return ($v_p &gt; $v_q) ? 1 : 0;
        } else {
            return ($v_p &gt; $v_q) ? 1 : undef;
        }
    }

    return;
}

=item $dep = deps_concat(@dep_list)

This function concatenates multiple dependency lines into a single line,
joining them with ", " if appropriate, and always returning a valid string.

=cut

sub deps_concat {
    my (@dep_list) = @_;

    return join ', ', grep { defined } @dep_list;
}

=item $dep = deps_parse($line, %options)

This function parses the dependency line and returns an object, either a
L&lt;Dpkg::Deps::AND&gt; or a L&lt;Dpkg::Deps::Union&gt;. Various options can alter the
behavior of that function.

=over 4

=item use_arch (defaults to 1)

Take into account the architecture restriction part of the dependencies.
Set to 0 to completely ignore that information.

=item host_arch (defaults to the current architecture)

Define the host architecture. By default it uses
Dpkg::Arch::get_host_arch() to identify the proper architecture.

=item build_arch (defaults to the current architecture)

Define the build architecture. By default it uses
Dpkg::Arch::get_build_arch() to identify the proper architecture.

=item reduce_arch (defaults to 0)

If set to 1, ignore dependencies that do not concern the current host
architecture. This implicitly strips off the architecture restriction
list so that the resulting dependencies are directly applicable to the
current architecture.

=item use_profiles (defaults to 1)

Take into account the profile restriction part of the dependencies. Set
to 0 to completely ignore that information.

=item build_profiles (defaults to no profile)

Define the active build profiles. By default no profile is defined.

=item reduce_profiles (defaults to 0)

If set to 1, ignore dependencies that do not concern the current build
profile. This implicitly strips off the profile restriction formula so
that the resulting dependencies are directly applicable to the current
profiles.

=item reduce_restrictions (defaults to 0)

If set to 1, ignore dependencies that do not concern the current set of
restrictions. This implicitly strips off any architecture restriction list
or restriction formula so that the resulting dependencies are directly
applicable to the current restriction.
This currently implies C&lt;reduce_arch&gt; and C&lt;reduce_profiles&gt;, and overrides
them if set.

=item union (defaults to 0)

If set to 1, returns a L&lt;Dpkg::Deps::Union&gt; instead of a L&lt;Dpkg::Deps::AND&gt;.
Use this when parsing non-dependency fields like Conflicts.

=item virtual (defaults to 0)

If set to 1, allow only virtual package version relations, that is none,
or "=".
This should be set whenever working with Provides fields.

=item build_dep (defaults to 0)

If set to 1, allow build-dep only arch qualifiers, that is ":native".
This should be set whenever working with build-deps.

=item tests_dep (defaults to 0)

If set to 1, allow tests-specific package names in dependencies, that is
"@" and "@builddeps@" (since dpkg 1.18.7). This should be set whenever
working with dependency fields from F&lt;debian/tests/control&gt;.

This option implicitly (and forcibly) enables C&lt;build_dep&gt; because test
dependencies are based on build dependencies (since dpkg 1.22.1).

=back

=cut

sub deps_parse {
    my ($dep_line, %options) = @_;

    # Validate arguments.
    croak "invalid host_arch $options{host_arch}"
        if defined $options{host_arch} and not defined debarch_to_debtuple($options{host_arch});
    croak "invalid build_arch $options{build_arch}"
        if defined $options{build_arch} and not defined debarch_to_debtuple($options{build_arch});

    $options{use_arch} //= 1;
    $options{reduce_arch} //= 0;
    $options{use_profiles} //= 1;
    $options{reduce_profiles} //= 0;
    $options{reduce_restrictions} //= 0;
    $options{union} //= 0;
    $options{virtual} //= 0;
    $options{build_dep} //= 0;
    $options{tests_dep} //= 0;

    if ($options{reduce_restrictions}) {
        $options{reduce_arch} = 1;
        $options{reduce_profiles} = 1;
    }
    if ($options{reduce_arch}) {
        $options{host_arch} //= get_host_arch();
        $options{build_arch} //= get_build_arch();
    }
    if ($options{reduce_profiles}) {
        $options{build_profiles} //= [ get_build_profiles() ];
    }
    if ($options{tests_dep}) {
        $options{build_dep} = 1;
    }

    # Options for Dpkg::Deps::Simple.
    my %deps_options = (
        host_arch =&gt; $options{host_arch},
        build_arch =&gt; $options{build_arch},
        build_dep =&gt; $options{build_dep},
        tests_dep =&gt; $options{tests_dep},
    );

    # Merge in a single-line
    $dep_line =~ s/\s*[\r\n]\s*/ /g;
    # Strip trailing/leading spaces
    $dep_line =~ s/^\s+//;
    $dep_line =~ s/\s+$//;

    my @dep_list;
    foreach my $dep_and (split(/\s*,\s*/m, $dep_line)) {
        my @or_list = ();
        foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
	    my $dep_simple = Dpkg::Deps::Simple-&gt;new($dep_or, %deps_options);
	    if (not defined $dep_simple-&gt;{package}) {
		warning(g_("can't parse dependency %s"), $dep_or);
		return;
	    }
            if ($options{virtual} &amp;&amp; defined $dep_simple-&gt;{relation} &amp;&amp;
                $dep_simple-&gt;{relation} ne '=') {
                warning(g_('virtual dependency contains invalid relation: %s'),
                        $dep_simple-&gt;output);
                return;
            }
	    $dep_simple-&gt;{arches} = undef if not $options{use_arch};
            if ($options{reduce_arch}) {
		$dep_simple-&gt;reduce_arch($options{host_arch});
		next if not $dep_simple-&gt;arch_is_concerned($options{host_arch});
	    }
	    $dep_simple-&gt;{restrictions} = undef if not $options{use_profiles};
	    if ($options{reduce_profiles}) {
		$dep_simple-&gt;reduce_profiles($options{build_profiles});
		next if not $dep_simple-&gt;profile_is_concerned($options{build_profiles});
	    }
	    push @or_list, $dep_simple;
        }
	next if not @or_list;
	if (scalar @or_list == 1) {
	    push @dep_list, $or_list[0];
	} else {
	    my $dep_or = Dpkg::Deps::OR-&gt;new();
	    $dep_or-&gt;add($_) foreach (@or_list);
	    push @dep_list, $dep_or;
	}
    }
    my $dep_and;
    if ($options{union}) {
	$dep_and = Dpkg::Deps::Union-&gt;new();
    } else {
	$dep_and = Dpkg::Deps::AND-&gt;new();
    }
    foreach my $dep (@dep_list) {
        if ($options{union} and not $dep-&gt;isa('Dpkg::Deps::Simple')) {
            warning(g_('an union dependency can only contain simple dependencies'));
            return;
        }
        $dep_and-&gt;add($dep);
    }
    return $dep_and;
}

=item $bool = deps_iterate($deps, $callback_func)

This function visits all elements of the dependency object, calling the
callback function for each element.

The callback function is expected to return true when everything is fine,
or false if something went wrong, in which case the iteration will stop.

Return the same value as the callback function.

=cut

sub deps_iterate {
    my ($deps, $callback_func) = @_;

    my $visitor_func = sub {
        foreach my $dep (@_) {
            return unless defined $dep;

            if ($dep-&gt;isa('Dpkg::Deps::Simple')) {
                return unless $callback_func-&gt;($dep);
            } else {
                return unless __SUB__-&gt;($dep-&gt;get_deps());
            }
        }
        return 1;
    };

    return $visitor_func-&gt;($deps);
}

=item deps_compare($a, $b)

Implements a comparison operator between two dependency objects.
This function is mainly used to implement the sort() method.

=back

=cut

my %relation_ordering = (
	undef =&gt; 0,
	REL_GE() =&gt; 1,
	REL_GT() =&gt; 2,
	REL_EQ() =&gt; 3,
	REL_LT() =&gt; 4,
	REL_LE() =&gt; 5,
);

sub deps_compare {
    my ($aref, $bref) = @_;

    my (@as, @bs);
    deps_iterate($aref, sub { push @as, @_ });
    deps_iterate($bref, sub { push @bs, @_ });

    while (1) {
        my ($a, $b) = (shift @as, shift @bs);
        my $aundef = not defined $a or $a-&gt;is_empty();
        my $bundef = not defined $b or $b-&gt;is_empty();

        return  0 if $aundef and $bundef;
        return -1 if $aundef;
        return  1 if $bundef;

        my $ar = $a-&gt;{relation} // 'undef';
        my $br = $b-&gt;{relation} // 'undef';
        my $av = $a-&gt;{version} // '';
        my $bv = $b-&gt;{version} // '';

        my $res = (($a-&gt;{package} cmp $b-&gt;{package}) ||
                   ($relation_ordering{$ar} &lt;=&gt; $relation_ordering{$br}) ||
                   ($av cmp $bv));
        return $res if $res != 0;
    }
}

=head1 CLASSES - Dpkg::Deps::*

There are several kind of dependencies. A L&lt;Dpkg::Deps::Simple&gt; dependency
represents a single dependency statement (it relates to one package only).
L&lt;Dpkg::Deps::Multiple&gt; dependencies are built on top of this class
and combine several dependencies in different manners. L&lt;Dpkg::Deps::AND&gt;
represents the logical "AND" between dependencies while L&lt;Dpkg::Deps::OR&gt;
represents the logical "OR". L&lt;Dpkg::Deps::Multiple&gt; objects can contain
L&lt;Dpkg::Deps::Simple&gt; object as well as other L&lt;Dpkg::Deps::Multiple&gt; objects.

In practice, the code is only meant to handle the realistic cases which,
given Debian's dependencies structure, imply those restrictions: AND can
contain Simple or OR objects, OR can only contain Simple objects.

L&lt;Dpkg::Deps::KnownFacts&gt; is a special class that is used while evaluating
dependencies and while trying to simplify them. It represents a set of
installed packages along with the virtual packages that they might
provide.

=head1 CHANGES

=head2 Version 1.07 (dpkg 1.20.0)

New option: Add virtual option to deps_parse().

=head2 Version 1.06 (dpkg 1.18.7; module version bumped on dpkg 1.18.24)

New option: Add tests_dep option to deps_parse().

=head2 Version 1.05 (dpkg 1.17.14)

New function: deps_iterate().

=head2 Version 1.04 (dpkg 1.17.10)

New options: Add use_profiles, build_profiles, reduce_profiles and
reduce_restrictions to deps_parse().

=head2 Version 1.03 (dpkg 1.17.0)

New option: Add build_arch option to deps_parse().

=head2 Version 1.02 (dpkg 1.17.0)

New function: deps_concat()

=head2 Version 1.01 (dpkg 1.16.1)

&lt;Used to document changes to Dpkg::Deps::* modules before they were split.&gt;

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut

1;
</pre></body></html>