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

Function cli

etc/scripts/licenses/report_license_rules.py:175–241  ·  view source on GitHub ↗

Write Licenses/Rules from scancode into a CSV file with all details. Output can be optionally filtered by category/license-key.

(licenses, rules, category, license_key, with_text)

Source from the content-addressed store, hash-verified

173)
174@click.help_option("-h", "--help")
175def cli(licenses, rules, category, license_key, with_text):
176 """
177 Write Licenses/Rules from scancode into a CSV file with all details.
178 Output can be optionally filtered by category/license-key.
179 """
180 licenses_output = []
181 rules_output = []
182
183 licenses_data = load_licenses()
184
185 if licenses:
186 for lic in licenses_data.values():
187 license_data = lic.to_dict()
188 if with_text:
189 license_data["text"] = lic.text[:200]
190 license_data["is_unknown"] = lic.is_unknown
191 license_data["length"] = len(lic.text)
192 license_data["reference_url"] = SCANCODE_LICENSEDB_URL.format(lic.key)
193 licenses_output.append(license_data)
194
195 if category:
196 licenses_output = filter_by_attribute(
197 data=licenses_output, attribute="category", required_key=category
198 )
199
200 if license_key:
201 licenses_output = filter_by_attribute(
202 data=licenses_output,
203 attribute="key",
204 required_key=license_key,
205 )
206
207 licenses_output = flatten_output(data=licenses_output)
208 write_data_to_csv(data=licenses_output, output_csv=licenses, fieldnames=LICENSES_FIELDNAMES)
209
210 if rules:
211 rules_data = list(load_rules())
212 for rule in rules_data:
213 rule_data = rule.to_dict()
214 rule_data["identifier"] = rule.identifier
215 rule_data["referenced_filenames"] = rule.referenced_filenames
216 if with_text:
217 rule_data["text"] = rule.text[:200]
218 rule_data["has_unknown"] = rule.has_unknown
219 rule_data["length"] = len(rule.text)
220 try:
221 rule_data["category"] = licenses_data[rule_data["license_expression"]].category
222 except KeyError:
223 pass
224 rules_output.append(rule_data)
225
226 if category:
227 rules_output = filter_by_attribute(
228 data=rules_output,
229 attribute="category",
230 required_key=category,
231 )
232

Callers 1

Calls 9

load_licensesFunction · 0.90
load_rulesFunction · 0.90
filter_by_attributeFunction · 0.85
flatten_outputFunction · 0.85
write_data_to_csvFunction · 0.85
valuesMethod · 0.45
to_dictMethod · 0.45
formatMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected