Skip to content

Commit

Permalink
Update logic in create_source_repo_packages
Browse files Browse the repository at this point in the history
    * Reduce the number of times .save() is called
    * Ensure that the source_package's package_set value is the same as the package_set value of the binary package.

Signed-off-by: Jono Yang <jyang@nexb.com>
  • Loading branch information
JonoYang committed Jun 23, 2023
1 parent fd269e9 commit 39effb4
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packagedb/management/commands/create_source_repo_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,43 @@ 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_or_none(qualifiers='')
binary_package_updated = False

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()
binary_package_updated = True

# Set package_content value for binary_package, if needed
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_updated = True

if binary_package_updated:
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_or_none(qualifiers__contains='classifier=sources')
source_package_updated = False

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

# Set source_package value for binary_package, if needed
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.package_content = PackageContentType.SOURCE_ARCHIVE
print(f'Set package_content for {source_package.purl} to SOURCE_ARCHIVE')
source_package_updated = True

if source_package_updated:
source_package.save()

# Create new Package from the source_ fields
Expand All @@ -121,7 +129,6 @@ def handle(self, *args, **options):
version=row['source_version'],
download_url=row['source_download_url'],
package_set=package_set,
# TODO: Should package_content be SOURCE_ARCHIVE or SOURCE_REPO?
package_content=PackageContentType.SOURCE_REPO,
)
if package:
Expand Down

0 comments on commit 39effb4

Please sign in to comment.