Deps.pm 35 KB

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