-
Notifications
You must be signed in to change notification settings - Fork 1
/
license_check.py
executable file
·101 lines (82 loc) · 3.47 KB
/
license_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# Copyright (c) 2018 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import argparse
import sys
import json
import yaml
def analyze_file(config_file, scancode_file, scanned_files_dir):
with open(config_file, 'r') as f:
config = yaml.safe_load(f.read())
report = ""
exclude = config.get("exclude")
if exclude:
never_check_ext = exclude.get("extensions", [])
never_check_langs = exclude.get("langs", [])
else:
never_check_ext = []
never_check_langs = []
lic_config = config.get("license")
lic_main = lic_config.get("main")
lic_cat = lic_config.get("category")
check_langs = ['CMake']
with open(scancode_file, 'r') as json_fp:
scancode_results = json.load(json_fp)
for file in scancode_results['files']:
if file['type'] == 'directory':
continue
orig_path = str(file['path']).replace(scanned_files_dir, '')
licenses = file['licenses']
file_type = file.get("file_type")
kconfig = "Kconfig" in orig_path and file_type in ['ASCII text']
check = False
if file.get("extension")[1:] in never_check_ext:
check = False
elif file.get("programming_language") in never_check_langs:
check = False
elif kconfig:
check = True
elif file.get("programming_language") in check_langs:
check = True
elif file.get("is_script"):
check = True
elif file.get("is_source"):
check = True
if check:
if not licenses:
report += ("* {} missing license.\n".format(orig_path))
else:
for lic in licenses:
if lic['key'] != lic_main:
report += ("* {} is not {} licensed: {}\n".format(
orig_path, lic_main, lic['key']))
if lic['category'] != lic_cat:
report += ("* {} has non-permissive license: {}\n".format(
orig_path, lic['key']))
if lic['key'] == 'unknown-spdx':
report += ("* {} has unknown SPDX: {}\n".format(
orig_path, lic['key']))
if not file['copyrights'] and file.get("programming_language") != 'CMake':
report += ("* {} missing copyright.\n".format(orig_path))
return(report)
def parse_args():
parser = argparse.ArgumentParser(
description="Analyze licenses...")
parser.add_argument('-s', '--scancode-output',
help='''JSON output from scancode-toolkit''')
parser.add_argument('-f', '--scanned-files',
help="Directory with scanned files")
parser.add_argument('-c', '--config-file',
help="Configuration file")
parser.add_argument('-o', '--output-file',
help="Output report file")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
if args.scancode_output and args.scanned_files and args.config_file:
report = analyze_file(args.config_file, args.scancode_output, args.scanned_files)
if report:
with open(args.output_file, "w") as fp:
fp.write(report)
else:
sys.exit("Provide files to analyze")