Exemple #1
0
def get_license_type(path_license: str,
                     default: Optional[str] = None) -> Optional[str]:
    """Function tries to match the license with one of the models present in
    grayskull/license/data

    :param path_license: Path to the license file
    :param default: Default value for the license type
    :return: License type
    """
    with open(path_license, "r") as license_file:
        license_content = license_file.read()
    print(
        f"{Fore.LIGHTBLACK_EX}Matching license file with database from Grayskull..."
    )
    all_licenses = get_all_licenses()
    licenses_text = list(map(itemgetter(1), all_licenses))
    best_match = process.extractOne(license_content,
                                    licenses_text,
                                    scorer=token_sort_ratio)

    if default and best_match[1] < 76:
        log.info(
            f"Match too low for recipe {best_match}, using the default {default}"
        )
        return default
    index_license = licenses_text.index(best_match[0])
    return all_licenses[index_license][0]
Exemple #2
0
def get_license_type(path_license: str,
                     default: Optional[str] = None) -> Optional[str]:
    """Function tries to match the license with one of the models present in
    grayskull/license/data

    :param path_license: Path to the license file
    :param default: Default value for the license type
    :return: License type
    """
    with open(path_license, "r") as license_file:
        license_content = license_file.read()
    find_apache = re.findall(
        r"apache\.org\/licenses\/LICENSE\-([0-9])\.([0-9])",
        license_content,
        re.IGNORECASE,
    )
    if find_apache:
        lic_type = find_apache[0]
        return f"Apache-{lic_type[0]}.{lic_type[1]}"
    print_msg(
        f"{Fore.LIGHTBLACK_EX}Matching license file with database from Grayskull..."
    )
    all_licenses = get_all_licenses()
    licenses_text = list(map(itemgetter(1), all_licenses))
    best_match = process.extract(license_content,
                                 licenses_text,
                                 scorer=token_sort_ratio)

    if default and best_match[0][1] < 51:
        log.info(
            f"Match too low for recipe {best_match}, using the default {default}"
        )
        return default

    higher_match = best_match[0]
    equal_values = [
        val[0] for val in best_match if val[1] > (higher_match[1] - 3)
    ]
    if len(equal_values) > 1:
        higher_match = process.extractOne(license_content,
                                          equal_values,
                                          scorer=token_set_ratio)
    index_license = licenses_text.index(higher_match[0])
    return all_licenses[index_license][0]