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|comment'
61
62 # default licensed files
63 #RE_default=$(grep --files-with-matches --recursive --null \
64 # --regexp='Copyright (C) .* Intel Corporation\. All rights reserved' \
65 # | tr '\0' '|' | perl -pe 's/\|$//')
66 RE_default=$(_file_regex --grantglob '*' -- *)
67
68 # generated files
69 RE_FSFUL_configure='(.*/)?configure'
70 RE_FSFULLR='(.*/)?(aclocal|libtool|lt(options|sugar|version|~obsolete))\.m4'
71 RE_FSFULLR_Makefile='(.*/)?Makefile\.in'
72 RE_GPL_Autoconf='(.*/)?(compile|config\.(guess|sub)|depcomp|missing|test-driver|ylwrap)'
73 RE_GPL_Libtool='(.*/)?ltmain\.sh'
74 RE_X11='(.*/)?install-sh'
75 RE_generated="$RE_FSFUL_configure|$RE_FSFULLR|$RE_FSFULLR_Makefile|$RE_GPL_Autoconf|$RE_GPL_Libtool|$RE_X11"
76
77 # TODO: automate more of this manual cleanup:
78 # * strip garbage copyright holders
79 # * optionally merge equally licensed Files sections
80 # * do "sort -k2 -k1,1 -u" on copyright holders
81 # * merge copyright years for each copyright holder
82 # TODO: strip files matching glob in current (only, no later) section
83 _licensecheck() {
84 SKIPFILES=$SKIPFILES perl -MGetopt::Long=:config,gnu_getopt -MIPC::System::Simple=capture -MList::Util=uniq \
85 -e 'GetOptions ( \%opt, "check=s", "ignore=s", "shortname=s", "subset=s", "merge-licenses" );'\
86 -e '@subset = split( " ", $opt{subset} );' \
87 -e '$subset_globs = join "\n ", @subset;' \
88 -E 'if ( $subset_globs =~ /^[*]$/ ) { say STDERR "check default section(s) ..." }' \
89 -E 'elsif ( @subset and $opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
90 -E 'elsif (@subset) { say STDERR "check section(s) @subset ..." }' \
91 -E 'elsif ($opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
92 -E 'else { say STDERR "check remaining upstream section(s) ..." };' \
93 -e '@cmd = ( qw(licensecheck --copyright --deb-machine --recursive --lines 0), "--check", $opt{check}, "--ignore", $opt{ignore}, $opt{"merge-licenses"} ? "--merge-licenses" : (), "--" );'\
94 -E 'say STDERR "@cmd *" if $ENV{DEBUG};'\
95 -e '$_ = $dump = capture( @cmd, glob "*" );'\
96 -e 'if ( !$ENV{NOGLOBMERGE} and grep /[*]/, @subset ) { s/^.*?\n\nFiles: \K.*?(?=\n\w)/$subset_globs/s }' \
97 -e 'elsif (@subset) { s/^.*?\n\nFiles: \K/$subset_globs\n /s };' \
98 -e 'if ( $subset[0] ne "*" ) { s/^.*?\n\n//s };' \
99 -e 's/^Files:\K /\n /mg;' \
100 -e 's/^Copyright:\K /\n /mg;' \
101 -e 's/(?:(?<=^ )|(?<=\d{4})),\K (?=\d{4})//mg;' \
102 -e 's/:(?:$ENV{SKIPFILES})$//mg;' \
103 -e 'if ($opt{shortname}) { s/^License: \K(.*)/ join " and\/or ", uniq sort grep( !m{\AUNKNOWN\Z}, split(" and\/or ",$1), $opt{shortname} ) /mge };' \
104 -e 'print $_;'\
105 -- "$@" \
106 >> debian/copyright_hints
107 }
108
109 rm -f debian/copyright_hints
110
111 # try check all upstream files to learn roughly what to check in which order
112 #rm -f debian/copyright_hints
113 #_licensecheck --check '.*' --ignore '^debian/'
114 #exit 0
115
116 # check default licensed files first
117 _licensecheck --shortname 'LGPL-2.1+' --subset '*' --check "^($RE_default)$" --ignore '^debian/' --merge-licenses
118
119 # check generated files
120 _licensecheck --shortname 'FSFUL' --subset '*/configure' --check "^($RE_FSFUL_configure)$" --ignore "^($RE_default|debian/.*)$"
121 _licensecheck --shortname 'FSFULLR' --subset '*/aclocal.m4 */libtool.m4 */ltoptions.m4 */ltsugar.m4 */ltversion.m4 */lt~obsolete.m4' --check "^($RE_FSFULLR)$" --ignore "^($RE_default|debian/.*)$"
122 _licensecheck --shortname 'FSFULLR~Makefile.in' --subset '*/Makefile.in' --check "^($RE_FSFULLR_Makefile)$" --ignore "^($RE_default|debian/.*)$"
123 _licensecheck --shortname 'GPL-2+ with Autoconf-data exception' --subset '*/compile */config.guess */config.sub */depcomp */missing */test-driver */ylwrap' --check "^($RE_GPL_Autoconf)$" --ignore "^($RE_default|debian/.*)$"
124 _licensecheck --shortname 'GPL-2+ with Libtool exception' --subset '*/ltmain.sh' --check "^($RE_GPL_Libtool)$" --ignore "^($RE_default|debian/.*)$"
125 _licensecheck --shortname 'X11' --subset '*/install-sh' --check "^($RE_X11)$" --ignore "^($RE_default|debian/.*)$"
126
127 # check generally
128 # * omit non-copyright-protected Debian files
129 _licensecheck --subset '' --check '.*' --ignore "^($RE_default|$RE_generated|debian/.*)$"
130 _licensecheck --subset 'debian/*' --check '^debian/' --ignore '^debian/(changelog|copyright(_hints)?|source/lintian-overrides)$'