Skip to content

Commit

Permalink
Merge pull request #4 from mhrivnak/fix-copy
Browse files Browse the repository at this point in the history
fixed unit copy with mongoengine; added cli commands to copy drpm and metafile
  • Loading branch information
bmbouter committed Dec 14, 2015
2 parents 53f46e4 + d97b5e4 commit 0722278
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 154 deletions.
9 changes: 8 additions & 1 deletion extensions_admin/pulp_rpm/extensions/admin/copy_commands.py
Expand Up @@ -7,7 +7,8 @@
from pulp_rpm.common.constants import DISPLAY_UNITS_THRESHOLD, CONFIG_RECURSIVE
from pulp_rpm.common.ids import (TYPE_ID_RPM, TYPE_ID_SRPM, TYPE_ID_DRPM, TYPE_ID_ERRATA,
TYPE_ID_DISTRO, TYPE_ID_PKG_GROUP, TYPE_ID_PKG_CATEGORY,
TYPE_ID_PKG_ENVIRONMENT, UNIT_KEY_RPM)
TYPE_ID_PKG_ENVIRONMENT, TYPE_ID_YUM_REPO_METADATA_FILE,
UNIT_KEY_RPM)


# -- constants ----------------------------------------------------------------
Expand All @@ -20,6 +21,7 @@
DESC_PKG_GROUP = _('copy package groups from one repository to another')
DESC_PKG_CATEGORY = _('copy package categories from one repository to another')
DESC_PKG_ENVIRONMENT = _('copy package environment from one repository to another')
DESC_METAFILE = _('copy yum repo metadata files from one repository to another')
DESC_ALL = _('copy all content units from one repository to another')

DESC_RECURSIVE = _(
Expand Down Expand Up @@ -137,6 +139,11 @@ def __init__(self, context):
TYPE_ID_PKG_ENVIRONMENT)


class YumRepoMetadataFileCommand(NonRecursiveCopyCommand):
def __init__(self, context):
NonRecursiveCopyCommand.__init__(self, context, 'metafile', DESC_METAFILE,
TYPE_ID_YUM_REPO_METADATA_FILE)

class AllCopyCommand(NonRecursiveCopyCommand):
def __init__(self, context):
NonRecursiveCopyCommand.__init__(self, context, 'all', DESC_ALL, None)
Expand Up @@ -39,6 +39,8 @@ def initialize(context):
copy_section.add_command(copy_commands.PackageEnvironmentCopyCommand(context))
copy_section.add_command(copy_commands.AllCopyCommand(context))
copy_section.add_command(copy_commands.SrpmCopyCommand(context))
copy_section.add_command(copy_commands.YumRepoMetadataFileCommand(context))
copy_section.add_command(copy_commands.DrpmCopyCommand(context))

# Disabled as per 950690. We'll likely be able to add these back once the new
# yum importer is finished and DRPMs are properly handled.
Expand Down
28 changes: 5 additions & 23 deletions extensions_admin/pulp_rpm/extensions/admin/units_display.py
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

from pulp_rpm.common.ids import (TYPE_ID_RPM, TYPE_ID_SRPM, TYPE_ID_DRPM, TYPE_ID_ERRATA,
TYPE_ID_DISTRO, TYPE_ID_PKG_GROUP, TYPE_ID_PKG_CATEGORY,
TYPE_ID_PKG_ENVIRONMENT, TYPE_ID_YUM_REPO_METADATA_FILE)
Expand All @@ -17,11 +15,11 @@ def get_formatter_for_type(type_id):
TYPE_ID_RPM: _details_package,
TYPE_ID_SRPM: _details_package,
TYPE_ID_DRPM: _details_drpm,
TYPE_ID_ERRATA: _details_id_only,
TYPE_ID_DISTRO: _details_id_only,
TYPE_ID_PKG_GROUP: _details_id_only,
TYPE_ID_PKG_CATEGORY: _details_id_only,
TYPE_ID_PKG_ENVIRONMENT: _details_id_only,
TYPE_ID_ERRATA: lambda x: x.get('errata_id'),
TYPE_ID_DISTRO: lambda x: x.get('distribution_id'),
TYPE_ID_PKG_GROUP: lambda x: x.get('package_group_id'),
TYPE_ID_PKG_CATEGORY: lambda x: x.get('package_category_id'),
TYPE_ID_PKG_ENVIRONMENT: lambda x: x.get('package_environment_id'),
TYPE_ID_YUM_REPO_METADATA_FILE: _yum_repo_metadata_name_only,
}
return type_formatters[type_id]
Expand Down Expand Up @@ -64,22 +62,6 @@ def _details_drpm(drpm):
return drpm['filename']


