Deps.pm 31 KB

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