]> git.lttng.org Git - lttng-tools.git/commitdiff
Misc: Add pre-commit check for SPDX License-Identifier and FileCopyrightText
authorKienan Stewart <kstewart@efficios.com>
Mon, 20 Jan 2025 20:08:59 +0000 (15:08 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 28 Jan 2025 19:11:09 +0000 (19:11 +0000)
Observed issue
==============

There are frequently small typos or slightly deviant SPDX tags set on files.

Cause
=====

Mostly me.

Solution
========

Add a pre-commit check with the following allowed tags:

 * SPDX-License-Identifier
 * SPDX-FileCopyrightText
 * SPDX-URL

Known drawbacks
===============

This is not a full SPDX validation, and does not validate the license
identifier values.

Change-Id: Iabd78ae14ef143ab4a956e0c5c31c1490b43aeda
Signed-off-by: Kienan Stewart <kstewart@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
extras/pre-commit.py

index f7013a73e7aab85db3d0fd60e54eaf3b93782c2e..acbd18f51b64c0e02c19351e1aef61c8f8afe37a 100755 (executable)
@@ -147,6 +147,42 @@ def python_blacken(files):
     return stdout.decode("utf-8"), black.returncode
 
 
+def spdx_tags(files):
+    tag_re = re.compile(r"SPDX-(?P<tag>[^ :]+): (?P<value>[^\n]+)")
+    valid_tags = [
+        "License-Identifier",
+        "FileCopyrightText",
+        "URL",
+    ]
+    stdout = ""
+    returncode = 0
+    for f in files:
+        content = ""
+        with open(f, "r") as fd:
+            content = fd.read()
+        has_license = False
+        has_copyrighttext = False
+        for m in tag_re.finditer(content):
+            tag = m.group(1)
+            value = m.group(2)
+            if tag == "License-Identifier":
+                has_license = True
+            elif tag == "FileCopyrightText":
+                has_copyrighttext = True
+            elif tag not in valid_tags:
+                stdout += "File `{}` has unknown SPDX tag `{}`\n".format(f, tag)
+                # This is strict, but lttng-tools usage of SPDX is minimal
+                returncode = 1
+        if not has_license:
+            stdout += "File `{}` is missing SPDX-License-Identifier\n".format(f)
+            returncode = 1
+        if not has_copyrighttext:
+            stdout += "File `{}` is missing SPDX-FileCopyrightText\n".format(f)
+            returncode = 1
+
+    return stdout, returncode
+
+
 if __name__ == "__main__":
     logging.basicConfig()
     failures = []
@@ -164,6 +200,9 @@ if __name__ == "__main__":
         "python-black": {
             "func": python_blacken,
         },
+        "spdx": {
+            "func": spdx_tags,
+        },
     }
 
     git = subprocess.Popen(
This page took 0.02982 seconds and 4 git commands to generate.