Deps.pm 30 KB

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