-
-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathapi.py
More file actions
344 lines (285 loc) · 11.9 KB
/
Copy pathapi.py
File metadata and controls
344 lines (285 loc) · 11.9 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
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
341
342
343
344
#
# 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 = False
logger = logging.getLogger(__name__)
if TRACE:
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
"""
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
copyrights = []
holders = []
authors = []
for dtype, value, start, end in detect_copyrights(location, deadline=deadline):
if dtype == 'copyrights':
copyrights.append(
dict([
('value', value),
('start_line', start),
('end_line', end)
])
)
elif dtype == 'holders':
holders.append(
dict([
('value', value),
('start_line', start),
('end_line', end)
])
)
elif dtype == 'authors':
authors.append(
dict([
('value', value),
('start_line', start),
('end_line', end)
])
)
results = dict([
('copyrights', copyrights),
('holders', holders),
('authors', authors),
])
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/{}'
def get_licenses(location, min_score=0,
include_text=False, license_text_diagnostics=False,
license_url_template=SCANCODE_LICENSEDB_URL,
deadline=sys.maxsize, **kwargs):
"""
Return a mapping or detected_licenses for licenses detected in the file at
`location`
This mapping contains two keys:
- 'licenses' with a value that is list of mappings of license information.
- 'license_expressions' with a value that is list of license expression
strings.
`minimum_score` is a minimum score threshold from 0 to 100. The default is 0
means that all license matches are returned. Otherwise, matches with a score
below `minimum_score` are 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` percentage to
indicate the overall proportion of detected license text and license notice
words in the file. This is used to determine if a file contains mostly
licensing information.
"""
from licensedcode import cache
from licensedcode.spans import Span
idx = cache.get_index()
detected_licenses = []
detected_expressions = []
matches = idx.match(
location=location, min_score=min_score, deadline=deadline, **kwargs
)
qspans = []
match = None
for match in matches:
qspans.append(match.qspan)
detected_expressions.append(match.rule.license_expression)
detected_licenses.extend(
_licenses_data_from_match(
match=match,
include_text=include_text,
license_text_diagnostics=license_text_diagnostics,
license_url_template=license_url_template)
)
percentage_of_license_text = 0
if match:
# we need at least one match to compute a license_coverage
matched_tokens_length = len(Span().union(*qspans))
query_tokens_length = match.query.tokens_length(with_unknown=True)
percentage_of_license_text = round((matched_tokens_length / query_tokens_length) * 100, 2)
detected_spdx_expressions = []
return dict([
('licenses', detected_licenses),
('license_expressions', detected_expressions),
('spdx_license_expressions', detected_spdx_expressions),
('percentage_of_license_text', percentage_of_license_text),
])
def _licenses_data_from_match(
match, include_text=False, license_text_diagnostics=False,
license_url_template=SCANCODE_LICENSEDB_URL):
"""
Return a list of "licenses" scan data built from a license match.
Used directly only internally for testing.
"""
from licensedcode import cache
licenses = cache.get_licenses_db()
matched_text = None
if include_text:
if license_text_diagnostics:
matched_text = match.matched_text(whole_lines=False, highlight=True)
else:
matched_text = match.matched_text(whole_lines=True, highlight=False)
SCANCODE_BASE_URL = 'https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses'
SCANCODE_LICENSE_TEXT_URL = SCANCODE_BASE_URL + '/{}.LICENSE'
SCANCODE_LICENSE_DATA_URL = SCANCODE_BASE_URL + '/{}.yml'
detected_licenses = []
for license_key in match.rule.license_keys():
lic = licenses.get(license_key)
result = {}
detected_licenses.append(result)
result['key'] = lic.key
result['score'] = match.score()
result['name'] = lic.name
result['short_name'] = lic.short_name
result['category'] = lic.category
result['is_exception'] = lic.is_exception
result['is_unknown'] = lic.is_unknown
result['owner'] = lic.owner
result['homepage_url'] = lic.homepage_url
result['text_url'] = lic.text_urls[0] if lic.text_urls else ''
result['reference_url'] = license_url_template.format(lic.key)
result['scancode_text_url'] = SCANCODE_LICENSE_TEXT_URL.format(lic.key)
result['scancode_data_url'] = SCANCODE_LICENSE_DATA_URL.format(lic.key)
spdx_key = lic.spdx_license_key
result['spdx_license_key'] = spdx_key
if spdx_key:
is_license_ref = spdx_key.lower().startswith('licenseref-')
if is_license_ref:
spdx_url = SCANCODE_LICENSE_TEXT_URL.format(lic.key)
else:
spdx_key = lic.spdx_license_key.rstrip('+')
spdx_url = SPDX_LICENSE_URL.format(spdx_key)
else:
spdx_url = ''
result['spdx_url'] = spdx_url
result['start_line'] = match.start_line
result['end_line'] = match.end_line
matched_rule = result['matched_rule'] = {}
matched_rule['identifier'] = match.rule.identifier
matched_rule['license_expression'] = match.rule.license_expression
matched_rule['licenses'] = match.rule.license_keys()
matched_rule['referenced_filenames'] = match.rule.referenced_filenames
matched_rule['is_license_text'] = match.rule.is_license_text
matched_rule['is_license_notice'] = match.rule.is_license_notice
matched_rule['is_license_reference'] = match.rule.is_license_reference
matched_rule['is_license_tag'] = match.rule.is_license_tag
matched_rule['is_license_intro'] = match.rule.is_license_intro
matched_rule['has_unknown'] = match.rule.has_unknown
matched_rule['matcher'] = match.matcher
matched_rule['rule_length'] = match.rule.length
matched_rule['matched_length'] = match.len()
matched_rule['match_coverage'] = match.coverage()
matched_rule['rule_relevance'] = match.rule.relevance
# FIXME: for sanity this should always be included?????
if include_text:
result['matched_text'] = matched_text
return detected_licenses
SCANCODE_DEBUG_PACKAGE_API = os.environ.get('SCANCODE_DEBUG_PACKAGE_API', False)
def get_package_manifest_info(location, **kwargs):
"""
Return a mapping of package manifest information detected in the
file at `location`.
Note that all exceptions are caught if there are any errors while parsing a
package manifest.
"""
from packagedcode.recognize import recognize_package_manifests
try:
recognized_package_manifests = recognize_package_manifests(location)
if recognized_package_manifests:
return dict(package_manifests=[
package_manifest.to_dict()
for package_manifest in recognized_package_manifests
])
except Exception as e:
if TRACE:
logger.error('get_package_info: {}: Exception: {}'.format(location, e))
if SCANCODE_DEBUG_PACKAGE_API:
raise
else:
# attention: we are swallowing ALL exceptions here!
pass
return dict(package_manifests=[])
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