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