@@ -1254,7 +1254,7 @@ def assemble_from_many(
12541254 pkgdata_resources ,
12551255 codebase ,
12561256 package_adder = add_to_package ,
1257- ignore_name_check = False ,
1257+ relaxed_purl_comparision = False ,
12581258 parent_resource = None ,
12591259 ):
12601260 """
@@ -1287,34 +1287,40 @@ def assemble_from_many(
12871287 base_resource = parent_resource
12881288
12891289 # process each package in sequence. The first item creates a package and
1290- # the other only update
1290+ # thereafter we create new packages to update the old packages
12911291 # We are saving the Packages, Dependencies, and Resources in lists until
12921292 # after we go through `pkgdata_resources` for all Package data, then we
12931293 # yield Packages, then Dependencies, then Resources.
12941294 dependencies = []
12951295 resources = []
12961296 resources_from_package = []
1297+ packages_by_purl = {}
1298+
12971299 for package_data , resource in pkgdata_resources :
12981300 if not base_resource :
12991301 base_resource = resource
13001302
1301- if not package :
1302- # create package from the first item first package_data
1303- if package_data .purl :
1304- package = Package .from_package_data (
1305- package_data = package_data ,
1306- datafile_path = resource .path ,
1307- )
1308- package_uid = package .package_uid
1309- else :
1310- # FIXME: What is the package_data is NOT for the same package as package?
1311- # FIXME: What if the update did not do anything? (it does return True or False)
1312- # FIXME: There we would be missing out packges AND/OR errors
1303+ compatible_package = get_compatible_package (
1304+ package_data = package_data ,
1305+ packages_by_purl = packages_by_purl ,
1306+ relaxed_purl_comparision = True ,
1307+ )
1308+ if package_data .purl and not compatible_package :
1309+ package = Package .from_package_data (
1310+ package_data = package_data ,
1311+ datafile_path = resource .path ,
1312+ )
1313+ package_uid = package .package_uid
1314+ packages_by_purl [package .purl ] = package
1315+
1316+ elif compatible_package :
1317+ package = compatible_package
13131318 package .update (
13141319 package_data = package_data ,
13151320 datafile_path = resource .path ,
1316- ignore_name_check = ignore_name_check ,
1321+ relaxed_purl_comparision = relaxed_purl_comparision ,
13171322 )
1323+ package_uid = package .package_uid
13181324
13191325 if package_uid :
13201326 resources_from_package .append ((package_uid , resource ,))
@@ -1334,7 +1340,7 @@ def assemble_from_many(
13341340 resources .append (resource )
13351341
13361342 # Yield Packages, Dependencies, and Resources
1337- if package :
1343+ for package in packages_by_purl . values () :
13381344 package .populate_license_fields ()
13391345 yield package
13401346 yield from dependencies
@@ -1419,7 +1425,7 @@ def assemble_from_many_datafiles_in_directory(
14191425 directory ,
14201426 codebase ,
14211427 package_adder = add_to_package ,
1422- ignore_name_check = False ,
1428+ relaxed_purl_comparision = False ,
14231429 parent_resource = None ,
14241430 ):
14251431 """
@@ -1440,7 +1446,7 @@ def assemble_from_many_datafiles_in_directory(
14401446 multiple PackageData for unrelated Packages.
14411447 """
14421448 if TRACE :
1443- logger_debug (f'assemble_from_many_datafiles: datafile_name_patterns: { datafile_name_patterns !r} ' )
1449+ logger_debug (f'assemble_from_many_datafiles: directory: { directory } datafile_name_patterns: { datafile_name_patterns !r} ' )
14441450
14451451 if not codebase .has_single_resource :
14461452 resources = [
@@ -1471,7 +1477,7 @@ def assemble_from_many_datafiles_in_directory(
14711477 pkgdata_resources = pkgdata_resources ,
14721478 codebase = codebase ,
14731479 package_adder = package_adder ,
1474- ignore_name_check = ignore_name_check ,
1480+ relaxed_purl_comparision = relaxed_purl_comparision ,
14751481 parent_resource = parent_resource ,
14761482 )
14771483
@@ -1713,7 +1719,7 @@ def update(
17131719 include_version = True ,
17141720 include_qualifiers = False ,
17151721 include_subpath = False ,
1716- ignore_name_check = False ,
1722+ relaxed_purl_comparision = False ,
17171723 default_relation = 'AND' ,
17181724 licensing = Licensing (),
17191725 ):
@@ -1748,7 +1754,8 @@ def update(
17481754 include_version = include_version ,
17491755 include_qualifiers = include_qualifiers ,
17501756 include_subpath = include_subpath ,
1751- ignore_name_check = ignore_name_check ,
1757+ relaxed_purl_comparision = relaxed_purl_comparision ,
1758+ pname_match = does_pname_match (self , package_data ),
17521759 ):
17531760 if TRACE_UPDATE :
17541761 logger_debug (f'update: skipping: { self .purl } is not compatible with: { package_data .purl } ' )
@@ -1853,18 +1860,27 @@ def get_packages_files(self, codebase):
18531860 yield resource
18541861
18551862
1863+ # In certain cases package manifests should be assembled even if
1864+ # the PURLs are slightly different
1865+ compatible_types = {
1866+ "maven" : "jar" ,
1867+ "jar" : "maven" ,
1868+ }
1869+
1870+
18561871def is_compatible (
18571872 purl1 ,
18581873 purl2 ,
18591874 include_version = True ,
18601875 include_qualifiers = True ,
18611876 include_subpath = True ,
1862- ignore_name_check = False ,
1877+ relaxed_purl_comparision = False ,
1878+ pname_match = False ,
18631879):
18641880 """
18651881 Return True if the ``purl1`` PackageURL-like object is compatible with
18661882 the ``purl2`` PackageURL-like object, e.g. it is about the same package.
1867- PackageData objectys are PackageURL-like.
1883+ PackageData objects are PackageURL-like.
18681884
18691885 For example::
18701886 >>> p1 = PackageURL.from_string('pkg:deb/libncurses5@6.1-1ubuntu1.18.04?arch=arm64')
@@ -1887,17 +1903,23 @@ def is_compatible(
18871903 >>> is_compatible(p1, p5, include_subpath=False)
18881904 True
18891905 """
1890- if ignore_name_check :
1891- is_compatible = (
1892- purl1 .type == purl2 .type
1893- and purl1 .namespace == purl2 .namespace
1906+ is_compatible_purl = (
1907+ purl1 .type == purl2 .type
1908+ and purl1 .namespace == purl2 .namespace
1909+ and purl1 .name == purl2 .name
1910+ )
1911+ if relaxed_purl_comparision :
1912+ is_compatible_type = (
1913+ purl1 .type in compatible_types
1914+ and purl2 .type == compatible_types .get (purl1 .type )
18941915 )
1895- else :
18961916 is_compatible = (
1897- purl1 .type == purl2 .type
1898- and purl1 .namespace == purl2 .namespace
1899- and purl1 .name == purl2 .name
1917+ is_compatible_purl or (is_compatible_type and pname_match ) or
1918+ (is_compatible_type and purl1 .type != purl2 .type and purl1 .namespace == purl2 .namespace )
19001919 )
1920+ else :
1921+ is_compatible = is_compatible_purl
1922+
19011923 if include_version :
19021924 is_compatible = is_compatible and (purl1 .version == purl2 .version )
19031925
@@ -1910,6 +1932,38 @@ def is_compatible(
19101932 return is_compatible
19111933
19121934
1935+ def get_compatible_package (
1936+ package_data ,
1937+ packages_by_purl ,
1938+ include_version = True ,
1939+ include_qualifiers = True ,
1940+ include_subpath = True ,
1941+ relaxed_purl_comparision = False ,
1942+ ):
1943+ if not package_data .purl :
1944+ return
1945+
1946+ for purl_to_compare , package in packages_by_purl .items ():
1947+ if is_compatible (
1948+ purl1 = PackageURL .from_string (purl_to_compare ),
1949+ purl2 = PackageURL .from_string (package_data .purl ),
1950+ include_version = include_version ,
1951+ include_qualifiers = include_qualifiers ,
1952+ include_subpath = include_subpath ,
1953+ relaxed_purl_comparision = relaxed_purl_comparision ,
1954+ pname_match = does_pname_match (package , package_data ),
1955+ ):
1956+ return package
1957+
1958+
1959+ def does_pname_match (package1 , package2 ):
1960+ pname1 = package1 .extra_data .get ("pname" )
1961+ pname2 = package2 .extra_data .get ("pname" )
1962+ if pname1 and pname2 and pname1 == pname2 :
1963+ return True
1964+
1965+ return False
1966+
19131967@attr .attributes (slots = True )
19141968class PackageWithResources (Package ):
19151969 """
0 commit comments