Beispiel #1
0
def _modulemd_read_packager_string(mod_yaml, name=None, stream=None):
    """
    For the time being we happen to be in a transition state when
    `Modulemd.ModuleStream.read_string` is deprecated and throws warnings on
    Fedora but we still use old libmodulemd (2.9.4) on RHEL8, which doesn't
    provide its replacement in the form of `Modulemd.read_packager_string`.
    """
    if StrictVersion(Modulemd.get_version()) < StrictVersion("2.11"):
        mod_stream = Modulemd.ModuleStreamV2.new(name, stream)
        mod_stream = mod_stream.read_string(mod_yaml, True, name, stream)
        return mod_stream

    return Modulemd.read_packager_string(mod_yaml, name, stream)
Beispiel #2
0
def process_string(logger, content, ignore_unsuitable, old_platform,
                   new_platform):
    """Add a configuration for a new platform to the modulemd document string.

    It returns an error code and a string.
    In case of no error, code will be 0 and the string will be the processed,
    output document.
    In case of an error, code will be nonzero and the string will contain an
    error message.
    A special error -1 means the document needs no editing. Either because it
    already contains a configuration the new platform, or because
    ignore_unsuitable argument is True and the string does not contain
    a configuration for the old platform or the string is a modulemd-v2
    document, which does not use configurations.
    """

    # Parse and validate the content
    try:
        document = Modulemd.read_packager_string(content)
    except Exception as e:
        return 1, 'Unable to parse a modulemd-packager document: {}'.format(e)
    # Ignore unsuitable documents if requested
    if type(document) == Modulemd.ModuleStreamV2:
        return -1 if ignore_unsuitable else 1, 'This is a modulemd-v2 document'

    # Enumerate all contexts with the old platform
    contexts = document.get_build_config_contexts_as_strv()
    template_configurations = []
    for context in contexts:
        configuration = document.get_build_config(context)
        if configuration.get_platform() == new_platform:
            # If there is already a context for the new platform,
            # there no work for us.
            return -1, 'A context for the new {} platform already exists: {}' \
                .format(configuration.get_context(), new_platform)
        if configuration.get_platform() == old_platform:
            template_configurations.append(configuration)

    # Duplicate configurations
    new_configurations = []
    old_to_new_context_map = {}
    if len(template_configurations) == 0:
        return -1 if ignore_unsuitable else 2, \
            'No context with the old platform {}.'.format(old_platform)
    elif len(template_configurations) == 1:
        # Try using the new platform as the new context
        if new_platform in contexts:
            new_context = generate_context(contexts)
        elif not validate_context(new_platform):
            new_context = generate_context(contexts)
        else:
            new_context = new_platform
        new_configurations.append(
            duplicate_configuration(template_configurations[0], new_context,
                                    new_platform))
        contexts.append(new_context)
        old_to_new_context_map[template_configurations[0].get_context()] = \
            new_context
    else:
        for template_configuration in template_configurations:
            new_context = generate_context(contexts)
            new_configurations.append(
                duplicate_configuration(template_configuration, new_context,
                                        new_platform))
            contexts.append(new_context)
            old_to_new_context_map[template_configuration.get_context()] = \
                new_context
    for new_configuration in new_configurations:
        document.add_build_config(new_configuration)

    # Edit the document preserving formatting
    edited_content = edit(logger, content, old_platform, new_platform,
                          old_to_new_context_map)

    # Reparse the document to verify it was not damaged
    try:
        edited_document = Modulemd.read_packager_string(edited_content)
    except Exception as e:
        return 3, 'Unable to parse the edited document: {}:\n{}'.format(
            e, edited_content)

    # Compare library-edited and manually edited documents
    if not equaled_modulemd_packager(document, edited_document):
        return 4, \
            'Editing would demage the modulemd-packager document:\n{}'.format(edited_content)

    return 0, edited_content