Ejemplo n.º 1
0
    def test_custom_temaplte(self):
        test_file = get_test_loc('test_attrib/scancode_custom_template/clean-text-0.3.0-mod-lceupi.json')
        custom_template =  get_test_loc('test_attrib/scancode_custom_template/scancode.template')
        errors, abouts = util.load_inventory(test_file, scancode=True)
        assert not errors

        lic_dict = {'isc': {'key': 'isc', 'short_name': 'ISC License', 'name': 'ISC License',
                            'category': 'Permissive', 'owner': 'ISC - Internet Systems Consortium',
                            'homepage_url': 'https://www.isc.org/software/license', 'notes': 'Per SPDX.org, this license is OSI certified.',
                            'spdx_license_key': 'ISC', 'text_urls': ['http://fedoraproject.org/wiki/Licensing:MIT#Old_Style_with_legal_disclaimer_2', 'http://openbsd.wikia.com/wiki/OpenBSD%27s_BSD_license', 'http://opensource.org/licenses/isc-license.txt', 'https://www.isc.org/software/license'],
                            'osi_url': 'http://opensource.org/licenses/isc-license.txt', 'other_urls': ['http://openbsd.wikia.com/wiki/OpenBSD%27s_BSD_license', 'http://www.isc.org/software/license', 'http://www.opensource.org/licenses/ISC', 'https://opensource.org/licenses/ISC', 'https://www.isc.org/downloads/software-support-policy/isc-license/', 'https://www.isc.org/isc-license-1.0.html'],
                            'license_text': 'Permission to use, copy, modify, and/or distribute this software for any purpose\nwith or without fee is hereby granted, provided that the above copyright notice\nand this permission notice appear in all copies.\n\nTHE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS\nOF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\nTORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\nTHIS SOFTWARE.\n'}}

        error, result = attrib.generate_from_file(abouts, lic_dict, min_license_score=0, template_loc=custom_template)
        assert not error

        expected_file = get_test_loc(
            'test_attrib/scancode_custom_template/expect.html')
        with open(expected_file) as exp:
            expected = exp.read()

        # strip the timestamp: the timestamp is wrapped in italic block
        result = remove_timestamp(result)
        expected = remove_timestamp(expected)
        assert expected == result
Ejemplo n.º 2
0
    def test_load_inventory_simple_xlsx(self):
        location = get_test_loc('test_util/load/simple_sample.xlsx')
        base_dir = get_temp_dir()
        errors, abouts = util.load_inventory(location)
        assert errors == []

        assert abouts[0].name.value == 'cryptohash-sha256'
        assert abouts[1].name.value == 'some_component'

        assert abouts[0].version.value == 'v 0.11.100.1'
        assert abouts[1].version.value == 'v 0.0.1'

        assert abouts[0].license_expression.value == 'bsd-new and mit'
        assert abouts[1].license_expression.value == 'mit'
Ejemplo n.º 3
0
    def test_generate_with_default_template(self):
        test_file = get_test_loc('test_attrib/default_template/simple_sample.csv')
        errors, abouts = util.load_inventory(test_file)
        assert not errors

        lic_dict = {'bsd-new': 
                    {'key': 'bsd-new', 'short_name': 'BSD-3-Clause', 'name': 'BSD-3-Clause',
                     'category': 'Permissive', 'owner': 'Regents of the University of California',
                     'homepage_url': 'http://www.opensource.org/licenses/BSD-3-Clause',
                     'notes': 'Per SPDX.org, this license is OSI certified.',
                     'spdx_license_key': 'BSD-3-Clause', 'osi_license_key': 'BSD-3',
                     'text_urls': ['http://www.opensource.org/licenses/BSD-3-Clause'],
                     'osi_url': 'http://www.opensource.org/licenses/BSD-3-Clause',
                     'other_urls': ['http://framework.zend.com/license/new-bsd', 'https://opensource.org/licenses/BSD-3-Clause'],
                     'license_text': 'Redistribution and use in source and binary forms, with or without modification,\nare permitted provided that the following conditions are met:\n\nRedistributions of source code must retain the above copyright notice, this list\nof conditions and the following disclaimer.\n\nRedistributions in binary form must reproduce the above copyright notice, this\nlist of conditions and the following disclaimer in the documentation and/or\nother materials provided with the distribution.\n\nNeither the name of the ORGANIZATION nor the names of its contributors may be\nused to endorse or promote products derived from this software without specific\nprior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\nARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS\nBE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\nGOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\nHOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\nLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF\nTHE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.'},
                    'mit': {'key': 'mit', 'short_name': 'MIT License', 'name': 'MIT License',
                            'category': 'Permissive', 'owner': 'MIT',
                            'homepage_url': 'http://opensource.org/licenses/mit-license.php',
                            'notes': 'Per SPDX.org, this license is OSI certified.', 'spdx_license_key': 'MIT',
                            'text_urls': ['http://opensource.org/licenses/mit-license.php'],
                            'osi_url': 'http://www.opensource.org/licenses/MIT',
                            'other_urls': ['https://opensource.com/article/18/3/patent-grant-mit-license', 'https://opensource.com/article/19/4/history-mit-license', 'https://opensource.org/licenses/MIT'],
                            'license_text': 'Permission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n"Software"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'}
                    }
        error, result = attrib.generate_from_file(abouts, lic_dict, min_license_score=0)
        assert not error

        expected_file = get_test_loc(
            'test_attrib/default_template/expect.html')
        with open(expected_file) as exp:
            expected = exp.read()

        # strip the timestamp: the timestamp is wrapped in italic block
        result = remove_timestamp(result)
        expected = remove_timestamp(expected)
        assert expected == result
