Re: [PATCH] docs: license-rules.txt: cover SPDX headers on Python scripts

From: Mauro Carvalho Chehab
Date: Fri Sep 06 2019 - 14:17:12 EST


Em Fri, 06 Sep 2019 10:33:42 -0700
Joe Perches <joe@xxxxxxxxxxx> escreveu:

> On Fri, 2019-09-06 at 08:34 -0300, Mauro Carvalho Chehab wrote:
> > I did some changes on it, plus one change at the pedantic mode of
> > scripts/spdxcheck.py, and placed the corresponding patches at:
> >
> > https://git.linuxtv.org/mchehab/experimental.git/log/?h=spdx_pedantic
>
> Your script needs a modification of this line:

It is yours script, I just made a few improvements for it to also catch
scripts.

> $spdx = $1 if m/(SPDX-License-Identifier:.*)/;
>
> This is on $_ and not $spdx.
>
> This line needs to be:
>
> $spdx = $1 if $spdx =~ m/(SPDX-License-Identifier:.*)/;

Gah, true!

It also doesn't get the case where SPDX-License-Identifier: is
followed by a tab, and fixed an issue when detecting files without
any extension.

The enclosed script does a better job.

I changed the output of it on my testing branch:
https://git.linuxtv.org/mchehab/experimental.git/log/?h=spdx_pedantic

The only thing that it doesn't change is an issue with an asm code,
with uses "@" for comments.

As this is a single file, better to do it manually:

https://git.linuxtv.org/mchehab/experimental.git/commit/?h=spdx_pedantic&id=f1b7b2169ae2c8d8ab80a5997ebef9aa03a42d30

Thanks,
Mauro

---

#!/usr/bin/perl

@file_arr = qx(git grep -nE 'SPDX-License-Identifier:\\s' | grep -v ':1:');
foreach (@file_arr) {
/^([^:]+):([^:]+):(.*)/;
my ($file, $line, $spdx) = ($1, $2, $3);

next if ($file =~ m,(COPYING|LICENSES/|sha1-armv4-large.S),);
next if ($line > 10);

$spdx =~ s/^\s*\/?\*\s*//;
$spdx =~ s/\s*\*\/\s*$//;
$spdx = $1 if $spdx =~ m/(SPDX-License-Identifier:.*)/;
$spdx =~ s/(SPDX-License-Identifier:)\s+/$1 /;

if ($file =~ /\.(h|dts|dtsi|S)$/) {
$spdx = "/* $spdx */";
} elsif ($file =~ /\.(c)$/) {
$spdx = "// $spdx";
} elsif ($file =~ /\.(rst)$/) {
$spdx = ".. $spdx";
} elsif ($file =~ /\.(py|pl|sh|tc)$/ || !($file =~ /\./)) {
$spdx = "# $spdx";
} else {
next;
}

open(FH, '<', $file) or die $!;
my @lines = <FH>;
close FH;
open(FH, '>', $file) or die $!;
my $count = 0;
my $print_spdx = 1;
foreach (@lines) {
$count++;
if ($print_spdx) {
if ($count == 1 && (/:orphan:/ || /^#\!/)) {
print FH "$_";
next;
}
if ($count <= 2 && /^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)/) {
print FH "$_";
next;
}
print FH "$spdx\n";
$print_spdx = 0;
}
next if ($count == $line);
next if ($count == $line - 1 && $_ =~ /^\s*\*\s*$/);
next if ($count == $line + 1 && $_ =~ /^\s*\*\s*$/);
next if ($count == $line - 1 && $_ =~ /^$/);
print FH "$_";
}
close FH;
}