Пример #1
0
class DebianCopyrightOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(
            (
                '--debian',
                'output_debian',
            ),
            type=FileOptionType(mode='w', lazy=True),
            metavar='FILE',
            help=
            'Write scan output in machine-readable Debian copyright format to FILE.',
            help_group=OUTPUT_GROUP,

            # this is temporary , we should not needed these options explicitly
            # but instead adapt to the available data
            required_options=['copyright', 'license', 'license_text'],
            sort_order=60),
    ]

    def is_enabled(self, output_debian, **kwargs):
        return output_debian

    def process_codebase(self, codebase, output_debian, **kwargs):
        debian_copyright = build_debian_copyright(codebase, **kwargs)
        write_debian_copyright(
            debian_copyright=debian_copyright,
            output_file=output_debian,
        )
Пример #2
0
class CycloneDxXmlOutput(OutputPlugin):
    """
    Output plugin to write scan results in CycloneDX XML format.
    For additional information on the format,
    please see https://cyclonedx.org/specification/overview/
    """

    options = [
        PluggableCommandLineOption(
            (
                '--cyclonedx-xml',
                'output_cyclonedx_xml',
            ),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            metavar='FILE',
            help='Write scan output in CycloneDX XML format to FILE.',
            help_group=OUTPUT_GROUP,
            sort_order=70,
        ),
    ]

    def is_enabled(self, output_cyclonedx_xml, **kwargs):
        return output_cyclonedx_xml

    def process_codebase(self, codebase, output_cyclonedx_xml, **kwargs):
        bom = CycloneDxBom.from_codebase(codebase)
        bom.write_xml(output_file=output_cyclonedx_xml)
Пример #3
0
class SpdxRdfOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(
            ('--spdx-rdf', ),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            metavar='FILE',
            help='Write scan output as SPDX RDF to FILE.',
            help_group=OUTPUT_GROUP)
    ]

    def is_enabled(self, spdx_rdf, **kwargs):
        return spdx_rdf

    def process_codebase(self, codebase, spdx_rdf, **kwargs):
        check_sha1(codebase)
        files = self.get_files(codebase, **kwargs)
        header = codebase.get_or_create_current_header()
        tool_name = header.tool_name
        tool_version = header.tool_version
        notice = header.notice
        input = kwargs.get('input', '')  # NOQA

        write_spdx(spdx_rdf,
                   files,
                   tool_name,
                   tool_version,
                   notice,
                   input,
                   as_tagvalue=False)
Пример #4
0
class JsonLinesOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(
            (
                '--json-lines',
                'output_json_lines',
            ),
            type=FileOptionType(mode=mode, 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 = OrderedDict(headers=codebase.get_headers())

        simplejson_kwargs = dict(iterable_as_array=True,
                                 encoding='utf-8',
                                 separators=(
                                     comma,
                                     colon,
                                 ))
        output_json_lines.write(simplejson.dumps(headers, **simplejson_kwargs))
        output_json_lines.write(eol)

        for name, value in codebase.attributes.to_dict().items():
            if value:
                smry = {name: value}
                output_json_lines.write(
                    simplejson.dumps(smry, **simplejson_kwargs))
                output_json_lines.write(eol)

        for scanned_file in files:
            scanned_file_line = {file_key: [scanned_file]}
            output_json_lines.write(
                simplejson.dumps(scanned_file_line, **simplejson_kwargs))
            output_json_lines.write(eol)
Пример #5
0
class JsonPrettyOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(('--json-pp', 'output_json_pp',),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            metavar='FILE',
            help='Write scan output as pretty-printed JSON to FILE.',
            help_group=OUTPUT_GROUP,
            sort_order=10),
    ]

    def is_enabled(self, output_json_pp, **kwargs):
        return output_json_pp

    def process_codebase(self, codebase, output_json_pp, **kwargs):
        write_results(codebase, output_file=output_json_pp, pretty=True, **kwargs)
Пример #6
0
class CsvOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(('--csv',),
            type=FileOptionType(mode='wb', lazy=True),
            metavar='FILE',
            help='Write scan output as CSV to FILE.',
            help_group=OUTPUT_GROUP,
            sort_order=30),
    ]

    def is_enabled(self, csv, **kwargs):
        return csv

    def process_codebase(self, codebase, csv, **kwargs):
        results = self.get_files(codebase, **kwargs)
        write_csv(results, csv)
