def add_rule(spdx_text, license_obj):
    """
    Add a new rule with text `spdx_text` for the `license_obj` License.
    """
    rule_base_name = "spdx_license_id_" + spdx_text.lower(
    ) + "_for_" + license_obj.key
    text_file = os.path.join(rules_data_dir, rule_base_name + ".RULE")
    data_file = os.path.join(rules_data_dir, rule_base_name + ".yml")
    if os.path.exists(text_file) or os.path.exists(data_file):
        raise Exception(
            "Cannot create new SPDX rules text file for {text}. "
            "File already exists at: {text_file}".format(**locals()))

    with io.open(text_file, "w", encoding="utf-8") as tf:
        tf.write(spdx_text)

    rule = Rule(
        text_file=text_file,
        license_expression=license_obj.key,
        relevance=80,
        minimum_coverage=100,
        notes="Used to detect a bare SPDX license id",
    )
    rule.data_file = data_file
    rule.dump()
    click.echo("Added new rule: " + repr(rule))
Example #2
0
def add_rule(spdx_text, license_obj):
    """
    Add a new rule with text `spdx_text` for the `license_obj` License.
    """
    rule_base_name = 'spdx_license_id_' + spdx_text.lower(
    ) + '_for_' + license_obj.key
    text_file = os.path.join(rules_data_dir, rule_base_name + '.RULE')
    data_file = os.path.join(rules_data_dir, rule_base_name + '.yml')
    if os.path.exists(text_file) or os.path.exists(data_file):
        raise Exception(
            'Cannot create new SPDX rules text file for {text}. '
            'File already exists at: {text_file}'.format(**locals()))

    with open(text_file, 'wb') as tf:
        tf.write(spdx_text)

    rule = Rule(
        text_file=text_file,
        license_expression=license_obj.key,
        relevance=80,
        minimum_coverage=100,
        notes='Used to detect a bare SPDX license id',
    )
    rule.data_file = data_file
    rule.dump()
    click.echo('Added new rule: ' + repr(rule))
def gen_rules(urls):
    """
    Create rules from license URLs listed in FILE (one URL per line)

    Each line can be a single URL or a URL|license-expression.
    If a single URL, download the content at URL and run license detection,
    then generate a rule for each URL using the detected license.
    If the license expression in provided, use this for the generated rule.
    """

    errors = []
    seen = set()
    with open(urls) as uf:
        for i, url in enumerate(uf):
            url = url.strip()
            if not url or url.startswith('#'):
                continue
            if url in seen:
                continue
            else:
                seen.add(url)

            has_exp = False
            print('Processing:', i, repr(url))
            if '|' in url:
                url, has_exp, license_expression = url.partition('|')
            if not url:
                continue

            existing = get_existing_rule(url)
            if existing:
                print('  Rule already exists, skipping')
                continue

            if url and not has_exp:
                try:
                    fetched = fetch_text(url)
                    license_expression = get_license_expression(fetched)
                except Exception as e:
                    print('  Failed:', repr(e))
                    errors.append(url)
                    continue

            base_loc = find_rule_base_loc(license_expression)
            data_file = base_loc + '.yml'
            text_file = base_loc + '.RULE'
            with open(text_file, 'w') as o:
                o.write(url)

            rule = Rule(license_expression=license_expression,
                        is_license_reference=True,
                        relevance=95,
                        text_file=text_file)
            rule.data_file = data_file
            rule.dump()
            print('  Rule added:', rule.identifier)

    if errors:
        print('Failed to process these URLs:')
        for r in errors:
            print(r)