Skip to content

Commit a0d246e

Browse files
Add rule attributes to todo items
In case of license detection issues in todo items, add rule attributes as additional context which are usually not added to license match data. Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent a1fc5d8 commit a0d246e

File tree

6 files changed

+90
-4
lines changed

6 files changed

+90
-4
lines changed

src/licensedcode/detection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ def to_dict(
643643
include_text=False,
644644
license_text_diagnostics=False,
645645
whole_lines=True,
646+
rule_details=False,
646647
):
647648
"""
648649
Return a "result" scan data built from a LicenseMatch object.
@@ -668,6 +669,14 @@ def to_dict(
668669
result['rule_relevance'] = self.rule.relevance
669670
result['rule_url'] = self.rule.rule_url
670671

672+
# Extra rule details added optionally
673+
if rule_details:
674+
result.update(self.rule.get_flags_mapping())
675+
result["rule_length"] = self.rule.length
676+
result["rule_text"] = self.rule.text
677+
result["rule_notes"] = self.rule.notes
678+
result["referenced_filenames"] = self.rule.referenced_filenames
679+
671680
if include_text:
672681
result['matched_text'] = matched_text
673682

src/licensedcode/models.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,33 @@ def get_min_high_matched_length(self, unique=False):
19501950
return (self.min_high_matched_length_unique if unique
19511951
else self.min_high_matched_length)
19521952

1953+
def get_flags_mapping(self):
1954+
"""
1955+
Return a list of boolean attributes for a rule which are set to True.
1956+
"""
1957+
1958+
rule_boolean_attributes = [
1959+
'is_license_text',
1960+
'is_license_text',
1961+
'is_license_notice',
1962+
'is_license_reference',
1963+
'is_license_tag',
1964+
'is_license_intro',
1965+
'is_license_clue',
1966+
'is_continuous',
1967+
'is_builtin',
1968+
'is_from_license',
1969+
'is_synthetic',
1970+
]
1971+
1972+
mapping = {}
1973+
for attribute in rule_boolean_attributes:
1974+
value = getattr(self, attribute)
1975+
if value:
1976+
mapping[attribute] = True
1977+
1978+
return mapping
1979+
19531980
def to_reference(self):
19541981
"""
19551982
Return a mapping of reference data for this Rule object.

src/summarycode/todo.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from licensedcode.detection import get_ambiguous_license_detections_by_type
2121
from licensedcode.detection import get_uuid_on_content
2222
from licensedcode.detection import UniqueDetection
23+
from licensedcode.detection import LicenseMatchFromResult
2324
from plugincode.post_scan import PostScanPlugin, post_scan_impl
2425
from packageurl import PackageURL
2526

@@ -222,13 +223,23 @@ class AmbiguousDetection:
222223
"""
223224
Detections which needs review.
224225
"""
226+
227+
detection_type = attr.ib(
228+
default=None,
229+
metadata=dict(
230+
help='A string determining what type of detection this object is, '
231+
'the possible values for this are : `package` and `license` '
232+
)
233+
)
234+
225235
detection_id = attr.ib(
226236
default=None,
227237
metadata=dict(
228238
help='A detection ID identifying an unique detection. '
229-
'This has two parts one with the type of detection in string, '
230-
'like `package`/`license` and a positive integer '
231-
'denoting the detection number.'
239+
'For a license detection this is an id with the license, '
240+
'expression and an UUID based on the match content. '
241+
'For a package detection this is the purl and the UUID as '
242+
'a qualifier.'
232243
)
233244
)
234245

