2626from component_catalog .models import PACKAGE_URL_FIELDS
2727from component_catalog .models import Component
2828from component_catalog .models import Package
29+ from dejacode_toolkit import download
2930from dejacode_toolkit .scancodeio import ScanCodeIO
3031from dje .copier import copy_object
3132from dje .importers import BaseImporter
@@ -649,7 +650,15 @@ class ImportPackageFromScanCodeIO:
649650 "filename" ,
650651 ]
651652
652- def __init__ (self , user , project_uuid , product , update_existing = False , scan_all_packages = False ):
653+ def __init__ (
654+ self ,
655+ user ,
656+ project_uuid ,
657+ product ,
658+ update_existing = False ,
659+ scan_all_packages = False ,
660+ infer_download_urls = False ,
661+ ):
653662 self .licensing = Licensing ()
654663 self .created = defaultdict (list )
655664 self .existing = defaultdict (list )
@@ -662,6 +671,7 @@ def __init__(self, user, project_uuid, product, update_existing=False, scan_all_
662671 self .product = product
663672 self .update_existing = update_existing
664673 self .scan_all_packages = scan_all_packages
674+ self .infer_download_urls = infer_download_urls
665675
666676 scancodeio = ScanCodeIO (user .dataspace )
667677 self .packages = scancodeio .fetch_project_packages (self .project_uuid )
@@ -696,6 +706,15 @@ def import_package(self, package_data):
696706 # Check if the package already exists to prevent duplication.
697707 package = self .look_for_existing_package (package_data )
698708
709+ # Infer a download URL from the Package URL
710+ if (
711+ self .infer_download_urls
712+ and not package_data .get ("download_url" )
713+ and (purl := package_data .get ("purl" ))
714+ and (download_url := download .infer_download_url (purl ))
715+ ):
716+ package_data ["download_url" ] = download_url
717+
699718 if license_expression := package_data .get ("declared_license_expression" ):
700719 license_expression = str (self .licensing .dedup (license_expression ))
701720 package_data ["license_expression" ] = license_expression
0 commit comments