Debian Codegraph @

debian/copyright-check

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