Skip to content

Commit 91b5f72

Browse files
authored
Merge pull request #2244 from nexB/2208-yaml-output
Add basic YAML output plugin #2208
2 parents 5e0c6aa + 33d3714 commit 91b5f72

24 files changed

Lines changed: 672 additions & 6009 deletions

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ scancode_output =
192192
jsonlines = formattedcode.output_jsonlines:JsonLinesOutput
193193
template = formattedcode.output_html:CustomTemplateOutput
194194
debian = formattedcode.output_debian:DebianCopyrightOutput
195+
yaml = formattedcode.output_yaml:YamlOutput
195196

196197

197198
[tool:pytest]

src/formattedcode/output_json.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ def write_results(codebase, output_file, pretty=False, **kwargs):
9898
close_fd = True
9999

100100
# Begin wri'w' JSON to `output_file`
101-
with jsonstreams.Stream(jsonstreams.Type.object, fd=output_file, close_fd=close_fd, **jsonstreams_kwargs) as s:
101+
with jsonstreams.Stream(
102+
jsonstreams.Type.object,
103+
fd=output_file,
104+
close_fd=close_fd,
105+
**jsonstreams_kwargs
106+
) as s:
102107
# Write headers
103108
codebase.add_files_count_to_current_header()
104109
codebase_headers = codebase.get_headers()

src/formattedcode/output_yaml.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# ScanCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/scancode-toolkit for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import saneyaml
11+
12+
from commoncode.cliutils import PluggableCommandLineOption
13+
from commoncode.cliutils import OUTPUT_GROUP
14+
from formattedcode import FileOptionType
15+
from formattedcode import output_json
16+
from plugincode.output import output_impl
17+
from plugincode.output import OutputPlugin
18+
19+
"""
20+
Output plugin to write scan results as YAML.
21+
"""
22+
23+
24+
@output_impl
25+
class YamlOutput(OutputPlugin):
26+
27+
options = [
28+
PluggableCommandLineOption(('--yaml', 'output_yaml',),
29+
type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
30+
metavar='FILE',
31+
help='Write scan output as YAML to FILE.',
32+
help_group=OUTPUT_GROUP,
33+
sort_order=20
34+
),
35+
]
36+
37+
def is_enabled(self, output_yaml, **kwargs):
38+
return output_yaml
39+
40+
def process_codebase(self, codebase, output_yaml, **kwargs):
41+
results = output_json.get_results(codebase, as_list=True, **kwargs)
42+
write_yaml(results, output_file=output_yaml, pretty=False)
43+
44+
45+
def write_yaml(results, output_file, **kwargs):
46+
"""
47+
Write `results` to the `output_file` opened file-like object.
48+
"""
49+
output_file.write(saneyaml.dump(results, indent=4))
50+
output_file.write('\n')

src/scancode/cli_test_utils.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818

1919
def run_scan_plain(
20-
options,
21-
cwd=None,
22-
test_mode=True,
23-
expected_rc=0,
24-
env=None,
20+
options,
21+
cwd=None,
22+
test_mode=True,
23+
expected_rc=0,
24+
env=None,
2525
retry=True,
2626
):
2727
"""
@@ -229,6 +229,22 @@ def load_json_result_from_string(string, remove_file_date=False):
229229
return scan_results
230230

231231

232+
def cleanup_scan(scan_results, remove_file_date=False):
233+
"""
234+
Cleanup in place the ``scan_results`` mapping for dates, headers and
235+
other variable data that break tests otherwise.
236+
"""
237+
# clean new headers attributes
238+
streamline_headers(scan_results.get('headers', []))
239+
# clean file_level attributes
240+
for scanned_file in scan_results['files']:
241+
streamline_scanned_file(scanned_file, remove_file_date)
242+
243+
# TODO: remove sort, this should no longer be needed
244+
scan_results['files'].sort(key=lambda x: x['path'])
245+
return scan_results
246+
247+
232248
def streamline_errors(errors):
233249
"""
234250
Modify the `errors` list in place to make it easier to test

0 commit comments

Comments
 (0)