Skip to content

Commit dfcf51a

Browse files
Add script to report rules
Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent 3be07db commit dfcf51a

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (c) nexB Inc. and others. All rights reserved.
4+
# ScanCode is a trademark of nexB Inc.
5+
# SPDX-License-Identifier: Apache-2.0
6+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
# See https://github.com/nexB/scancode-toolkit for support or download.
8+
# See https://aboutcode.org for more information about nexB OSS projects.
9+
#
10+
11+
import io
12+
13+
import click
14+
import csv
15+
16+
from licensedcode.models import load_licenses
17+
from licensedcode.models import load_rules
18+
19+
@click.command()
20+
@click.argument('csv_file', type=click.Path(), metavar='FILE', help='The CSV filename where the licenses/rules will be written.')
21+
@click.option('-l', '--licenses', is_flag=True, default=False, conflicting_options=['rules'], help='Include Licenses in the CSV output.')
22+
@click.option('-r', '--rules', is_flag=True, default=False, conflicting_options=['licenses'], help='Include Rules in the CSV output.')
23+
@click.option('-c', '--category', type=str, default=None, help='An optional filter to only output licenses/rules of this category. Example: `permissive`.')
24+
@click.option('-e', '--license-expression', type=str, default=None, help='An optional filter to only output licenses/rules which has this license-expression. Example: `mit`.')
25+
@click.help_option('-h', '--help')
26+
def cli(csv_file, licenses, rules, type, license_expression):
27+
"""
28+
Write Licenses/Rules from scancode into a CSV file with all details.
29+
Output can be optionally filtered by category/license-expression.
30+
"""
31+
output = []
32+
33+
licenses_data = load_licenses()
34+
35+
if licenses:
36+
for license in licenses_data.values():
37+
output.append(license.to_dict())
38+
39+
if rules:
40+
rules_data = list(load_rules())
41+
for rule in rules_data:
42+
rule_data = rule.to_dict()
43+
rule_data['category'] = licenses_data[rule_data['license_expression']].category
44+
output.append(rule_data)
45+
46+
print()
47+
48+
with open(csv_file,'w',encoding='utf-8-sig',newline='') as f:
49+
w = csv.DictWriter(f,fieldnames=output[0].keys())
50+
w.writeheader()
51+
52+
for entry in output:
53+
w.writerow(entry)
54+
55+
56+
if __name__ == '__main__':
57+
cli()

0 commit comments

Comments
 (0)