Deps.pm 29 KB

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