| 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 | set -eu |
| 20 | |
| 21 | SKIPFILES='skip|meta' |
| 22 | |
| 23 | # cleanup stray hint files from a previous run |
| 24 | find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete |
| 25 | |
| 26 | 1>&2 echo 'skip binary files without parsable metadata ...' |
| 27 | RE_skip='.*\.(icns|ico|html|po)|doc/html/Encodings\.ps\.gz|tests/fonts/CantarellMin\.woff2' |
| 28 | find -type f -regextype posix-egrep -regex "^($RE_skip)$" -exec sh -c "echo 'License: UNKNOWN' > '{}:skip'" ';' |
| 29 | |
| 30 | 1>&2 echo 'extract metadata from binary files ...' |
| 31 | RE_meta='.*\.(gif|jpeg|pdf|pfb|png|svg|ttf|otf)' |
| 32 | exiftool '-textOut!' %d%f.%e:meta -short -short -recurse -ext gif -ext jpeg -ext pdf -ext pfb -ext png -ext svg -ext ttf -ext otf . |
| 33 | |
| 34 | RE_SKIP="$RE_skip|$RE_meta" |
| 35 | |
| 36 | # TODO: automate more of this manual cleanup: |
| 37 | # * strip garbage copyright holders |
| 38 | # * optionally merge equally licensed Files sections |
| 39 | # * do "sort -k2 -k1,1 -u" on copyright holders |
| 40 | # * merge copyright years for each copyright holder |
| 41 | # TODO: strip files matching glob in current (only, no later) section |
| 42 | _licensecheck() { |
| 43 | GLOB=$1 |
| 44 | shift |
| 45 | case "$GLOB" in |
| 46 | '*') 1>&2 echo "check default section(s) ...";; |
| 47 | '') 1>&2 echo "check remaining upstream section(s) ...";; |
| 48 | *) 1>&2 echo "check section(s) $GLOB ...";; |
| 49 | esac |
| 50 | licensecheck --copyright --deb-machine --recursive --lines 0 "$@" -- * \ |
| 51 | | GLOB=$GLOB SKIPFILES=$SKIPFILES perl -0777 -p \ |
| 52 | -e 'BEGIN { our $GLOB = join "\n ", split(" ",$ENV{GLOB}) }' \ |
| 53 | -e 's/^.*?\n\nFiles: \K/$GLOB\n /s if $GLOB;' \ |
| 54 | -e 's/^.*?\n\nFiles: \K.*?(?=\n\w)/$GLOB/s if $GLOB and $GLOB =~ /^[*]\//;' \ |
| 55 | -e 's/^.*?\n\n//s unless $GLOB and $GLOB =~ /^[*]$/m;' \ |
| 56 | -e 's/^Files:\K /\n /mg;' \ |
| 57 | -e 's/^Copyright:\K /\n /mg;' \ |
| 58 | -e 's/(?:(?<=^ )|(?<=\d{4})),\K (?=\d{4})//mg;' \ |
| 59 | -e 's/:(?:$ENV{SKIPFILES})$//mg;' \ |
| 60 | >> debian/copyright_hints |
| 61 | } |
| 62 | |
| 63 | rm -f debian/copyright_hints |
| 64 | |
| 65 | # initially, check all to know roughly what to group and in which order |
| 66 | #rm -f debian/copyright_hints |
| 67 | #_licensecheck '' --check '.*' --ignore "^($RE_SKIP|debian/.*)$" |
| 68 | #exit 0 |
| 69 | |
| 70 | # check generally |
| 71 | # * omit non-copyright-protected Debian files |
| 72 | _licensecheck '*' --check '.*' --ignore "^($RE_SKIP|debian/.*)$" |
| 73 | _licensecheck '*/debian' --check '^debian/' --ignore "($RE_SKIP|^debian/(changelog|copyright(_hints)?|source/lintian-overrides))$" |
| 74 | |
| 75 | # cleanup hint files |
| 76 | find -type f -regextype posix-egrep -regex "^.*:($SKIPFILES)$" -delete |