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 9, 2020
1 parent 0423b0c commit cbee293
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 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.
25 changes: 25 additions & 0 deletions pulp_rpm/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,28 @@
)

PACKAGES_DIRECTORY = "Packages"

# Types of advisory packages' sum_types
# Please keep keys by UPPER keys by value as is in
# https://github.com/rpm-software-management/createrepo_c/blob/master/src/checksum.h#L43-L54
# and keep both dicts same for performance reason
ADVISORY_SUM_TYPES_NUMS = {
"UNKNOWN": 0,
"MD5": 1,
"SHA": 2,
"SHA1": 3,
"SHA224": 4,
"SHA256": 5,
"SHA384": 6,
"SHA512": 7
}
ADVISORY_SUM_TYPES_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_TYPES_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_TYPES_NAME[pkg['sum_type']],
'version': pkg['version'],
})

Expand Down
28 changes: 28 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,28 @@
# 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')
for package in UpdateCollectionPackage.objects.all():
package.sum_type = int(package.sum_type) if package.sum_type else 0
package.save()


class Migration(migrations.Migration):

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

operations = [
migrations.RunPython(translate_sum_type),
migrations.AlterField(
model_name='updatecollectionpackage',
name='sum_type',
field=models.PositiveIntegerField(choices=[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)]),
),
]
9 changes: 6 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_TYPES_NUMS
)

log = getLogger(__name__)
Expand Down Expand Up @@ -350,7 +351,9 @@ class UpdateCollectionPackage(BaseModel):
release = models.TextField()
src = models.TextField()
sum = models.TextField()
sum_type = models.TextField()
sum_type = models.PositiveIntegerField(
choices=[(sum_type, sum_type) for sum_type in ADVISORY_SUM_TYPES_NUMS.values()]
)
version = models.TextField()

update_collection = models.ForeignKey(UpdateCollection, related_name='packages',
Expand Down Expand Up @@ -390,7 +393,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 0,
PULP_UPDATE_COLLECTION_PACKAGE_ATTRS.VERSION: getattr(
package, CR_UPDATE_COLLECTION_PACKAGE_ATTRS.VERSION) or ''
}
Expand Down Expand Up @@ -419,7 +422,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 or 0

return pkg

Expand Down
4 changes: 4 additions & 0 deletions pulp_rpm/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
)

from pulp_rpm.app.constants import (
ADVISORY_SUM_TYPES_NUMS,
CR_UPDATE_REFERENCE_ATTRS,
PULP_UPDATE_COLLECTION_ATTRS,
PULP_UPDATE_RECORD_ATTRS,
Expand Down Expand Up @@ -490,6 +491,9 @@ def create(self, validated_data):
coll.update_record.add(update_record)
for package in packages:
pkg = UpdateCollectionPackage(**package)
if (isinstance(pkg.sum_type, str)
and pkg.sum_type.upper() in ADVISORY_SUM_TYPES_NUMS.keys()):
pkg.sum_type = ADVISORY_SUM_TYPES_NUMS[pkg.sum_type.upper()]
pkg.update_collection = coll
update_collection_packages_to_save.append(pkg)
for reference in references:
Expand Down

0 comments on commit cbee293

Please sign in to comment.