| 1 |
#!/bin/sh |
| 2 |
# Copyright 2020 Jonas Smedegaard <[email protected]> |
| 3 |
# Copyright 2020 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 |
# libregexp-assemble-perl, |
| 23 |
# perl, |
| 24 |
|
| 25 |
set -eu |
| 26 |
|
| 27 |
_content2regex() { |
| 28 |
match=$(echo "$1" | perl -ane "print join '\W+', @F") |
| 29 |
shift |
| 30 |
grep --files-with-matches --recursive --null --null-data --perl-regexp --regexp="$match" -- "$@" \ |
| 31 |
| perl -n0E 'chomp; say quotemeta' | regexp-assemble -bnr | perl -pe 's/\(\?:/\(/g' |
| 32 |
} |
| 33 |
|
| 34 |
SKIPFILES='skip|meta|comment' |
| 35 |
|
| 36 |
# cleanup stray hint files from a previous run |
| 37 |
find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete |
| 38 |
|
| 39 |
# omit files not copyright protected nor stating copyright or licensing |
| 40 |
RE_omit='.*\.(icns|ico)' |
| 41 |
|
| 42 |
1>&2 echo 'skip binary files without parsable metadata ...' |
| 43 |
RE_skip='.*\.(psd|snap)' |
| 44 |
find -type f -regextype posix-egrep -regex "^($RE_skip)$" -exec sh -c "echo 'License: UNKNOWN' > '{}:skip'" ';' |
| 45 |
|
| 46 |
1>&2 echo 'extract metadata from binary files ...' |
| 47 |
RE_meta='.*\.(icc|pdf|png|ttf)' |
| 48 |
exiftool '-textOut!' %d%f.%e:meta -short -short -recurse -ext icc -ext pdf -ext png -ext ttf . |
| 49 |
|
| 50 |
RE_SKIP="$RE_omit|$RE_skip|$RE_meta" |
| 51 |
|
| 52 |
# TODO: automate more of this manual cleanup: |
| 53 |
# * strip garbage copyright holders |
| 54 |
# * optionally merge equally licensed Files sections |
| 55 |
# * do "sort -k2 -k1,1 -u" on copyright holders |
| 56 |
# * merge copyright years for each copyright holder |
| 57 |
# TODO: strip files matching glob in current (only, no later) section |
| 58 |
_licensecheck() { |
| 59 |
GLOB=$1 |
| 60 |
shift |
| 61 |
case "$GLOB" in |
| 62 |
'*') 1>&2 echo "check default section(s) ...";; |
| 63 |
'') 1>&2 echo "check remaining upstream section(s) ...";; |
| 64 |
*) 1>&2 echo "check section(s) $GLOB ...";; |
| 65 |
esac |
| 66 |
licensecheck --copyright --deb-machine --recursive --lines 0 "$@" -- * \ |
| 67 |
| GLOB=$GLOB SKIPFILES=$SKIPFILES perl -0777 -p \ |
| 68 |
-e 'BEGIN { our $GLOB = join "\n ", split(" ",$ENV{GLOB}) }' \ |
| 69 |
-e 's/^.*?\n\nFiles: \K/$GLOB\n /s if $GLOB;' \ |
| 70 |
-e 's/^.*?\n\nFiles: \K.*?(?=\n\w)/$GLOB/s if $GLOB and $GLOB =~ /^[*]\//;' \ |
| 71 |
-e 's/^.*?\n\n//s unless $GLOB and $GLOB =~ /^[*]$/m;' \ |
| 72 |
-e 's/^Files:\K /\n /mg;' \ |
| 73 |
-e 's/^Copyright:\K /\n /mg;' \ |
| 74 |
-e 's/(?:(?<=^ )|(?<=\d{4})),\K (?=\d{4})//mg;' \ |
| 75 |
-e 's/:(?:$ENV{SKIPFILES})$//mg;' \ |
| 76 |
>> debian/copyright_hints |
| 77 |
} |
| 78 |
|
| 79 |
rm -f debian/copyright_hints |
| 80 |
|
| 81 |
# check generally |
| 82 |
# * omit non-copyright-protected Debian files |
| 83 |
_licensecheck '*' --check '.*' --ignore "^($RE_SKIP|debian/.*)$" |
| 84 |
_licensecheck '*/debian' --check '^debian/' --ignore "^($RE_SKIP|debian/(changelog|copyright(_hints)?|source/lintian-overrides))$" |
| 85 |
|
| 86 |
# cleanup hint files |
| 87 |
find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete |