-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathapi.py
340 lines (278 loc) · 10.7 KB
/
api.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
from itertools import islice
from os.path import getsize
import logging
import os
import sys
from commoncode.filetype import get_last_modified_date
from commoncode.hash import multi_checksums
from scancode import ScancodeError
from typecode.contenttype import get_type
TRACE = os.environ.get('SCANCODE_DEBUG_API', False)
def logger_debug(*args):
pass
logger = logging.getLogger(__name__)
if TRACE:
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(
' '.join(isinstance(a, str) and a or repr(a) for a in args)
)
"""
Main scanning functions.
Each scanner is a function that accepts a location and returns a sequence of
mappings as results.
Note: this API is unstable and still evolving.
"""
def get_copyrights(
location,
deadline=sys.maxsize,
**kwargs,
):
"""
Return a mapping with a single 'copyrights' key with a value that is a list
of mappings for copyright detected in the file at `location`.
"""
from cluecode.copyrights import detect_copyrights
from cluecode.copyrights import Detection
detections = detect_copyrights(
location,
include_copyrights=True,
include_holders=True,
include_authors=True,
include_copyright_years=True,
include_copyright_allrights=False,
deadline=deadline,
)
copyrights, holders, authors = Detection.split(detections, to_dict=True)
results = dict([
('copyrights', copyrights),
('holders', holders),
('authors', authors),
])
# TODO: do something if we missed the deadline
return results
def get_emails(
location,
threshold=50,
test_slow_mode=False,
test_error_mode=False,
**kwargs,
):
"""
Return a mapping with a single 'emails' key with a value that is a list of
mappings for emails detected in the file at `location`.
Return only up to `threshold` values. Return all values if `threshold` is 0.
If test_mode is True, the scan will be slow for testing purpose and pause
for one second.
"""
if test_error_mode:
raise ScancodeError('Triggered email failure')
if test_slow_mode:
import time
time.sleep(1)
from cluecode.finder import find_emails
results = []
found_emails = ((em, ln) for (em, ln) in find_emails(location) if em)
if threshold:
found_emails = islice(found_emails, threshold)
for email, line_num in found_emails:
result = {}
results.append(result)
result['email'] = email
result['start_line'] = line_num
result['end_line'] = line_num
return dict(emails=results)
def get_urls(location, threshold=50, **kwargs):
"""
Return a mapping with a single 'urls' key with a value that is a list of
mappings for urls detected in the file at `location`.
Return only up to `threshold` values. Return all values if `threshold` is 0.
"""
from cluecode.finder import find_urls
results = []
found_urls = ((u, ln) for (u, ln) in find_urls(location) if u)
if threshold:
found_urls = islice(found_urls, threshold)
for urls, line_num in found_urls:
result = {}
results.append(result)
result['url'] = urls
result['start_line'] = line_num
result['end_line'] = line_num
return dict(urls=results)
SPDX_LICENSE_URL = 'https://spdx.org/licenses/{}'
DEJACODE_LICENSE_URL = 'https://enterprise.dejacode.com/urn/urn:dje:license:{}'
SCANCODE_LICENSEDB_URL = 'https://scancode-licensedb.aboutcode.org/{}'
SCANCODE_DATA_BASE_URL = 'https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data'
SCANCODE_LICENSE_URL = f'{SCANCODE_DATA_BASE_URL}/licenses/{{}}.LICENSE'
SCANCODE_RULE_URL = f'{SCANCODE_DATA_BASE_URL}/rules/{{}}'
def get_licenses(
location,
min_score=0,
include_text=False,
license_text_diagnostics=False,
license_diagnostics=False,
deadline=sys.maxsize,
unknown_licenses=False,
**kwargs,
):
"""
Return a mapping or license_detections for licenses detected in the file at
`location`
This mapping contains two keys:
- 'license_detections' with a value that is list of mappings of license information.
- 'detected_license_expression' with a value that is a license expression string.
`min_score` is a minimum score threshold from 0 to 100. The default is 0,
meaning that all license matches are returned. If specified, matches with a
score lower than `minimum_score` are not returned.
If `include_text` is True, matched text is included in the returned
`licenses` data as well as a file-level `percentage_of_license_text`
as the percentage of file words detected as license text or notice.
This is used to determine if a file contains mostly licensing.
If ``unknown_licenses`` is True, also detect unknown licenses.
"""
from licensedcode.cache import build_spdx_license_expression
from licensedcode.cache import get_cache
from licensedcode.detection import detect_licenses
from packagedcode.utils import combine_expressions
license_clues = []
license_detections = []
detected_expressions = []
detected_license_expression = None
detected_license_expression_spdx = None
detections = detect_licenses(
location=location,
min_score=min_score,
deadline=deadline,
unknown_licenses=unknown_licenses,
**kwargs,
)
all_qspans = []
detection = None
for detection in detections:
all_qspans.extend(detection.qspans)
if detection.license_expression is None:
detection_mapping = detection.to_dict(
include_text=include_text,
license_text_diagnostics=license_text_diagnostics,
license_diagnostics=license_diagnostics,
)
license_clues.extend(detection_mapping["matches"])
else:
detected_expressions.append(detection.license_expression)
license_detections.append(
detection.to_dict(
include_text=include_text,
license_text_diagnostics=license_text_diagnostics,
license_diagnostics=license_diagnostics,
)
)
if TRACE:
logger_debug(f"api: get_licenses: license_detections: {license_detections}")
logger_debug(f"api: get_licenses: license_clues: {license_clues}")
if detected_expressions:
detected_license_expression = combine_expressions(
expressions=detected_expressions,
relation='AND',
unique=True,
)
detected_license_expression_spdx = str(build_spdx_license_expression(
detected_license_expression,
licensing=get_cache().licensing
))
percentage_of_license_text = 0
if detection:
percentage_of_license_text = detection.percentage_license_text_of_file(all_qspans)
return dict([
('detected_license_expression', detected_license_expression),
('detected_license_expression_spdx', detected_license_expression_spdx),
('license_detections', license_detections),
('license_clues', license_clues),
('percentage_of_license_text', percentage_of_license_text),
])
SCANCODE_DEBUG_PACKAGE_API = os.environ.get('SCANCODE_DEBUG_PACKAGE_API', False)
def _get_package_data(location, application=True, system=False, package_only=False, **kwargs):
"""
Return a mapping of package manifest information detected in the file at ``location``.
Include ``application`` packages (such as pypi) and/or ``system`` packages.
Note that all exceptions are caught if there are any errors while parsing a
package manifest.
"""
assert application or system or package_only
from packagedcode.recognize import recognize_package_data
try:
return recognize_package_data(
location=location,
application=application,
system=system,
package_only=package_only,
) or []
except Exception as e:
if TRACE:
logger.error(f'_get_package_data: location: {location!r}: Exception: {e}')
if SCANCODE_DEBUG_PACKAGE_API:
raise
else:
# attention: we are swallowing ALL exceptions here!
pass
def get_package_info(location, **kwargs):
"""
Return a mapping of package information detected in the file at `location`.
This API function is DEPRECATED, use `get_package_data` instead.
"""
import warnings
warnings.warn(
"`get_package_info` is deprecated. Use `get_package_data` instead.",
DeprecationWarning,
stacklevel=1
)
packages = _get_package_data(location, **kwargs) or []
return dict(packages=[p.to_dict() for p in packages])
def get_package_data(location, application=True, system=False, package_only=False, **kwargs):
"""
Return a mapping of package manifest information detected in the file at
`location`.
Include ``application`` packages (such as pypi) and/or ``system`` packages.
"""
if TRACE:
print(' scancode.api.get_package_data: kwargs', kwargs)
package_datas = _get_package_data(
location=location,
application=application,
system=system,
package_only=package_only,
**kwargs,
) or []
return dict(package_data=[pd.to_dict() for pd in package_datas])
def get_file_info(location, **kwargs):
"""
Return a mapping of file information collected for the file at `location`.
"""
result = {}
# TODO: move date and size these to the inventory collection step???
result['date'] = get_last_modified_date(location) or None
result['size'] = getsize(location) or 0
sha1, md5, sha256 = multi_checksums(location, ('sha1', 'md5', 'sha256')).values()
result['sha1'] = sha1
result['md5'] = md5
result['sha256'] = sha256
collector = get_type(location)
result['mime_type'] = collector.mimetype_file or None
result['file_type'] = collector.filetype_file or None
result['programming_language'] = collector.programming_language or None
result['is_binary'] = bool(collector.is_binary)
result['is_text'] = bool(collector.is_text)
result['is_archive'] = bool(collector.is_archive)
result['is_media'] = bool(collector.is_media)
result['is_source'] = bool(collector.is_source)
result['is_script'] = bool(collector.is_script)
return result