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
20 # Depends:
21 # licensecheck,
22 # libimage-exiftool-perl,
23 # libipc-system-simple-perl,
24 # libpath-tiny-perl,
25 # libregexp-assemble-perl,
26 # perl,
27
28 set -eu
29
30 # resolve file regex from contained license or shebang, or path regex
31 _file_regex() {
32 perl -MGetopt::Long=:config,gnu_getopt -MPath::Tiny -MRegexp::Assemble -MList::Util=any \
33 -e 'GetOptions ( \%opt, "shortname=s@", "grantglob=s@", "regex=s@", "shebang=s@", "nonverb=s@" );'\
34 -e '@section = split /\n\n+/, path("debian/copyright")->slurp_utf8;'\
35 -e 'for $name ( @{ $opt{shortname} } ) {'\
36 -e 'push @license, map { /^License:\h*\Q$name\E\n\h+(\S[^\n]*(?:\n\h+\S[^\n]*)*)/ } @section };'\
37 -e 'for $glob ( @{ $opt{grantglob} } ) {'\
38 -e 'push @files, grep { /^Files:\n?\h(?:\S[^\n]*\n?\h)*\Q$glob\E\s/ } @section };'\
39 -e '@grant = map { /^License(?:-Grant:|:\h*\S[^\n]*)\h*\n\h+(\S[^\n]*(?:\n\h+\S[^\n]*)*)/mg } @files;'\
40 -e '@firstline_re = map { qr/^\Q$_\E/ } @{ $opt{shebang} };'\
41 -e '$nonverb_re = Regexp::Assemble->new->add( "\\W+", map { "\\W+(?:$_)\\W+" } @{ $opt{nonverb} } )->as_string;'\
42 -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;'\
43 -e '$inspect = sub {'\
44 -e 'return if $_[0]->is_dir;'\
45 -e '$file = $_[0];'\
46 -e 'if (@firstline_re) {'\
47 -e '($head) = $file->lines( { count => 1 } );'\
48 -e 'push @match, quotemeta and return '\
49 -e 'if any { $head =~ $_ } @firstline_re };'\
50 -e 'push @match, quotemeta'\
51 -e 'if any { $file->slurp_raw =~ $_ } @content_re };'\
52 -e 'for (@ARGV) {'\
53 -e 'path($_)->is_dir'\
54 -e '? path($_)->visit( \&$inspect, { recurse => 1 } )'\
55 -e ': &$inspect( path($_) ) };'\
56 -e '$files_re = Regexp::Assemble->new->add( @match, @{ $opt{regex} } );'\
57 -e 'print $files_re->as_string =~ s/\(\?:/\(/gr;'\
58 -- "$@"
59 }
60
61 SKIPFILES='skip|meta|comment'
62
63 # cleanup stray hint files from a previous run
64 find ./* -type f -regextype posix-egrep -regex "^\./.*:($SKIPFILES)$" -delete
65
66 # omit files not copyright protected nor stating copyright or licensing
67 RE_omit='.*/words/words\.[^/]+'
68
69 RE_SKIP="$RE_omit"
70
71 # TODO: automate more of this manual cleanup:
72 # * strip garbage copyright holders
73 # * optionally merge equally licensed Files sections
74 # * do "sort -k2 -k1,1 -u" on copyright holders
75 # * merge copyright years for each copyright holder
76 # TODO: strip files matching glob in current (only, no later) section
77 _licensecheck() {
78 SKIPFILES=$SKIPFILES perl -MGetopt::Long=:config,gnu_getopt -MIPC::System::Simple=capture -MList::Util=uniq \
79 -e 'GetOptions ( \%opt, "check=s", "ignore=s", "shortname=s", "subset=s", "merge-licenses" );'\
80 -e '@subset = split( " ", $opt{subset} );' \
81 -e '$subset_globs = join "\n ", @subset;' \
82 -E 'if ( $subset_globs =~ /^[*]$/ ) { say STDERR "check default section(s) ..." }' \
83 -E 'elsif ( @subset and $opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
84 -E 'elsif (@subset) { say STDERR "check section(s) @subset ..." }' \
85 -E 'elsif ($opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
86 -E 'else { say STDERR "check remaining upstream section(s) ..." };' \
87 -e '@cmd = ( qw(licensecheck --copyright --deb-machine --recursive --lines 0), "--check", $opt{check}, "--ignore", $opt{ignore}, $opt{"merge-licenses"} ? "--merge-licenses" : (), "--" );'\
88 -E 'say STDERR "@cmd *" if $ENV{DEBUG};'\
89 -e '$_ = $dump = capture( @cmd, glob "*" );'\
90 -e 'if ( !$ENV{NOGLOBMERGE} and grep /[*]/, @subset ) { s/^.*?\n\nFiles: \K.*?(?=\n\w)/$subset_globs/s }' \
91 -e 'elsif (@subset) { s/^.*?\n\nFiles: \K/$subset_globs\n /s };' \
92 -e 'if ( $subset[0] ne "*" ) { s/^.*?\n\n//s };' \
93 -e 's/^Files:\K /\n /mg;' \
94 -e 's/^Copyright:\K /\n /mg;' \
95 -e 's/(?:(?<=^ )|(?<=\d{4})),\K (?=\d{4})//mg;' \
96 -e 's/:(?:$ENV{SKIPFILES})$//mg;' \
97 -e 'if ($opt{shortname}) { s/^License: \K(.*)/ join " and\/or ", uniq sort grep( !m{\AUNKNOWN\Z}, split(" and\/or ",$1), $opt{shortname} ) /mge };' \
98 -e 'print $_;'\
99 -- "$@" \
100 >> debian/copyright_hints
101 }
102
103 rm -f debian/copyright_hints
104
105 # initially, check all to know roughly what to group and in which order
106 #rm -f debian/copyright_hints
107 #_licensecheck --check '.*' --ignore "^($RE_SKIP|debian/.*)$"
108 #exit 0
109
110 # common licensing patterns
111 RE_main=$(_file_regex --grantglob '*' -- *)
112
113 # check default licensed files first, with merged licenses where reliable
114 _licensecheck --shortname 'LGPL-2.1' --subset '*' --check "^($RE_main)$" --ignore "^($RE_SKIP|debian/.*)$" --merge-licenses
115
116 # check generally
117 # * omit non-copyright-protected Debian files
118 _licensecheck --check '.*' --ignore "^($RE_main|$RE_SKIP|debian/.*)$"
119 _licensecheck --subset 'debian/*' --check '^debian/' --ignore "^($RE_SKIP|debian/(changelog|copyright(_hints)?|source/lintian-overrides))$"
120
121 # cleanup hint files
122 find ./* -type f -regextype posix-egrep -regex "^\./.*:($SKIPFILES)$" -delete