Deps.pm 36 KB

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