Skip to content

Commit e73da98

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

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")
@@ -245,6 +246,37 @@ def update_or_create_dependency(
245246
return dependency
246247

247248

249+
def update_or_create_license_detection(project, detection_data):
250+
"""
251+
Get, update or create a DiscoveredLicense object then return it.
252+
Use the `project` and `detection_data` mapping to lookup and creates the
253+
DiscoveredLicense using its detection identifier as a unique key.
254+
"""
255+
detection_identifier = detection_data["identifier"]
256+
257+
license_detection = project.discoveredlicenses.get_or_none(
258+
identifier=detection_identifier,
259+
)
260+
detection_data = _clean_license_detection_data(detection_data)
261+
262+
if license_detection:
263+
license_detection.update_from_data(detection_data)
264+
else:
265+
license_detection = DiscoveredLicense.create_from_data(
266+
project,
267+
detection_data,
268+
)
269+
270+
return license_detection
271+
272+
273+
def _clean_license_detection_data(detection_data):
274+
detection_data = detection_data.copy()
275+
matches = detection_data.pop("sample_matches")
276+
detection_data["matches"] = matches
277+
return detection_data
278+
279+
248280
def get_or_create_relation(project, relation_data):
249281
"""
250282
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
@@ -586,6 +586,14 @@ def create_codebase_resources(project, scanned_codebase):
586586
discovered_package=package,
587587
)
588588

589+
license_detections = getattr(scanned_resource, "license_detections", [])
590+
for detection_data in license_detections:
591+
detection_identifier = detection_data.get("identifier")
592+
license_detection = project.discoveredlicenses.get_or_none(
593+
identifier=detection_identifier
594+
)
595+
logger.debug(f"Add {codebase_resource} to {license_detection}")
596+
589597

590598
def create_discovered_packages(project, scanned_codebase):
591599
"""
@@ -620,6 +628,17 @@ def create_discovered_dependencies(
620628
)
621629

622630

631+
def create_discovered_licenses(project, scanned_codebase):
632+
"""
633+
Save the license detections of a ScanCode `scanned_codebase`
634+
scancode.resource.Codebase object to the database as a DiscoveredLicense of
635+
`project`.
636+
"""
637+
if hasattr(scanned_codebase.attributes, "license_detections"):
638+
for detection_data in scanned_codebase.attributes.license_detections:
639+
pipes.update_or_create_license_detection(project, detection_data)
640+
641+
623642
def set_codebase_resource_for_package(codebase_resource, discovered_package):
624643
"""
625644
Assign the `discovered_package` to the `codebase_resource` and set its

0 commit comments

Comments
 (0)