Ejemplo n.º 4
0
    def test_convert_object_to_dict(self):
        location = get_test_loc('test_util/load/simple_sample.csv')
        base_dir = get_temp_dir()
        errors, abouts = util.load_inventory(location)
        assert errors == []

        expected = {
            'name': 'cryptohash-sha256',
            'version': 'v 0.11.100.1',
            'download_url': '',
            'homepage_url': '',
            'package_url': '',
            'notes': '',
            'license_expression': 'bsd-new and mit',
            'license_key': ['bsd-new', 'mit'],
            'license_name': '',
            'license_file': '',
            'license_url': '',
            'copyright': '',
            'notice_file': '',
            'path': '/project/cryptohash-sha256'
        }
        results = util.convert_object_to_dict(abouts[0])
        assert results == expected
Ejemplo n.º 5
0
def attributecode(input, output, configuration, djc, scancode,
                  min_license_score, reference, template, vartext, quiet,
                  verbose):
    """
    Generate attribution from a JSON, CSV or Excel file.
    """
    if scancode:
        if not input.endswith('.json'):
            msg = 'The input file from scancode toolkit needs to be in JSON format.'
            click.echo(msg)
            sys.exit(1)
        if not min_license_score:
            min_license_score = DEFAULT_LICENSE_SCORE

    if min_license_score:
        if not scancode:
            msg = (
                'This option requires a JSON file generated by scancode toolkit as the input. '
                + 'The "--scancode" option is required.')
            click.echo(msg)
            sys.exit(1)

    errors, abouts = load_inventory(location=input,
                                    configuration=configuration,
                                    scancode=scancode,
                                    reference_dir=reference)

    license_dict, lic_errors = pre_process_and_fetch_license_dict(
        abouts, djc, scancode, reference)
    errors.extend(lic_errors)
    sorted_license_dict = sorted(license_dict)

    # Read the license_file and store in a dictionary
    for about in abouts:
        if about.license_file.value or about.notice_file.value:
            if not reference:
                msg = '"license_file" / "notice_file" field contains value. Use `--reference` to indicate its parent directory.'
                click.echo(msg)
                sys.exit(1)
            if about.license_file.value:
                file_name = about.license_file.value
                error, text = get_file_text(file_name, reference)
                if not error:
                    about.license_file.value = {}
                    about.license_file.value[file_name] = text
                else:
                    errors.append(error)
            if about.notice_file.value:
                file_name = about.notice_file.value
                error, text = get_file_text(file_name, reference)
                if not error:
                    about.notice_file.value = {}
                    about.notice_file.value[file_name] = text
                else:
                    errors.append(error)

    rendered = ''
    if abouts:
        attrib_errors, rendered = generate_attribution_doc(
            abouts=abouts,
            license_dict=dict(sorted(license_dict.items())),
            output_location=output,
            min_license_score=min_license_score,
            template_loc=template,
            variables=vartext,
        )
        errors.extend(attrib_errors)

    errors = unique(errors)
    errors_count = report_errors(errors,
                                 quiet,
                                 verbose,
                                 log_file_loc=output + '-error.log')

    if rendered:
        # Check if the default template is used
        import filecmp
        default_template = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            '../../templates/default_html.template')
        if filecmp.cmp(default_template, template):
            num_comps = number_of_component_generated_from_default_template(
                output)
            msg = '{num_comps} component(s) is/are in the generated attribution at the {output}'.format(
                **locals())
        else:
            msg = 'Attribution generated at: {output}'.format(**locals())
        click.echo(msg)
    else:
        msg = 'Attribution generation failed.'
        click.echo(msg)

    sys.exit(errors_count)