Deps.pm 29 KB

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