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-2019 Russ Allbery <eagle@eyrie.org>
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
34use 5.008;
35use strict;
36use warnings;
37
38use lib 't/lib';
39
40use Test::RRA qw(skip_unless_automated);
41
42use File::Find qw(find);
43use Test::More;
44
45# File name (the file without any directory component) and path patterns to
46# skip for this check.
47## no critic (RegularExpressions::ProhibitFixedStringMatches)
48my @IGNORE = (
49    qr{ \A Build ( [.] (?!PL) .* )? \z }ixms,    # Generated file from Build.PL
50    qr{ \A LICENSE \z }xms,                 # Generated file, no license itself
51    qr{ \A (Changes|NEWS|THANKS) \z }xms,   # Package license should be fine
52    qr{ \A TODO \z }xms,                    # Package license should be fine
53    qr{ \A MANIFEST ( [.] .* )? \z }xms,    # Package license should be fine
54    qr{ \A Makefile \z }xms,                # Generated file, no license itself
55    qr{ \A (MY)? META [.] .* }xms,          # Generated file, no license itself
56    qr{ [.] output \z }xms,                 # Test data
57    qr{ pod2htm . [.] tmp \z }xms,          # Windows pod2html output
58);
59my @IGNORE_PATHS = (
60    qr{ \A [.] / [.] git/ }xms,               # Version control files
61    qr{ \A [.] /_build/ }xms,                 # Module::Build metadata
62    qr{ \A [.] /blib/ }xms,                   # Perl build system artifacts
63    qr{ \A [.] /cover_db/ }xms,               # Artifacts from coverage testing
64    qr{ \A [.] /debian/ }xms,                 # Found in debian/* branches
65    qr{ \A [.] /docs/metadata/ }xms,          # Package license should be fine
66    qr{ \A [.] /README ( [.] .* )? \z }xms,   # Package license should be fine
67    qr{ \A [.] /share/ }xms,                  # Package license should be fine
68    qr{ \A [.] /t/data .* /metadata/ }xms,    # Test metadata
69    qr{ \A [.] /t/data .* /output/ }xms,      # Test output
70    qr{ \A [.] /t/data .* [.] json \z }xms,   # Test metadata
71);
72## use critic
73
74# Only run this test during automated testing, since failure doesn't indicate
75# any user-noticable flaw in the package itself.
76skip_unless_automated('SPDX identifier tests');
77
78# Check a single file for an occurrence of the string.
79#
80# $path - Path to the file
81#
82# Returns: undef
83sub check_file {
84    my $filename = $_;
85    my $path     = $File::Find::name;
86
87    # Ignore files in the whitelist and binary files.
88    for my $pattern (@IGNORE) {
89        return if $filename =~ $pattern;
90    }
91    for my $pattern (@IGNORE_PATHS) {
92        if ($path =~ $pattern) {
93            $File::Find::prune = 1;
94            return;
95        }
96    }
97    return if -d $filename;
98    return if !-T $filename;
99
100    # Scan the file.
101    my ($saw_legacy_notice, $saw_spdx, $skip_spdx);
102    open(my $file, '<', $filename) or BAIL_OUT("Cannot open $path");
103    while (defined(my $line = <$file>)) {
104        if ($line =~ m{ \b See \s+ LICENSE \s+ for \s+ licensing }xms) {
105            $saw_legacy_notice = 1;
106        }
107        if ($line =~ m{ \b SPDX-License-Identifier: \s+ \S+ }xms) {
108            $saw_spdx = 1;
109            last;
110        }
111        if ($line =~ m{ no \s SPDX-License-Identifier \s registered }xms) {
112            $skip_spdx = 1;
113            last;
114        }
115    }
116    close($file) or BAIL_OUT("Cannot close $path");
117
118    # If there is a legacy license notice, report a failure regardless of file
119    # size.  Otherwise, skip files under 1KB.  They can be rolled up into the
120    # overall project license and the license notice may be a substantial
121    # portion of the file size.
122    if ($saw_legacy_notice) {
123        ok(!$saw_legacy_notice, "$path has legacy license notice");
124    } else {
125        ok($saw_spdx || $skip_spdx || -s $filename < 1024, $path);
126    }
127    return;
128}
129
130# Use File::Find to scan all files from the top of the directory.
131find(\&check_file, q{.});
132done_testing();
133