-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy path__init__.py
More file actions
208 lines (169 loc) · 5.03 KB
/
__init__.py
File metadata and controls
208 lines (169 loc) · 5.03 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""Define a foss compatability license_matrix.
Standard disclaimer:: I am not a lawyer and there is no guarantee that the
information provided here is complete or correct. Do not take this as legal
advice on foss license compatability
https://en.wikipedia.org/wiki/IANAL
Types of license/ compatability
Public Domain
- Unlicense
Permissive Compatible
Permissive license compatible with gpl
- Mit
- Boost
- Bsd
- Isc
- Ncsa
Permissive Not Compatible
Permissive license NOT compatible with gpl
- Apache
- Eclipse
- Academic Free
Copyleft
permissive -> lgpl 2.1 -> gpl 2
permissive -> lgpl 3 -> gpl 3 -> agpl
permissive -> mpl -> gpl -> agpl (3 only)
permissive (any) -> EU
EU -> gpl -> agpl (3 only)
"""
from __future__ import annotations
import csv
from pathlib import Path
from loguru import logger
from licensecheck.types import JOINS, ucstr
from licensecheck.types import License as L
THISDIR = Path(__file__).resolve().parent
with Path(THISDIR / "matrix.csv").open(mode="r", newline="", encoding="utf-8") as csv_file:
LICENSE_MATRIX = list(csv.reader(csv_file))
termToLicense = {
"UNKNOWN": L.UNKNOWN,
"PUBLIC DOMAIN": L.PUBLIC,
"CC-PDDC": L.PUBLIC,
"CC0-1.0": L.PUBLIC,
"UNLICENSE": L.UNLICENSE,
"WTFPL": L.UNLICENSE,
"BOOST": L.BOOST,
"BSL-1.0": L.BOOST,
"MIT": L.MIT,
"BSD": L.BSD,
"ISC": L.ISC,
"NCSA": L.NCSA,
"PYTHON": L.PSFL,
"PSF-2.0": L.PSFL,
"APACHE": L.APACHE,
"ECLIPSE": L.ECLIPSE,
"EPL-1.0": L.ECLIPSE,
"EPL-2.0": L.ECLIPSE,
"AFL": L.ACADEMIC_FREE,
"LGPLV2+": L.LGPL_2_PLUS,
"LGPL-2.0-OR-LATER": L.LGPL_2_PLUS,
"LGPLV3+": L.LGPL_3_PLUS,
"LGPL-3.0-OR-LATER": L.LGPL_3_PLUS,
"LGPL-2.0-ONLY": L.LGPL_2,
"LGPLV2": L.LGPL_2,
"LGPL-3.0-ONLY": L.LGPL_3,
"LGPLV3": L.LGPL_3,
"LGPL": L.LGPL_X,
"AGPL": L.AGPL_3_PLUS,
"GNU AFFERO GENERAL PUBLIC LICENSE V3": L.AGPL_3_PLUS,
"GPL-2.0-OR-LATER": L.GPL_2_PLUS,
"GPLV2+": L.GPL_2_PLUS,
"GPL-3.0-OR-LATER": L.GPL_3_PLUS,
"GPLV3+": L.GPL_3_PLUS,
"GPLV2": L.GPL_2,
"GPL-2.0": L.GPL_2,
"GPLV3": L.GPL_3,
"GPL-3.0": L.GPL_3,
"GPL": L.GPL_X,
"MPL": L.MPL,
"EUPL": L.EU,
"PROPRIETARY": L.PROPRIETARY,
}
def licenseLookup(licenseStr: ucstr, ignoreLicenses: list[ucstr] | None = None) -> L:
"""Identify a license from an uppercase string representation of a license.
:param ucstr licenseStr: uppercase string representation of a license
:param list[ucstr] | None ignoreLicenses: licenses to ignore, defaults to None
:return L: License represented by licenseStr
"""
if len(licenseStr or "") < 1:
return L.NO_LICENSE
for liceStr, lice in termToLicense.items():
if liceStr.strip() in licenseStr:
return lice
if licenseStr not in (ignoreLicenses or ""):
logger.warning(f"'{licenseStr}' License not identified so falling back to UNKNOWN")
return L.UNKNOWN
def licenseType(lice: ucstr, ignoreLicenses: list[ucstr] | None = None) -> list[L]:
"""Return a list of license types from a license string.
Args:
----
lice (ucstr): license name
ignoreLicenses (list[ucstr]): a list of licenses to ignore (skipped, compat may still be
False)
Returns:
-------
list[L]: the license
"""
if len(lice or "") < 1:
return [L.NO_LICENSE]
return [licenseLookup(ucstr(x), ignoreLicenses) for x in lice.split(JOINS)]
def depCompatWMyLice(
myLicense: L,
depLice: list[L],
ignoreLicenses: list[L] | None = None,
failLicenses: list[L] | None = None,
onlyLicenses: list[L] | None = None,
) -> bool:
"""Identify if the end user license is compatible with the dependency license(s).
Args:
----
myLicense (L): end user license to check
depLice (list[L]): dependency license
ignoreLicenses (list[L], optional): list of licenses to ignore. Defaults to None.
failLicenses (list[L], optional): list of licenses to fail on. Defaults to None.
onlyLicenses (list[L], optional): list of allowed licenses. Defaults to None.
Returns:
-------
bool: True if compatible, otherwise False
"""
# Protect against None
failLicenses = failLicenses or []
ignoreLicenses = ignoreLicenses or []
onlyLicenses = onlyLicenses or []
return any(
liceCompat(
myLicense,
lice,
ignoreLicenses,
failLicenses,
onlyLicenses,
)
for lice in depLice
)
def liceCompat(
myLicense: L,
lice: L,
ignoreLicenses: list[L],
failLicenses: list[L],
onlyLicenses: list[L],
) -> bool:
"""Identify if the end user license is compatible with the dependency license.
:param L myLicense: end user license
:param L lice: dependency license
:param list[L] ignoreLicenses: list of licenses to ignore. Defaults to None.
:param list[L] failLicenses: list of licenses to fail on. Defaults to None.
:param list[L] onlyLicenses: list of allowed licenses. Defaults to None.
:return bool: True if compatible, otherwise False
"""
if lice in failLicenses:
return False
if lice in ignoreLicenses:
return True
if len(onlyLicenses) > 0 and (lice not in onlyLicenses):
return False
licenses = list(L)
row, col = licenses.index(myLicense) + 1, licenses.index(lice) + 1
try:
return LICENSE_MATRIX[row][col] == "1"
except KeyError:
logger.warning(f"Licenses {myLicense} and {lice} cannot be compared")
return False