Skip to content

Commit 16f9b4c

Browse files
committed
make create_codebase_resource function less complex
Signed-off-by: Aayush Kumar <[email protected]>
1 parent 111a901 commit 16f9b4c

File tree

1 file changed

+73
-66
lines changed

1 file changed

+73
-66
lines changed

scanpipe/pipes/scancode.py

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -900,87 +900,94 @@ def get_virtual_codebase(project, input_location):
900900
return VirtualCodebase(input_location, temp_dir=str(temp_path), max_in_memory=0)
901901

902902

903-
def create_codebase_resources(project, scanned_codebase):
904-
"""
905-
Save the resources of a ScanCode `scanned_codebase` scancode.resource.Codebase
906-
object to the database as a CodebaseResource of the `project`.
907-
This function can be used to expend an existing `project` Codebase with new
908-
CodebaseResource objects as the existing objects (based on the `path`) will be
909-
skipped.
910-
"""
911-
for scanned_resource in scanned_codebase.walk(skip_root=True):
912-
resource_data = {}
913-
914-
for field in CodebaseResource._meta.fields:
915-
# Do not include the path as provided by the scanned_resource since it
916-
# includes the "root". The `get_path` method is used instead.
917-
if field.name == "path":
918-
continue
919-
if field.name == "parent_path":
920-
continue
921-
value = getattr(scanned_resource, field.name, None)
922-
if value is not None:
923-
resource_data[field.name] = value
924-
925-
resource_type = "FILE" if scanned_resource.is_file else "DIRECTORY"
926-
resource_data["type"] = CodebaseResource.Type[resource_type]
927-
resource_path = scanned_resource.get_path(strip_root=True)
928-
929-
parent_path = str(Path(resource_path).parent)
930-
if parent_path == ".":
931-
parent_path = ""
932-
resource_data["parent_path"] = parent_path
933-
934-
codebase_resource, _ = CodebaseResource.objects.get_or_create(
903+
def create_codebase_resource(project, scanned_resource):
904+
"""Create a CodebaseResource entry from ScanCode scanned data."""
905+
resource_data = {}
906+
907+
for field in CodebaseResource._meta.fields:
908+
# Do not include the path as provided by the scanned_resource since it
909+
# includes the "root". The `get_path` method is used instead.
910+
if field.name in ["path", "parent_path"]:
911+
continue
912+
value = getattr(scanned_resource, field.name, None)
913+
if value is not None:
914+
resource_data[field.name] = value
915+
916+
resource_type = "FILE" if scanned_resource.is_file else "DIRECTORY"
917+
resource_data["type"] = CodebaseResource.Type[resource_type]
918+
resource_path = scanned_resource.get_path(strip_root=True)
919+
920+
parent_path = str(Path(resource_path).parent)
921+
if parent_path == ".":
922+
parent_path = ""
923+
resource_data["parent_path"] = parent_path
924+
925+
codebase_resource, _ = CodebaseResource.objects.get_or_create(
926+
project=project,
927+
path=resource_path,
928+
defaults=resource_data,
929+
)
930+
931+
# Handle package assignments
932+
for_packages = getattr(scanned_resource, "for_packages", [])
933+
for package_uid in for_packages:
934+
logger.debug(f"Assign {package_uid} to {codebase_resource}")
935+
package = project.discoveredpackages.get(package_uid=package_uid)
936+
set_codebase_resource_for_package(
937+
codebase_resource=codebase_resource,
938+
discovered_package=package,
939+
)
940+
941+
# Handle license detections
942+
license_detections = getattr(scanned_resource, "license_detections", [])
943+
for detection_data in license_detections:
944+
detection_identifier = detection_data.get("identifier")
945+
pipes.update_or_create_license_detection(
935946
project=project,
936-
path=resource_path,
937-
defaults=resource_data,
947+
detection_data=detection_data,
948+
resource_path=resource_path,
949+
count_detection=False,
938950
)
951+
logger.debug(f"Add {codebase_resource} to {detection_identifier}")
939952

940-
for_packages = getattr(scanned_resource, "for_packages", [])
941-
for package_uid in for_packages:
942-
logger.debug(f"Assign {package_uid} to {codebase_resource}")
943-
package = project.discoveredpackages.get(package_uid=package_uid)
944-
set_codebase_resource_for_package(
945-
codebase_resource=codebase_resource,
946-
discovered_package=package,
947-
)
953+
# Handle license clues
954+
license_clues = getattr(scanned_resource, "license_clues", [])
955+
for clue_data in license_clues:
956+
pipes.update_or_create_license_detection(
957+
project=project,
958+
detection_data=clue_data,
959+
resource_path=resource_path,
960+
is_license_clue=True,
961+
)
962+
logger.debug(f"Add license clue at {codebase_resource}")
948963

949-
license_detections = getattr(scanned_resource, "license_detections", [])
964+
# Handle package data
965+
packages = getattr(scanned_resource, "package_data", [])
966+
for package_data in packages:
967+
license_detections = package_data.get("license_detections", [])
968+
license_detections.extend(package_data.get("other_license_detections", []))
950969
for detection_data in license_detections:
951970
detection_identifier = detection_data.get("identifier")
952971
pipes.update_or_create_license_detection(
953972
project=project,
954973
detection_data=detection_data,
955974
resource_path=resource_path,
956975
count_detection=False,
976+
from_package=True,
957977
)
958978
logger.debug(f"Add {codebase_resource} to {detection_identifier}")
959979

960-
license_clues = getattr(scanned_resource, "license_clues", [])
961-
for clue_data in license_clues:
962-
pipes.update_or_create_license_detection(
963-
project=project,
964-
detection_data=clue_data,
965-
resource_path=resource_path,
966-
is_license_clue=True,
967-
)
968-
logger.debug(f"Add license clue at {codebase_resource}")
969980

970-
packages = getattr(scanned_resource, "package_data", [])
971-
for package_data in packages:
972-
license_detections = package_data.get("license_detections", [])
973-
license_detections.extend(package_data.get("other_license_detections", []))
974-
for detection_data in license_detections:
975-
detection_identifier = detection_data.get("identifier")
976-
pipes.update_or_create_license_detection(
977-
project=project,
978-
detection_data=detection_data,
979-
resource_path=resource_path,
980-
count_detection=False,
981-
from_package=True,
982-
)
983-
logger.debug(f"Add {codebase_resource} to {detection_identifier}")
981+
def create_codebase_resources(project, scanned_codebase):
982+
"""
983+
Save the resources of a ScanCode `scanned_codebase` scancode.resource.Codebase
984+
object to the database as a CodebaseResource of the `project`.
985+
This function can be used to expend an existing `project` Codebase with new
986+
CodebaseResource objects as the existing objects (based on the `path`) will be
987+
skipped.
988+
"""
989+
for scanned_resource in scanned_codebase.walk(skip_root=True):
990+
create_codebase_resource(project, scanned_resource)
984991

985992

986993
def create_discovered_packages(project, scanned_codebase):

0 commit comments

Comments
 (0)