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