Deps.pm 40 KB

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