-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspdxl.pl
219 lines (168 loc) · 5.46 KB
/
spdxl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/perl
use warnings;
use strict;
=head1 NAME
spdxl.pl -- Find files and grep SPDX tags in them
=head1 VERSION
Version 0.1.1
=cut
our $VERSION = '0.1.1';
=head1 SYNOPSIS
spdxl.pl [-cdfhv] [long options...] <args>
-d STR --dir STR Directory to search
-f STR --fil STR Single file to check
-c --color Color output
-h --htmlout Produce HTML output
-v --verbose Wordy
--help Print usage message and exit
=head1 LICENSE
SPDX-License-Identifier: GPL-3.0-or-later
SPDX-FileCopyrightText: 2015 Jeremiah C. Foster <[email protected]>
This file is part of the SPDXL package.
GNU Public License v3.0
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
=head1 COPYRIGHT
Copyright (C) 2015, Jeremiah C. Foster <[email protected]>
=over
List of changes:
Aug. 2015, spdxl.pl, created file
Feb. 2016, spdxl.pl, MVP working
=back
=head1 METHODS
=head2 main
The main subroutine goes through all the files we've found, checks if
its a directory, skip it if it is, print the file name if we've found
a tag and then go through the file to pull out the identifier line.
=head2 nogit
If we find a git repo, i.e. ".git" tell File::Find to ignore it.
=head2 htmlout
HTML output works, but it means that we do not have any other output
ATM. Plain text and colored ASCII text needs to be fixed next.
=cut
## TODO -- the first issue we'll face is the high likelyhood of being
## in a git dir. Obviously we'll want to switch to a git handling
## module for that work or we'll just ignore it which is probably a
## really bad idea since important data can be gleaned or even
## determined with a high degree of certainty.
## TODO -- in the future we'll use this data to put out a conformant
## SPDX doc
## TODO -- grep for copyright (don't use String::Copyright which is
## just a formatter.)
## Separate html output from text output, currently if you specify
## html output, it comes at the end of the text output.
use Path::Tiny;
use File::Find;
use File::Compare;
use File::Slurp;
use Getopt::Long::Descriptive;
use Pod::Usage;
use Term::ANSIColor;
use autodie;
use feature "say";
# --- Command line options
my ($opt, $usage) = describe_options
('spdxl.pl %o <args>',
[ 'dir|d=s', "Directory to search" ],
[ 'fil|f=s' , "Single file to check" ],
[ 'color|c', "Color output" ],
[ 'htmlout|h', "Produce HTML output" ],
[ 'verbose|v', "Wordy" ],
[ 'help', "Print usage message and exit" ],
);
if ($opt->fil) {
print "File: " . $opt->fil . "\n" if $opt->verbose;
check_each_line($opt->fil);
exit;
}
print($usage->text), exit if $opt->help;
print "Searching through " . $opt->dir . "\n" if $opt->verbose;
# go through each dir
find(\&nogit, $opt->dir);
my @files;
sub nogit {
/^\.git.*\z/s && ($File::Find::prune = 1)
||
push @files, $File::Find::name;
}
my (@lines, @spdxtags);
sub grep_for_tags {
map {
my ($row, $line);
if (! -d $_) {
@lines = read_file("$_");
print "File: $_ "; # This prints the files we've found
foreach my $line (@lines) {
if ($line =~ /SPDX.?[Ll]ic/) {
chomp($line);
# Here we put the line in an array. Perhaps make a hash with
# file name and tag?
push @{ $spdxtags[$row++] }, $line;
if ($opt->color) { colored_output($line); }
}
} print "\n";
}
} @files;
}
grep_for_tags();
sub check_each_line { # This should just build up the data structure, don't print anything
my $file = shift;
my ($row, $line);
if (! -d $file && -e $file) {
@lines = read_file("$file");
# print "File: $file "
foreach my $line (@lines) {
if ($line =~ /SPDX.?[Ll]ic/) { # This just looks for a SPDX-License tag
chomp($line);
# Here we put the line in an array. Perhaps make a hash with
# file name and tag?
push @{ $spdxtags[$row++] }, $line;
#if ($opt->color) { colored_output($line) } else { print "$line"; }
}
} print "\n";
}
}
sub colored_output {
my $line = shift;
print colored ['bright_yellow on_black'], "$line";
}
sub htmlout {
use Text::Xslate;
my $tx = Text::Xslate->new();
my @tags = shift;
my %vars = ( tags => \@tags, );
print $tx->render("spdxl.tx", \%vars);
}
# Create html output from the tags found if requested
htmlout(@spdxtags) if $opt->htmlout;
sub cmp_2_files {
# Should the comparison short-circuit if hashsums match?
# list of files names that match the regex below
my (@licenses) = map { $_ } grep /^\.\/(?:LICEN[CS]E|COPYING)$/, @files;
if (compare($licenses[0], $licenses[1]) == 0) {
say "files identical.";
}
else { # If the files *don't* match
say "files not identical.";
use Text::Diff;
# magic
}
}
sub gitish {
# git ls-files?
}
my $git_dir = path("./.git/");
if (-e $git_dir) {
# handle the git dir
};
1;