-
Notifications
You must be signed in to change notification settings - Fork 782
/
Copy pathSPDX.py
132 lines (116 loc) · 4.25 KB
/
SPDX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# SPDX-FileCopyrightText: 2022 Eva Herrada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import os
import subprocess
import sys
print("Starting SPDX Check")
# add user bin to path!
BUILD_DIR = ""
# add user bin to path!
try:
# If we're on actions
BUILD_DIR = os.environ["GITHUB_WORKSPACE"]
except KeyError:
try:
# If we're on travis
BUILD_DIR = os.environ["TRAVIS_BUILD_DIR"]
except KeyError:
# If we're running on local machine
BUILD_DIR = os.path.abspath(".")
print(f"Running in {BUILD_DIR}\n")
files = []
missing_file = []
fail = False
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"):
files.append(os.path.join(r, file))
for file in files:
with open(file, "r") as F:
lines = []
for line in F.readlines():
if line[0] != "#" and line[:2] != "//":
break
lines.append(line)
status = {"copyright": False, "license": False, "licensefile": False}
for line in lines:
if "SPDX-FileCopyrightText" in line:
status["copyright"] = True
if (
"# SPDX-FileCopyrightText: " not in line
and "// SPDX-FileCopyrightText: " not in line
):
if file.endswith(".py"):
if compare(file, line, "# SPDX-FileCopyrightText: "):
fail = True
else:
if compare(file, line, "// SPDX-FileCopyrightText: "):
fail = True
if "SPDX-License-Identifier" 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
failed = True
if not failed:
license_name = line.split("SPDX-License-Identifier: ")[1].strip()
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\n")
continue
if not status["copyright"]:
fail = True
print(f"{file}: SPDX-FileCopyrightText line is missing")
if not status["license"]:
fail = True
print(f"{file}: SPDX-License-Identifier line is missing")
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:
print("Missing files:", missing_file)
sys.exit(-1)
sys.exit(0)