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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 | class PythonAdapter(LicenseAdapter):
"""Adapter for checking Python package licenses."""
def __init__(self):
"""Initialize file handlers for Python dependency manifests."""
self.file_handlers = {
"requirements.txt": self.parse_requirements_file,
"uv.lock": self.parse_lock_files,
"poetry.lock": self.parse_lock_files,
}
def _detect_bsd_license(self, lines: list[str], license_upper: str) -> str | None:
"""
Detect BSD license variants from license text.
Parameters
----------
lines
Lines of license text to analyze
license_upper
Uppercase version of full license text
Returns
-------
BSD license type if detected, None otherwise
"""
for line in lines[:MAX_LINES_TO_SCAN]:
line_upper = line.upper()
if any(indicator in line_upper for indicator in BSD_INDICATORS):
if any(indicator in line_upper for indicator in BSD_3_INDICATORS):
return "BSD-3-Clause"
if any(indicator in line_upper for indicator in BSD_2_INDICATORS):
return "BSD-2-Clause"
return "BSD License"
if all(marker in license_upper for marker in BSD_3_STRUCTURAL_MARKERS) and any(
marker in license_upper for marker in BSD_3_ENDORSEMENT_MARKERS
):
return "BSD-3-Clause"
return None
def _detect_mit_license(self, lines: list[str]) -> bool:
"""
Detect MIT license from license text.
Parameters
----------
lines
Lines of license text to analyze
Returns
-------
True if MIT license detected, False otherwise
"""
for line in lines[:MAX_LINES_TO_SCAN]:
if any(indicator in line.upper() for indicator in MIT_INDICATORS):
return True
return False
def _detect_gpl_license(self, license_upper: str) -> str | None:
"""
Detect GPL license variants from license text.
Parameters
----------
license_upper
Uppercase version of license text
Returns
-------
GPL license type if detected, None otherwise
"""
if any(indicator in license_upper for indicator in GPL_INDICATORS):
if any(indicator in license_upper for indicator in GPL_V3_INDICATORS):
return "GPL-3.0"
if any(indicator in license_upper for indicator in GPL_V2_INDICATORS):
return "GPL-2.0"
return "GPL"
return None
def _clean_license_text(self, license_text: str) -> str:
"""
Clean and truncate verbose license text to keep only the essential license name.
Parameters
----------
license_text
The raw license text from metadata or PyPI.
Returns
-------
Cleaned license name or identifier.
"""
if not license_text or not license_text.strip():
return "Unknown"
license_text = license_text.strip()
if len(license_text) <= MAX_LICENSE_TEXT_LENGTH:
return license_text
lines = license_text.split("\n")
first_line = lines[0].strip()
license_upper = license_text.upper()
bsd_license = self._detect_bsd_license(lines, license_upper)
if bsd_license:
return bsd_license
if self._detect_mit_license(lines):
return "MIT"
gpl_license = self._detect_gpl_license(license_upper)
if gpl_license:
return gpl_license
if first_line and not first_line.startswith("Copyright"):
return first_line[:MAX_FIRST_LINE_LENGTH]
return "Unknown"
def _get_license_from_pypi(self, pkg_name: str) -> str:
"""
Fetch license information from PyPI JSON API.
Parameters
----------
pkg_name
The name of the package to query.
Returns
-------
The license of the package as a string.
"""
try:
url = f"https://pypi.org/pypi/{pkg_name}/json"
with urllib.request.urlopen(url, timeout=PYPI_TIMEOUT_SECONDS) as response:
data = json.loads(response.read().decode())
info = data.get("info", {})
license_info = info.get("license")
if license_info and license_info.strip() and license_info != "UNKNOWN":
return self._clean_license_text(license_info)
classifiers = info.get("classifiers", [])
license_classifier = next((c for c in classifiers if c.startswith("License ::")), None)
if license_classifier:
parts = license_classifier.split(" :: ")
if len(parts) >= 3:
return self._clean_license_text(parts[-1])
return "Unknown"
except (urllib.error.URLError, urllib.error.HTTPError, json.JSONDecodeError):
return "Unknown"
except Exception:
return "Unknown"
def _get_metadata(self, pkg_name: str) -> str:
"""
Get the license metadata for a given package.
Parameters
----------
pkg_name
The name of the package to query.
Returns
-------
The license of the package as a string.
"""
try:
meta = metadata.metadata(str(pkg_name))
except metadata.PackageNotFoundError:
return self._get_license_from_pypi(str(pkg_name))
license_field = meta.get("License")
if license_field and license_field.strip() and license_field != "UNKNOWN":
return self._clean_license_text(license_field)
license_expr = meta.get("License-Expression")
if license_expr and license_expr.strip():
return self._clean_license_text(license_expr)
classifiers = meta.get_all("Classifier") or []
license_classifier = next((c for c in classifiers if c.startswith("License ::")), None)
if license_classifier:
parts = license_classifier.split(" :: ")
if len(parts) >= 3:
return self._clean_license_text(parts[-1])
return self._get_license_from_pypi(str(pkg_name))
def parse_requirements_file(self, content: str) -> list[dict[str, str]]:
"""
Parse requirements.txt file
Parameters
----------
content
Content of the requirements.txt
Returns
-------
A list of dictionaries with package and license information
"""
lines = content.splitlines()
packages = []
for raw_line in lines:
stripped = raw_line.strip()
if not stripped or stripped.startswith("#"):
continue
pkg_name = re.split(r"[=<>!~]", stripped)[0].strip()
if pkg_name:
packages.append({"package": pkg_name, "license": self._get_metadata(pkg_name)})
return packages
def parse_lock_files(self, content: str) -> list[dict[str, str]]:
"""
Parse uv.lock or poetry.lock file
Parameters
----------
content
Content of the lock file
Returns
-------
A list of dictionaries with package and license information
"""
lock_data = tomllib.loads(content)
packages = lock_data.get("package", [])
return [
{"package": pkg_name, "license": self._get_metadata(pkg_name)}
for p in packages
if (pkg_name := p.get("name"))
]
|