|
| 1 | +# SPDX-FileCopyrightText: 2022 Eva Herrada for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | + |
| 8 | +print("Starting SPDX Check") |
| 9 | + |
| 10 | +# add user bin to path! |
| 11 | +BUILD_DIR = '' |
| 12 | +# add user bin to path! |
| 13 | +try: |
| 14 | + # If we're on actions |
| 15 | + BUILD_DIR = os.environ["GITHUB_WORKSPACE"] |
| 16 | +except KeyError: |
| 17 | + try: |
| 18 | + # If we're on travis |
| 19 | + BUILD_DIR = os.environ["TRAVIS_BUILD_DIR"] |
| 20 | + except KeyError: |
| 21 | + # If we're running on local machine |
| 22 | + BUILD_DIR = os.path.abspath(".") |
| 23 | + |
| 24 | +print(f"Running in {BUILD_DIR}") |
| 25 | +files = [] |
| 26 | +missing_file = [] |
| 27 | + |
| 28 | +fail = False |
| 29 | + |
| 30 | +for r, d, f in os.walk(BUILD_DIR): |
| 31 | + for file in f: |
| 32 | + if file.split('.')[-1] in ("py", "cpp", "ino", "h"): |
| 33 | + files.append(os.path.join(r, file)) |
| 34 | + |
| 35 | +for file in files: |
| 36 | + with open(file, "r") as F: |
| 37 | + lines = [] |
| 38 | + for line in F.readlines(): |
| 39 | + if line[0] != "#" and line[:2] != "//": |
| 40 | + break |
| 41 | + lines.append(line) |
| 42 | + status = {"copyright": False, |
| 43 | + "license": False, |
| 44 | + "licensefile": False} |
| 45 | + for line in lines: |
| 46 | + if "SPDX-FileCopyrightText:" in line: |
| 47 | + status["copyright"] = True |
| 48 | + if "SPDX-License-Identifier:" in line: |
| 49 | + license_name = line.split("SPDX-License-Identifier: ")[1][:-1] |
| 50 | + status["license"] = True |
| 51 | + if os.path.isfile(BUILD_DIR+f"/LICENSES/{license_name}.txt"): |
| 52 | + status["licensefile"] = True |
| 53 | + elif license_name not in missing_file: |
| 54 | + missing_file.append(f"LICENSES/{license_name}.txt") |
| 55 | + |
| 56 | + if not all(status.values()): |
| 57 | + fail = True |
| 58 | + print(f"{file} is missing SPDX") |
| 59 | + continue |
| 60 | + if not status["copyright"]: |
| 61 | + fail = True |
| 62 | + print(f"{file}: SPDX-FileCopyrightText line is missing") |
| 63 | + if not status["license"]: |
| 64 | + fail = True |
| 65 | + print(f"{file}: SPDX-License-Identifier line is missing") |
| 66 | + if not status["licensefile"] and status["license"]: |
| 67 | + fail = True |
| 68 | + print(f"{file}: {license_name}.txt is missing from LICENSES/") |
| 69 | + |
| 70 | +if fail: |
| 71 | + if missing_file: |
| 72 | + print("Missing files:", missing_file) |
| 73 | + sys.exit(-1) |
| 74 | +sys.exit(0) |
0 commit comments