Deps.pm 29 KB

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