Deps.pm 31 KB

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