Repository navigation
Expand file tree
/
Copy pathparse_scancode_results.py
More file actions
327 lines (297 loc) Β· 13.7 KB
/
Copy pathparse_scancode_results.py
File metadata and controls
327 lines (297 loc) Β· 13.7 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python3
"""
ScanCode JSON Results Parser
This script parses ScanCode JSON output and identifies license issues and compliance problems.
"""
import json
import sys
import os
from collections import defaultdict, Counter
from datetime import datetime
class ScanCodeAnalyzer:
GPL_2_0 = 'gpl-2.0'
GPL_3_0 = 'gpl-3.0'
AGPL_3_0 = 'agpl-3.0'
GPL_2_0_PLUS = 'gpl-2.0-plus'
GPL_3_0_PLUS = 'gpl-3.0-plus'
LGPL_2_1 = 'lgpl-2.1'
LGPL_3_0 = 'lgpl-3.0'
CC_BY_SA_4_0 = 'cc-by-sa-4.0'
def __init__(self, json_file):
self.json_file = json_file
self.data = None
self.load_data()
self.problematic_licenses = {
'gpl': [self.GPL_2_0, self.GPL_3_0, self.GPL_2_0_PLUS, self.GPL_3_0_PLUS],
'agpl': [self.AGPL_3_0, self.GPL_3_0_PLUS],
'copyleft': [self.GPL_2_0, self.GPL_3_0, self.LGPL_2_1, self.LGPL_3_0, self.AGPL_3_0],
'commercial_unfriendly': [self.GPL_2_0, self.GPL_3_0, self.AGPL_3_0, self.CC_BY_SA_4_0],
'unknown': ['unknown', 'other-permissive', 'other-copyleft', 'unknown-license-reference']
}
self.permissive_licenses = [
'mit', 'apache-2.0', 'bsd-2-clause', 'bsd-3-clause', 'isc',
'cc0-1.0', 'unlicense', 'wtfpl', 'boost-1.0'
]
def load_data(self):
try:
with open(self.json_file, 'r', encoding='utf-8') as f:
self.data = json.load(f)
print(f"β
Successfully loaded {self.json_file}")
except FileNotFoundError:
print(f"β Error: File {self.json_file} not found")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"β Error parsing JSON: {e}")
sys.exit(1)
def extract_licenses_from_file(self, file_info):
"""
Π‘ΠΎΠ±ΠΈΡΠ°Π΅Ρ Π²ΡΠ΅ Π»ΠΈΡΠ΅Π½Π·ΠΈΠΈ ΠΈΠ· ΡΠ°ΠΉΠ»Π°: detected_license_expression, license_detections, licenses.
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΠΏΠΈΡΠΎΠΊ ΡΠ»ΠΎΠ²Π°ΡΠ΅ΠΉ: {'key': ..., 'name': ..., 'score': ...}
"""
results = []
# detected_license_expression (ΡΡΡΠΎΠΊΠ°)
expr = file_info.get('detected_license_expression')
if expr and expr != '':
results.append({'key': expr, 'name': expr, 'score': 100})
# license_detections (ΡΠΏΠΈΡΠΎΠΊ)
for lic in file_info.get('license_detections', []):
key = lic.get('license_expression') or lic.get('license_expression_spdx') or 'unknown'
score = 100
# ΠΡΠ»ΠΈ Π΅ΡΡΡ matches, Π±Π΅ΡΠ΅ΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ score
if lic.get('matches'):
score = max([m.get('score', 100) for m in lic['matches']])
results.append({'key': key, 'name': key, 'score': score})
# licenses (ΡΠΏΠΈΡΠΎΠΊ, ΡΡΠ°ΡΡΠΉ ΡΡΠΈΠ»Ρ)
for lic in file_info.get('licenses', []):
key = lic.get('key', 'unknown')
name = lic.get('name', key)
score = lic.get('score', 100)
results.append({'key': key, 'name': name, 'score': score})
return results
def get_files_with_issues(self):
issues = []
if not self.data or 'files' not in self.data:
print("β No 'files' section found in JSON")
return issues
for file_info in self.data['files']:
file_path = file_info.get('path', 'Unknown')
# Check for scan errors
if file_info.get('scan_errors'):
issues.append({
'file': file_path,
'type': 'scan_error',
'details': file_info['scan_errors']
})
# Check for license detection issues (Π½ΠΈΠ·ΠΊΠΈΠΉ score)
for lic in self.extract_licenses_from_file(file_info):
if lic.get('score', 100) < 50:
issues.append({
'file': file_path,
'type': 'low_confidence_license',
'details': f"License: {lic.get('key', 'unknown')}, Score: {lic.get('score', 0)}"
})
return issues
def analyze_licenses(self):
license_stats = defaultdict(list)
all_licenses = Counter()
if not self.data or 'files' not in self.data:
return license_stats, all_licenses
for file_info in self.data['files']:
file_path = file_info.get('path', 'Unknown')
for lic in self.extract_licenses_from_file(file_info):
self._process_license_detection(lic, file_path, license_stats, all_licenses)
return license_stats, all_licenses
def _process_license_detection(self, lic_info, file_path, license_stats, all_licenses):
key = lic_info.get('key', 'unknown')
name = lic_info.get('name', key)
score = lic_info.get('score', 100)
all_licenses[key] += 1
license_stats[key].append({
'file': file_path,
'name': name,
'score': score
})
def identify_problematic_licenses(self, license_stats):
problems = {
'copyleft': [],
'gpl': [],
'agpl': [],
'commercial_unfriendly': [],
'unknown': [],
'low_confidence': []
}
# Use sets to avoid duplicates if a file matches multiple criteria
problem_sets = {key: set() for key in problems.keys()}
for license_key, file_detections in license_stats.items():
for detection_info in file_detections: # detection_info is {'file': ..., 'name': ..., 'score': ...}
file_path = detection_info['file']
score = detection_info['score']
self._add_to_problem_sets(problem_sets, license_key, file_path, score)
return self._convert_problem_sets_to_list(problem_sets)
def _add_to_problem_sets(self, problem_sets, license_key, file_path, score):
# Check for problematic license types
for category, license_list in self.problematic_licenses.items():
if any(problem_license in license_key.lower() for problem_license in license_list):
problem_sets[category].add((file_path, license_key, score)) # Store as tuple for uniqueness
# Check for low confidence scores (regardless of license type)
if score < 70:
problem_sets['low_confidence'].add((file_path, license_key, score))
def _convert_problem_sets_to_list(self, problem_sets):
problems = {
'copyleft': [],
'gpl': [],
'agpl': [],
'commercial_unfriendly': [],
'unknown': [],
'low_confidence': []
}
for category, item_set in problem_sets.items():
for file_path, license_key, score in item_set:
problems[category].append({
'file': file_path,
'name': license_key, # Use key for simplicity, name is often the same
'score': score
})
return problems
def _print_report_header(self):
print("=" * 80)
print("π SCANCODE LICENSE ANALYSIS REPORT")
print("=" * 80)
print(f"π Analyzed file: {self.json_file}")
print("π
Generated on: {}".format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print()
def _print_basic_statistics(self, issues):
total_files = len(self.data.get('files', []) if self.data else [])
print("π BASIC STATISTICS")
print(f" Total files scanned: {total_files}")
if issues:
print(f" Files with scan errors: {len([i for i in issues if i['type'] == 'scan_error'])}")
print(f" Files with low confidence licenses: {len([i for i in issues if i['type'] == 'low_confidence_license'])}")
print()
def _print_license_summary(self, all_licenses):
print("π LICENSE SUMMARY")
print(f" Unique licenses detected: {len(all_licenses)}")
print(f" Total license detections: {sum(all_licenses.values())}")
print()
print("π TOP 10 MOST COMMON LICENSES")
for license_key, count in all_licenses.most_common(10):
print(f" {license_key:30} : {count:4} files")
print()
def _print_potential_issues(self, problems):
print("β οΈ POTENTIAL LICENSE ISSUES")
print("-" * 40)
total_issues = sum(len(v) for v in problems.values())
if total_issues == 0:
print(" β
No major license issues detected!")
else:
for category, issue_list in problems.items():
if issue_list:
print(f"\n π¨ {category.upper().replace('_', ' ')} LICENSES ({len(issue_list)} files):")
for issue in issue_list[:5]:
print(f" - {issue['file']}")
print(f" License: {issue.get('name', 'Unknown')} (Score: {issue.get('score', 0)})")
if len(issue_list) > 5:
print(f" ... and {len(issue_list) - 5} more files")
print()
def _print_scan_errors(self, issues):
scan_errors = [i for i in issues if i['type'] == 'scan_error']
if scan_errors:
print("β FILES WITH SCAN ERRORS")
print("-" * 40)
for error in scan_errors[:10]:
print(f" {error['file']}")
print(f" Error: {error['details']}")
print()
def _print_recommendations(self, problems):
print("π‘ RECOMMENDATIONS")
print("-" * 40)
gpl_files = len(problems['gpl'])
agpl_files = len(problems['agpl'])
unknown_files = len(problems['unknown'])
if gpl_files > 0:
print(f" π΄ {gpl_files} files with GPL licenses detected")
print(" - Review if GPL compatibility is acceptable for your project")
print(" - Consider alternative implementations for critical files")
if agpl_files > 0:
print(f" π΄ {agpl_files} files with AGPL licenses detected")
print(" - AGPL has network copyleft requirements")
print(" - Avoid AGPL code in web services/SaaS applications")
if unknown_files > 0:
print(f" π‘ {unknown_files} files with unknown/unclear licenses")
print(" - Manual review required for these files")
print(" - Contact original authors for clarification")
low_confidence = len(problems['low_confidence'])
if low_confidence > 0:
print(f" π‘ {low_confidence} files with low confidence license detection")
print(" - Manual verification recommended")
print(" - Check original source for accurate license information")
print("\n" + "=" * 80)
def generate_report(self):
self._print_report_header()
issues = self.get_files_with_issues()
self._print_basic_statistics(issues)
license_stats, all_licenses = self.analyze_licenses()
self._print_license_summary(all_licenses)
problems = self.identify_problematic_licenses(license_stats)
self._print_potential_issues(problems)
self._print_scan_errors(issues)
self._print_recommendations(problems)
def export_detailed_report(self, output_file='license_analysis_detailed.json'):
license_stats, all_licenses = self.analyze_licenses()
problems = self.identify_problematic_licenses(license_stats)
issues = self.get_files_with_issues()
detailed_report = {
'metadata': {
'source_file': self.json_file,
'analysis_date': datetime.now().isoformat(),
'total_files': len(self.data.get('files', []) if self.data else []),
'total_licenses': len(all_licenses),
'total_license_detections': sum(all_licenses.values())
},
'license_statistics': dict(all_licenses),
'problematic_licenses': problems,
'scan_issues': issues,
'recommendations': self.generate_recommendations(problems)
}
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(detailed_report, f, indent=2, ensure_ascii=False)
print("π Detailed report exported to: {}".format(output_file))
def generate_recommendations(self, problems):
recommendations = []
if problems['gpl']:
recommendations.append({
'severity': 'high',
'category': 'gpl',
'message': 'GPL licenses detected. Review compatibility with your project license.',
'affected_files': len(problems['gpl'])
})
if problems['agpl']:
recommendations.append({
'severity': 'critical',
'category': 'agpl',
'message': 'AGPL licenses detected. Network copyleft requirements apply.',
'affected_files': len(problems['agpl'])
})
if problems['unknown']:
recommendations.append({
'severity': 'medium',
'category': 'unknown',
'message': 'Unknown licenses detected. Manual review required.',
'affected_files': len(problems['unknown'])
})
return recommendations
def main():
if len(sys.argv) != 2:
print("Usage: python parse_scancode_results.py <scancode_results.json>")
print("\nExample:")
print("python parse_scancode_results.py hwid_go_server_analysis.json")
sys.exit(1)
json_file = sys.argv[1]
if not os.path.exists(json_file):
print(f"β Error: File {json_file} does not exist")
sys.exit(1)
analyzer = ScanCodeAnalyzer(json_file)
analyzer.generate_report()
analyzer.export_detailed_report()
if __name__ == "__main__":
main()