@@ -269,6 +280,7 @@ def from_package(cls, package_data, detection_log, file_path):
269280
)
270281
review_comments = get_review_comments(detection_log)
271282
return cls(
283+
detection_type='package',
272284
detection_id=detection_id,
273285
detection=package_data,
274286
review_comments=review_comments,
@@ -287,6 +299,7 @@ def from_license(cls, detection, detection_log, file_regions):
287299
license_diagnostics=license_diagnostics,
288300
)
289301
return cls(
302+
detection_type='license',
290303
detection_id=detection.identifier,
291304
detection=detection_mapping,
292305
review_comments=review_comments,
@@ -299,9 +312,25 @@ def dict_fields(attr, value):
299312
if attr.name == 'file_regions':
300313
return False
301314

315+
if attr.name == 'detection_type':
316+
return False
317+
302318
return True
303319

304-
return attr.asdict(self, filter=dict_fields, dict_factory=dict)
320+
detection_mapping = attr.asdict(self, filter=dict_fields, dict_factory=dict)
321+
if self.detection_type == 'license':
322+
# add rule attributes to the match details
323+
matches_with_details = []
324+
for license_match in detection_mapping["detection"]["matches"]:
325+
license_match_obj = LicenseMatchFromResult.from_dict(license_match)
326+
matches_with_details.append(license_match_obj.to_dict(
327+
include_text=True,
328+
license_text_diagnostics=True,
329+
rule_details=True,
330+
))
331+
detection_mapping["detection"]["matches"] = matches_with_details
332+
333+
return detection_mapping
305334

306335

307336
class PackageDetectionCategory(Enum):

tests/summarycode/data/todo/todo_present/README.multi-orig-tarball-package-expected-diag.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
"rule_identifier": "borceux.LICENSE",
2222
"rule_relevance": 100,
2323
"rule_url": "https://github.com/nexB/scancode-toolkit/tree/develop/src/licensedcode/data/licenses/borceux.LICENSE",
24+
"is_license_text": true,
25+
"is_builtin": true,
26+
"is_from_license": true,
27+
"rule_length": 85,
28+
"rule_text": "Copyright 1993 Francis Borceux\nYou may freely use, modify, and/or distribute each of the files in this package without limitation. The package consists of the following files:\n\nREADME\ncompatibility/OldDiagram\ncompatibility/OldMaxiDiagram\ncompatibility/OldMicroDiagram\ncompatibility/OldMiniDiagram\ncompatibility/OldMultipleArrows\ndiagram/Diagram\ndiagram/MaxiDiagram\ndiagram/MicroDiagram\ndiagram/MiniDiagram\ndiagram/MultipleArrows\nuser-guides/Diagram_Mode_d_Emploi\nuser-guides/Diagram_Read_Me\n\nOf course no support is guaranteed, but the author will attempt to assist with problems. Current email address:\nfrancis dot borceux at uclouvain dot be.",
29+
"rule_notes": null,
30+
"referenced_filenames": [],
2431
"matched_text": "package consists of [various] [tarballs].\n\n[This] README"
2532
}
2633
],

tests/summarycode/data/todo/todo_present/unknown-license-expected-diag.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
"rule_identifier": "license-detection-unknown-3d4d4e847eacf4b9efcc80ca90df1eea689f5cee",
4747
"rule_relevance": 100,
4848
"rule_url": null,
49+
"is_license_notice": true,
50+
"is_builtin": true,
51+
"is_synthetic": true,
52+
"rule_length": 53,
53+
"rule_text": "form shall mean the preferred form for making\nthe purposes of this definition control\n[software] [is] [modified] [by] [someone] [else]\n\n\n\nrepresent, as a whole, an original work of authorship. For the purposes\n of this License, Derivative Works shall not include works that remain\n separable from, or merely link (or bind by name) [to] [the] interfaces of,\n the Work and Derivative Works thereof.",
54+
"rule_notes": "Unknown license based on a composite of license words.",
55+
"referenced_filenames": [],
4956
"matched_text": "form shall mean the preferred form for making\nthe purposes of this definition control\n[software] [is] [modified] [by] [someone] [else]\n\n\n\nrepresent, as a whole, an original work of authorship. For the purposes\n of this License, Derivative Works shall not include works that remain\n separable from, or merely link (or bind by name) [to] [the] interfaces of,\n the Work and Derivative Works thereof."
5057
}
5158
],

tests/summarycode/data/todo/todo_present/unknown-license-expected.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@
4343
"rule_identifier": "license-detection-unknown-51bcd994ad60981fff8c5f1b1b9af6e3d4f5ba67",
4444
"rule_relevance": 100,
4545
"rule_url": null,
46+
"is_license_notice": true,
47+
"is_builtin": true,
48+
"is_synthetic": true,
49+
"rule_length": 53,
50+
"rule_text": "form shall mean the preferred form for making\nthe purposes of this definition control\nsoftware is modified by someone else\n\n\n\nrepresent, as a whole, an original work of authorship. For the purposes\n of this License, Derivative Works shall not include works that remain\n separable from, or merely link (or bind by name) to the interfaces of,\n the Work and Derivative Works thereof.",
51+
"rule_notes": "Unknown license based on a composite of license words.",
52+
"referenced_filenames": [],
4653
"matched_text": "form shall mean the preferred form for making\nthe purposes of this definition control\nsoftware is modified by someone else\n\n\n\nrepresent, as a whole, an original work of authorship. For the purposes\n of this License, Derivative Works shall not include works that remain\n separable from, or merely link (or bind by name) to the interfaces of,\n the Work and Derivative Works thereof."
4754
}
4855
],

0 commit comments

Comments
 (0)