コード例 #1
0
    def process_codebase(self, codebase, licenses_reference, **kwargs):
        from licensedcode.cache import get_licenses_db
        licensing = Licensing()

        license_keys = set()

        for resource in codebase.walk():
            licexps = getattr(resource, 'license_expressions', []) or []
            for expression in licexps:
                if expression:
                    license_keys.update(licensing.license_keys(expression))

        packages = getattr(codebase, 'packages', []) or []
        for package in packages:
            # FXIME: license_expression attribute name is changing soon
            expression = package.get('license_expression')
            if expression:
                license_keys.update(licensing.license_keys(expression))

                resource.save(codebase)

        db = get_licenses_db()
        for key in sorted(license_keys):
            license_details = db[key].to_dict(
                include_ignorables=False,
                include_text=True,
            )
            codebase.attributes.licenses_reference.append(license_details)
コード例 #2
0
def get_declared_license_keys_in_packages(codebase):
    """
    Return a list of declared license keys found in packages.

    A package manifest (such as Maven POM file or an npm package.json file)
    contains structured declared license information. This is further normalized
    as a license_expression. We extract the list of licenses from the normalized
    license expressions.
    """
    packages = chain.from_iterable(
        getattr(res, 'packages', []) or []
        for res in codebase.walk(topdown=True))

    licensing = Licensing()
    detected_good_licenses = []
    for package in packages:
        expression = package.get('license_expression')
        if expression:
            exp = licensing.parse(expression,
                                  validate=False,
                                  strict=False,
                                  simple=True)
            keys = licensing.license_keys(exp, unique=True)
            detected_good_licenses.extend(keys)
    return detected_good_licenses
コード例 #3
0
ファイル: model.py プロジェクト: nexB/attributecode
def parse_license_expression(lic_expression):
    licensing = Licensing()
    lic_list = []
    special_char = detect_special_char(lic_expression)
    if not special_char:
        # Parse the license expression and save it into a list
        lic_list = licensing.license_keys(lic_expression)
    return special_char, lic_list
コード例 #4
0
def determine_licences_not_in_list(
        licence_expression: str, licence_list: Iterator[str]) -> Iterator[str]:
    """Determines all the licences in an expression which are not in list."""
    licensing_util = Licensing()
    licence_keys = licensing_util.license_keys(
        _parse_licence_expression(licensing_util, licence_expression))
    for licence in licence_keys:
        if licence not in licence_list:
            yield licence
コード例 #5
0
    def test_get_expression_without_lid(self):
        licensing = Licensing()
        spdx_symbols = get_spdx_symbols()
        unknown_symbol = get_unknown_spdx_symbol()
        line_text = ('EPL-2.0 OR Apache-2.0 OR '
                     'GPL-2.0 WITH Classpath-exception-2.0 OR '
                     'GPL-2.0')
        expression = get_expression(line_text, licensing, spdx_symbols, unknown_symbol)

        expected = 'epl-2.0 OR apache-2.0 OR gpl-2.0 WITH classpath-exception-2.0 OR gpl-2.0'
        assert expression.render() == expected

        expected = ['epl-2.0', u'apache-2.0', u'gpl-2.0', u'classpath-exception-2.0', u'gpl-2.0']
        assert licensing.license_keys(expression, unique=False) == expected

        assert all(s.wrapped for s in licensing.license_symbols(expression, decompose=True))
コード例 #6
0
    def test_get_expression_complex(self):
        licensing = Licensing()
        spdx_symbols = get_spdx_symbols()
        unknown_symbol = get_unknown_spdx_symbol()
        line_text = ('* SPDX-License-Identifier: '
                     'EPL-2.0 OR aPache-2.0 OR '
                     'GPL-2.0 WITH classpath-exception-2.0 OR '
                     'GPL-2.0')
        expression = get_expression(line_text, licensing, spdx_symbols, unknown_symbol)

        expected = 'epl-2.0 OR apache-2.0 OR gpl-2.0 WITH classpath-exception-2.0 OR gpl-2.0'
        assert expected == expression.render()

        expected = ['epl-2.0', u'apache-2.0', u'gpl-2.0', u'classpath-exception-2.0']
        assert expected == licensing.license_keys(expression, unique=True)

        assert all(s.wrapped for s in licensing.license_symbols(expression, decompose=True))
コード例 #7
0
    def test_get_expression_complex_with_unknown_symbols_and_refs(self):
        licensing = Licensing()
        spdx_symbols = get_spdx_symbols()
        unknown_symbol = get_unknown_spdx_symbol()
        line_text = ('* SPDX-License-Identifier: '
                     'EPL-2.0 OR Apache-2.0 '
                     'OR GPL-2.0  WITH Classpath-exception-2.0 '
                     'OR LicenseRef-GPL-2.0 WITH Assembly-exception')

        expression = get_expression(line_text, licensing, spdx_symbols, unknown_symbol)

        expected = 'epl-2.0 OR apache-2.0 OR gpl-2.0 WITH classpath-exception-2.0 OR unknown-spdx WITH unknown-spdx'
        assert expression.render() == expected

        expected = ['epl-2.0', 'apache-2.0', 'gpl-2.0', 'classpath-exception-2.0', 'unknown-spdx', 'unknown-spdx']
        assert licensing.license_keys(expression, unique=False) == expected

        assert all(s.wrapped for s in licensing.license_symbols(expression, decompose=True))