Skip to content

Commit

Permalink
Add get or none on Package manager
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 committed Jun 23, 2023
1 parent 7a32896 commit fd269e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packagedb/management/commands/create_source_repo_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,37 @@ def handle(self, *args, **options):

# Get the binary package
# We use .get(qualifiers="") because the binary maven JAR has no qualifiers
binary_package = packages.get(qualifiers='')
binary_package = packages.get_or_none(qualifiers='')

package_set = binary_package.package_set
if not binary_package.package_set:
package_set = binary_package.package_set if binary_package else None
if binary_package and not binary_package.package_set:
# Create a new UUID and set it as this set of packages package_set value
package_set = uuid4()
print(f'Set package_set for {binary_package.purl} to {package_set}')
binary_package.package_set = package_set
binary_package.save()

# Set package_content value for binary_package, if needed
if not binary_package.package_content:
if binary_package and not binary_package.package_content:
binary_package.package_content = PackageContentType.BINARY
print(f'Set package_content for {binary_package.purl} to BINARY')
binary_package.save()

# Get the source package
# We use .get(qualifiers__contains='classifier=sources') as source JARs have the qualifiers set
source_package = packages.get(qualifiers__contains='classifier=sources')
source_package = packages.get_or_none(qualifiers__contains='classifier=sources')

# Set the package_set value to be the same as the one used for binary_package, if needed
if not source_package.package_set:
source_package.package_set = package_set
if source_package and not source_package.package_set:
source_package.package_set = package_set if package_set else uuid4()
print(f'Set package_set for {source_package.purl} to {package_set}')
source_package.save()

# Set source_package value for binary_package, if needed
if not source_package.package_content:
if source_package and not source_package.package_content:
source_package.package_content = PackageContentType.SOURCE_REPO
print(f'Set package_content for {source_package.purl} to SOURCE_REPO')
source_package.save()

# Create new Package from the source_ fields
package = Package.objects.create(
Expand Down
9 changes: 9 additions & 0 deletions packagedb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def insert(self, download_url, **extra_fields):
package, created = self.get_or_create(download_url=download_url, defaults=extra_fields)
if created:
return package

def get_or_none(self, *args, **kwargs):
"""
Return the object matching the given lookup parameters, or None if no match exists.
"""
try:
return self.get(*args, **kwargs)
except self.DoesNotExist:
return


VCS_CHOICES = [
Expand Down

0 comments on commit fd269e9

Please sign in to comment.