Deps.pm 35 KB

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