Skip to content

Commit e10358e

Browse files
Add license_expression_spdx in Detections
Adds a new attribute license_expression_spdx to both LicenseMatch and LicenseDetection objects, and reflected also in top-level detections and todo items. Basically this adds the SPDX license-expression everywhere we have a license-expression. Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent a0d246e commit e10358e

File tree

794 files changed

+9436
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

794 files changed

+9436
-25
lines changed

src/licensedcode/cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,12 @@ def validate_spdx_license_keys(license_expression, licensing):
545545
try:
546546
parsed.render(template='{symbol.wrapped.spdx_license_key}')
547547
except AttributeError:
548+
msg = f"Error rendering SPDX license key for: {key}"
548549
messages.append(msg)
549550
pass
550551

551552
if messages:
552-
raise InvalidLicenseKeyError(messages)
553+
raise InvalidLicenseKeyError(f"ERROR in parsing license_expression: {license_expression}: type: {type(license_expression)} :{messages}")
553554

554555

555556
class InvalidLicenseKeyError(Exception):

src/licensedcode/detection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ class LicenseDetection:
169169
'using the SPDX license expression syntax and ScanCode license keys.')
170170
)
171171

172+
license_expression_spdx = attr.ib(
173+
default=None,
174+
metadata=dict(
175+
help='Full license expression string with SPDX license keys.')
176+
)
177+
172178
matches = attr.ib(
173179
default=attr.Factory(list),
174180
metadata=dict(
@@ -248,8 +254,17 @@ def from_matches(
248254
detection_log=detection_log,
249255
)
250256
detection.identifier = detection.identifier_with_expression
257+
detection.license_expression_spdx = detection.spdx_license_expression()
251258
return detection
252259

260+
def spdx_license_expression(self):
261+
from licensedcode.cache import build_spdx_license_expression
262+
from licensedcode.cache import get_cache
263+
return str(build_spdx_license_expression(
264+
license_expression=self.license_expression,
265+
licensing=get_cache().licensing,
266+
))
267+
253268
def __eq__(self, other):
254269
return (
255270
isinstance(other, LicenseDetection)
@@ -515,6 +530,7 @@ def from_license_detection_mapping(
515530

516531
detection = cls(
517532
license_expression=license_detection_mapping["license_expression"],
533+
license_expression_spdx=license_detection_mapping["license_expression_spdx"],
518534
detection_log=license_detection_mapping.get("detection_log", []) or None,
519535
identifier=license_detection_mapping["identifier"],
520536
matches=matches,
@@ -665,6 +681,7 @@ def to_dict(
665681

666682
# LicenseDB Level Information (Rule that was matched)
667683
result['license_expression'] = self.rule.license_expression
684+
result['license_expression_spdx'] = self.rule.spdx_license_expression()
668685
result['rule_identifier'] = self.rule.identifier
669686
result['rule_relevance'] = self.rule.relevance
670687
result['rule_url'] = self.rule.rule_url
@@ -865,6 +882,7 @@ class UniqueDetection:
865882
"""
866883
identifier = attr.ib(default=None)
867884
license_expression = attr.ib(default=None)
885+
license_expression_spdx = attr.ib(default=None)
868886
detection_count = attr.ib(default=None)
869887
matches = attr.ib(default=attr.Factory(list))
870888
detection_log = attr.ib(default=attr.Factory(list))
@@ -897,12 +915,14 @@ def get_unique_detections(cls, license_detections):
897915
for match in detection.matches
898916
]
899917
))
918+
detection.license_expression_spdx = detection.spdx_license_expression()
900919
detection.identifier = detection.identifier_with_expression
901920

902921
unique_license_detections.append(
903922
cls(
904923
identifier=detection.identifier,
905924
license_expression=detection.license_expression,
925+
license_expression_spdx=detection.license_expression_spdx,
906926
detection_log=detection_log or [],
907927
matches=detection.matches,
908928
detection_count=len(file_regions),
@@ -937,6 +957,7 @@ def dict_fields(attr, value):
937957
def get_license_detection_object(self):
938958
return LicenseDetection(
939959
license_expression=self.license_expression,
960+
license_expression_spdx=self.license_expression_spdx,
940961
detection_log=self.detection_log,
941962
matches=self.matches,
942963
identifier=self.identifier,

src/licensedcode/match.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ def to_dict(
802802

803803
# LicenseDB Level Information (Rule that was matched)
804804
result['license_expression'] = self.rule.license_expression
805+
result['spdx_license_expression'] = self.rule.spdx_license_expression()
805806
result['rule_identifier'] = self.rule.identifier
806807
result['rule_relevance'] = self.rule.relevance
807808
result['rule_url'] = self.rule.rule_url

src/licensedcode/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,9 +1932,10 @@ def licensing_contains(self, other):
19321932
expression2=other.license_expression_object,
19331933
)
19341934

1935-
def spdx_license_expression(self, licensing=None):
1935+
def spdx_license_expression(self):
19361936
from licensedcode.cache import build_spdx_license_expression
1937-
return str(build_spdx_license_expression(self.license_expression, licensing=licensing))
1937+
from licensedcode.cache import get_cache
1938+
return str(build_spdx_license_expression(self.license_expression, licensing=get_cache().licensing))
19381939

19391940
def get_length(self, unique=False):
19401941
return self.length_unique if unique else self.length

src/licensedcode/plugin_license.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ def add_referenced_filenames_license_matches_for_detections(resource, codebase):
297297
analysis=DetectionCategory.UNKNOWN_FILE_REFERENCE_LOCAL.value,
298298
post_scan=True,
299299
)
300+
license_expression_spdx = build_spdx_license_expression(
301+
license_expression=str(license_expression),
302+
licensing=get_cache().licensing,
303+
)
300304
license_detection_mapping["license_expression"] = str(license_expression)
305+
license_detection_mapping["license_expression_spdx"] = str(license_expression_spdx)
301306
license_detection_mapping["detection_log"] = detection_log
302307
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
303308
initial_detection=license_detection_mapping,

src/packagedcode/licensing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ def add_referenced_license_matches_for_package(resource, codebase):
133133
analysis=DetectionCategory.PACKAGE_UNKNOWN_FILE_REFERENCE_LOCAL.value,
134134
post_scan=True,
135135
)
136+
license_expression_spdx = build_spdx_license_expression(
137+
license_expression=str(license_expression),
138+
licensing=get_cache().licensing,
139+
)
136140
license_detection_mapping["license_expression"] = str(license_expression)
141+
license_detection_mapping["license_expression_spdx"] = str(license_expression_spdx)
137142
license_detection_mapping["detection_log"] = detection_log
138143
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
139144
initial_detection=license_detection_mapping,
@@ -253,7 +258,12 @@ def add_referenced_license_detection_from_package(resource, codebase):
253258
analysis=analysis,
254259
post_scan=True,
255260
)
261+
license_expression_spdx = build_spdx_license_expression(
262+
license_expression=str(license_expression),
263+
licensing=get_cache().licensing,
264+
)
256265
license_detection_mapping["license_expression"] = str(license_expression)
266+
license_detection_mapping["license_expression_spdx"] = str(license_expression_spdx)
257267
license_detection_mapping["detection_log"] = detection_log
258268
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
259269
initial_detection=license_detection_mapping,

tests/cluecode/data/plugin_filter_clues/filtered-expected.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
"identifier": "apache_1_1-1712efcb-d696-b5e4-214d-b2ab69680c2a",
55
"license_expression": "apache-1.1",
6+
"license_expression_spdx": "Apache-1.1",
67
"detection_count": 1,
78
"sample_matches": [
89
{
@@ -14,6 +15,7 @@
1415
"match_coverage": 100.0,
1516
"matcher": "3-seq",
1617
"license_expression": "apache-1.1",
18+
"license_expression_spdx": "Apache-1.1",
1719
"rule_identifier": "apache-1.1_63.RULE",
1820
"rule_relevance": 100,
1921
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/apache-1.1_63.RULE",
@@ -47,6 +49,7 @@
4749
"license_detections": [
4850
{
4951
"license_expression": "apache-1.1",
52+
"license_expression_spdx": "Apache-1.1",
5053
"matches": [
5154
{
5255
"score": 96.07,
@@ -57,6 +60,7 @@
5760
"match_coverage": 100.0,
5861
"matcher": "3-seq",
5962
"license_expression": "apache-1.1",
63+
"spdx_license_expression": "Apache-1.1",
6064
"rule_identifier": "apache-1.1_63.RULE",
6165
"rule_relevance": 100,
6266
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/apache-1.1_63.RULE"

tests/cluecode/data/plugin_filter_clues/filtered-expected2.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
"identifier": "pygres_2_2-04f085e8-3db1-f9e2-8dde-7ba8a7ba619a",
55
"license_expression": "pygres-2.2",
6+
"license_expression_spdx": "LicenseRef-scancode-pygres-2.2",
67
"detection_count": 1,
78
"sample_matches": [
89
{
@@ -14,6 +15,7 @@
1415
"match_coverage": 100.0,
1516
"matcher": "2-aho",
1617
"license_expression": "pygres-2.2",
18+
"license_expression_spdx": "LicenseRef-scancode-pygres-2.2",
1719
"rule_identifier": "pygres-2.2_2.RULE",
1820
"rule_relevance": 100,
1921
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/pygres-2.2_2.RULE",
@@ -47,6 +49,7 @@
4749
"license_detections": [
4850
{
4951
"license_expression": "pygres-2.2",
52+
"license_expression_spdx": "LicenseRef-scancode-pygres-2.2",
5053
"matches": [
5154
{
5255
"score": 100.0,
@@ -57,6 +60,7 @@
5760
"match_coverage": 100.0,
5861
"matcher": "2-aho",
5962
"license_expression": "pygres-2.2",
63+
"spdx_license_expression": "LicenseRef-scancode-pygres-2.2",
6064
"rule_identifier": "pygres-2.2_2.RULE",
6165
"rule_relevance": 100,
6266
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/rules/pygres-2.2_2.RULE"

tests/cluecode/data/plugin_filter_clues/filtered-expected3.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
"identifier": "pcre-c61d8210-7748-d787-5e3e-fd64c6cd6b6a",
55
"license_expression": "pcre",
6+
"license_expression_spdx": "LicenseRef-scancode-pcre",
67
"detection_count": 1,
78
"sample_matches": [
89
{
@@ -14,6 +15,7 @@
1415
"match_coverage": 100.0,
1516
"matcher": "1-hash",
1617
"license_expression": "pcre",
18+
"license_expression_spdx": "LicenseRef-scancode-pcre",
1719
"rule_identifier": "pcre.LICENSE",
1820
"rule_relevance": 100,
1921
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/pcre.LICENSE",
@@ -47,6 +49,7 @@
4749
"license_detections": [
4850
{
4951
"license_expression": "pcre",
52+
"license_expression_spdx": "LicenseRef-scancode-pcre",
5053
"matches": [
5154
{
5255
"score": 100.0,
@@ -57,6 +60,7 @@
5760
"match_coverage": 100.0,
5861
"matcher": "1-hash",
5962
"license_expression": "pcre",
63+
"spdx_license_expression": "LicenseRef-scancode-pcre",
6064
"rule_identifier": "pcre.LICENSE",
6165
"rule_relevance": 100,
6266
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/pcre.LICENSE"

0 commit comments

Comments
 (0)