Example #1
0
def get_licenses(location, min_score=100):
    """
    Yield an iterable of dictionaries of license data detected in the file at
    location for each detected license.

    minimum_score is the minimum score threshold from 0 to 100. The default is
    100 means only exact licenses will be detected. With any value below 100,
    approximate license results are included. Note that the minimum length for
    an approximate match is four words.
    """
    from licensedcode.index import get_index
    from licensedcode.models import get_license

    idx = get_index()

    for match in idx.match(location=location, min_score=min_score):
        for license_key in match.rule.licenses:
            lic = get_license(license_key)
            result = OrderedDict()
            result['key'] = lic.key
            result['score'] = match.normalized_score()
            result['short_name'] = lic.short_name
            result['category'] = lic.category
            result['owner'] = lic.owner
            result['homepage_url'] = lic.homepage_url
            result['text_url'] = lic.text_urls[0] if lic.text_urls else ''
            result['dejacode_url'] = DEJACODE_LICENSE_URL.format(lic.key)
            result['spdx_license_key'] = lic.spdx_license_key
            result['spdx_url'] = lic.spdx_url
            result['start_line'] = match.lines.start
            result['end_line'] = match.lines.end
            yield result
Example #2
0
def get_licenses(location=None):
    """
    Yield dictionaries of license data detected in the file at location for
    each detected license.
    """
    from licensedcode.models import get_license
    from licensedcode.detect import get_license_matches

    for match in get_license_matches(location):
        for license_key in match.rule.licenses:
            license = get_license(license_key)

            yield {
                'key': license.key,
                'short_name': license.short_name,
                'category': license.category,
                'owner': license.owner,
                'homepage_url': license.homepage_url,
                'text_url': license.text_urls[0] if license.text_urls else '',
                'dejacode_url': DEJACODE_LICENSE_URL.format(license.key),
                'spdx_license_key': license.spdx_license_key,
                'spdx_url': license.spdx_url,
                'start_line': match.query_position.start_line,
                'end_line': match.query_position.end_line,
            }
Example #3
0
def get_licenses(location=None):
    """
    Yield dictionaries of license data detected in the file at location for
    each detected license.
    """
    from licensedcode.models import get_license
    from licensedcode.detect import get_license_matches

    for match in get_license_matches(location):
        for license_key in match.rule.licenses:
            license = get_license(license_key)

            yield {
                "key": license.key,
                "short_name": license.short_name,
                "category": license.category,
                "owner": license.owner,
                "homepage_url": license.homepage_url,
                "text_url": license.text_urls[0] if license.text_urls else "",
                "dejacode_url": DEJACODE_LICENSE_URL.format(license.key),
                "spdx_license_key": license.spdx_license_key,
                "spdx_url": license.spdx_url,
                "start_line": match.query_position.start_line,
                "end_line": match.query_position.end_line,
            }
Example #4
0
def get_licenses(location, minimum_score=100):
    """
    Yield an iterable of dictionaries of license data detected in the file at
    location for each detected license.

    minimum_score is the minimum score threshold from 0 to 100. The default is
    100 means only exact licenses will be detected. With any value below 100,
    approximate license results are included. Note that the minimum length for
    an approximate match is four words.
    """
    from licensedcode.models import get_license
    from licensedcode.detect import get_license_matches

    for match in get_license_matches(location, minimum_score=minimum_score):
        for license_key in match.rule.licenses:
            lic = get_license(license_key)
            result = OrderedDict()
            result['key'] = lic.key
            result['score'] = match.score
            result['short_name'] = lic.short_name
            result['category'] = lic.category
            result['owner'] = lic.owner
            result['homepage_url'] = lic.homepage_url
            result['text_url'] = lic.text_urls[0] if lic.text_urls else ''
            result['dejacode_url'] = DEJACODE_LICENSE_URL.format(lic.key)
            result['spdx_license_key'] = lic.spdx_license_key
            result['spdx_url'] = lic.spdx_url
            result['start_line'] = match.query_position.start_line
            result['end_line'] = match.query_position.end_line
            yield result
