Deps.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you may redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #########################################################################
  16. # Several parts are inspired by lib/Dep.pm from lintian (same license)
  17. #
  18. # Copyright © 1998 Richard Braakman
  19. # Portions Copyright © 1999 Darren Benham
  20. # Portions Copyright © 2000 Sean 'Shaleh' Perry
  21. # Portions Copyright © 2004 Frank Lichtenheld
  22. # Portions Copyright © 2006 Russ Allbery
  23. package Dpkg::Deps;
  24. =head1 NAME
  25. Dpkg::Deps - parse and manipulate dependencies of Debian packages
  26. =head1 DESCRIPTION
  27. The Dpkg::Deps module provides objects implementing various types of
  28. dependencies.
  29. The most important function is deps_parse(), it turns a dependency line in
  30. a set of Dpkg::Deps::{Simple,AND,OR,Union} objects depending on the case.
  31. =head1 FUNCTIONS
  32. All the deps_* functions are exported by default.
  33. =over 4
  34. =cut
  35. use strict;
  36. use warnings;
  37. use Dpkg::Version;
  38. use Dpkg::Arch qw(get_host_arch);
  39. use Dpkg::ErrorHandling;
  40. use Dpkg::Gettext;
  41. use base qw(Exporter);
  42. our @EXPORT = qw(deps_parse deps_eval_implication deps_compare);
  43. # Some factorized function
  44. # Dpkg::Deps::_arch_is_superset(\@p, \@q)
  45. #
  46. # Returns true if the arch list @p is a superset of arch list @q.
  47. # The arguments can also be undef in case there's no explicit architecture
  48. # restriction.
  49. sub _arch_is_superset {
  50. my ($p, $q) = @_;
  51. my $p_arch_neg = defined($p) && $p->[0] =~ /^!/;
  52. my $q_arch_neg = defined($q) && $q->[0] =~ /^!/;
  53. # If "p" has no arches, it is a superset of q and we should fall through
  54. # to the version check.
  55. if (not defined $p) {
  56. return 1;
  57. }
  58. # If q has no arches, it is a superset of p and there are no useful
  59. # implications.
  60. elsif (not defined $q) {
  61. return 0;
  62. }
  63. # Both have arches. If neither are negated, we know nothing useful
  64. # unless q is a subset of p.
  65. elsif (not $p_arch_neg and not $q_arch_neg) {
  66. my %p_arches = map { $_ => 1 } @{$p};
  67. my $subset = 1;
  68. for my $arch (@{$q}) {
  69. $subset = 0 unless $p_arches{$arch};
  70. }
  71. return 0 unless $subset;
  72. }
  73. # If both are negated, we know nothing useful unless p is a subset of
  74. # q (and therefore has fewer things excluded, and therefore is more
  75. # general).
  76. elsif ($p_arch_neg and $q_arch_neg) {
  77. my %q_arches = map { $_ => 1 } @{$q};
  78. my $subset = 1;
  79. for my $arch (@{$p}) {
  80. $subset = 0 unless $q_arches{$arch};
  81. }
  82. return 0 unless $subset;
  83. }
  84. # If q is negated and p isn't, we'd need to know the full list of
  85. # arches to know if there's any relationship, so bail.
  86. elsif (not $p_arch_neg and $q_arch_neg) {
  87. return 0;
  88. }
  89. # If p is negated and q isn't, q is a subset of p if none of the
  90. # negated arches in p are present in q.
  91. elsif ($p_arch_neg and not $q_arch_neg) {
  92. my %q_arches = map { $_ => 1 } @{$q};
  93. my $subset = 1;
  94. for my $arch (@{$p}) {
  95. $subset = 0 if $q_arches{substr($arch, 1)};
  96. }
  97. return 0 unless $subset;
  98. }
  99. return 1;
  100. }
  101. =item deps_eval_implication($rel_p, $v_p, $rel_q, $v_q)
  102. ($rel_p, $v_p) and ($rel_q, $v_q) express two dependencies as (relation,
  103. version). The relation variable can have the following values that are
  104. exported by Dpkg::Version: REL_EQ, REL_LT, REL_LE, REL_GT, REL_GT.
  105. This functions returns 1 if the "p" dependency implies the "q"
  106. dependency. It returns 0 if the "p" dependency implies that "q" is
  107. not satisfied. It returns undef when there's no implication.
  108. The $v_p and $v_q parameter should be Dpkg::Version objects.
  109. =cut
  110. sub deps_eval_implication {
  111. my ($rel_p, $v_p, $rel_q, $v_q) = @_;
  112. # If versions are not valid, we can't decide of any implication
  113. return undef unless defined($v_p) and $v_p->is_valid();
  114. return undef unless defined($v_q) and $v_q->is_valid();
  115. # q wants an exact version, so p must provide that exact version. p
  116. # disproves q if q's version is outside the range enforced by p.
  117. if ($rel_q eq REL_EQ) {
  118. if ($rel_p eq REL_LT) {
  119. return ($v_p <= $v_q) ? 0 : undef;
  120. } elsif ($rel_p eq REL_LE) {
  121. return ($v_p < $v_q) ? 0 : undef;
  122. } elsif ($rel_p eq REL_GT) {
  123. return ($v_p >= $v_q) ? 0 : undef;
  124. } elsif ($rel_p eq REL_GE) {
  125. return ($v_p > $v_q) ? 0 : undef;
  126. } elsif ($rel_p eq REL_EQ) {
  127. return ($v_p == $v_q);
  128. }
  129. }
  130. # A greater than clause may disprove a less than clause. An equal
  131. # cause might as well. Otherwise, if
  132. # p's clause is <<, <=, or =, the version must be <= q's to imply q.
  133. if ($rel_q eq REL_LE) {
  134. if ($rel_p eq REL_GT) {
  135. return ($v_p >= $v_q) ? 0 : undef;
  136. } elsif ($rel_p eq REL_GE) {
  137. return ($v_p > $v_q) ? 0 : undef;
  138. } elsif ($rel_p eq REL_EQ) {
  139. return ($v_p <= $v_q) ? 1 : 0;
  140. } else { # <<, <=
  141. return ($v_p <= $v_q) ? 1 : undef;
  142. }
  143. }
  144. # Similar, but << is stronger than <= so p's version must be << q's
  145. # version if the p relation is <= or =.
  146. if ($rel_q eq REL_LT) {
  147. if ($rel_p eq REL_GT or $rel_p eq REL_GE) {
  148. return ($v_p >= $v_p) ? 0 : undef;
  149. } elsif ($rel_p eq REL_LT) {
  150. return ($v_p <= $v_q) ? 1 : undef;
  151. } elsif ($rel_p eq REL_EQ) {
  152. return ($v_p < $v_q) ? 1 : 0;
  153. } else { # <<, <=
  154. return ($v_p < $v_q) ? 1 : undef;
  155. }
  156. }
  157. # Same logic as above, only inverted.
  158. if ($rel_q eq REL_GE) {
  159. if ($rel_p eq REL_LT) {
  160. return ($v_p <= $v_q) ? 0 : undef;
  161. } elsif ($rel_p eq REL_LE) {
  162. return ($v_p < $v_q) ? 0 : undef;
  163. } elsif ($rel_p eq REL_EQ) {
  164. return ($v_p >= $v_q) ? 1 : 0;
  165. } else { # >>, >=
  166. return ($v_p >= $v_q) ? 1 : undef;
  167. }
  168. }
  169. if ($rel_q eq REL_GT) {
  170. if ($rel_p eq REL_LT or $rel_p eq REL_LE) {
  171. return ($v_p <= $v_q) ? 0 : undef;
  172. } elsif ($rel_p eq REL_GT) {
  173. return ($v_p >= $v_q) ? 1 : undef;
  174. } elsif ($rel_p eq REL_EQ) {
  175. return ($v_p > $v_q) ? 1 : 0;
  176. } else {
  177. return ($v_p > $v_q) ? 1 : undef;
  178. }
  179. }
  180. return undef;
  181. }
  182. =item my $dep = deps_parse($line, %options)
  183. This function parse the dependency line and returns an object, either a
  184. Dpkg::Deps::AND or a Dpkg::Deps::Union. Various options can alter the
  185. behaviour of that function.
  186. =over 4
  187. =item use_arch (defaults to 1)
  188. Take into account the architecture restriction part of the dependencies.
  189. Set to 0 to completely ignore that information.
  190. =item host_arch (defaults to the current architecture)
  191. Define the host architecture. Needed only if the reduce_arch option is
  192. set to 1. By default it uses Dpkg::Arch::get_host_arch() to identify
  193. the proper architecture.
  194. =item reduce_arch (defaults to 0)
  195. If set to 1, ignore dependencies that do not concern the current host
  196. architecture. This implicitely strips off the architecture restriction
  197. list so that the resulting dependencies are directly applicable to the
  198. current architecture.
  199. =item union (defaults to 0)
  200. If set to 1, returns a Dpkg::Deps::Union instead of a Dpkg::Deps::AND. Use
  201. this when parsing non-dependency fields like Conflicts.
  202. =back
  203. =cut
  204. sub deps_parse {
  205. my $dep_line = shift;
  206. my %options = (@_);
  207. $options{use_arch} = 1 if not exists $options{use_arch};
  208. $options{reduce_arch} = 0 if not exists $options{reduce_arch};
  209. $options{host_arch} = get_host_arch() if not exists $options{host_arch};
  210. $options{union} = 0 if not exists $options{union};
  211. # Strip trailing/leading spaces
  212. $dep_line =~ s/^\s+//;
  213. $dep_line =~ s/\s+$//;
  214. my @dep_list;
  215. foreach my $dep_and (split(/\s*,\s*/m, $dep_line)) {
  216. my @or_list = ();
  217. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  218. my $dep_simple = Dpkg::Deps::Simple->new($dep_or);
  219. if (not defined $dep_simple->{package}) {
  220. warning(_g("can't parse dependency %s"), $dep_or);
  221. return undef;
  222. }
  223. $dep_simple->{arches} = undef if not $options{use_arch};
  224. if ($options{reduce_arch}) {
  225. $dep_simple->reduce_arch($options{host_arch});
  226. next if not $dep_simple->arch_is_concerned($options{host_arch});
  227. }
  228. push @or_list, $dep_simple;
  229. }
  230. next if not @or_list;
  231. if (scalar @or_list == 1) {
  232. push @dep_list, $or_list[0];
  233. } else {
  234. my $dep_or = Dpkg::Deps::OR->new();
  235. $dep_or->add($_) foreach (@or_list);
  236. push @dep_list, $dep_or;
  237. }
  238. }
  239. my $dep_and;
  240. if ($options{union}) {
  241. $dep_and = Dpkg::Deps::Union->new();
  242. } else {
  243. $dep_and = Dpkg::Deps::AND->new();
  244. }
  245. foreach my $dep (@dep_list) {
  246. if ($options{union} and not $dep->isa("Dpkg::Deps::Simple")) {
  247. warning(_g("an union dependency can only contain simple dependencies"));
  248. return undef;
  249. }
  250. $dep_and->add($dep);
  251. }
  252. return $dep_and;
  253. }
  254. =item deps_compare($a, $b)
  255. Implements a comparison operator between two dependency objects.
  256. This function is mainly used to implement the sort() method.
  257. =back
  258. =cut
  259. our %relation_ordering = (
  260. 'undef' => 0,
  261. REL_GE => 1,
  262. REL_GT => 2,
  263. REL_EQ => 3,
  264. REL_LT => 4,
  265. REL_LE => 5,
  266. );
  267. sub deps_compare {
  268. my ($a, $b) = @_;
  269. return -1 if $a->is_empty();
  270. return 1 if $b->is_empty();
  271. while ($a->isa('Dpkg::Deps::Multiple')) {
  272. return -1 if $a->is_empty();
  273. my @deps = $a->get_deps();
  274. $a = $deps[0];
  275. }
  276. while ($b->isa('Dpkg::Deps::Multiple')) {
  277. return 1 if $b->is_empty();
  278. my @deps = $b->get_deps();
  279. $b = $deps[0];
  280. }
  281. my $ar = defined($a->{relation}) ? $a->{relation} : "undef";
  282. my $br = defined($b->{relation}) ? $b->{relation} : "undef";
  283. return (($a->{package} cmp $b->{package}) ||
  284. ($relation_ordering{$ar} <=> $relation_ordering{$br}) ||
  285. ($a->{version} cmp $b->{version}));
  286. }
  287. package Dpkg::Deps::Simple;
  288. =head1 OBJECTS - Dpkg::Deps::*
  289. There are several kind of dependencies. A Dpkg::Deps::Simple dependency
  290. represents a single dependency statement (it relates to one package only).
  291. Dpkg::Deps::Multiple dependencies are built on top of this object
  292. and combine several dependencies in a different manners. Dpkg::Deps::AND
  293. represents the logical "AND" between dependencies while Dpkg::Deps::OR
  294. represents the logical "OR". Dpkg::Deps::Multiple objects can contain
  295. Dpkg::Deps::Simple object as well as other Dpkg::Deps::Multiple objects.
  296. In practice, the code is only meant to handle the realistic cases which,
  297. given Debian's dependencies structure, imply those restrictions: AND can
  298. contain Simple or OR objects, OR can only contain Simple objects.
  299. Dpkg::Deps::KnowFacts is a special object that is used while evaluating
  300. dependencies and while trying to simplify them. It represents a set of
  301. installed packages along with the virtual packages that they might
  302. provide.
  303. =head2 Common functions
  304. =over 4
  305. =item $dep->is_empty()
  306. Returns true if the dependency is empty and doesn't contain any useful
  307. information. This is true when a Dpkg::Deps::Simple object has not yet
  308. been initialized or when a (descendant of) Dpkg::Deps::Multiple contains
  309. an empty list of dependencies.
  310. =item $dep->get_deps()
  311. Return a list of sub-dependencies. For Dpkg::Deps::Simple it returns
  312. itself.
  313. =item $dep->output([$fh])
  314. =item "$dep"
  315. Return a string representing the dependency. If $fh is set, it prints
  316. the string to the filehandle.
  317. =item $dep->implies($other_dep)
  318. Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
  319. NOT($other_dep). Returns undef when there's no implication. $dep and
  320. $other_dep do not need to be of the same type.
  321. =item $dep->sort()
  322. Sort alphabetically the internal list of dependencies. It's a no-op for
  323. Dpkg::Deps::Simple objects.
  324. =item $dep->arch_is_concerned($arch)
  325. Returns true if the dependency applies to the indicated architecture. For
  326. multiple dependencies, it returns true if at least one of the
  327. sub-dependencies apply to this architecture.
  328. =item $dep->reduce_arch($arch)
  329. Simplify the dependency to contain only information relevant to the given
  330. architecture. A Dpkg::Deps::Simple object can be left empty after this
  331. operation. For Dpkg::Deps::Multiple objects, the non-relevant
  332. sub-dependencies are simply removed.
  333. This trims off the architecture restriction list of Dpkg::Deps::Simple
  334. objects.
  335. =item $dep->get_evaluation($facts)
  336. Evaluates the dependency given a list of installed packages and a list of
  337. virtual packages provided. Those lists are part of the
  338. Dpkg::Deps::KnownFacts object given as parameters.
  339. Returns 1 when it's true, 0 when it's false, undef when some information
  340. is lacking to conclude.
  341. =item $dep->simplify_deps($facts, @assumed_deps)
  342. Simplify the dependency as much as possible given the list of facts (see
  343. object Dpkg::Deps::KnownFacts) and a list of other dependencies that we
  344. know to be true.
  345. =back
  346. =head2 Dpkg::Deps::Simple
  347. Such an object has four interesting properties:
  348. =over 4
  349. =item package
  350. The package name (can be undef if the dependency has not been initialized
  351. or if the simplification of the dependency lead to its removal).
  352. =item relation
  353. The relational operator: "=", "<<", "<=", ">=" or ">>". It can be
  354. undefined if the dependency had no version restriction. In that case the
  355. following field is also undefined.
  356. =item version
  357. The version.
  358. =item arches
  359. The list of architectures where this dependency is applicable. It's
  360. undefined when there's no restriction, otherwise it's an
  361. array ref. It can contain an exclusion list, in that case each
  362. architecture is prefixed with an exclamation mark.
  363. =back
  364. =head3 Methods
  365. =over 4
  366. =item $simple_dep->parse("dpkg-dev (>= 1.14.8) [!hurd-i386]")
  367. Parse the dependency and modify internal properties to match the parsed
  368. dependency.
  369. =item $simple_dep->merge_union($other_dep)
  370. Returns true if $simple_dep could be modified to represent the union of
  371. both dependencies. Otherwise returns false.
  372. =back
  373. =cut
  374. use strict;
  375. use warnings;
  376. use Dpkg::Arch qw(debarch_is);
  377. use Dpkg::Version;
  378. use Dpkg::ErrorHandling;
  379. use Dpkg::Gettext;
  380. use overload '""' => sub { $_[0]->output() };
  381. sub new {
  382. my ($this, $arg) = @_;
  383. my $class = ref($this) || $this;
  384. my $self = {
  385. 'package' => undef,
  386. 'relation' => undef,
  387. 'version' => undef,
  388. 'arches' => undef,
  389. };
  390. bless $self, $class;
  391. $self->parse($arg) if defined($arg);
  392. return $self;
  393. }
  394. sub parse {
  395. my ($self, $dep) = @_;
  396. return if not $dep =~
  397. /^\s* # skip leading whitespace
  398. ([a-zA-Z0-9][a-zA-Z0-9+.-]*) # package name
  399. (?: # start of optional part
  400. \s* \( # open parenthesis for version part
  401. \s* (<<|<=|=|>=|>>|<|>) # relation part
  402. \s* (.*?) # do not attempt to parse version
  403. \s* \) # closing parenthesis
  404. )? # end of optional part
  405. (?: # start of optional architecture
  406. \s* \[ # open bracket for architecture
  407. \s* (.*?) # don't parse architectures now
  408. \s* \] # closing bracket
  409. )? # end of optional architecture
  410. \s*$ # trailing spaces at end
  411. /x;
  412. $self->{package} = $1;
  413. $self->{relation} = version_normalize_relation($2) if defined($2);
  414. if (defined($3)) {
  415. $self->{version} = Dpkg::Version->new($3);
  416. }
  417. if (defined($4)) {
  418. $self->{arches} = [ split(/\s+/, $4) ];
  419. }
  420. }
  421. sub output {
  422. my ($self, $fh) = @_;
  423. my $res = $self->{package};
  424. if (defined($self->{relation})) {
  425. $res .= " (" . $self->{relation} . " " . $self->{version} . ")";
  426. }
  427. if (defined($self->{'arches'})) {
  428. $res .= " [" . join(" ", @{$self->{arches}}) . "]";
  429. }
  430. if (defined($fh)) {
  431. print $fh $res;
  432. }
  433. return $res;
  434. }
  435. # Returns true if the dependency in parameter can deduced from the current
  436. # dependency. Returns false if it can be negated. Returns undef if nothing
  437. # can be concluded.
  438. sub implies {
  439. my ($self, $o) = @_;
  440. if ($o->isa('Dpkg::Deps::Simple')) {
  441. # An implication is only possible on the same package
  442. return undef if $self->{package} ne $o->{package};
  443. # Our architecture set must be a superset of the architectures for
  444. # o, otherwise we can't conclude anything.
  445. return undef unless Dpkg::Deps::_arch_is_superset($self->{arches}, $o->{arches});
  446. # If o has no version clause, then our dependency is stronger
  447. return 1 if not defined $o->{relation};
  448. # If o has a version clause, we must also have one, otherwise there
  449. # can't be an implication
  450. return undef if not defined $self->{relation};
  451. return Dpkg::Deps::deps_eval_implication($self->{relation},
  452. $self->{version}, $o->{relation}, $o->{version});
  453. } elsif ($o->isa('Dpkg::Deps::AND')) {
  454. # TRUE: Need to imply all individual elements
  455. # FALSE: Need to NOT imply at least one individual element
  456. my $res = 1;
  457. foreach my $dep ($o->get_deps()) {
  458. my $implication = $self->implies($dep);
  459. unless (defined($implication) && $implication == 1) {
  460. $res = $implication;
  461. last if defined $res;
  462. }
  463. }
  464. return $res;
  465. } elsif ($o->isa('Dpkg::Deps::OR')) {
  466. # TRUE: Need to imply at least one individual element
  467. # FALSE: Need to not apply all individual elements
  468. # UNDEF: The rest
  469. my $res = undef;
  470. foreach my $dep ($o->get_deps()) {
  471. my $implication = $self->implies($dep);
  472. if (defined($implication)) {
  473. if (not defined $res) {
  474. $res = $implication;
  475. } else {
  476. if ($implication) {
  477. $res = 1;
  478. } else {
  479. $res = 0;
  480. }
  481. }
  482. last if defined($res) && $res == 1;
  483. }
  484. }
  485. return $res;
  486. } else {
  487. internerr("Dpkg::Deps::Simple can't evaluate implication with a %s!",
  488. ref($o));
  489. }
  490. }
  491. sub get_deps {
  492. my $self = shift;
  493. return $self;
  494. }
  495. sub sort {
  496. # Nothing to sort
  497. }
  498. sub arch_is_concerned {
  499. my ($self, $host_arch) = @_;
  500. return 0 if not defined $self->{package}; # Empty dep
  501. return 1 if not defined $self->{arches}; # Dep without arch spec
  502. my $seen_arch = 0;
  503. foreach my $arch (@{$self->{arches}}) {
  504. $arch=lc($arch);
  505. if ($arch =~ /^!/) {
  506. my $not_arch = $arch;
  507. $not_arch =~ s/^!//;
  508. if (debarch_is($host_arch, $not_arch)) {
  509. $seen_arch = 0;
  510. last;
  511. } else {
  512. # !arch includes by default all other arches
  513. # unless they also appear in a !otherarch
  514. $seen_arch = 1;
  515. }
  516. } elsif (debarch_is($host_arch, $arch)) {
  517. $seen_arch = 1;
  518. last;
  519. }
  520. }
  521. return $seen_arch;
  522. }
  523. sub reduce_arch {
  524. my ($self, $host_arch) = @_;
  525. if (not $self->arch_is_concerned($host_arch)) {
  526. $self->{package} = undef;
  527. $self->{relation} = undef;
  528. $self->{version} = undef;
  529. $self->{arches} = undef;
  530. } else {
  531. $self->{arches} = undef;
  532. }
  533. }
  534. sub get_evaluation {
  535. my ($self, $facts) = @_;
  536. return undef if not defined $self->{package};
  537. my ($check, $param) = $facts->check_package($self->{package});
  538. if ($check) {
  539. if (defined $self->{relation}) {
  540. if (ref($param)) {
  541. # Provided packages
  542. # XXX: Once support for versioned provides is in place,
  543. # this part must be adapted
  544. return 0;
  545. } else {
  546. if (defined($param)) {
  547. if (version_compare_relation($param, $self->{relation},
  548. $self->{version})) {
  549. return 1;
  550. } else {
  551. return 0;
  552. }
  553. } else {
  554. return undef;
  555. }
  556. }
  557. } else {
  558. return 1;
  559. }
  560. }
  561. return 0;
  562. }
  563. sub simplify_deps {
  564. my ($self, $facts) = @_;
  565. my $eval = $self->get_evaluation($facts);
  566. if (defined($eval) and $eval == 1) {
  567. $self->{package} = undef;
  568. $self->{relation} = undef;
  569. $self->{version} = undef;
  570. $self->{arches} = undef;
  571. }
  572. }
  573. sub is_empty {
  574. my $self = shift;
  575. return not defined $self->{package};
  576. }
  577. sub merge_union {
  578. my ($self, $o) = @_;
  579. return 0 if not $o->isa('Dpkg::Deps::Simple');
  580. return 0 if $self->is_empty() or $o->is_empty();
  581. return 0 if $self->{package} ne $o->{package};
  582. return 0 if defined $self->{arches} or defined $o->{arches};
  583. if (not defined $o->{relation} and defined $self->{relation}) {
  584. # Union is the non-versioned dependency
  585. $self->{relation} = undef;
  586. $self->{version} = undef;
  587. return 1;
  588. }
  589. my $implication = $self->implies($o);
  590. my $rev_implication = $o->implies($self);
  591. if (defined($implication)) {
  592. if ($implication) {
  593. $self->{relation} = $o->{relation};
  594. $self->{version} = $o->{version};
  595. return 1;
  596. } else {
  597. return 0;
  598. }
  599. }
  600. if (defined($rev_implication)) {
  601. if ($rev_implication) {
  602. # Already merged...
  603. return 1;
  604. } else {
  605. return 0;
  606. }
  607. }
  608. return 0;
  609. }
  610. package Dpkg::Deps::Multiple;
  611. =head2 Dpkg::Deps::Multiple
  612. This the base class for Dpkg::Deps::{AND,OR,Union}. It contains the
  613. =over 4
  614. =item $mul->add($dep)
  615. Add a new dependency object at the end of the list.
  616. =back
  617. =cut
  618. use strict;
  619. use warnings;
  620. use Dpkg::ErrorHandling;
  621. use overload '""' => sub { $_[0]->output() };
  622. sub new {
  623. my $this = shift;
  624. my $class = ref($this) || $this;
  625. my $self = { 'list' => [ @_ ] };
  626. bless $self, $class;
  627. return $self;
  628. }
  629. sub add {
  630. my $self = shift;
  631. push @{$self->{list}}, @_;
  632. }
  633. sub get_deps {
  634. my $self = shift;
  635. return grep { not $_->is_empty() } @{$self->{list}};
  636. }
  637. sub sort {
  638. my $self = shift;
  639. my @res = ();
  640. @res = sort { Dpkg::Deps::deps_compare($a, $b) } @{$self->{list}};
  641. $self->{list} = [ @res ];
  642. }
  643. sub arch_is_concerned {
  644. my ($self, $host_arch) = @_;
  645. my $res = 0;
  646. foreach my $dep (@{$self->{list}}) {
  647. $res = 1 if $dep->arch_is_concerned($host_arch);
  648. }
  649. return $res;
  650. }
  651. sub reduce_arch {
  652. my ($self, $host_arch) = @_;
  653. my @new;
  654. foreach my $dep (@{$self->{list}}) {
  655. $dep->reduce_arch($host_arch);
  656. push @new, $dep if $dep->arch_is_concerned($host_arch);
  657. }
  658. $self->{list} = [ @new ];
  659. }
  660. sub is_empty {
  661. my $self = shift;
  662. return scalar @{$self->{list}} == 0;
  663. }
  664. sub merge_union {
  665. internerr("The method merge_union() is only valid for Dpkg::Deps::Simple");
  666. }
  667. package Dpkg::Deps::AND;
  668. =head2 Dpkg::Deps::AND
  669. This object represents a list of dependencies who must be met at the same
  670. time.
  671. =over 4
  672. =item $and->output([$fh])
  673. The output method uses ", " to join the list of sub-dependencies.
  674. =back
  675. =cut
  676. use strict;
  677. use warnings;
  678. use base qw(Dpkg::Deps::Multiple);
  679. sub output {
  680. my ($self, $fh) = @_;
  681. my $res = join(", ", map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  682. if (defined($fh)) {
  683. print $fh $res;
  684. }
  685. return $res;
  686. }
  687. sub implies {
  688. my ($self, $o) = @_;
  689. # If any individual member can imply $o or NOT $o, we're fine
  690. foreach my $dep ($self->get_deps()) {
  691. my $implication = $dep->implies($o);
  692. return 1 if defined($implication) && $implication == 1;
  693. return 0 if defined($implication) && $implication == 0;
  694. }
  695. # If o is an AND, we might have an implication, if we find an
  696. # implication within us for each predicate in o
  697. if ($o->isa('Dpkg::Deps::AND')) {
  698. my $subset = 1;
  699. foreach my $odep ($o->get_deps()) {
  700. my $found = 0;
  701. foreach my $dep ($self->get_deps()) {
  702. $found = 1 if $dep->implies($odep);
  703. }
  704. $subset = 0 if not $found;
  705. }
  706. return 1 if $subset;
  707. }
  708. return undef;
  709. }
  710. sub get_evaluation {
  711. my ($self, $facts) = @_;
  712. # Return 1 only if all members evaluates to true
  713. # Return 0 if at least one member evaluates to false
  714. # Return undef otherwise
  715. my $result = 1;
  716. foreach my $dep ($self->get_deps()) {
  717. my $eval = $dep->get_evaluation($facts);
  718. if (not defined $eval) {
  719. $result = undef;
  720. } elsif ($eval == 0) {
  721. $result = 0;
  722. last;
  723. } elsif ($eval == 1) {
  724. # Still possible
  725. }
  726. }
  727. return $result;
  728. }
  729. sub simplify_deps {
  730. my ($self, $facts, @knowndeps) = @_;
  731. my @new;
  732. WHILELOOP:
  733. while (@{$self->{list}}) {
  734. my $dep = shift @{$self->{list}};
  735. my $eval = $dep->get_evaluation($facts);
  736. next if defined($eval) and $eval == 1;
  737. foreach my $odep (@knowndeps, @new) {
  738. next WHILELOOP if $odep->implies($dep);
  739. }
  740. # When a dependency is implied by another dependency that
  741. # follows, then invert them
  742. # "a | b, c, a" becomes "a, c" and not "c, a"
  743. my $i = 0;
  744. foreach my $odep (@{$self->{list}}) {
  745. if (defined $odep and $odep->implies($dep)) {
  746. splice @{$self->{list}}, $i, 1;
  747. unshift @{$self->{list}}, $odep;
  748. next WHILELOOP;
  749. }
  750. $i++;
  751. }
  752. push @new, $dep;
  753. }
  754. $self->{list} = [ @new ];
  755. }
  756. package Dpkg::Deps::OR;
  757. =head2 Dpkg::Deps::OR
  758. This object represents a list of dependencies of which only one must be met
  759. for the dependency to be true.
  760. =over 4
  761. =item $or->output([$fh])
  762. The output method uses " | " to join the list of sub-dependencies.
  763. =back
  764. =cut
  765. use strict;
  766. use warnings;
  767. use base qw(Dpkg::Deps::Multiple);
  768. sub output {
  769. my ($self, $fh) = @_;
  770. my $res = join(" | ", map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  771. if (defined($fh)) {
  772. print $fh $res;
  773. }
  774. return $res;
  775. }
  776. sub implies {
  777. my ($self, $o) = @_;
  778. # Special case for AND with a single member, replace it by its member
  779. if ($o->isa('Dpkg::Deps::AND')) {
  780. my @subdeps = $o->get_deps();
  781. if (scalar(@subdeps) == 1) {
  782. $o = $subdeps[0];
  783. }
  784. }
  785. # In general, an OR dependency can't imply anything except if each
  786. # of its member implies a member in the other OR dependency
  787. if ($o->isa('Dpkg::Deps::OR')) {
  788. my $subset = 1;
  789. foreach my $dep ($self->get_deps()) {
  790. my $found = 0;
  791. foreach my $odep ($o->get_deps()) {
  792. $found = 1 if $dep->implies($odep);
  793. }
  794. $subset = 0 if not $found;
  795. }
  796. return 1 if $subset;
  797. }
  798. return undef;
  799. }
  800. sub get_evaluation {
  801. my ($self, $facts) = @_;
  802. # Returns false if all members evaluates to 0
  803. # Returns true if at least one member evaluates to true
  804. # Returns undef otherwise
  805. my $result = 0;
  806. foreach my $dep ($self->get_deps()) {
  807. my $eval = $dep->get_evaluation($facts);
  808. if (not defined $eval) {
  809. $result = undef;
  810. } elsif ($eval == 1) {
  811. $result = 1;
  812. last;
  813. } elsif ($eval == 0) {
  814. # Still possible to have a false evaluation
  815. }
  816. }
  817. return $result;
  818. }
  819. sub simplify_deps {
  820. my ($self, $facts) = @_;
  821. my @new;
  822. WHILELOOP:
  823. while (@{$self->{list}}) {
  824. my $dep = shift @{$self->{list}};
  825. my $eval = $dep->get_evaluation($facts);
  826. if (defined($eval) and $eval == 1) {
  827. $self->{list} = [];
  828. return;
  829. }
  830. foreach my $odep (@new, @{$self->{list}}) {
  831. next WHILELOOP if $odep->implies($dep);
  832. }
  833. push @new, $dep;
  834. }
  835. $self->{list} = [ @new ];
  836. }
  837. package Dpkg::Deps::Union;
  838. =head2 Dpkg::Deps::Union
  839. This object represents a list of relationships.
  840. =over 4
  841. =item $union->output([$fh])
  842. The output method uses ", " to join the list of relationships.
  843. =item $union->implies($other_dep)
  844. =item $union->get_evaluation($other_dep)
  845. Those methods are not meaningful for this object and always return undef.
  846. =item $union->simplify_deps($facts)
  847. The simplication is done to generate an union of all the relationships.
  848. It uses $simple_dep->merge_union($other_dep) to get the its job done.
  849. =back
  850. =cut
  851. use strict;
  852. use warnings;
  853. use base qw(Dpkg::Deps::Multiple);
  854. sub output {
  855. my ($self, $fh) = @_;
  856. my $res = join(", ", map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  857. if (defined($fh)) {
  858. print $fh $res;
  859. }
  860. return $res;
  861. }
  862. sub implies {
  863. # Implication test are not useful on Union
  864. return undef;
  865. }
  866. sub get_evaluation {
  867. # Evaluation are not useful on Union
  868. return undef;
  869. }
  870. sub simplify_deps {
  871. my ($self, $facts) = @_;
  872. my @new;
  873. WHILELOOP:
  874. while (@{$self->{list}}) {
  875. my $dep = shift @{$self->{list}};
  876. foreach my $odep (@new) {
  877. next WHILELOOP if $dep->merge_union($odep);
  878. }
  879. push @new, $dep;
  880. }
  881. $self->{list} = [ @new ];
  882. }
  883. package Dpkg::Deps::KnownFacts;
  884. =head2 Dpkg::Deps::KnowFacts
  885. This object represents a list of installed packages and a list of virtual
  886. packages provided (by the set of installed packages).
  887. =over 4
  888. =item my $facts = Dpkg::Deps::KnownFacts->new();
  889. Create a new object.
  890. =cut
  891. use strict;
  892. use warnings;
  893. sub new {
  894. my $this = shift;
  895. my $class = ref($this) || $this;
  896. my $self = { 'pkg' => {}, 'virtualpkg' => {} };
  897. bless $self, $class;
  898. return $self;
  899. }
  900. =item $facts->add_installed_package($package, $version)
  901. Record that the given version of the package is installed. If $version is
  902. undefined we know that the package is installed but we don't know which
  903. version it is.
  904. =cut
  905. sub add_installed_package {
  906. my ($self, $pkg, $ver) = @_;
  907. $self->{pkg}{$pkg} = $ver;
  908. }
  909. =item $facts->add_provided_package($virtual, $relation, $version, $by)
  910. Record that the "$by" package provides the $virtual package. $relation
  911. and $version correspond to the associated relation given in the Provides
  912. field. This might be used in the future for versioned provides.
  913. =cut
  914. sub add_provided_package {
  915. my ($self, $pkg, $rel, $ver, $by) = @_;
  916. if (not exists $self->{virtualpkg}{$pkg}) {
  917. $self->{virtualpkg}{$pkg} = [];
  918. }
  919. push @{$self->{virtualpkg}{$pkg}}, [ $by, $rel, $ver ];
  920. }
  921. =item my ($check, $param) = $facts->check_package($package)
  922. $check is one when the package is found. For a real package, $param
  923. contains the version. For a virtual package, $param contains an array
  924. reference containing the list of packages that provide it (each package is
  925. listed as [ $provider, $relation, $version ]).
  926. =back
  927. =cut
  928. sub check_package {
  929. my ($self, $pkg) = @_;
  930. if (exists $self->{pkg}{$pkg}) {
  931. return (1, $self->{pkg}{$pkg});
  932. }
  933. if (exists $self->{virtualpkg}{$pkg}) {
  934. return (1, $self->{virtualpkg}{$pkg});
  935. }
  936. return (0, undef);
  937. }
  938. 1;