Skip to content

Commit e367fa2

Browse files
Fix license tallies to reflect license detections
Fix license tallies to be keyed by license expressions for each license detection, instead of the detected_license_expression for a file. Also take into account package license detections. Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 3706744 commit e367fa2

18 files changed

+64
-96
lines changed

src/summarycode/tallies.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,46 @@ def license_tallies(resource, children, keep_details=False):
159159
sorted by decreasing count.
160160
"""
161161
LIC_EXP = 'detected_license_expression'
162+
LIC_DET = 'license_detections'
163+
LIC_CLUE = 'license_clues'
162164
license_expressions = []
163165

164166
# Collect current data
165-
lic_expression = getattr(resource, LIC_EXP, None)
166-
if not lic_expression and resource.is_file:
167+
detected_expressions = []
168+
for detection in getattr(resource, LIC_DET, []):
169+
detected_expressions.append(detection["license_expression"])
170+
for match in getattr(resource, LIC_CLUE, []):
171+
detected_expressions.append(match["license_expression"])
172+
173+
package_license_detections = []
174+
PACKAGE_DATA = 'package_data'
175+
package_data = getattr(resource, PACKAGE_DATA, [])
176+
if package_data:
177+
package_license_detections.extend(
178+
[
179+
detection
180+
for detection in getattr(package_data, LIC_DET, [])
181+
if detection
182+
]
183+
)
184+
185+
for detection in package_license_detections:
186+
detected_expressions.append(detection["license_expression"])
187+
188+
if not detected_expressions and resource.is_file:
167189
# also count files with no detection
168190
license_expressions.append(None)
169191
else:
170-
license_expressions.append(lic_expression)
192+
license_expressions.extend(detected_expressions)
171193

172194
# Collect direct children expression tallies
173195
for child in children:
174196
child_tallies = get_resource_tallies(child, key=LIC_EXP, as_attribute=keep_details) or []
175197
for child_tally in child_tallies:
176198
# TODO: review this: this feels rather weird
177199
child_sum_val = child_tally.get('value')
178-
if child_sum_val:
179-
values = [child_sum_val] * child_tally['count']
180-
license_expressions.extend(values)
200+
values = [child_sum_val] * child_tally['count']
201+
license_expressions.extend(values)
181202

182203
# summarize proper
183204
licenses_counter = tally_licenses(license_expressions)

tests/formattedcode/data/common/manifests-expected.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ summary:
4747
declared_holder:
4848
primary_language: Python
4949
other_license_expressions:
50-
- value:
51-
count: 1
50+
- value: lgpl-3.0
51+
count: 3
5252
- value: apache-2.0
53-
count: 1
53+
count: 2
5454
- value: cddl-1.0
5555
count: 1
56-
- value: lgpl-3.0
57-
count: 1
5856
- value: mit
5957
count: 1
6058
other_holders:

tests/formattedcode/data/yaml/package-and-licenses-expected.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ summary:
4747
declared_holder: Example Corp.
4848
primary_language: Python
4949
other_license_expressions:
50-
- value:
51-
count: 1
5250
- value: apache-2.0 AND (apache-2.0 OR mit)
5351
count: 1
5452
- value: mit

tests/summarycode/data/summary/end-2-end/bug-1141.expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"other_license_expressions": [
1616
{
1717
"value": null,
18-
"count": 1
18+
"count": 2
1919
},
2020
{
2121
"value": "gpl-2.0-plus",

tests/summarycode/data/summary/holders/clear_holder.expected.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
"declared_holder": "Example Corp.",
1414
"primary_language": "Python",
1515
"other_license_expressions": [
16-
{
17-
"value": null,
18-
"count": 1
19-
},
2016
{
2117
"value": "apache-2.0",
2218
"count": 1

tests/summarycode/data/summary/holders/combined_holders.expected.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
"declared_holder": "Example Corp., Demo Corp.",
1414
"primary_language": "Python",
1515
"other_license_expressions": [
16-
{
17-
"value": null,
18-
"count": 1
19-
},
2016
{
2117
"value": "apache-2.0",
2218
"count": 1

tests/summarycode/data/summary/license_ambiguity/unambiguous.expected.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
"declared_holder": "Example Corp.",
1414
"primary_language": null,
1515
"other_license_expressions": [
16-
{
17-
"value": null,
18-
"count": 1
19-
},
2016
{
2117
"value": "apache-2.0",
2218
"count": 1

tests/summarycode/data/summary/multiple_package_data/multiple_package_data.expected.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
"value": "mit",
2222
"count": 2
2323
},
24-
{
25-
"value": null,
26-
"count": 1
27-
},
2824
{
2925
"value": "apache-2.0 AND (apache-2.0 OR mit)",
3026
"count": 1

tests/summarycode/data/summary/single_file/single_file.expected.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
},
1313
"declared_holder": "Mort Bay Consulting Pty. Ltd. (Australia) and others, Sun Microsystems",
1414
"primary_language": null,
15-
"other_license_expressions": [
16-
{
17-
"value": null,
18-
"count": 1
19-
}
20-
],
15+
"other_license_expressions": [],
2116
"other_holders": [],
2217
"other_languages": []
2318
},

tests/summarycode/data/summary/summary_without_holder/summary-without-holder-pypi.expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"other_license_expressions": [
1616
{
1717
"value": null,
18-
"count": 1
18+
"count": 9
1919
}
2020
],
2121
"other_holders": [],

0 commit comments

Comments
 (0)