Skip to content
Snippets Groups Projects
Commit fca87f70 authored by Ryan Gonzalez's avatar Ryan Gonzalez
Browse files

dh_setup_copyright: Copy in copyright information from external sources

This will copy the copyright and copyright_report files for any packages
whose files are found inside the resulting binaries, making them
available at `/usr/share/doc/PACKAGE/PACKAGE_external_copyrights_ARCH`.

https://phabricator.apertis.org/T8554



Signed-off-by: default avatarRyan Gonzalez <ryan.gonzalez@collabora.com>
parent e02b550f
No related branches found
No related tags found
3 merge requests!21Merge changes from apertis/v2022-updates into apertis/v2022,!20Merge updates from release v2023dev3,!18dh_dwarf2sources: Copy in copyright information from external sources
......@@ -10,7 +10,9 @@ use strict;
use warnings;
use File::Find;
use File::Basename;
use File::Copy;
use File::Path;
use JSON::PP;
use Debian::Debhelper::Dh_Lib;
our $VERSION = DH_BUILTIN_VERSION;
......@@ -22,7 +24,8 @@ B<dh_setup_copyright> [S<I<debhelper options>>] [B<-X>I<item>] [S<B<--> I<params
=head1 DESCRIPTION
B<dh_setup_copyright> is a debhelper program that generates the list for source
file names used to build every binary. This list is extracted from DWARF debug
file names used to build every binary and pulls the licenses from any sources
part of other packages. The source file list is extracted from DWARF debug
information by running L<dwarf2sources(1)> on every ELF binaries in the package
and saving the output to /usr/share/doc/<package>.
......@@ -79,6 +82,72 @@ sub extract_filenames {
doit_noerror({chdir => $tmpfolder}, "dwarf2sources", "-o", $fname_list, @binaries);
}
sub find_package_providing_path {
my ($path) = @_;
my ($package) = `dpkg-query -S "$path" 2>/dev/null` =~ /^([^\s:]+):(?:[^\s:]+:)? /;
$package;
}
sub copy_external_package_copyright {
my ($fname_external_dir, $package) = @_;
print "Importing copyright from: $package\n";
my @report_filenames = (
'copyright', 'copyright.gz',
'copyright_report', 'copyright_report.gz',
);
for my $filename (@report_filenames) {
my $src = "/usr/share/doc/$package/$filename";
my $dst = "$fname_external_dir/$package.$filename";
next if ! -e $src || -e $dst;
mkpath $fname_external_dir;
copy $src, $dst;
}
}
sub copy_external_source_copyrights {
my ($fname_list, $fname_external_dir, $metadata) = @_;
print "Scanning external source files\n";
open my $sources_handle, '<', $fname_list or die "Failed to open $fname_list: $!";
my $sources_data = do { local $/; <$sources_handle> };
close $sources_handle;
my %index = ();
my %unknown_files = ();
my $sources = decode_json $sources_data;
while (my ($file, $units) = each %$sources) {
foreach my $unit (@{$units->{'units'}}) {
my $comp_name = $unit->{'comp_name'};
if ($comp_name =~ /^\//) {
# Absolute path means it was likely a globally installed source file,
# if so, find its copyright and install it.
next if defined $index{$comp_name} || defined $unknown_files{$comp_name};
my $package = find_package_providing_path $comp_name;
if (!defined $package) {
$unknown_files{$comp_name} = 1;
next;
}
$index{$comp_name} = $package;
copy_external_package_copyright $fname_external_dir, $package;
}
}
}
if (%index) {
print "Adding metadata for @{[scalar %index]} external source file(s)\n";
$metadata->{'external_sources_to_packages'} = \%index;
}
}
for my $package (@{$dh{DOPACKAGES}}) {
my $tmp = tmpdir($package);
my @binaries;
......@@ -92,6 +161,9 @@ for my $package (@{$dh{DOPACKAGES}}) {
}
my $arch = package_binary_arch($package);
my $fname_list = "$fname_dir/" . $package . "_bin2sources_" . $arch . ".json";
my $fname_external_dir = "$fname_dir/external_copyrights";
my $fname_metadata = "$fname_dir/${package}_metadata_${arch}.json";
print "Checking file $fname_list\n";
if (-e "$tmp/$fname_list") {
print "File $tmp/$fname_list already exists, skipping\n";
......@@ -124,7 +196,16 @@ for my $package (@{$dh{DOPACKAGES}}) {
mkpath("$tmp/$fname_dir");
}
my %metadata = ();
extract_filenames($fname_list, $tmp, @binaries);
copy_external_source_copyrights("$tmp/$fname_list", "$tmp/$fname_external_dir", \%metadata);
if (%metadata) {
open my $meta_handle, '>', "$tmp/$fname_metadata" or die "Failed to open $fname_metadata: $!";
print $meta_handle encode_json(\%metadata);
close $meta_handle;
}
}
=head1 SEE ALSO
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment