Skip to content

Commit

Permalink
Advisory package sum_type
Browse files Browse the repository at this point in the history
Advisory package sum_type will be stored as INT as createrepo_c uses it.
Upload will translate supported checksum from str to int.
User will still see a string representation of sum_type.

closes #6442
https://pulp.plan.io/issues/6442
  • Loading branch information
pavelpicka committed Apr 17, 2020
1 parent 4ec3c7e commit 50be24e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGES/6442.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Save advisory packages' sum_type as integer as createrepo_c uses. Still showed to user as a string.
15 changes: 15 additions & 0 deletions pulp_rpm/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,18 @@
)

PACKAGES_DIRECTORY = "Packages"

# Mappings of the possible integer values of "sum_type" on Advisory packages to their user-facing
# string representation. Should mirror the createrepo_c source code:
# https://github.com/rpm-software-management/createrepo_c/blob/master/src/checksum.h#L43-L54
# Please keep dict consistent with createrepo_c mapping mentioned above.
ADVISORY_SUM_TYPE_TO_NAME = {
0: "UNKNOWN",
1: "MD5",
2: "SHA",
3: "SHA1",
4: "SHA224",
5: "SHA256",
6: "SHA384",
7: "SHA512"
}
3 changes: 2 additions & 1 deletion pulp_rpm/app/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rest_framework import serializers
from pulp_rpm.app.constants import ADVISORY_SUM_TYPE_TO_NAME
from pulp_rpm.app.models import UpdateReference


Expand Down Expand Up @@ -33,7 +34,7 @@ def to_representation(self, obj):
'release': pkg['release'],
'src': pkg['src'],
'sum': pkg['sum'],
'sum_type': pkg['sum_type'],
'sum_type': ADVISORY_SUM_TYPE_TO_NAME.get(pkg['sum_type'], ""),
'version': pkg['version'],
})

Expand Down
44 changes: 44 additions & 0 deletions pulp_rpm/app/migrations/0007_advisory_pkg_sumtype_as_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 2.2.11 on 2020-04-06 16:05

from django.db import migrations, models, transaction


def translate_sum_type(apps, schema_editor):
# use sum_type as int as createrepo_c uses
with transaction.atomic():
UpdateCollectionPackage = apps.get_model('rpm', 'UpdateCollectionPackage')
update_collection_package_to_save = []
for package in UpdateCollectionPackage.objects.all():
package.sum_type_temp = int(package.sum_type) if package.sum_type else None
update_collection_package_to_save.append(package)
UpdateCollectionPackage.objects.bulk_update(
update_collection_package_to_save,
['sum_type_temp']
)


class Migration(migrations.Migration):

dependencies = [
('rpm', '0006_opensuse_support'),
]

operations = [
migrations.AddField(
model_name='updatecollectionpackage',
name='sum_type_temp',
field=models.PositiveIntegerField(
null=True, default=None
)
),
migrations.RunPython(translate_sum_type),
migrations.RemoveField(
model_name='updatecollectionpackage',
name='sum_type'
),
migrations.RenameField(
model_name='updatecollectionpackage',
old_name='sum_type_temp',
new_name='sum_type'
)
]
31 changes: 0 additions & 31 deletions pulp_rpm/app/migrations/0007_checksum_types.py

This file was deleted.

12 changes: 9 additions & 3 deletions pulp_rpm/app/models/advisory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
PULP_UPDATE_COLLECTION_PACKAGE_ATTRS,
PULP_UPDATE_RECORD_ATTRS,
PULP_UPDATE_REFERENCE_ATTRS,
ADVISORY_SUM_TYPE_TO_NAME
)

log = getLogger(__name__)
Expand Down Expand Up @@ -358,7 +359,12 @@ class UpdateCollectionPackage(BaseModel):
release = models.TextField()
src = models.TextField()
sum = models.TextField()
sum_type = models.TextField()
sum_type = models.PositiveIntegerField(
null=True,
choices=[
(sum_type, sum_type) for sum_type in ADVISORY_SUM_TYPE_TO_NAME.keys()
]
)
version = models.TextField()

update_collection = models.ForeignKey(UpdateCollection, related_name='packages',
Expand Down Expand Up @@ -398,7 +404,7 @@ def createrepo_to_dict(cls, package):
PULP_UPDATE_COLLECTION_PACKAGE_ATTRS.SUM: getattr(
package, CR_UPDATE_COLLECTION_PACKAGE_ATTRS.SUM) or '',
PULP_UPDATE_COLLECTION_PACKAGE_ATTRS.SUM_TYPE: getattr(
package, CR_UPDATE_COLLECTION_PACKAGE_ATTRS.SUM_TYPE) or '',
package, CR_UPDATE_COLLECTION_PACKAGE_ATTRS.SUM_TYPE) or None,
PULP_UPDATE_COLLECTION_PACKAGE_ATTRS.VERSION: getattr(
package, CR_UPDATE_COLLECTION_PACKAGE_ATTRS.VERSION) or ''
}
Expand Down Expand Up @@ -427,7 +433,7 @@ def to_createrepo_c(self):
pkg.restart_suggested = self.restart_suggested
if self.sum:
pkg.sum = self.sum
pkg.sum_type = int(self.sum_type or 0)
pkg.sum_type = self.sum_type

return pkg

Expand Down
5 changes: 5 additions & 0 deletions pulp_rpm/app/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import createrepo_c
import json
from gettext import gettext as _

Expand Down Expand Up @@ -504,6 +505,10 @@ def create(self, validated_data):
coll.update_record.add(update_record)
for package in packages:
pkg = UpdateCollectionPackage(**package)
try:
pkg.sum_type = createrepo_c.checksum_type(pkg.sum_type)
except TypeError:
raise TypeError(f'"{pkg.sum_type}" is not supported.')
pkg.update_collection = coll
update_collection_packages_to_save.append(pkg)
for reference in references:
Expand Down

0 comments on commit 50be24e

Please sign in to comment.