Debian Codegraph @

dh_sameversiondep

raw
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2010 Modestas Vainius <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>
17
18 =head1 NAME
19
20 dh_sameversiondep - generate versioned dependency based on the versioned
21 dependencies of the reference package.
22
23 =head1 SYNOPSIS
24
25 B<dh_sameversiondep> [S<I<debhelper options>>]
26
27 =head1 DESCRIPTION
28
29 B<dh_sameversiondep> is a helper tool which is able to generate a dependency
30 that is versioned the same way as a dependency (coming from the same source) of
31 another reference package. B<dh_sameversiondep> scans debian/control for the
32 specially formatted substvar (see L</sameVersionDep substvar SPECIFICATION>
33 section below), determines its value and writes it to
34 F<debian/package.substvars> file.
35
36 The most common use case for B<dh_sameversiondep> tool is to keep your
37 I<liba-dev> package dependency on the external I<libb-dev> package as tight as
38 your I<liba> package depends on the respective external I<libb> package (see
39 L</EXAMPLE> section for more information about this use case).
40
41 B<dh_sameversiondep> works as follows:
42
43 =over 4
44
45 =item *
46
47 Searches for the B<sameVersionDep> substvar in the Depends, Recommends,
48 Suggests, Enhances and Pre-Depends fields of the requested packages. When one
49 is found, it is parsed and the I<dependency package> name, I<reference package>
50 name and I<dependency type> (either Depends or Recommends etc.) are determined.
51
52 =item *
53
54 All dependencies of the requested I<type> are collected for the I<dependency
55 package> based on the I<dpkg-query --status> output. If a multi-arch aware dpkg
56 is detected, this query is architecture-qualified as needed.
57
58 =item *
59
60 All dependencies of the requested I<type> are collected for the I<reference
61 package> either from F<debian/control> (substvars are expanded) or from
62 I<dpkg-query --status> output if the package was not found in
63 F<debian/control>.
64
65 =item *
66
67 Both collections are intersected leaving only common packages in both
68 collections.
69
70 =item *
71
72 Common package list is filtered by leaving only those which come from the same
73 source as I<dependency package>.
74
75 =item *
76
77 Whatever packages are left (most likely only one), their names are replaced
78 with I<dependency package> name preserving all versioning information. This
79 result is written to F<debian/package.substvars> file as a value of the
80 B<sameVersionDep> substvar being processed.
81
82 =back
83
84 B<dh_sameversiondep> is very strict about errors. If either I<dependency
85 package> or the I<reference package> cannot be found or the resulting
86 dependency set is empty, it will fail with an error.
87
88 B<dh_sameversiondep> MUST be run before L<dh_gencontrol>. However, it is
89 recommended to run B<dh_sameversiondep> after L<dh_shlibdeps>.
90
91 =head1 sameVersionDep substvar SPECIFICATION
92
93 B<sameVersionDep> substvar can appear in either Depends, Recommends, Suggests,
94 Enhances or Pre-Depends field of any binary package. The field, which the
95 substvar appears in, becomes the default I<dependency type> for that substvar.
96 B<sameVersionDep> should be formatted as follows (everything is case sensitive):
97
98 S<< ${B<sameVersionDep>:I<dependency>[[:I<reference>]-I<dependency type>]} >>
99
100 =over 4
101
102 =item I<dependency> (mandatory)
103
104 The name of the package which you want to add as a dependency.
105
106 =item I<reference> (optional)
107
108 The name of the package which dependencies are to be intersected with the
109 dependencies of the I<dependency> package. Defaults to the first package in
110 debian/control if omitted.
111
112 =item I<dependency type> (optional)
113
114 Can be either Depends, Recommends, Suggests, Enhances or Pre-Depends. Defaults
115 to the name of the field which the substvar was found in. Specifies which type
116 of dependencies to consider when analyzing I<dependency> package and
117 I<reference> package.
118
119 =back
120
121 =head1 EXAMPLE
122
123 Assume we have the following in F<debian/control>:
124
125 Package: liba
126 Depends: libc (>= 0.1), depa, depb, depc
127
128 Package: libb
129 Depends: libd (>= 0.2), depd, depe, depf
130
131 Package: libab-dev
132 Depends: ${sameVersionDep:libc-dev}, ${sameVersionDep:libd-dev:libb}
133
134 Assuming that libc and libc-dev (both from the same source), as well as libd
135 and libd-dev (both from the same source) are installed, the value of
136 C<sameVersionDep:libc-dev> will be I<< libc-dev (>= 0.1) >> and the value of
137 C<sameVersionDep:libd-dev:libb> will be I<< libd-dev (>= 0.2) >>.
138
139 C<sameVersionDep:libc-dev> could also be written as
140 C<sameVersionDep:libc-dev:liba-Depends> and C<sameVersionDep:libd-dev:libb> as
141 C<sameVersionDep:libd-dev:libb-Depends> but it is not necessary because
142 defaults are sufficient.
143
144 =head1 SEE ALSO
145
146 L<debhelper(7)>
147
148 =head1 AUTHOR
149
150 Modestas Vainius <[email protected]>
151
152 =cut
153
154 use strict;
155 use warnings;
156
157 use Dpkg::Control::Info;
158 use Dpkg::Substvars;
159 use Dpkg::ErrorHandling ();
160 use File::Copy;
161 use POSIX ":sys_wait_h";
162
163 use Debian::Debhelper::Dh_Lib;
164
165 my $namespace = "sameVersionDep";
166 my @fields = qw(Depends Recommends Suggests Enhances Pre-Depends);
167 my $re_fields = join("|", @fields);
168 my $re_pkgname = qr/[a-z0-9][a-z0-9+.-]*/;
169 my $re_oursubstvar = qr/\$\{($namespace:(.*?))\}/;
170 my $re_splitsubstvar = qr/^($re_pkgname)(?::($re_pkgname))?(?:-($re_fields))?$/;
171
172 # Global substvars file
173 my $g_substvars = new Dpkg::Substvars;
174 $g_substvars->load("debian/substvars") if (-r "debian/substvars");
175
176 my $MULTIARCH_ARCH;
177
178 sub extract_package_names {
179 my $val = shift;
180 $val =~ s/\([^)]+\)//g;
181 $val =~ s/^\s+//;
182 $val =~ s/\s+$//;
183 return split(/\s*,\s*/, $val);
184 }
185
186 sub extract_deps {
187 my ($val, $deppkg) = @_;
188
189 # Extract dependency fields we need
190 my @matched_deps;
191 for my $dep (split(/\s*,\s*/, $val)) {
192 if ($dep =~ /^\Q$deppkg\E(?:$|[\W])/) {
193 push @matched_deps, $dep;
194 }
195 }
196 return @matched_deps;
197 }
198
199 sub Shlibsvars::new {
200 my ($cls, $package, $control, $substvars_file) = @_;
201 my $self = bless ( {
202 "package" => $package,
203 "control" => $control,
204 "file" => $substvars_file,
205 }, $cls);
206 $self->{substvars} = new Dpkg::Substvars;
207 if (-r $self->{file}) {
208 $self->{substvars}->load($self->{file});
209 }
210 return $self;
211 }
212
213 sub Shlibsvars::get_fieldval {
214 my ($self, $field) = @_;
215
216 my $pkg = $self->{control}->get_pkg_by_name($self->{package});
217 return undef if (!defined $pkg || !exists $pkg->{$field});
218
219 # Turn of warnings for substvars runs
220 Dpkg::ErrorHandling::report_options(quiet_warnings => 1);
221
222 my $val = $pkg->{$field};
223 $val = $self->{substvars}->substvars($val);
224 $val = $g_substvars->substvars($val);
225
226 Dpkg::ErrorHandling::report_options(quiet_warnings => 0);
227 return $val;
228 }
229
230 sub Shlibsvars::extract_deps {
231 my ($self, $field, $deppkg) = @_;
232
233 my $val = $self->get_fieldval($field);
234 return undef() unless defined $val;
235 return extract_deps($val, $deppkg);
236 }
237
238 sub Shlibsvars::get_dep_package_names {
239 my ($self, $field) = @_;
240
241 my $val = $self->get_fieldval($field);
242 return undef() unless defined $val;
243 return extract_package_names($val);
244 }
245
246 sub supports_multiarch {
247 if (!defined $MULTIARCH_ARCH) {
248 my $multiarch_assert = system('dpkg', '--assert-multi-arch');
249 if ($multiarch_assert == 0) {
250 $MULTIARCH_ARCH = dpkg_architecture_value('DEB_HOST_ARCH');
251 } else {
252 $MULTIARCH_ARCH = ""; # empty indicates no multiarch support
253 }
254 }
255 return $MULTIARCH_ARCH ne "";
256 }
257
258 sub arch_qualify {
259 if (supports_multiarch()) {
260 return map { "$_:$MULTIARCH_ARCH" } @_;
261 } else {
262 return @_;
263 }
264 }
265
266 sub execute_dpkg_status {
267 my $binpkgs = shift;
268 my $fields = shift;
269 $fields = [ "Source", "Version" ] unless defined $fields;
270 my $status = shift;
271
272 my $regexp_fields = join("|", @$fields);
273
274 my $pid = open(DPKG, "-|");
275 error("cannot fork for dpkg-query --status") unless defined($pid);
276 if (!$pid) {
277 # Child process running dpkg-query --status and discarding errors
278 close STDERR;
279 open STDERR, ">", "/dev/null";
280 $ENV{LC_ALL} = "C";
281 exec("dpkg-query", "--status", "--", @$binpkgs) or error("cannot exec dpkg-query");
282 }
283 my $curpkg;
284 while (defined($_ = <DPKG>)) {
285 if (m/^Package:\s*(.*)$/) {
286 $curpkg = $1;
287 $status->{$curpkg} = {};
288 } elsif (defined($curpkg)) {
289 if (m/^($regexp_fields):\s*(.*)$/) {
290 my $field = $1;
291 error("dublicate field $field for the $curpkg package in the dpkg status file")
292 if (exists $status->{$curpkg}{$field});
293 $status->{$curpkg}{$field} = $2;
294 }
295 }
296 }
297 close(DPKG);
298
299 my $exit_code = $?;
300 if (WIFEXITED($exit_code)) {
301 return WEXITSTATUS($exit_code);
302 } else {
303 error("failed to terminate dpkg --status process PID $pid");
304 }
305 }
306
307 sub get_package_dpkg_status {
308 my $binpkgs = shift;
309 my $fields = shift;
310 my %status;
311
312 my $exit_code = execute_dpkg_status($binpkgs, $fields, \%status);
313 if ($exit_code == 2 && supports_multiarch()) {
314 # Most likely dpkg-query --status failed due to some of package names
315 # being ambiguous on this system. So just requery them using
316 # arch-qualified syntax.
317 # NOTE: we have to query twice since arch-qualified syntax won't find
318 # arch:all packages.
319 my @ambiguous_pkgs = arch_qualify(grep { ! exists $status{$_} } @$binpkgs);
320 if (@ambiguous_pkgs) {
321 execute_dpkg_status(\@ambiguous_pkgs, $fields, \%status);
322 }
323 }
324 return \%status;
325 }
326
327 sub write_substvar($$$$) {
328 my ($pkgname, $varname, $value, $substvars) = @_;
329 my @contents;
330 my $varset = 0;
331
332 my $file = (-r $substvars) ? $substvars : "debian/substvars";
333 if (-r $file) {
334 open(FILE, "<$file") or die "Unable to open substvars file '$file' for reading\n";
335 while (<FILE>) {
336 if (!$varset && /^\s*\Q$varname=\E/) {
337 push @contents, "$varname=$value\n";
338 $varset = 1;
339 } else {
340 push @contents, $_;
341 }
342 }
343 close(FILE);
344 } else {
345 # Fallback to default
346 $file = $substvars;
347 }
348
349 open(FILE, ">$file.tmp") or die "Unable to open substvars file '$file.tmp' for writing\n";
350 for (@contents) {
351 print FILE $_;
352 }
353 if (!$varset) {
354 print FILE "$varname=$value", "\n";
355 }
356 close(FILE);
357
358 File::Copy::move("$file.tmp", "$file");
359 }
360
361 init();
362
363 my $control = Dpkg::Control::Info->new();
364 my %shlibsvars;
365
366 foreach my $package (@{$dh{DOPACKAGES}}) {
367 my $pkg_substvars = sprintf("debian/%ssubstvars", pkgext($package));
368 my $pkg = $control->get_pkg_by_name($package);
369
370 for my $fieldname (@fields) {
371 if (exists $pkg->{$fieldname}) {
372 my $fieldval = $pkg->{$fieldname};
373 my $pkgname = $pkg->{Package};
374
375 while ($fieldval =~ m/\G.*?$re_oursubstvar/gs) {
376 my $varname = $1;
377 my $varparams = $2;
378 if ($varparams =~ m/$re_splitsubstvar/) {
379 my $dep2add = $1;
380
381 # Scan package default to MAINPACKAGE.
382 my $refpkg = $2;
383 $refpkg = $dh{MAINPACKAGE} unless defined $refpkg;
384
385 my $deptype = $3;
386 $deptype = $fieldname unless defined $deptype;
387
388 # Initialize some dep2add and refpkg data.
389 # refpkg might also come from external source. Use dpkg-query
390 # to get its dpkg status then.
391 my $refpkg_status;
392 my $dep2add_status;
393 my $vars;
394 if ($control->get_pkg_by_name($refpkg)) {
395 if (!exists $shlibsvars{$refpkg}) {
396 my $refpkg_substvars = sprintf("debian/%ssubstvars", pkgext($refpkg));
397 $shlibsvars{$refpkg} = new Shlibsvars($refpkg, $control, $refpkg_substvars);
398 }
399 $vars = $shlibsvars{$refpkg};
400
401 $dep2add_status = get_package_dpkg_status( [ $dep2add ], [ "Source", "Version", $deptype ] );
402 } else {
403 my $status = get_package_dpkg_status( [ $refpkg, $dep2add ], [ "Source", "Version", $deptype ] );
404 error("cannot continue because the reference package $refpkg could not be found in debian/control or dpkg status")
405 unless (exists $status->{$refpkg});
406 error("cannot continue because package $refpkg has no $deptype field to scan")
407 unless (exists $status->{$refpkg}{$deptype});
408 $refpkg_status = $status->{$refpkg};
409
410 $dep2add_status = $status; # see code below
411 }
412
413 ##### Process and verify dep2add status #####
414 error("cannot continue because package $dep2add could not be found in dpkg status")
415 unless (exists $dep2add_status->{$dep2add});
416 $dep2add_status = $dep2add_status->{$dep2add};
417
418 # If the source is named the same as the binary package, there
419 # will be no Source field. Use package name as Source then.
420 $dep2add_status->{Source} = $dep2add
421 unless ($dep2add_status->{Source});
422
423 # Check validility of dep2add status
424 error("could not retreive source package name for $dep2add package. Is it installed?")
425 unless exists $dep2add_status->{Source} && exists $dep2add_status->{Version};
426 error("package $dep2add has no $deptype field. This configuration is unsupported. ")
427 unless exists $dep2add_status->{$deptype};
428 my @dep2add_deps = extract_package_names($dep2add_status->{$deptype});
429
430 # Get deptype packages of refpkg
431 my @refpkg_deps;
432 if ($vars) {
433 @refpkg_deps = $vars->get_dep_package_names($deptype);
434 } else {
435 @refpkg_deps = extract_package_names($refpkg_status->{$deptype});
436 }
437 error("cannot continue because package $refpkg has no $deptype field to scan")
438 unless (@refpkg_deps);
439
440 # Intersect both _deps arrays to find common dependencies
441 my @commondeps;
442 {
443 my %_map;
444 map { $_map{$_} = 1; } @refpkg_deps;
445 map { push @commondeps, $_ if exists $_map{$_} } @dep2add_deps;
446 }
447
448 # Get status information about common packages. They need to come from the
449 # same source package as dep2add package and their versions should match
450 my $depstatus = get_package_dpkg_status(\@commondeps, [ "Source", "Version" ]);
451
452 # Check if all packages were found
453 for my $dep (@commondeps) {
454 error("package $dep was not found in the dpkg status. Internal error")
455 unless exists $depstatus->{$dep};
456 }
457
458 # Filter commondeps
459 @commondeps = ();
460 while (my ($pkg, $status) = each(%$depstatus)) {
461 # If the source is named the same as the binary package, there
462 # will be no Source field. Use package name as Source then.
463 $status->{Source} = $pkg unless ($status->{Source});
464
465 push @commondeps, $pkg
466 if (exists $status->{Source} && exists $status->{Version} &&
467 ($status->{Source} eq $dep2add_status->{Source}) &&
468 ($status->{Version} eq $dep2add_status->{Version}));
469 }
470
471 # Ideally we should have got the list down to one. if not, combine
472 # version relationships
473 my @fulldeps;
474 if (!@commondeps) {
475 error("$0: no same version dependencies for '$varname' found (at $fieldname of the $package package)");
476 } else {
477 my $refpkg_deptypeval = ($vars) ? $vars->get_fieldval($deptype) :
478 $refpkg_status->{$deptype};
479 for my $deppkg (@commondeps) {
480 my @full_dep_spec = extract_deps($refpkg_deptypeval, $deppkg);
481 map s/\b\Q$deppkg\E\b/$dep2add/g, @full_dep_spec;
482 push @fulldeps, @full_dep_spec;
483 }
484
485 # Drop dupes
486 @fulldeps = sort @fulldeps;
487 my @uniqdeps;
488 my $_prevdep;
489 for my $dep (@fulldeps) {
490 my $tmp = "$dep";
491 $tmp =~ s/\s//g;
492 push @uniqdeps, $dep if (!defined $_prevdep || $_prevdep ne $tmp);
493 $_prevdep = $tmp;
494 }
495 # Write substvar for the package
496 write_substvar($pkgname, $varname, join(", ", @uniqdeps), $pkg_substvars);
497 }
498 } else {
499 error("invalid '$namespace' substvar syntax: $varparams");
500 }
501 }
502 }
503 }
504 }
505
506 exit 0