Deps.pm 30 KB

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