|
| 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') |
0 commit comments