Пример #7
0
class HtmlOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(('--html',),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            metavar='FILE',
            help='Write scan output as HTML to FILE.',
            help_group=OUTPUT_GROUP,
            sort_order=50),
    ]

    def is_enabled(self, html, **kwargs):
        return html

    def process_codebase(self, codebase, html, **kwargs):
        results = self.get_files(codebase, **kwargs)
        version = codebase.get_or_create_current_header().tool_version
        template_loc = join(TEMPLATES_DIR, 'html', 'template.html')
        output_file = html
        write_templated(output_file, results, version, template_loc)
Пример #8
0
class JsonCompactOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(
            (
                '--json',
                'output_json',
            ),
            type=FileOptionType(mode=mode, lazy=True),
            metavar='FILE',
            help='Write scan output as compact JSON to FILE.',
            help_group=OUTPUT_GROUP,
            sort_order=10),
    ]

    def is_enabled(self, output_json, **kwargs):
        return output_json

    def process_codebase(self, codebase, output_json, **kwargs):
        results = get_results(codebase, as_list=False, **kwargs)
        write_json(results, output_file=output_json, pretty=False)
Пример #9
0
class SpdxRdfOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(
            ('--spdx-rdf', ),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            metavar='FILE',
            help='Write scan output as SPDX RDF to FILE.',
            help_group=OUTPUT_GROUP)
    ]

    def is_enabled(self, spdx_rdf, **kwargs):
        return spdx_rdf

    def process_codebase(self, codebase, spdx_rdf, **kwargs):
        _process_codebase(spdx_plugin=self,
                          codebase=codebase,
                          input_path=kwargs.get('input', ''),
                          output_file=spdx_rdf,
                          as_tagvalue=False,
                          **kwargs)
Пример #10
0
class CustomTemplateOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption(
            ('--custom-output', ),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            required_options=['custom_template'],
            metavar='FILE',
            help='Write scan output to FILE formatted with '
            'the custom Jinja template file.',
            help_group=OUTPUT_GROUP,
            sort_order=60),
        PluggableCommandLineOption(
            ('--custom-template', ),
            type=click.Path(exists=True,
                            file_okay=True,
                            dir_okay=False,
                            readable=True,
                            path_type=PATH_TYPE),
            required_options=['custom_output'],
            metavar='FILE',
            help='Use this Jinja template FILE as a custom template.',
            help_group=OUTPUT_GROUP,
            sort_order=65),
    ]

    def is_enabled(self, custom_output, custom_template, **kwargs):
        return custom_output and custom_template

    def process_codebase(self, codebase, custom_output, custom_template,
                         **kwargs):
        results = self.get_files(codebase, **kwargs)
        version = codebase.get_or_create_current_header().tool_version

        if on_linux and py2:
            custom_template = fsencode(custom_template)

        template_loc = custom_template
        output_file = custom_output
        write_templated(output_file, results, version, template_loc)
Пример #11
0
class YamlOutput(OutputPlugin):

    options = [
        PluggableCommandLineOption((
            '--yaml',
            'output_yaml',
        ),
                                   type=FileOptionType(mode='w',
                                                       encoding='utf-8',
                                                       lazy=True),
                                   metavar='FILE',
                                   help='Write scan output as YAML to FILE.',
                                   help_group=OUTPUT_GROUP,
                                   sort_order=20),
    ]

    def is_enabled(self, output_yaml, **kwargs):
        return output_yaml

    def process_codebase(self, codebase, output_yaml, **kwargs):
        results = output_json.get_results(codebase, as_list=True, **kwargs)
        write_yaml(results, output_file=output_yaml, pretty=False)
Пример #12
0
class HtmlAppOutput(OutputPlugin):
    """
    Write scan output as a mini HTML application.
    """
    options = [
        PluggableCommandLineOption(('--html-app',),
            type=FileOptionType(mode='w', encoding='utf-8', lazy=True),
            metavar='FILE',
            help='(DEPRECATED: use the ScanCode Workbench app instead ) '
                  'Write scan output as a mini HTML application to FILE.',
            help_group=OUTPUT_GROUP,
            sort_order=1000),
    ]

    def is_enabled(self, html_app, **kwargs):
        return html_app

    def process_codebase(self, codebase, input, html_app, **kwargs):  # NOQA
        results = self.get_files(codebase, **kwargs)
        version = codebase.get_or_create_current_header().tool_version
        output_file = html_app
        scanned_path = input
        create_html_app(output_file, results, version, scanned_path)