Example #5
0
def as_template(scan_data, template='html'):
    """
    Return an string built from a list of results and the provided template.
    The template defaults to the standard HTML template format or can point to
    the path of a custom template file.
    """
    from licensedcode.models import get_license

    if template == 'html':
        template = get_template(get_template_dir('html'))
    else:
        # load a custom template
        tpath = fileutils.as_posixpath(abspath(expanduser(template)))
        assert isfile(tpath)
        tdir = fileutils.parent_directory(tpath)
        tfile = fileutils.file_name(tpath)
        template = get_template(tdir, tfile)

    converted = OrderedDict()
    converted_infos = OrderedDict()
    converted_packages = OrderedDict()
    licenses = {}

    # Create a flattened data dict keyed by location
    for scan_result in scan_data:
        location = scan_result['location']
        results = []
        if 'copyrights' in scan_result:
            for entry in scan_result['copyrights']:
                results.append({
                    'start': entry['start_line'],
                    'end': entry['end_line'],
                    'what': 'copyright',
                    # NOTE: we display one statement per line.
                    'value': '\n'.join(entry['statements']),
                })
        if 'licenses' in scan_result:
            for entry in scan_result['licenses']:
                results.append({
                    'start': entry['start_line'],
                    'end': entry['end_line'],
                    'what': 'license',
                    'value': entry['key'],
                })

                if entry['key'] not in licenses:
                    licenses[entry['key']] = entry
                    entry['object'] = get_license(entry['key'])
        if results:
            converted[location] = sorted(results, key=itemgetter('start'))

        if 'infos' in scan_result:
            converted_infos[location] = scan_result['infos']

        if 'packages' in scan_result:
            converted_packages[location] = scan_result['packages']

        licenses = OrderedDict(sorted(licenses.items()))

    results = {
        'license_copyright': converted,
        'infos': converted_infos,
        'packages': converted_packages
    }

    return template.render(results=results, licenses=licenses)
Example #6
0
def as_template(scan_data, template='html'):
    """
    Return an string built from a list of results and the provided template.
    The template defaults to the standard HTML template format or can point to
    the path of a custom template file.
    """
    from licensedcode.models import get_license

    if template == 'html':
        template = get_template(get_template_dir('html'))
    else:
        # load a custom template
        tpath = fileutils.as_posixpath(abspath(expanduser(template)))
        assert isfile(tpath)
        tdir = fileutils.parent_directory(tpath)
        tfile = fileutils.file_name(tpath)
        template = get_template(tdir, tfile)

    converted = OrderedDict()
    converted_infos = OrderedDict()
    converted_packages = OrderedDict()
    licenses = {}

    # Create a flattened data dict keyed by location
    for scan_result in scan_data:
        location = scan_result['location']
        results = []
        if 'copyrights' in scan_result:
            for entry in scan_result['copyrights']:
                results.append({
                    'start': entry['start_line'],
                    'end': entry['end_line'],
                    'what': 'copyright',
                    # NOTE: we display one statement per line.
                    'value': '\n'.join(entry['statements']),
                })
        if 'licenses' in scan_result:
            for entry in scan_result['licenses']:
                results.append({
                    'start': entry['start_line'],
                    'end': entry['end_line'],
                    'what': 'license',
                    'value': entry['key'],
                })

                if entry['key'] not in licenses:
                    licenses[entry['key']] = entry
                    entry['object'] = get_license(entry['key'])
        if results:
            converted[location] = sorted(results, key=itemgetter('start'))

        if 'infos' in scan_result:
            converted_infos[location] = scan_result['infos']

        if 'packages' in scan_result:
            converted_packages[location] = scan_result['packages']

        licenses = OrderedDict(sorted(licenses.items()))

    results = {
        'license_copyright': converted,
        'infos': converted_infos,
        'packages': converted_packages
    }

    return template.render(results=results, licenses=licenses)