linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
To: Linux Media Mailing List <linux-media@vger.kernel.org>
Cc: "Mauro Carvalho Chehab" <mchehab+samsung@kernel.org>,
	"Mauro Carvalho Chehab" <mchehab@infradead.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Joe Perches" <joe@perches.com>,
	linux-kernel@vger.kernel.org, "Jonathan Corbet" <corbet@lwn.net>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Sven Eckelmann" <sven@narfation.org>,
	"Thierry Reding" <treding@nvidia.com>,
	"Aurélien Cedeyn" <aurelien.cedeyn@gmail.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Vincenzo Frascino" <vincenzo.frascino@arm.com>
Subject: [PATCH 6/6] scripts/spdxcheck.py: check if the line number follows the strict rule
Date: Thu,  5 Sep 2019 16:57:53 -0300	[thread overview]
Message-ID: <1b7b5c906646f8165fd818ec9e400609d7a7290b.1567712829.git.mchehab+samsung@kernel.org> (raw)
In-Reply-To: <cover.1567712829.git.mchehab+samsung@kernel.org>

There is a very strict rule saying on what line a SPDX header
should be. Add an optional pedantic check for it.

When the check is enabled, it will verify if the file has the
SPDX header "at the first possible line in a file which can contain
a comment", as stated at:

	Documentation/process/license-rules.rst

Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
---
 scripts/spdxcheck.py | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/scripts/spdxcheck.py b/scripts/spdxcheck.py
index c969b050366f..f3260391f091 100755
--- a/scripts/spdxcheck.py
+++ b/scripts/spdxcheck.py
@@ -164,9 +164,15 @@ class id_parser(object):
         self.lastid = None
         self.parser.parse(expr, lexer = self.lexer)
 
-    def parse_lines(self, fd, maxlines, fname):
+    def parse_lines(self, fd, maxlines, fname, strict):
         self.checked += 1
         self.curline = 0
+        self.max_line = 1
+        self.is_python = False
+
+        if fname.find("COPYING") >= 0:
+            self.max_line = maxlines
+
         try:
             for line in fd:
                 line = line.decode(locale.getpreferredencoding(False), errors='ignore')
@@ -174,6 +180,13 @@ class id_parser(object):
                 if self.curline > maxlines:
                     break
                 self.lines_checked += 1
+                if self.curline == 1:
+		    if re.match("\#\!", line):
+                        self.max_line = 2
+			if re.match("\#\!.*python", line):
+			    is_python = True
+                if self.curline == 2 and self.is_python and re.match("^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)", line):
+                        self.max_line = 3
                 if line.find("SPDX-License-Identifier:") < 0:
                     continue
                 expr = line.split(':')[1].strip()
@@ -189,6 +202,8 @@ class id_parser(object):
                 # Should we check for more SPDX ids in the same file and
                 # complain if there are any?
                 #
+                if strict and self.curline > self.max_line:
+                    sys.stderr.write('Warning: SPDX header for file %s is at line %d\n' % (fname,self.curline))
                 return self.curline - 1
 
             return -1
@@ -202,7 +217,7 @@ class id_parser(object):
                 sys.stdout.write('%s: %d:0 %s\n' %(fname, self.curline, col, pe.txt))
             self.spdx_errors += 1
 
-def scan_git_tree(ln_count, tree):
+def scan_git_tree(ln_count, tree, strict):
     for el in tree.traverse():
         # Exclude stuff which would make pointless noise
         # FIXME: Put this somewhere more sensible
@@ -213,15 +228,15 @@ def scan_git_tree(ln_count, tree):
         if not os.path.isfile(el.path):
             continue
         with open(el.path, 'rb') as fd:
-            ln = parser.parse_lines(fd, args.maxlines, el.path)
+            ln = parser.parse_lines(fd, args.maxlines, el.path, strict)
             if ln >= 0:
                 ln_count[ln] += 1;
     return ln_count
 
-def scan_git_subtree(ln_count, tree, path):
+def scan_git_subtree(ln_count, tree, path, strict):
     for p in path.strip('/').split('/'):
         tree = tree[p]
-    scan_git_tree(ln_count, tree)
+    scan_git_tree(ln_count, tree, strict)
 
 if __name__ == '__main__':
 
