Deps.pm 42 KB

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