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 6, 2020
1 parent ea7a72c commit debb356
Show file tree
Hide file tree
Showing 7 changed files with 82 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.
14 changes: 14 additions & 0 deletions pulp_rpm/app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,17 @@
DESC_BY_LANG='desc_by_lang',
NAME_BY_LANG='name_by_lang'
)

# 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
ADVISORY_SUM_TYPES = {
"UNKNOWN": 0,
"MD5": 1,
"SHA": 2,
"SHA1": 3,
"SHA224": 4,
"SHA256": 5,
"SHA384": 6,
"SHA512": 7
}
4 changes: 3 additions & 1 deletion pulp_rpm/app/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework import serializers
from pulp_rpm.app.constants import ADVISORY_SUM_TYPES
from pulp_rpm.app.models import UpdateReference
from pulp_rpm.app.shared_utils import get_key_by_value


class UpdateCollectionPackagesField(serializers.ListField):
Expand Down Expand Up @@ -31,7 +33,7 @@ def to_representation(self, obj):
'release': pkg['release'],
'src': pkg['src'],
'sum': pkg['sum'],
'sum_type': pkg['sum_type'],
'sum_type': get_key_by_value(ADVISORY_SUM_TYPES, pkg['sum_type']),
'version': pkg['version'],
})

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

from django.db import migrations, models, transaction


ADVISORY_SUM_TYPES = {
"UNKNOWN": 0,
"MD5": 1,
"SHA": 2,
"SHA1": 3,
"SHA224": 4,
"SHA256": 5,
"SHA384": 6,
"SHA512": 7
}


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 = ADVISORY_SUM_TYPES[package.sum_type.upper()]
package.save()


class Migration(migrations.Migration):

dependencies = [
('rpm', '0005_optimize_sync'),
]

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
)

log = getLogger(__name__)
Expand Down Expand Up @@ -344,7 +345,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.values()]
)
version = models.TextField()

update_collection = models.ForeignKey(UpdateCollection, related_name='packages',
Expand Down Expand Up @@ -380,7 +383,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 All @@ -404,7 +407,7 @@ def to_createrepo_c(self):
pkg.reboot_suggested = self.reboot_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,
CR_UPDATE_REFERENCE_ATTRS,
PULP_UPDATE_COLLECTION_ATTRS,
PULP_UPDATE_RECORD_ATTRS,
Expand Down Expand Up @@ -480,6 +481,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.keys()):
pkg.sum_type = ADVISORY_SUM_TYPES[pkg.sum_type.upper()]
pkg.update_collection = coll
update_collection_packages_to_save.append(pkg)
for reference in references:
Expand Down
14 changes: 14 additions & 0 deletions pulp_rpm/app/shared_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ def is_previous_revision(revision, target_revision):
return revision == target_revision

return False

def get_key_by_value(dictionary, value):
"""
Get key from dict by its value.
Args:
dictionary: dictionary to search
value: value of key
Returns: key of dict or None
"""
for k, v in dictionary.items():
if v == value:
return k

0 comments on commit debb356

Please sign in to comment.