@@ -231,6 +246,7 @@ if __name__ == '__main__':
                     help='Maximum number of lines to scan in a file. Default 15')
     ap.add_argument('-v', '--verbose', action='store_true', help='Verbose statistics output')
     ap.add_argument('-H', '--histogram', action='store_true', help='Verbose histogram about SPDX header position')
+    ap.add_argument('-s', '--strict', action='store_true', help='Enable strict mode, making it complain about SPDX line position')
     args = ap.parse_args()
 
     # Sanity check path arguments
@@ -266,7 +282,7 @@ if __name__ == '__main__':
     try:
         if len(args.path) and args.path[0] == '-':
             stdin = os.fdopen(sys.stdin.fileno(), 'rb')
-            ln = parser.parse_lines(stdin, args.maxlines, '-')
+            ln = parser.parse_lines(stdin, args.maxlines, '-', args.strict)
             if ln >= 0:
                 ln_count[ln] += 1;
 
@@ -274,18 +290,18 @@ if __name__ == '__main__':
             if args.path:
                 for p in args.path:
                     if os.path.isfile(p):
-                        ln = parser.parse_lines(open(p, 'rb'), args.maxlines, p)
+                        ln = parser.parse_lines(open(p, 'rb'), args.maxlines, p, args.strict)
                         if ln >= 0:
                             ln_count[ln] += 1;
 
                     elif os.path.isdir(p):
-                        scan_git_subtree(ln_count, repo.head.reference.commit.tree, p)
+                        scan_git_subtree(ln_count, repo.head.reference.commit.tree, p, args.strict)
                     else:
                         sys.stderr.write('path %s does not exist\n' %p)
                         sys.exit(1)
             else:
                 # Full git tree scan
-                scan_git_tree(ln_count, repo.head.commit.tree)
+                scan_git_tree(ln_count, repo.head.commit.tree, args.strict)
 
             if args.verbose:
                 sys.stderr.write('\n')
-- 
2.21.0


  parent reply	other threads:[~2019-09-05 19:58 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05 19:57 [PATCH 0/6] Address issues with SPDX requirements and PEP-263 Mauro Carvalho Chehab
2019-09-05 19:57 ` [PATCH 1/6] docs: sphinx: add SPDX header for some sphinx extensions Mauro Carvalho Chehab
2019-09-05 19:57 ` [PATCH 2/6] tools: perf: fix SPDX header in the light of PEP-263 Mauro Carvalho Chehab
2019-09-05 19:57 ` [PATCH 3/6] tools: intel_pstate_tracer.py: " Mauro Carvalho Chehab
2019-09-05 19:57 ` [PATCH 4/6] docs: license-rules.txt: cover SPDX headers on Python scripts Mauro Carvalho Chehab
2019-09-05 19:57 ` [PATCH 5/6] scripts/spdxcheck.py: keep track on what line SPDX header was found Mauro Carvalho Chehab
2019-09-05 19:57 ` Mauro Carvalho Chehab [this message]
2019-09-06 12:04   ` [PATCH v2 6/6] scripts/spdxcheck.py: check if the line number follows the strict rule Mauro Carvalho Chehab
2019-09-05 20:05 ` [PATCH 0/6] Address issues with SPDX requirements and PEP-263 Joe Perches
2019-09-06 12:02   ` Mauro Carvalho Chehab
2019-09-06 12:22     ` Joe Perches
2019-09-07 13:34 ` Jonathan Corbet
2019-09-07 14:36   ` Markus Heiser
2019-09-07 16:22     ` Mauro Carvalho Chehab
2019-09-07 17:33       ` Markus Heiser
2019-09-07 18:04         ` Mauro Carvalho Chehab
2019-09-07 18:37           ` Markus Heiser
2019-09-07 21:17             ` Thomas Gleixner
2019-09-08 10:03               ` Matthew Wilcox
2019-09-08 14:46                 ` Thomas Gleixner
2019-09-10  6:31                   ` Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1b7b5c906646f8165fd818ec9e400609d7a7290b.1567712829.git.mchehab+samsung@kernel.org \
    --to=mchehab+samsung@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aurelien.cedeyn@gmail.com \
    --cc=corbet@lwn.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@infradead.org \
    --cc=sven@narfation.org \
    --cc=tglx@linutronix.de \
    --cc=treding@nvidia.com \
    --cc=u.kleine-koenig@pengutronix.de \
    --cc=vincenzo.frascino@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).