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 # libimage-exiftool-perl,
22 # libipc-system-simple-perl,
23 # perl,
24
25 set -eu
26
27 SKIPFILES='skip|meta|comment'
28
29 # cleanup stray hint files from a previous run
30 find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete
31
32 # omit files not copyright protected nor stating copyright or licensing
33 RE_omit='.*\.(icns|ico)'
34
35 1>&2 echo 'skip binary files without parsable metadata ...'
36 RE_skip='.*\.(psd|snap)'
37 find -type f -regextype posix-egrep -regex "^($RE_skip)$" -exec sh -c "echo 'License: UNKNOWN' > '{}:skip'" ';'
38
39 1>&2 echo 'extract metadata from binary files ...'
40 RE_meta='.*\.(icc|pdf|png|ttf)'
41 exiftool '-textOut!' %d%f.%e:meta -short -short -recurse -ext icc -ext pdf -ext png -ext ttf .
42
43 RE_SKIP="$RE_omit|$RE_skip|$RE_meta"
44
45 # TODO: automate more of this manual cleanup:
46 # * strip garbage copyright holders
47 # * optionally merge equally licensed Files sections
48 # * do "sort -k2 -k1,1 -u" on copyright holders
49 # * merge copyright years for each copyright holder
50 # TODO: strip files matching glob in current (only, no later) section
51 _licensecheck() {
52 SKIPFILES=$SKIPFILES perl -MGetopt::Long=:config,gnu_getopt -MIPC::System::Simple=capture -MList::Util=uniq \
53 -e 'GetOptions ( \%opt, "check=s", "ignore=s", "shortname=s", "subset=s", "merge-licenses" );'\
54 -e '@subset = split( " ", $opt{subset} );' \
55 -e '$subset_globs = join "\n ", @subset;' \
56 -E 'if ( $subset_globs =~ /^[*]$/ ) { say STDERR "check default section(s) ..." }' \
57 -E 'elsif ( @subset and $opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
58 -E 'elsif (@subset) { say STDERR "check section(s) @subset ..." }' \
59 -E 'elsif ($opt{shortname} ) { say STDERR "check $opt{shortname} section(s) @subset ..." }' \
60 -E 'else { say STDERR "check remaining upstream section(s) ..." };' \
61 -e '@cmd = ( qw(licensecheck --copyright --deb-machine --recursive --lines 0), "--check", $opt{check}, "--ignore", $opt{ignore}, $opt{"merge-licenses"} ? "--merge-licenses" : (), "--" );'\
62 -E 'say STDERR "@cmd *" if $ENV{DEBUG};'\
63 -e '$_ = $dump = capture( @cmd, glob "*" );'\
64 -e 'if ( !$ENV{NOGLOBMERGE} and grep /[*]/, @subset ) { s/^.*?\n\nFiles: \K.*?(?=\n\w)/$subset_globs/s }' \
65 -e 'elsif (@subset) { s/^.*?\n\nFiles: \K/$subset_globs\n /s };' \
66 -e 'if ( $subset[0] ne "*" ) { s/^.*?\n\n//s };' \
67 -e 's/^Files:\K /\n /mg;' \
68 -e 's/^Copyright:\K /\n /mg;' \
69 -e 's/(?:(?<=^ )|(?<=\d{4})),\K (?=\d{4})//mg;' \
70 -e 's/:(?:$ENV{SKIPFILES})$//mg;' \
71 -e 'if ($opt{shortname}) { s/^License: \K(.*)/ join " and\/or ", uniq sort grep( !m{\AUNKNOWN\Z}, split(" and\/or ",$1), $opt{shortname} ) /mge };' \
72 -e 'print $_;'\
73 -- "$@" \
74 >> debian/copyright_hints
75 }
76
77 rm -f debian/copyright_hints
78
79 # check generally
80 # * omit non-copyright-protected Debian files
81 _licensecheck --subset '*' --check '.*' --ignore "^($RE_SKIP|debian/.*)$"
82 _licensecheck --subset '*/debian' --check '^debian/' --ignore "^($RE_SKIP|debian/(changelog|copyright(_hints)?|source/lintian-overrides))$"
83
84 # cleanup hint files
85 find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete