Skip to content

Commit

Permalink
Cleanup now unneeded legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
quba42 authored and mibanescu committed Dec 11, 2018
1 parent 1c8ded3 commit cf098ba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
54 changes: 0 additions & 54 deletions plugins/pulp_deb/plugins/db/models.py
Expand Up @@ -2,7 +2,6 @@
from copy import deepcopy
from os.path import getsize
from debian import debfile, deb822
from debpkgr import debpkg
from pulp.server import util
from pulp.server.controllers import repository as repo_controller
from pulp.server.db.model import ContentUnit, FileContentUnit
Expand Down Expand Up @@ -219,43 +218,6 @@ def _check_for_required_fields(cls, metadata):
elif field not in metadata:
raise Error('Required field is missing: {}'.format(field))

@classmethod
def from_file(cls, filename, user_metadata=None):
if hasattr(filename, "read"):
fobj = filename
else:
try:
fobj = open(filename, "r")
except IOError as e:
raise Error(str(e))
unit_md = cls._read_metadata(filename)
unit_md.update(checksumtype=util.TYPE_SHA256,
checksum=cls._compute_checksum(fobj),
size=fobj.tell())

return cls.from_metadata(unit_md, user_metadata)

@classmethod
def from_metadata(cls, unit_md, user_metadata=None):
ignored = set(['filename'])

metadata = dict()
for attr, fdef in cls._fields.items():
if attr == 'id' or attr.startswith('_'):
continue
if user_metadata and attr in user_metadata:
# We won't be mapping fields from
# user_metadata, if the user wanted to overwrite something
# they'd have done it with the properties pulp expects.
metadata[attr] = user_metadata[attr]
prop_name = cls.TO_DEB822_MAP.get(attr, attr)
val = unit_md.get(prop_name)
if val is None and fdef.required and attr not in ignored:
raise Error('Required field is missing: {}'.format(attr))
metadata[attr] = val
metadata['filename'] = cls.filename_from_unit_key(metadata)
return cls(**metadata)

@classmethod
def _compute_checksum(cls, fobj):
cstype = util.TYPE_SHA256
Expand Down Expand Up @@ -303,22 +265,6 @@ def save_and_associate(self, file_path, repo):
unit.associate(repo)
return unit

@classmethod
def _read_metadata(cls, filename):
try:
deb = debfile.DebFile(filename)
except debfile.ArError as e:
raise InvalidPackageError(str(e))
ret = dict(deb.debcontrol())
deps = debpkg.DebPkgRequires(**ret)
# Munge relation fields

for fname in cls.REL_FIELDS:
vals = deps.relations.get(fname, [])
vals = DependencyParser.parse(vals)
ret[fname] = vals
return ret


class DebComponent(ContentUnit):
"""
Expand Down
50 changes: 46 additions & 4 deletions plugins/pulp_deb/plugins/migrations/0001_add_rel_fields.py
@@ -1,27 +1,69 @@
import logging
import os

from debian.debfile import DebFile, ArError

from pulp.server.db import connection
from pulp_deb.plugins.db import models
from pulp_deb.plugins.db.models import DependencyParser


_logger = logging.getLogger(__name__)

REL_FIELDS_MAP = dict(
breaks="Breaks",
conflicts="Conflicts",
depends="Depends",
enhances="Enhances",
pre_depends="Pre-Depends",
provides="Provides",
recommends="Recommends",
replaces="Replaces",
suggests="Suggests",
)


def migrate(*args, **kwargs):
"""
Add relationship fields (breaks/depends/etc)
"""
warnings_encountered = False
collection = connection.get_collection('units_deb')
for unit in collection.find({}):
unit_id = unit['_id']
path = unit['_storage_path']
if not os.path.exists(path):
warnings_encountered = True
msg = 'deb package file corresponding to db_unit with _id = {}\n'\
'was not found at _storage_path = {}.\n'\
'The unit was not migrated!'.format(unit_id, path)
_logger.warn(msg)
continue

try:
control_fields = DebFile(path).debcontrol()
except ArError as error:
warnings_encountered = True
msg = 'deb package file corresponding to db_unit with _id = {}\n'\
'with _storage_path = {}\n'\
'was not recognized as a valid deb file:\n'\
'{}\n'\
'The unit was not migrated!'.format(unit_id,
path,
str(error),)
_logger.warn(msg)
continue

m = models.DebPackage.from_file(path)
update_dict = dict()
for fname in m.REL_FIELDS:
update_dict[fname] = getattr(m, fname)
for field, deb_key in REL_FIELDS_MAP.iteritems():
if deb_key in control_fields:
update_dict[field] = DependencyParser.from_string(control_fields[deb_key])
else:
update_dict[field] = []

collection.update_one(dict(_id=unit_id),
{'$set': update_dict})

if warnings_encountered:
msg = 'Warnings were encountered during the db migration!\n'\
'Check the logs for more information, and consider deleting broken units.'
_logger.warn(msg)

0 comments on commit cf098ba

Please sign in to comment.