Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Package/PackageData creation bugs in purldb #3710

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/packagedcode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,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 @@ -1532,6 +1535,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 @@ -1546,7 +1551,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 @@ -1561,20 +1566,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
Loading