Deps.pm 40 KB

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