Skip to content

Commit 716d959

Browse files
Add requested changes to script
Add licensedb url, modify csv output to correct bugs, only add first 200 characters in case of text, modify functions and add docstrings. Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent a6c2cdb commit 716d959

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

etc/scripts/licenses/report_license_rules.py

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
LICENSES_FIELDNAMES = [
2222
'key', 'short_name', 'name', 'category', 'owner', 'text', 'words_count', 'notes',
2323
'minimum_coverage', 'homepage_url', 'is_exception', 'language', 'is_unknown',
24-
'spdx_license_key', 'text_urls', 'other_urls', 'standard_notice',
24+
'spdx_license_key', 'reference_url', 'text_urls', 'other_urls', 'standard_notice',
2525
'license_filename', 'faq_url', 'ignorable_authors',
2626
'ignorable_copyrights', 'ignorable_holders', 'ignorable_urls', 'ignorable_emails',
2727
'osi_license_key', 'osi_url', 'other_spdx_license_keys',
@@ -37,48 +37,44 @@
3737
]
3838

3939

40+
SCANCODE_LICENSEDB_URL = 'https://scancode-licensedb.aboutcode.org/{}'
41+
42+
4043
def write_data_to_csv(data, output_csv, fieldnames):
41-
42-
with open(output_csv,'w',encoding='utf-8-sig',newline='') as f:
43-
w = csv.DictWriter(f,fieldnames=fieldnames)
44-
w.writeheader()
4544

45+
with open(output_csv, 'w') as f:
46+
w = csv.DictWriter(f, fieldnames=fieldnames)
47+
w.writeheader()
4648
for entry in data:
4749
w.writerow(entry)
4850

4951

50-
def filter_by_attribute(data, attribute, expected):
51-
52-
filtered_output = []
53-
for entry in data:
54-
if entry.get(attribute, 'None') == expected:
55-
filtered_output.append(entry)
56-
57-
return filtered_output
52+
def filter_by_attribute(data, attribute, required_key):
53+
"""
54+
Filters by attribute, if value is required_key.
55+
Example `attribute`: `category`. Example `required_key`: `Permissive`.
56+
"""
57+
return [entry for entry in data if entry.get(attribute, 'None') == required_key]
5858

5959
def flatten_output(data):
6060

61-
if not isinstance(data, list):
62-
return
61+
assert isinstance(data, list)
6362

6463
output = []
6564
for entry in data:
66-
if not isinstance(entry, dict):
67-
continue
65+
assert isinstance(entry, dict)
6866

6967
output_entry = {}
7068
for key, value in entry.items():
71-
entry_value = None
7269
if value is None:
7370
continue
74-
elif isinstance(value, list):
75-
entry_value = ' '.join(value)
71+
72+
if isinstance(value, list):
73+
value = ' '.join(value)
7674
elif not isinstance(value, str):
77-
entry_value = repr(value)
78-
elif not entry_value:
79-
entry_value = value
75+
value = repr(value)
8076

81-
output_entry[key] = entry_value
77+
output_entry[key] = value
8278

8379
output.append(output_entry)
8480

@@ -103,22 +99,22 @@ def flatten_output(data):
10399
type=str,
104100
default=None,
105101
metavar='STRING',
106-
help='An optional filter to only output licenses/rules of this category'
107-
'. Example STRING: `permissive`.',
102+
help='An optional filter to only output licenses/rules of this category. '
103+
'Example STRING: `permissive`.',
108104
cls=PluggableCommandLineOption,
109105
)
110106
@click.option('-k', '--license-key',
111107
type=str,
112108
default=None,
113109
metavar='STRING',
114-
help='An optional filter to only output licenses/rules which has this license key.'
110+
help='An optional filter to only output licenses/rules which has this license key. '
115111
'Example STRING: `mit`.',
116112
cls=PluggableCommandLineOption,
117113
)
118114
@click.option('-t', '--with-text',
119115
is_flag=True,
120116
default=False,
121-
help='Also include the license/rules texts.'
117+
help='Also include the license/rules texts (First 200 characters). '
122118
'Note that this increases the file size significantly.',
123119
cls=PluggableCommandLineOption,
124120
)
@@ -137,23 +133,24 @@ def cli(licenses, rules, category, license_key, with_text):
137133
for license in licenses_data.values():
138134
license_data = license.to_dict()
139135
if with_text:
140-
license_data['text'] = license.text
136+
license_data['text'] = license.text[:200]
141137
license_data['is_unknown'] = license.is_unknown
142138
license_data['words_count'] = len(license.text)
139+
license_data['reference_url'] = SCANCODE_LICENSEDB_URL.format(license.key)
143140
licenses_output.append(license_data)
144141

145142
if category:
146143
licenses_output = filter_by_attribute(
147144
data=licenses_output,
148145
attribute='category',
149-
expected=category
146+
required_key=category
150147
)
151148

152149
if license_key:
153150
licenses_output = filter_by_attribute(
154151
data=licenses_output,
155152
attribute='key',
156-
expected=license_key,
153+
required_key=license_key,
157154
)
158155

159156
licenses_output = flatten_output(data=licenses_output)
@@ -167,7 +164,7 @@ def cli(licenses, rules, category, license_key, with_text):
167164
rule_data['identifier'] = rule.identifier
168165
rule_data['referenced_filenames'] = rule.referenced_filenames
169166
if with_text:
170-
rule_data['text'] = rule.text()
167+
rule_data['text'] = rule.text()[:200]
171168
rule_data['has_unknown'] = rule.has_unknown
172169
rule_data['words_count'] = len(rule.text())
173170
try:
@@ -180,14 +177,14 @@ def cli(licenses, rules, category, license_key, with_text):
180177
rules_output = filter_by_attribute(
181178
data=rules_output,
182179
attribute='category',
183-
expected=category
180+
required_key=category,
184181
)
185182

186183
if license_key:
187184
rules_output = filter_by_attribute(
188185
data=rules_output,
189186
attribute='license_expression',
190-
expected=license_key,
187+
required_key=license_key,
191188
)
192189

193190
rules_output = flatten_output(rules_output)

0 commit comments

Comments
 (0)