MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / cli

Function cli

etc/scripts/licenses/buildrules.py:272–372  ·  view source on GitHub ↗

Create rules from a text file with delimited blocks of metadata and texts. As an example a file would contains one of more blocks such as this: \b ---------------------------------------- license_expression: lgpl-2.1 relevance: 100

(licenses_file, dump_to_file_on_errors=False)

Source from the content-addressed store, hash-verified

270
271@click.help_option("-h", "--help")
272def cli(licenses_file, dump_to_file_on_errors=False):
273 """
274 Create rules from a text file with delimited blocks of metadata and texts.
275
276 As an example a file would contains one of more blocks such as this:
277
278 \b
279 ----------------------------------------
280 license_expression: lgpl-2.1
281 relevance: 100
282 is_license_notice: yes
283 ---
284 This program is free software; you can redistribute it and/or modify
285 it under the terms of the GNU Lesser General Public License
286 version 2.1 as published by the Free Software Foundation;
287 ----------------------------------------
288 license_expression: lgpl-2.1
289 relevance: 100
290 is_license_reference: yes
291 ---
292 LGPL-2.1
293 ----------------------------------------
294 """
295
296 rules_data = load_data(licenses_file)
297 rule_by_tokens = all_rule_by_tokens()
298
299 licenses_by_key = cache.get_licenses_db()
300 skinny_rules = []
301
302 for rdata in rules_data:
303 relevance = rdata.data.get("relevance")
304 rdata.data["has_stored_relevance"] = bool(relevance)
305
306 license_expression = rdata.data.get("license_expression")
307 if license_expression:
308 rdata.data["license_expression"] = license_expression.lower().strip()
309
310 minimum_coverage = rdata.data.get("minimum_coverage")
311 rdata.data["has_stored_minimum_coverage"] = bool(minimum_coverage)
312
313 rl = models.BasicRule(text=rdata.text, **rdata.data)
314 skinny_rules.append(rl)
315
316 # these will raise an exception and exit on errors
317 if not dump_to_file_on_errors:
318 models.validate_rules(rules=skinny_rules, licenses_by_key=licenses_by_key, with_text=True, thorough=True)
319 else:
320 validate_and_dump_rules(rules=skinny_rules, licenses_by_key=licenses_by_key, licenses_file_path=licenses_file)
321
322 print()
323 for rule in skinny_rules:
324
325 if rule.is_false_positive:
326 base_name = "false-positive"
327 elif rule.is_license_intro:
328 base_name = "license-intro"
329 elif rule.is_license_clue:

Callers 1

buildrules.pyFile · 0.70

Calls 15

set_relevanceMethod · 0.95
tokensMethod · 0.95
get_rule_id_for_textFunction · 0.90
load_dataFunction · 0.85
all_rule_by_tokensFunction · 0.85
boolFunction · 0.85
validate_and_dump_rulesFunction · 0.85
find_rule_base_locFunction · 0.85
validate_rulesMethod · 0.80
getMethod · 0.65
appendMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected