Пример #1
0
def process_package_element(element):
    """
    Process one XML block from prestodelta.xml and return a models.DRPM instance

    :param element: object representing one "DRPM" block from the XML file
    :type  element: xml.etree.ElementTree.Element

    :return:    models.DRPM instance for the XML block
    :rtype:     pulp_rpm.plugins.db.models.DRPM
    """
    delta = element.find('delta')
    filename = delta.find('filename')
    sequence = delta.find('sequence')
    size = delta.find('size')
    checksum = delta.find('checksum')
    checksum_type = util.sanitize_checksum_type(checksum.attrib['type'])

    return models.DRPM(new_package=element.attrib['name'],
                       epoch=element.attrib['epoch'],
                       version=element.attrib['version'],
                       release=element.attrib['release'],
                       arch=element.attrib['arch'],
                       oldepoch=delta.attrib['oldepoch'],
                       oldversion=delta.attrib['oldversion'],
                       oldrelease=delta.attrib['oldrelease'],
                       filename=filename.text,
                       sequence=sequence.text,
                       size=int(size.text),
                       checksum=checksum.text,
                       checksumtype=checksum_type)
Пример #2
0
    def test___init___sanitizes_checksum_type(self):
        """
        Ensure that __init__() calls sanitize_checksum_type correctly.
        """
        # The sha should get changed to sha1
        drpm = models.DRPM('epoch', 'version', 'release', 'filename', 'sha', 'checksum', {})

        self.assertEqual(drpm.unit_key['checksumtype'], 'sha1')
Пример #3
0
def drpm_models(num, same_filename_and_arch=False):
    ret = []
    count = _drpm_counter.next()
    filename = 'filename-%d' % count
    for i in range(num):
        if not same_filename_and_arch:
            filename = 'filename-%d' % count
        ret.append(
            models.DRPM('0', '2.1.%d' % count, '1-1', filename, 'sha256',
                        'somehash-%d' % count, {}))
        count = _drpm_counter.next()
    return ret
Пример #4
0
def drpm_models(num, same_filename_and_arch=False):
    ret = []
    count = _drpm_counter.next()
    filename = 'filename-%d' % count
    for i in range(num):
        if not same_filename_and_arch:
            filename = 'filename-%d' % count
        ret.append(
            models.DRPM(epoch='0',
                        version='2.1.%d' % count,
                        release='1-1',
                        filename=filename,
                        checksumtype='sha256',
                        checksum='somehash-%d' % count))
        count = _drpm_counter.next()
    return ret
Пример #5
0
    def test___init___sanitizes_checksum_type(self):
        """
        Ensure that __init__() calls sanitize_checksum_type correctly.
        """
        unit_metadata = {
            'epoch': 'epoch',
            'version': 'version',
            'release': 'release',
            'filename': 'filename',
            'checksumtype': 'sha',
            'checksum': 'checksum'
        }

        # The sha should get changed to sha1
        drpm = models.DRPM(**unit_metadata)

        self.assertEqual(drpm.checksumtype, 'sha1')
Пример #6
0
def _handle_package(repo, type_id, unit_key, metadata, file_path, conduit,
                    config):
    """
    Handles the upload for an RPM, SRPM or DRPM.

    This inspects the package contents to determine field values. The unit_key
    and metadata fields overwrite field values determined through package inspection.

    :param repo: The repository to import the package into
    :type  repo: pulp.server.db.model.Repository

    :param type_id: The type_id of the package being uploaded
    :type  type_id: str

    :param unit_key: A dictionary of fields to overwrite introspected field values
    :type  unit_key: dict

    :param metadata: A dictionary of fields to overwrite introspected field values, or None
    :type  metadata: dict or None

    :param file_path: The path to the uploaded package
    :type  file_path: str

    :param conduit: provides access to relevant Pulp functionality
    :type  conduit: pulp.plugins.conduits.upload.UploadConduit

    :param config: plugin configuration for the repository
    :type  config: pulp.plugins.config.PluginCallConfiguration

    :raises PulpCodedException PLP1005: if the checksum type from the user is not recognized
    :raises PulpCodedException PLP1013: if the checksum value from the user does not validate
    """
    try:
        if type_id == models.DRPM._content_type_id.default:
            unit = models.DRPM(**_extract_drpm_data(file_path))
        else:
            repodata = rpm_parse.get_package_xml(file_path,
                                                 sumtype=util.TYPE_SHA256)
            package_xml = (utils.fake_xml_element(
                repodata['primary'],
                constants.COMMON_NAMESPACE).find(primary.PACKAGE_TAG))
            unit = primary.process_package_element(package_xml)
    except Exception:
        raise PulpCodedException(error_codes.RPM1016)

    # metadata can be None
    metadata = metadata or {}

    model_class = plugin_api.get_unit_model_by_id(type_id)
    update_fields_inbound(model_class, unit_key or {})
    update_fields_inbound(model_class, metadata or {})

    with open(file_path) as fp:
        sums = util.calculate_checksums(fp,
                                        models.RpmBase.DEFAULT_CHECKSUM_TYPES)

    # validate checksum if possible
    if metadata.get('checksum'):
        checksumtype = metadata.pop('checksum_type', util.TYPE_SHA256)
        checksumtype = util.sanitize_checksum_type(checksumtype)
        if checksumtype not in sums:
            raise PulpCodedException(error_code=error_codes.RPM1009,
                                     checksumtype=checksumtype)
        if metadata['checksum'] != sums[checksumtype]:
            raise PulpCodedException(error_code=platform_errors.PLP1013)
        _LOGGER.debug(_('Upload checksum matches.'))

    # Save all uploaded RPMs with sha256 in the unit key, since we can now publish with other
    # types, regardless of what is in the unit key.
    unit.checksumtype = util.TYPE_SHA256
    unit.checksum = sums[util.TYPE_SHA256]
    # keep all available checksum values on the model
    unit.checksums = sums

    # Update the RPM-extracted data with anything additional the user specified.
    # Allow the user-specified values to override the extracted ones.
    for key, value in metadata.items():
        setattr(unit, key, value)
    for key, value in unit_key.items():
        setattr(unit, key, value)

    if type_id != models.DRPM._content_type_id.default:
        # Extract/adjust the repodata snippets
        unit.signing_key = rpm_parse.package_signature(
            rpm_parse.package_headers(file_path))
        # construct filename from metadata (BZ #1101168)
        if type_id == models.SRPM._content_type_id.default:
            rpm_basefilename = "%s-%s-%s.src.rpm" % (unit.name, unit.version,
                                                     unit.release)
        else:
            rpm_basefilename = "%s-%s-%s.%s.rpm" % (unit.name, unit.version,
                                                    unit.release, unit.arch)
        unit.relativepath = rpm_basefilename
        unit.filename = rpm_basefilename
        _update_files(unit, repodata)
        unit.modify_xml(repodata)

    # check if the unit has duplicate nevra
    purge.remove_unit_duplicate_nevra(unit, repo)

    unit.set_storage_path(os.path.basename(file_path))
    try:
        unit.save_and_import_content(file_path)
    except TypeError:
        raise ModelInstantiationError()
    except NotUniqueError:
        unit = unit.__class__.objects.filter(**unit.unit_key).first()

    if rpm_parse.signature_enabled(config):
        rpm_parse.filter_signature(unit, config)
    repo_controller.associate_single_unit(repo, unit)