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

From: Mauro Carvalho Chehab
Date: Fri Sep 06 2019 - 07:37:22 EST


Em Fri, 6 Sep 2019 08:34:57 -0300
Mauro Carvalho Chehab <mchehab+samsung@xxxxxxxxxx> escreveu:

> Em Thu, 05 Sep 2019 10:45:36 -0700
> Joe Perches <joe@xxxxxxxxxxx> escreveu:
>
> > On Thu, 2019-09-05 at 09:07 -0300, Mauro Carvalho Chehab wrote:
> > > There are currently 227 files that don't complain with the "up to line 3"
> > > rule, including COPYING (with should probably be excluded from the check).
> > >
> > > Patches are at:
> > >
> > > https://git.linuxtv.org/mchehab/experimental.git/log/?h=spdx_pedantic
> > >
> > > Btw, most violations are due to:
> > >
> > > /*
> > > * SPDX...
> >
> > Hey Mauro.
> >
> > Here's a little script to move all of the improperly located
> > SPDX-License-Identifier lines in [.ch] files.
> >
> > Using this command line:
> >
> > $ git grep -n 'SPDX-License-Identifier: ' -- '*.[ch]' | \
> > grep -v ':1:' | perl move_spdx.pl
> >
> > and
> >
> > $ cat move_spdx.pl
> > while (<>) {
> > /^([^:]+):([^:]+):(.*)/;
> > my ($file, $line, $spdx) = ($1, $2, $3);
> > $spdx =~ s/^\s*\/?\*\s*//;
> > $spdx =~ s/\s*\*\/\s*$//;
> > if ($file =~ /\.h$/) {
> > $spdx = "/* $spdx */";
> > } else {
> > $spdx = "// $spdx";
> > }
> > open(FH, '<', $file) or die $!;
> > my @lines = <FH>;
> > close FH;
> > open(FH, '>', $file) or die $!;
> > print FH "$spdx\n";
> > my $count = 0;
> > foreach (@lines) {
> > $count++;
> > next if ($count == $line);
> > next if ($count == $line - 1 && $_ =~ /^\s*\*\s*$/);
> > next if ($count == $line + 1 && $_ =~ /^\s*\*\s*$/);
> > print FH "$_";
> > }
> > close FH;
> > }
> >
> > which gives this diff for today's -next
>
> Nice script!
>
> I did some changes on it

In time: I'm enclosing the modified script. just run it directly with:

perl ./move_spdx.pl

and it will move the SPDX headers where pertinent.

Thanks,
Mauro

---

#!/usr/bin/perl

@file_arr = qx(git grep -n 'SPDX-License-Identifier: ' | 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 m/(SPDX-License-Identifier:.*)/;
$is_script = 0;
if ($file =~ /\.(h|dts|dtsi|S)$/) {
$spdx = "/* $spdx */";
} elsif ($file =~ /\.(c)$/) {
$spdx = "// $spdx";
} elsif ($file =~ /\.(rst)$/) {
$spdx = ".. $spdx";
} elsif ($file =~ /\.(py|pl|sh)$/ || !$file =~ /\./) {
$is_script = 1;
$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;
}