Repository navigation
Expand file tree
/
Copy pathoutput_jsonlines.py
More file actions
62 lines (50 loc) · 2.14 KB
/
Copy pathoutput_jsonlines.py
File metadata and controls
62 lines (50 loc) · 2.14 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
#
# 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.
#
import json
from formattedcode import FileOptionType
from commoncode.cliutils import OUTPUT_GROUP
from commoncode.cliutils import PluggableCommandLineOption
from plugincode.output import OutputPlugin
from plugincode.output import output_impl
"""
Output plugin to write scan results as JSON lines.
"""
@output_impl
class JsonLinesOutput(OutputPlugin):
options = [
PluggableCommandLineOption(('--json-lines', 'output_json_lines',),
type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
metavar='FILE',
help='Write scan output as JSON Lines to FILE.',
help_group=OUTPUT_GROUP,
sort_order=15),
]
def is_enabled(self, output_json_lines, **kwargs):
return output_json_lines
# TODO: reuse the json output code and merge that in a single plugin
def process_codebase(self, codebase, output_json_lines, **kwargs):
# NOTE: we write as binary, not text
files = self.get_files(codebase, **kwargs)
codebase.add_files_count_to_current_header()
headers = dict(headers=codebase.get_headers())
compact_separators = (u',', u':',)
output_json_lines.write(
json.dumps(headers, separators=compact_separators))
output_json_lines.write('\n')
for name, value in codebase.attributes.to_dict().items():
if value:
smry = {name: value}
output_json_lines.write(
json.dumps(smry, separators=compact_separators))
output_json_lines.write('\n')
for scanned_file in files:
scanned_file_line = {'files': [scanned_file]}
output_json_lines.write(
json.dumps(scanned_file_line, separators=compact_separators))
output_json_lines.write('\n')