From: Kienan Stewart Date: Mon, 20 Jan 2025 20:08:59 +0000 (-0500) Subject: Misc: Add pre-commit check for SPDX License-Identifier and FileCopyrightText X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=86de00ce653c0a1c3d774e2cb5cd807a27a275ed;p=lttng-tools.git Misc: Add pre-commit check for SPDX License-Identifier and FileCopyrightText 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 Signed-off-by: Jérémie Galarneau --- diff --git a/extras/pre-commit.py b/extras/pre-commit.py index f7013a73e..acbd18f51 100755 --- a/extras/pre-commit.py +++ b/extras/pre-commit.py @@ -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[^ :]+): (?P[^\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(