mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Breno Rodrigues Alves <breno3011alves@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
	Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Lukas Bulwahn <lukas.bulwahn@gmail.com>,
	Breno Rodrigues Alves <breno3011alves@gmail.com>
Subject: [RFC PATCH 2/7] scripts: convert checkversion.pl to Python
Date: Sun, 18 Oct 2026 08:06:33 -0300	[thread overview]
Message-ID: <20261018110638.9947-3-breno3011alves@gmail.com> (raw)
In-Reply-To: <20261018110638.9947-1-breno3011alves@gmail.com>

Faithful re-implementation of checkversion.pl in Python 3
(stdlib only). Same comment/string stripping, same
line-number reporting.

Signed-off-by: Breno Rodrigues Alves <breno3011alves@gmail.com>
---
 scripts/checkversion.py | 121 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
 create mode 100755 scripts/checkversion.py

diff --git a/scripts/checkversion.py b/scripts/checkversion.py
new file mode 100755
index 000000000..4016cb5cb
--- /dev/null
+++ b/scripts/checkversion.py
@@ -0,0 +1,121 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# checkversion finds uses of macros in <linux/version.h> without the
+# corresponding #include; or includes of <linux/version.h> without any
+# use.
+# Copyright (C) 2003, Randy Dunlap <rdunlap@infradead.org>
+
+import re
+import sys
+
+SKIP_FILES = (
+    'include/generated/uapi/linux/version.h',
+    'usr/include/linux/version.h',
+)
+
+RE_VERSION_MACROS = re.compile(
+    r'LINUX_VERSION_CODE|(?<!\w)KERNEL_VERSION|LINUX_VERSION_MAJOR'
+    r'|LINUX_VERSION_PATCHLEVEL|LINUX_VERSION_SUBLEVEL')
+RE_INCLUDE_Q = re.compile(r'^\s*#\s*include\s*"linux/version\.h"')
+RE_INCLUDE_A = re.compile(r'^\s*#\s*include\s*<linux/version\.h>')
+
+
+def strip_comments(line, in_comment):
+    """Return (line_without_comments, in_comment)."""
+    out = []
+    i = 0
+    while i < len(line):
+        if in_comment:
+            end = line.find('*/', i)
+            if end == -1:
+                return ''.join(out), True
+            i = end + 2
+            in_comment = False
+        else:
+            start = line.find('/*', i)
+            if start == -1:
+                out.append(line[i:])
+                break
+            out.append(line[i:start])
+            i = start + 2
+            in_comment = True
+    return ''.join(out), in_comment
+
+
+def strip_strings(line, in_string):
+    """Return (line_without_strings, in_string)."""
+    out = []
+    i = 0
+    while i < len(line):
+        if in_string:
+            end = line.find('"', i)
+            if end == -1:
+                return ''.join(out), True
+            i = end + 1
+            in_string = False
+        else:
+            start = line.find('"', i)
+            if start == -1:
+                out.append(line[i:])
+                break
+            out.append(line[i:start])
+            i = start + 1
+            in_string = True
+    return ''.join(out), in_string
+
+
+def check_file(path, debugging=False):
+    try:
+        with open(path, 'r') as f:
+            lines = f.readlines()
+    except OSError as e:
+        print(f"Can't open {path}: {e}", file=sys.stderr)
+        return
+
+    in_comment = False
+    in_string = False
+    version_linenr = 0
+    uses_version = False
+
+    for idx, raw in enumerate(lines, 1):
+        line, in_comment = strip_comments(raw, in_comment)
+        if in_comment and not line.strip():
+            continue
+
+        if RE_INCLUDE_Q.search(line):
+            version_linenr = idx
+
+        line, in_string = strip_strings(line, in_string)
+        if in_string and not line.strip():
+            continue
+
+        if RE_INCLUDE_A.search(line):
+            version_linenr = idx
+
+        if RE_VERSION_MACROS.search(line):
+            uses_version = True
+            if version_linenr:
+                break
+
+    if uses_version and not version_linenr:
+        print(f"{path}: {len(lines)}: need linux/version.h")
+    if version_linenr and not uses_version:
+        print(f"{path}: {version_linenr} linux/version.h not needed.")
+    if debugging:
+        if version_linenr and uses_version:
+            print(f"{path}: version use is OK ({version_linenr})")
+        if not version_linenr and not uses_version:
+            print(f"{path}: version use is OK (none)")
+
+
+def main(argv):
+    for path in argv:
+        if any(skip in path for skip in SKIP_FILES):
+            continue
+        check_file(path)
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv[1:]))
\ No newline at end of file
-- 
2.43.0


  parent reply	other threads:[~2026-10-18 11:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18 11:06 [RFC PATCH 0/7] scripts: convert seven more Perl utilities " Breno Rodrigues Alves
2026-10-18 11:06 ` [RFC PATCH 1/7] scripts: convert checkdeclares.pl " Breno Rodrigues Alves
2026-10-18 11:06 ` Breno Rodrigues Alves [this message]
2026-10-18 11:06 ` [RFC PATCH 3/7] scripts: convert extract_xc3028.pl " Breno Rodrigues Alves
2026-10-18 11:06 ` [RFC PATCH 4/7] scripts: convert generate_initcall_order.pl " Breno Rodrigues Alves
2026-10-18 11:06 ` [RFC PATCH 5/7] scripts: convert get_maintainer.pl " Breno Rodrigues Alves
2026-10-18 11:06 ` [RFC PATCH 6/7] scripts: convert leaking_addresses.pl " Breno Rodrigues Alves
2026-10-18 11:06 ` [RFC PATCH 7/7] scripts: convert recordmcount.pl " Breno Rodrigues Alves
2026-10-18 20:59 ` [RFC PATCH 0/7] scripts: convert seven more Perl utilities " Jonathan Corbet
2026-10-18 23:19   ` Breno Rodrigues Alves
2026-10-19 10:05     ` Heiko Carstens

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=20261018110638.9947-3-breno3011alves@gmail.com \
    --to=breno3011alves@gmail.com \
    --cc=apw@canonical.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas.bulwahn@gmail.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

all inboxes | Powered by JetHome®