diff --git a/SPDX.py b/SPDX.py index de9245363..75ff65e4d 100644 --- a/SPDX.py +++ b/SPDX.py @@ -3,13 +3,13 @@ # SPDX-License-Identifier: MIT import os -import sys import subprocess +import sys print("Starting SPDX Check") # add user bin to path! -BUILD_DIR = '' +BUILD_DIR = "" # add user bin to path! try: # If we're on actions @@ -22,28 +22,43 @@ except KeyError: # If we're running on local machine BUILD_DIR = os.path.abspath(".") -print(f"Running in {BUILD_DIR}") +print(f"Running in {BUILD_DIR}\n") files = [] missing_file = [] fail = False -def compare(file, line, correct) - old = line[:-1] - new = f"{correct}{line.split(':')[1][:-1].strip()}" - command = f'CMD="diff <(echo \\"{old}\\") <(echo \\"{new}\\")"; /bin/bash -c "$CMD"' - output = subprocess.getoutput(command).split('\n') - if output: - print(f"{file.split('Adafruit_Learning_System_Guides/')[1]} appears to have a SPDX formatting typo:") - print("Change this:") - print(output[1]) - print("To this:") - print(output[3]) - return True + +def compare(file_, line_, correct): + old = line_[:-1] + try: + right = line_.split(":")[1][:-1] + except IndexError: + print(f'{file_.split("_Guides/")[1]} may have an SPDX format issue:') + print("The following line:") + print(old) + print("May be missing a colon.\nIt should look like this:") + print(correct, "\n") + return True + + new = f"{correct}{right.strip()}" + cmd = f'CMD="diff <(echo \\"{old}\\") <(echo \\"{new}\\")"; /bin/bash -c "$CMD"' + output = subprocess.getoutput(cmd).split("\n") + + if output: + print(f'{file_.split("_Guides/")[1]} may have an SPDX format issue:') + print("Change this:") + print(output[1][2:]) + print("To this:") + print(output[3][2:], "\n") + return True + + return False + for r, d, f in os.walk(BUILD_DIR): for file in f: - if file.split('.')[-1] in ("py", "cpp", "ino", "h"): + if file.split(".")[-1] in ("py", "cpp", "ino", "h"): files.append(os.path.join(r, file)) for file in files: @@ -53,13 +68,14 @@ for file in files: if line[0] != "#" and line[:2] != "//": break lines.append(line) - status = {"copyright": False, - "license": False, - "licensefile": False} + status = {"copyright": False, "license": False, "licensefile": False} for line in lines: - if "SPDX-FileCopyrightText:" in line: + if "SPDX-FileCopyrightText" in line: status["copyright"] = True - if "# SPDX-FileCopyrightText: " not in line and "// SPDX-FileCopyrightText: " not in line: + if ( + "# SPDX-FileCopyrightText: " not in line + and "// SPDX-FileCopyrightText: " not in line + ): if file.endswith(".py"): if compare(file, line, "# SPDX-FileCopyrightText: "): fail = True @@ -68,23 +84,30 @@ for file in files: fail = True if "SPDX-License-Identifier" in line: - license_name = line.split("SPDX-License-Identifier: ")[1][:-1] - status["license"] = True - if "# SPDX-License-Identifier: " not in line and "// SPDX-License-Identifier: " not in line: + failed = False + if ( + "# SPDX-License-Identifier: " not in line + and "// SPDX-License-Identifier: " not in line + ): if file.endswith(".py"): if compare(file, line, "# SPDX-License-Identifier: "): fail = True + failed = True else: if compare(file, line, "// SPDX-License-Identifier: "): fail = True - if os.path.isfile(BUILD_DIR+f"/LICENSES/{license_name}.txt"): - status["licensefile"] = True - elif license_name not in missing_file: - missing_file.append(f"LICENSES/{license_name}.txt") + failed = True + if not failed: + license_name = line.split("SPDX-License-Identifier: ")[1][:-1] + status["license"] = True + if os.path.isfile(BUILD_DIR + f"/LICENSES/{license_name}.txt"): + status["licensefile"] = True + elif license_name not in missing_file: + missing_file.append(f"LICENSES/{license_name}.txt") if not all(status.values()): fail = True - print(f"{file} is missing SPDX") + print(f"{file} is missing SPDX\n") continue if not status["copyright"]: fail = True @@ -95,6 +118,12 @@ for file in files: if not status["licensefile"] and status["license"]: fail = True print(f"{file}: {license_name}.txt is missing from LICENSES/") + if ( + not status["copyright"] + or not status["license"] + or not status["licensefile"] + ): + print("\n") if fail: if missing_file: