Skip to content

Commit e9f4b77

Browse files
Support LicenseDetection models in load_inventory
Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent fcbb801 commit e9f4b77

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

scanpipe/pipes/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from scanpipe.models import CodebaseResource
3838
from scanpipe.models import DiscoveredDependency
3939
from scanpipe.models import DiscoveredPackage
40+
from scanpipe.models import DiscoveredLicense
4041
from scanpipe.pipes import scancode
4142

4243
logger = logging.getLogger("scanpipe.pipes")
@@ -237,6 +238,37 @@ def update_or_create_dependency(
237238
return dependency
238239

239240

241+
def update_or_create_license_detection(project, detection_data):
242+
"""
243+
Get, update or create a DiscoveredLicense object then return it.
244+
Use the `project` and `detection_data` mapping to lookup and creates the
245+
DiscoveredLicense using its detection identifier as a unique key.
246+
"""
247+
detection_identifier = detection_data["identifier"]
248+
249+
license_detection = project.discoveredlicenses.get_or_none(
250+
identifier=detection_identifier,
251+
)
252+
detection_data = _clean_license_detection_data(detection_data)
253+
254+
if license_detection:
255+
license_detection.update_from_data(detection_data)
256+
else:
257+
license_detection = DiscoveredLicense.create_from_data(
258+
project,
259+
detection_data,
260+
)
261+
262+
return license_detection
263+
264+
265+
def _clean_license_detection_data(detection_data):
266+
detection_data = detection_data.copy()
267+
matches = detection_data.pop("sample_matches")
268+
detection_data["matches"] = matches
269+
return detection_data
270+
271+
240272
def get_or_create_relation(project, relation_data):
241273
"""
242274
Get or create a CodebaseRelation then return it.

scanpipe/pipes/input.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from scanpipe.models import CodebaseResource
3636
from scanpipe.models import DiscoveredDependency
3737
from scanpipe.models import DiscoveredPackage
38+
from scanpipe.models import DiscoveredLicense
3839
from scanpipe.pipes import scancode
3940
from scanpipe.pipes.output import mappings_key_by_fieldname
4041

@@ -78,10 +79,11 @@ def is_archive(location):
7879

7980
def load_inventory_from_toolkit_scan(project, input_location):
8081
"""
81-
Create packages, dependencies, and resources loaded from the ScanCode-toolkit scan
82-
results located at ``input_location``.
82+
Create license detections, packages, dependencies, and resources
83+
loaded from the ScanCode-toolkit scan results located at ``input_location``.
8384
"""
8485
scanned_codebase = scancode.get_virtual_codebase(project, input_location)
86+
scancode.create_discovered_licenses(project, scanned_codebase)
8587
scancode.create_discovered_packages(project, scanned_codebase)
8688
scancode.create_codebase_resources(project, scanned_codebase)
8789
scancode.create_discovered_dependencies(
@@ -91,9 +93,12 @@ def load_inventory_from_toolkit_scan(project, input_location):
9193

9294
def load_inventory_from_scanpipe(project, scan_data):
9395
"""
94-
Create packages, dependencies, resources, and relations loaded from a ScanCode.io
95-
JSON output provided as ``scan_data``.
96+
Create license detections, packages, dependencies, resources, and relations
97+
loaded from a ScanCode.io JSON output provided as ``scan_data``.
9698
"""
99+
for detection_data in scan_data.get("license_detections", []):
100+
pipes.update_or_create_license_detection(project, detection_data)
101+
97102
for package_data in scan_data.get("packages", []):
98103
pipes.update_or_create_package(project, package_data)
99104

@@ -110,12 +115,14 @@ def load_inventory_from_scanpipe(project, scan_data):
110115
model_to_object_maker_func = {
111116
DiscoveredPackage: pipes.update_or_create_package,
112117
DiscoveredDependency: pipes.update_or_create_dependency,
118+
DiscoveredLicense: pipes.update_or_create_license_detection,
113119
CodebaseResource: pipes.update_or_create_resource,
114120
CodebaseRelation: pipes.get_or_create_relation,
115121
}
116122

117123
worksheet_name_to_model = {
118124
"PACKAGES": DiscoveredPackage,
125+
"LICENSE_DETECTIONS": DiscoveredLicense,
119126
"RESOURCES": CodebaseResource,
120127
"DEPENDENCIES": DiscoveredDependency,
121128
"RELATIONS": CodebaseRelation,

scanpipe/pipes/scancode.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,14 @@ def create_codebase_resources(project, scanned_codebase):
572572
discovered_package=package,
573573
)
574574

575+
license_detections = getattr(scanned_resource, "license_detections", [])
576+
for detection_data in license_detections:
577+
detection_identifier = detection_data.get("identifier")
578+
license_detection = project.discoveredlicenses.get_or_none(
579+
identifier=detection_identifier
580+
)
581+
logger.debug(f"Add {codebase_resource} to {license_detection}")
582+
575583

576584
def create_discovered_packages(project, scanned_codebase):
577585
"""
@@ -606,6 +614,17 @@ def create_discovered_dependencies(
606614
)
607615

608616

617+
def create_discovered_licenses(project, scanned_codebase):
618+
"""
619+
Save the license detections of a ScanCode `scanned_codebase`
620+
scancode.resource.Codebase object to the database as a DiscoveredLicense of
621+
`project`.
622+
"""
623+
if hasattr(scanned_codebase.attributes, "license_detections"):
624+
for detection_data in scanned_codebase.attributes.license_detections:
625+
pipes.update_or_create_license_detection(project, detection_data)
626+
627+
609628
def set_codebase_resource_for_package(codebase_resource, discovered_package):
610629
"""
611630
Assign the `discovered_package` to the `codebase_resource` and set its

0 commit comments

Comments
 (0)