@@ -901,80 +901,94 @@ def get_virtual_codebase(project, input_location):
901901 return VirtualCodebase (input_location , temp_dir = str (temp_path ), max_in_memory = 0 )
902902
903903
904- def create_codebase_resources (project , scanned_codebase ):
905- """
906- Save the resources of a ScanCode `scanned_codebase` scancode.resource.Codebase
907- object to the database as a CodebaseResource of the `project`.
908- This function can be used to expend an existing `project` Codebase with new
909- CodebaseResource objects as the existing objects (based on the `path`) will be
910- skipped.
911- """
912- for scanned_resource in scanned_codebase .walk (skip_root = True ):
913- resource_data = {}
914-
915- for field in CodebaseResource ._meta .fields :
916- # Do not include the path as provided by the scanned_resource since it
917- # includes the "root". The `get_path` method is used instead.
918- if field .name == "path" :
919- continue
920- value = getattr (scanned_resource , field .name , None )
921- if value is not None :
922- resource_data [field .name ] = value
923-
924- resource_type = "FILE" if scanned_resource .is_file else "DIRECTORY"
925- resource_data ["type" ] = CodebaseResource .Type [resource_type ]
926- resource_path = scanned_resource .get_path (strip_root = True )
927-
928- codebase_resource , _ = CodebaseResource .objects .get_or_create (
904+ def create_codebase_resource (project , scanned_resource ):
905+ """Create a CodebaseResource entry from ScanCode scanned data."""
906+ resource_data = {}
907+
908+ for field in CodebaseResource ._meta .fields :
909+ # Do not include the path as provided by the scanned_resource since it
910+ # includes the "root". The `get_path` method is used instead.
911+ if field .name in ["path" , "parent_path" ]:
912+ continue
913+ value = getattr (scanned_resource , field .name , None )
914+ if value is not None :
915+ resource_data [field .name ] = value
916+
917+ resource_type = "FILE" if scanned_resource .is_file else "DIRECTORY"
918+ resource_data ["type" ] = CodebaseResource .Type [resource_type ]
919+ resource_path = scanned_resource .get_path (strip_root = True )
920+
921+ parent_path = str (Path (resource_path ).parent )
922+ if parent_path == "." :
923+ parent_path = ""
924+ resource_data ["parent_path" ] = parent_path
925+
926+ codebase_resource , _ = CodebaseResource .objects .get_or_create (
927+ project = project ,
928+ path = resource_path ,
929+ defaults = resource_data ,
930+ )
931+
932+ # Handle package assignments
933+ for_packages = getattr (scanned_resource , "for_packages" , [])
934+ for package_uid in for_packages :
935+ logger .debug (f"Assign { package_uid } to { codebase_resource } " )
936+ package = project .discoveredpackages .get (package_uid = package_uid )
937+ set_codebase_resource_for_package (
938+ codebase_resource = codebase_resource ,
939+ discovered_package = package ,
940+ )
941+
942+ # Handle license detections
943+ license_detections = getattr (scanned_resource , "license_detections" , [])
944+ for detection_data in license_detections :
945+ detection_identifier = detection_data .get ("identifier" )
946+ pipes .update_or_create_license_detection (
929947 project = project ,
930- path = resource_path ,
931- defaults = resource_data ,
948+ detection_data = detection_data ,
949+ resource_path = resource_path ,
950+ count_detection = False ,
932951 )
952+ logger .debug (f"Add { codebase_resource } to { detection_identifier } " )
933953
934- for_packages = getattr (scanned_resource , "for_packages" , [])
935- for package_uid in for_packages :
936- logger .debug (f"Assign { package_uid } to { codebase_resource } " )
937- package = project .discoveredpackages .get (package_uid = package_uid )
938- set_codebase_resource_for_package (
939- codebase_resource = codebase_resource ,
940- discovered_package = package ,
941- )
954+ # Handle license clues
955+ license_clues = getattr (scanned_resource , "license_clues" , [])
956+ for clue_data in license_clues :
957+ pipes .update_or_create_license_detection (
958+ project = project ,
959+ detection_data = clue_data ,
960+ resource_path = resource_path ,
961+ is_license_clue = True ,
962+ )
963+ logger .debug (f"Add license clue at { codebase_resource } " )
942964
943- license_detections = getattr (scanned_resource , "license_detections" , [])
965+ # Handle package data
966+ packages = getattr (scanned_resource , "package_data" , [])
967+ for package_data in packages :
968+ license_detections = package_data .get ("license_detections" , [])
969+ license_detections .extend (package_data .get ("other_license_detections" , []))
944970 for detection_data in license_detections :
945971 detection_identifier = detection_data .get ("identifier" )
946972 pipes .update_or_create_license_detection (
947973 project = project ,
948974 detection_data = detection_data ,
949975 resource_path = resource_path ,
950976 count_detection = False ,
977+ from_package = True ,
951978 )
952979 logger .debug (f"Add { codebase_resource } to { detection_identifier } " )
953980
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 } " )
963981
964- packages = getattr (scanned_resource , "package_data" , [])
965- for package_data in packages :
966- license_detections = package_data .get ("license_detections" , [])
967- license_detections .extend (package_data .get ("other_license_detections" , []))
968- for detection_data in license_detections :
969- detection_identifier = detection_data .get ("identifier" )
970- pipes .update_or_create_license_detection (
971- project = project ,
972- detection_data = detection_data ,
973- resource_path = resource_path ,
974- count_detection = False ,
975- from_package = True ,
976- )
977- logger .debug (f"Add { codebase_resource } to { detection_identifier } " )
982+ def create_codebase_resources (project , scanned_codebase ):
983+ """
984+ Save the resources of a ScanCode `scanned_codebase` scancode.resource.Codebase
985+ object to the database as a CodebaseResource of the `project`.
986+ This function can be used to expend an existing `project` Codebase with new
987+ CodebaseResource objects as the existing objects (based on the `path`) will be
988+ skipped.
989+ """
990+ for scanned_resource in scanned_codebase .walk (skip_root = True ):
991+ create_codebase_resource (project , scanned_resource )
978992
979993
980994def create_discovered_packages (project , scanned_codebase ):
0 commit comments