def _details_id_only(unit):
"""
A formatter that prints the id of a unit.
The unit argument is expected to contain the key 'id', and is expected to have a string
as the corresponding value. The formatter returns the value of 'id' as the formatted
string to use. This is a simple package formatter.
:param unit: The unit to have its formatting returned.
:type unit: dict
:return: The display string of the unit
:rtype: str
"""
return unit['id']


def _yum_repo_metadata_name_only(unit):
"""
A formatter for yum repo metadata units.
Expand Down
Expand Up @@ -27,17 +27,12 @@ def test_details_id_only(self):
def test__yum_repo_metadata_name_only(self):
self.assertEqual(units_display._yum_repo_metadata_name_only({'data_type': 'foo'}), 'foo')

@patch('pulp_rpm.extensions.admin.units_display._details_id_only')
@patch('pulp_rpm.extensions.admin.units_display._details_package')
@patch('pulp_rpm.extensions.admin.units_display._details_drpm')
@patch('pulp_rpm.extensions.admin.units_display._yum_repo_metadata_name_only')
def test_get_formatter_for_type(self, mock_metadata, mock_drpm, mock_package, mock_id_only):
def test_get_formatter_for_type(self, mock_metadata, mock_drpm, mock_package):
self.assertTrue(mock_package is units_display.get_formatter_for_type(TYPE_ID_RPM))
self.assertTrue(mock_package is units_display.get_formatter_for_type(TYPE_ID_SRPM))
self.assertTrue(mock_drpm is units_display.get_formatter_for_type(TYPE_ID_DRPM))
self.assertTrue(mock_id_only is units_display.get_formatter_for_type(TYPE_ID_ERRATA))
self.assertTrue(mock_id_only is units_display.get_formatter_for_type(TYPE_ID_DISTRO))
self.assertTrue(mock_id_only is units_display.get_formatter_for_type(TYPE_ID_PKG_GROUP))
self.assertTrue(mock_id_only is units_display.get_formatter_for_type(TYPE_ID_PKG_CATEGORY))
self.assertTrue(
mock_metadata is units_display.get_formatter_for_type(TYPE_ID_YUM_REPO_METADATA_FILE))
19 changes: 19 additions & 0 deletions plugins/pulp_rpm/plugins/db/models.py
Expand Up @@ -43,6 +43,25 @@ def create_legacy_metadata_dict(self):
metadata_dict[name] = getattr(self, name)
return metadata_dict

def clone(self):
"""
Creates a new instance of an equivalent unit, with the unique ID missing. This is useful
for the PackageGroup and similar units, where there is a regular need to create a copy of
a unit, changing just one field in the unit key.
This is a strange use case, so at this point it doesn't seem worth putting this method
into the platform.
deepcopy does not appear to work, presumably because of all the special handling that
mongoengine does in the metaclass. Suggestions are welcome for a better way to clone.
:return: a new unit instance without a unique ID
:rtype: Package
"""
son_data = self.to_mongo()
son_data.pop('_id')
return self.__class__(**son_data)


class NonMetadataPackage(Package):
"""
Expand Down
Expand Up @@ -23,17 +23,17 @@ def __init__(self, working_dir, checksum_type=None):

def add_unit_metadata(self, delta_unit):

new_package_attributes = {'name': delta_unit.metadata['new_package'],
new_package_attributes = {'name': delta_unit.new_package,
'epoch': delta_unit.unit_key['epoch'],
'version': delta_unit.unit_key['version'],
'release': delta_unit.unit_key['release'],
'arch': delta_unit.metadata['arch']}
'arch': delta_unit.arch}

new_package_element = ElementTree.Element('newpackage', new_package_attributes)

delta_attributes = {'oldepoch': delta_unit.metadata['oldepoch'],
'oldversion': delta_unit.metadata['oldversion'],
'oldrelease': delta_unit.metadata['oldrelease']}
delta_attributes = {'oldepoch': delta_unit.oldepoch,
'oldversion': delta_unit.oldversion,
'oldrelease': delta_unit.oldrelease}

delta_element = ElementTree.SubElement(new_package_element, 'delta', delta_attributes)

Expand All @@ -42,10 +42,10 @@ def add_unit_metadata(self, delta_unit):
file_name_element.text = os.path.join('drpms', unit_filename)

sequence_element = ElementTree.SubElement(delta_element, 'sequence')
sequence_element.text = delta_unit.metadata['sequence']
sequence_element.text = delta_unit.sequence

size_element = ElementTree.SubElement(delta_element, 'size')
size_element.text = str(delta_unit.metadata['size'])
size_element.text = str(delta_unit.size)

checksum_attributes = {'type': delta_unit.unit_key['checksumtype']}

Expand Down

0 comments on commit 0722278

Please sign in to comment.