Deps.pm 29 KB

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