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