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
133
134
135
136
137
138
139
140
141
142
143
144
|
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2020 Andreas Cord-Landwehr <cordlandwehr@kde.org>
# SPDX-License-Identifier: BSD-3-Clause
# key : outbound license identifier
# values : list of acceptable licenses that are compatible with outbound license
compatibilityMatrix = {
"MIT": [
"CC0-1.0",
"MIT"],
"BSD-2-Clause": [
"CC0-1.0",
"MIT",
"BSD-2-Clause"],
"BSD-3-Clause": [
"CC0-1.0",
"MIT",
"BSD-2-Clause",
"BSD-3-Clause"],
"LGPL-2.0-only": [
"CC0-1.0",
"LGPL-2.0-only",
"LGPL-2.0-or-later",
"MIT",
"BSD-2-Clause",
"BSD-3-Clause",
"bzip2-1.0.6"],
"LGPL-2.1-only": [
"CC0-1.0",
"LGPL-2.0-or-later",
"LGPL-2.1-only",
"LGPL-2.1-or-later",
"MIT",
"BSD-2-Clause",
"BSD-3-Clause",
"bzip2-1.0.6"],
"LGPL-3.0-only": [
"CC0-1.0",
"LGPL-2.0-or-later",
"LGPL-2.1-or-later",
"LGPL-3.0-only",
"LGPL-3.0-or-later",
"LicenseRef-KDE-Accepted-LGPL",
"MIT",
"BSD-2-Clause",
"BSD-3-Clause",
"bzip2-1.0.6"],
"GPL-2.0-only": [
"CC0-1.0",
"LGPL-2.0-only",
"LGPL-2.1-only",
"LGPL-2.0-or-later",
"LGPL-2.1-or-later",
"GPL-2.0-only",
"GPL-2.0-or-later",
"MIT",
"BSD-2-Clause",
"BSD-3-Clause",
"bzip2-1.0.6"],
"GPL-3.0-only": [
"CC0-1.0",
"LGPL-2.0-or-later",
"LGPL-2.1-or-later",
"LGPL-3.0-only",
"LGPL-3.0-or-later",
"GPL-2.0-or-later",
"GPL-3.0-only",
"GPL-3.0-or-later",
"LicenseRef-KDE-Accepted-GPL",
"LicenseRef-KDE-Accepted-LGPL",
"MIT",
"BSD-2-Clause",
"BSD-3-Clause",
"bzip2-1.0.6"]
}
def check_outbound_license(license, files, spdxDictionary):
"""
Asserts that the list of source files @p files, when combined into
a library or executable, can be delivered under the combined license @p license .
"""
print("Checking Target License: " + license)
if not license in compatibilityMatrix:
print("Error: unknown license selected")
return False
allLicensesAreCompatible = True
for sourceFile in files:
compatible = False
sourceFileStripped = sourceFile.strip()
for fileLicense in spdxDictionary[sourceFileStripped]:
if fileLicense in compatibilityMatrix[license]:
compatible = True
print("OK " + sourceFileStripped + " : " + fileLicense)
if not compatible:
allLicensesAreCompatible = False
print("-- " + sourceFileStripped + " : ( " + ", ".join([str(i) for i in spdxDictionary[sourceFileStripped]]) + " )")
return allLicensesAreCompatible
if __name__ == '__main__':
print("Parsing SPDX BOM file")
import sys
import argparse
# parse commands
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--license", help="set outbound license to test")
parser.add_argument("-s", "--spdx", help="spdx bill-of-materials file")
parser.add_argument("-i", "--input", help="input file with list of source files to test")
args = parser.parse_args()
# TODO check if required arguments are present and give meaningful feedback
# collect name and licenses from SPDX blocks
spdxDictionary = {}
fileName = ""
licenses = []
f = open(args.spdx, "r")
for line in f:
if line.startswith("FileName:"):
# strip "FileName: "
# thus name expected to start with "./", which is relative to CMAKE_SOURCE_DIR
fileName = line[10:].strip()
if line.startswith("LicenseInfoInFile:"):
licenses.append(line[19:].strip())
if line == '' or line == "\n":
spdxDictionary[fileName] = licenses
fileName = ""
licenses = []
f.close()
spdxDictionary[fileName] = licenses
# read file with list of test files
f = open(args.input, "r")
testfiles = f.readlines()
f.close()
if check_outbound_license(args.license, testfiles, spdxDictionary) is True:
sys.exit(0)
# in any other case, return error code
sys.exit(1)
|