@@ -557,25 +557,40 @@ def assign_objects(self, related_objects, user, replace_version=False):
557557
558558 return created_count , updated_count , unchanged_count
559559
560- def scan_all_packages_task (self , user ):
560+ def scan_all_packages_task (self , user , infer_download_urls = False ):
561561 """
562562 Submit a Scan request to ScanCode.io for each package assigned to this Product.
563563 Only packages with a proper download URL are sent.
564564 """
565- package_urls = [
565+ if infer_download_urls :
566+ self .improve_packages_from_purl ()
567+
568+ package_download_urls = [
566569 package .download_url
567- for package in self .all_packages
570+ for package in self .all_packages . has_download_url ()
568571 if package .download_url .startswith (("http" , "https" ))
569572 ]
570573
571574 tasks .scancodeio_submit_scan .delay (
572- uris = package_urls ,
575+ uris = package_download_urls ,
573576 user_uuid = user .uuid ,
574577 dataspace_uuid = user .dataspace .uuid ,
575578 )
576579
580+ def improve_packages_from_purl (self ):
581+ """Infer missing packages download URL using the Package URL when possible."""
582+ updated_packages = []
583+
584+ packages = self .all_packages .has_package_url ().filter (models .Q (download_url = "" ))
585+ for package in packages :
586+ if download_url := package .infer_download_url ():
587+ package .update (download_url = download_url )
588+ updated_packages .append (package )
589+
590+ return updated_packages
591+
577592 def improve_packages_from_purldb (self , user ):
578- """Update all Packages assigned to the Product using PurlDB data."""
593+ """Update all packages assigned to thepProduct using PurlDB data."""
579594 updated_packages = []
580595 for package in self .packages .all ():
581596 updated_fields = package .update_from_purldb (user )
@@ -1555,6 +1570,9 @@ class Status(models.TextChoices):
15551570 scan_all_packages = models .BooleanField (
15561571 default = False ,
15571572 )
1573+ infer_download_urls = models .BooleanField (
1574+ default = False ,
1575+ )
15581576 status = models .CharField (
15591577 max_length = 10 ,
15601578 choices = Status .choices ,
@@ -1615,6 +1633,7 @@ def import_data_from_scancodeio(self):
16151633 product = self .product ,
16161634 update_existing = self .update_existing_packages ,
16171635 scan_all_packages = self .scan_all_packages ,
1636+ infer_download_urls = self .infer_download_urls ,
16181637 )
16191638 created , existing , errors = importer .save ()
16201639
0 commit comments