Skip to content

Commit

Permalink
Fix Package/PackageData creation bugs in purldb (#3710)
Browse files Browse the repository at this point in the history
Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
  • Loading branch information
AyanSinhaMahapatra authored Jul 29, 2024
1 parent dd675aa commit 1dc4827
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/packagedcode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,10 @@ def get_license_detections_and_expression(self):

def get_default_relation_license(datasource_id):
from packagedcode import HANDLER_BY_DATASOURCE_ID
handler = HANDLER_BY_DATASOURCE_ID[datasource_id]
handler = HANDLER_BY_DATASOURCE_ID.get(datasource_id, None)
if not handler:
return 'AND'

return handler.default_relation_license


Expand Down Expand Up @@ -1562,6 +1565,8 @@ class Package(PackageData):
)

def __attrs_post_init__(self, *args, **kwargs):
if not self.purl:
self.purl = self.set_purl()
if not self.package_uid:
self.package_uid = build_package_uid(self.purl)

Expand All @@ -1576,7 +1581,7 @@ def to_package_data(self):
return PackageData.from_dict(mapping)

@classmethod
def from_package_data(cls, package_data, datafile_path, package_only=False):
def from_package_data(cls, package_data, datafile_path=None, package_only=False):
"""
Return a Package from a ``package_data`` PackageData object
or mapping. Or None.
Expand All @@ -1591,20 +1596,21 @@ def from_package_data(cls, package_data, datafile_path, package_only=False):
elif package_data:
raise Exception(f'Invalid type: {package_data!r}', package_data)

package_data_mapping['datafile_paths'] = [datafile_path]
package_data_mapping['datasource_ids'] = [dsid]

license_detections = package_data_mapping['license_detections']
for detection in license_detections:
for license_match in detection['matches']:
if not license_match['from_file']:
license_match['from_file'] = datafile_path
if datafile_path:
package_data_mapping['datafile_paths'] = [datafile_path]
license_detections = package_data_mapping.get('license_detections', [])
for detection in license_detections:
for license_match in detection['matches']:
if not license_match['from_file']:
license_match['from_file'] = datafile_path

package = cls.from_dict(package_data_mapping)

if not package.package_uid:
package.package_uid = build_package_uid(package.purl)

if not package_only:
package.populate_license_fields()
package.populate_holder_field()
Expand Down

0 comments on commit 1dc4827

Please sign in to comment.