Deps.pm 40 KB

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