Debian Codegraph @

debian/copyright-check

raw
1 #!/bin/sh
2 # Copyright 2020-2023 Jonas Smedegaard <[email protected]>
3 # Copyright 2020-2021 Purism, SPC
4 # Description: helper script to update copyright_hints
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 # Depends:
20 # licensecheck,
21 # libimage-exiftool-perl,
22 # libipc-system-simple-perl,
23 # libpath-tiny-perl,
24 # libregexp-assemble-perl,
25 # perl,
26
27 set -eu
28
29 # resolve file regex from contained license or shebang, or path regex
30 _file_regex() {
31 perl -MGetopt::Long=:config,gnu_getopt -MPath::Tiny -MRegexp::Assemble -MList::Util=any \
32 -e 'GetOptions ( \%opt, "shortname=s@", "grantglob=s@", "regex=s@", "shebang=s@", "nonverb=s@" );'\
33 -e '@section = split /\n\n+/, path("debian/copyright")->slurp_utf8;'\
34 -e 'for $name ( @{ $opt{shortname} } ) {'\
35 -e 'push @license, map { /^License:\h*\Q$name\E\n\h+(\S[^\n]*(?:\n\h+\S[^\n]*)*)/ } @section };'\
36 -e 'for $glob ( @{ $opt{grantglob} } ) {'\
37 -e 'push @files, grep { /^Files:\n?\h(?:\S[^\n]*\n?\h)*\Q$glob\E\s/ } @section };'\
38 -e '@grant = map { /^License(?:-Grant:|:\h*\S[^\n]*)\h*\n\h+(\S[^\n]*(?:\n\h+\S[^\n]*)*)/mg } @files;'\
39 -e '@firstline_re = map { qr/^\Q$_\E/ } @{ $opt{shebang} };'\
40 -e '$nonverb_re = Regexp::Assemble->new->add( "\\W+", map { "\\W+(?:$_)\\W+" } @{ $opt{nonverb} } )->as_string;'\
41 -e '@content_re = map { s/\W+/[**]/g; s/\Q[**]\E\d\Q[**]\E/[**]\\S{0,2}[**]/g; s/\Q[**]\E/$nonverb_re/g; qr/$_/ } @license, @grant;'\
42 -e '$inspect = sub {'\
43 -e 'return if $_[0]->is_dir;'\
44 -e '$file = $_[0];'\
45 -e 'if (@firstline_re) {'\
46 -e '($head) = $file->lines( { count => 1 } );'\
47 -e 'push @match, quotemeta and return '\
48 -e 'if any { $head =~ $_ } @firstline_re };'\
49 -e 'push @match, quotemeta'\
50 -e 'if any { $file->slurp_raw =~ $_ } @content_re };'\
51 -e 'for (@ARGV) {'\
52 -e 'path($_)->is_dir'\
53 -e '? path($_)->visit( \&$inspect, { recurse => 1 } )'\
54 -e ': &$inspect( path($_) ) };'\
55 -e '$files_re = Regexp::Assemble->new->add( @match, @{ $opt{regex} } );'\
56 -e 'print $files_re->as_string =~ s/\(\?:/\(/gr;'\
57 -- "$@"
58 }
59
60 SKIPFILES='skip|meta'
61
62 # cleanup stray hint files from a previous run
63 find ./* -type f -regextype posix-egrep -regex "^\./.*:($SKIPFILES)$" -delete
64
65 1>&2 echo 'skip binary files without parsable metadata ...'
66 RE_skip='fuzzer/CORPUS.*'
67 find ./* -type f -regextype posix-egrep -regex "^\./($RE_skip)$" -exec sh -c 'echo "License: UNKNOWN" > "$1:skip"' shell {} ';'
68
69 1>&2 echo 'extract metadata from binary files ...'
70 EXT_meta='jpg png wav'
71 RE_meta=$(echo "$EXT_meta" | perl -aE '$"="|";say ".*\\.(@F)"') #"
72 exiftool '-textOut!' %d%f.%e:meta -short -short -recurse -extractEmbedded $(echo "$EXT_meta" | perl -aE 'say map {" -ext $_"} @F') -- ./*
73
74 RE_SKIP="$RE_skip|$RE_meta"
75
76 # licensing patterns misdetected by licensecheck
77 RE_bsd3_usrsctp=$(_file_regex --shortname 'BSD-3-clause~usrsctp' -- *)
78
79 # TODO: automate more of this manual cleanup:
80 # * strip garbage copyright holders
81 # * optionally merge equally licensed Files sections
82 # * do "sort -k2 -k1,1 -u" on copyright holders
83 # * merge copyright years for each copyright holder
84 # TODO: strip files matching glob in current (only, no later) section
85 _licensecheck() {
86 SKIPFILES=$SKIPFILES perl -MGetopt::Long=:config,gnu_getopt -MIPC::System::Simple=capture -MList::Util=uniq \
87 -e 'GetOptions ( \%opt, "check=s", "ignore=s", "shortname=s", "subset=s", "merge-licenses" );'\
88 -e '@subset = split( " ", $opt{subset} );' \
89 -e '$subset_globs = join "\n ", @subset;' \
90 -E 'if ( $subset_globs =~ /^[*]$/ ) { say STDERR "check default section(s) ..." }' \
91 -E 'elsif ( @subset and $opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
92 -E 'elsif (@subset) { say STDERR "check section(s) @subset ..." }' \
93 -E 'elsif ($opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
94 -E 'else { say STDERR "check remaining upstream section(s) ..." };' \
95 -e '@cmd = ( qw(licensecheck --copyright --deb-machine --recursive --lines 0 --encoding utf8), "--check", $opt{check}, "--ignore", $opt{ignore}, $opt{"merge-licenses"} ? "--merge-licenses" : (), "--" );'\
96 -E 'say STDERR "@cmd *" if $ENV{DEBUG};'\
97 -e '$_ = $dump = capture( @cmd, glob "*" );'\
98 -e 'if ( !$ENV{NOGLOBMERGE} and grep /[*]/, @subset ) { s/^.*?\n\nFiles: \K.*?(?=\n\w)/$subset_globs/s }' \
99 -e 'elsif (@subset) { s/^.*?\n\nFiles: \K/$subset_globs\n /s };' \
100 -e 'if ( $subset[0] ne "*" ) { s/^.*?\n\n//s };' \
101 -e 's/^Files:\K /\n /mg;' \
102 -e 's/^Copyright:\K /\n /mg;' \
103 -e 's/(?:(?<=^ )|(?<=\d{4})),\K (?=\d{4})//mg;' \
104 -e 's/:(?:$ENV{SKIPFILES})$//mg;' \
105 -e 'if ($opt{shortname}) { s/^License: \K(.*)/ join " and\/or ", uniq sort grep( !m{\AUNKNOWN\Z}, split(" and\/or ",$1), $opt{shortname} ) /mge };' \
106 -e 'print $_;'\
107 -- "$@" \
108 >> debian/copyright_hints
109 }
110
111 rm -f debian/copyright_hints
112
113 # initially, check all to know roughly what to group and in which order
114 #rm -f debian/copyright_hints
115 #_licensecheck --check '.*' --ignore "^($RE_SKIP|debian/.*)$"
116 #exit 0
117
118 # check default licensed files first
119 _licensecheck --shortname 'BSD-3-clause~usrsctp' --subset '*' --check "^($RE_bsd3_usrsctp)$" --ignore "^($RE_SKIP|debian/.*)$"
120
121 # check other known clusters
122
123 # check generally
124 # * omit non-copyright-protected Debian files
125 _licensecheck --check '.*' --ignore "^($RE_bsd3_usrsctp|$RE_SKIP|debian/.*)$"
126 _licensecheck --subset 'debian/*' --check '^debian/' --ignore "^($RE_SKIP|debian/(changelog|copyright(_hints)?|source/lintian-overrides))$"
127
128 # cleanup hint files
129 find ./* -type f -regextype posix-egrep -regex "^\./.*:($SKIPFILES)$" -delete