| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Check source files for SPDX-License-Identifier fields. |
| 4 |
# |
| 5 |
# Examine all source files in a distribution to check that they contain an |
| 6 |
# SPDX-License-Identifier field. This does not check the syntax or whether |
| 7 |
# the identifiers are valid. |
| 8 |
# |
| 9 |
# The canonical version of this file is maintained in the rra-c-util package, |
| 10 |
# which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>. |
| 11 |
# |
| 12 |
# Copyright 2018-2021 Russ Allbery <[email protected]> |
| 13 |
# |
| 14 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 15 |
# copy of this software and associated documentation files (the "Software"), |
| 16 |
# to deal in the Software without restriction, including without limitation |
| 17 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 18 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 19 |
# Software is furnished to do so, subject to the following conditions: |
| 20 |
# |
| 21 |
# The above copyright notice and this permission notice shall be included in |
| 22 |
# all copies or substantial portions of the Software. |
| 23 |
# |
| 24 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 27 |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 29 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 30 |
# DEALINGS IN THE SOFTWARE. |
| 31 |
# |
| 32 |
# SPDX-License-Identifier: MIT |
| 33 |
|
| 34 |
use 5.010; |
| 35 |
use strict; |
| 36 |
use warnings; |
| 37 |
|
| 38 |
use lib "$ENV{C_TAP_SOURCE}/tap/perl"; |
| 39 |
|
| 40 |
use Test::RRA qw(skip_unless_automated); |
| 41 |
use Test::RRA::Automake qw(all_files automake_setup); |
| 42 |
|
| 43 |
use File::Basename qw(basename); |
| 44 |
use Test::More; |
| 45 |
|
| 46 |
# File name (the file without any directory component) and path patterns to |
| 47 |
# skip for this check. |
| 48 |
## no critic (RegularExpressions::ProhibitFixedStringMatches) |
| 49 |
my @IGNORE = ( |
| 50 |
qr{ \A LICENSE \z }xms, # Generated file with no license itself |
| 51 |
qr{ \A (NEWS|THANKS|TODO) \z }xms, # Package license should be fine |
| 52 |
qr{ \A README ( [.] .* )? \z }xms, # Package license should be fine |
| 53 |
qr{ \A (Makefile|libtool) \z }xms, # Generated file |
| 54 |
qr{ ~ \z }xms, # Backup files |
| 55 |
qr{ [.] l?a \z }xms, # Created by libtool |
| 56 |
qr{ [.] o \z }xms, # Compiler objects |
| 57 |
qr{ [.] output \z }xms, # Test data |
| 58 |
); |
| 59 |
my @IGNORE_PATHS = ( |
| 60 |
qr{ \A debian/ }xms, # Found in debian/* branches |
| 61 |
qr{ \A docs/metadata/ }xms, # Package license should be fine |
| 62 |
qr{ \A docs/protocol[.](html|txt) \z }xms, # Generated by xml2rfc |
| 63 |
qr{ \A m4/ (libtool|lt.*) [.] m4 \z }xms, # Files from Libtool |
| 64 |
qr{ \A perl/Build \z }xms, # Perl build files |
| 65 |
qr{ \A perl/MANIFEST \z }xms, # Perl build files |
| 66 |
qr{ \A perl/MYMETA [.] }xms, # Perl build files |
| 67 |
qr{ \A perl/blib/ }xms, # Perl build files |
| 68 |
qr{ \A perl/cover_db/ }xms, # Perl test files |
| 69 |
qr{ \A perl/_build }xms, # Perl build files |
| 70 |
qr{ \A php/Makefile [.] global \z }xms, # Created by phpize |
| 71 |
qr{ \A php/autom4te [.] cache/ }xms, # Created by phpize |
| 72 |
qr{ \A php/acinclude [.] m4 \z }xms, # Created by phpize |
| 73 |
qr{ \A php/build/ }xms, # Created by phpize |
| 74 |
qr{ \A php/config [.] (guess|sub) \z }xms, # Created by phpize |
| 75 |
qr{ \A php/configure [.] (ac|in) \z }xms, # Created by phpize |
| 76 |
qr{ \A php/ltmain [.] sh \z }xms, # Created by phpize |
| 77 |
qr{ \A php/run-tests [.] php \z }xms, # Created by phpize |
| 78 |
qr{ \A python/ .* [.] egg-info/ }xms, # Python build files |
| 79 |
qr{ \A tests/config/ (?!README) }xms, # Test configuration |
| 80 |
qr{ \A tests/tmp/ }xms, # Temporary test files |
| 81 |
); |
| 82 |
## use critic |
| 83 |
|
| 84 |
# Only run this test during automated testing, since failure doesn't indicate |
| 85 |
# any user-noticable flaw in the package itself. |
| 86 |
skip_unless_automated('SPDX identifier tests'); |
| 87 |
|
| 88 |
# Set up Automake testing. |
| 89 |
automake_setup(); |
| 90 |
|
| 91 |
# Check a single file for an occurrence of the string. |
| 92 |
# |
| 93 |
# $path - Path to the file |
| 94 |
# |
| 95 |
# Returns: undef |
| 96 |
sub check_file { |
| 97 |
my ($path) = @_; |
| 98 |
my $filename = basename($path); |
| 99 |
|
| 100 |
# Ignore files in the whitelist and binary files. |
| 101 |
for my $pattern (@IGNORE) { |
| 102 |
return if $filename =~ $pattern; |
| 103 |
} |
| 104 |
for my $pattern (@IGNORE_PATHS) { |
| 105 |
return if $path =~ $pattern; |
| 106 |
} |
| 107 |
return if !-T $path; |
| 108 |
|
| 109 |
# Scan the file. |
| 110 |
my ($saw_legacy_notice, $saw_spdx, $skip_spdx); |
| 111 |
open(my $file, '<', $path) or BAIL_OUT("Cannot open $path: $!"); |
| 112 |
while (defined(my $line = <$file>)) { |
| 113 |
if ($line =~ m{ Generated [ ] by [ ] libtool [ ] }xms) { |
| 114 |
close($file) or BAIL_OUT("Cannot close $path: $!"); |
| 115 |
return; |
| 116 |
} |
| 117 |
if ($line =~ m{ \b See \s+ LICENSE \s+ for \s+ licensing }xms) { |
| 118 |
$saw_legacy_notice = 1; |
| 119 |
} |
| 120 |
if ($line =~ m{ \b SPDX-License-Identifier: \s+ \S+ }xms) { |
| 121 |
$saw_spdx = 1; |
| 122 |
last; |
| 123 |
} |
| 124 |
if ($line =~ m{ no \s SPDX-License-Identifier \s registered }xms) { |
| 125 |
$skip_spdx = 1; |
| 126 |
last; |
| 127 |
} |
| 128 |
} |
| 129 |
close($file) or BAIL_OUT("Cannot close $path: $!"); |
| 130 |
|
| 131 |
# If there is a legacy license notice, report a failure regardless of file |
| 132 |
# size. Otherwise, skip files under 1KB. They can be rolled up into the |
| 133 |
# overall project license and the license notice may be a substantial |
| 134 |
# portion of the file size. |
| 135 |
if ($saw_legacy_notice) { |
| 136 |
ok(!$saw_legacy_notice, "$path has legacy license notice"); |
| 137 |
} else { |
| 138 |
ok($saw_spdx || $skip_spdx || -s $path < 1024, $path); |
| 139 |
} |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
# Scan every file. We don't declare a plan since we skip a lot of files and |
| 144 |
# don't want to precalculate the file list. |
| 145 |
my @paths = all_files(); |
| 146 |
for my $path (@paths) { |
| 147 |
check_file($path); |
| 148 |
} |
| 149 |
done_testing(); |