示例#1
0
 def __init__(self, description=None, derived_from=None, xml_content=None):
     if description and xml_content:
         raise KiwiDescriptionConflict(
             'description and xml_content are mutually exclusive')
     self.markup = Markup(description or xml_content)
     self.description = self.markup.get_xml_description()
     self.derived_from = derived_from
     self.description_origin = description
     self.extension_data = {}
示例#2
0
 def __init__(
     self, description: str = '', derived_from: str = None
 ):
     self.markup = Markup.new(description)
     self.description = self.markup.get_xml_description()
     self.derived_from = derived_from
     self.description_origin = description
     self.extension_data: Dict = {}
示例#3
0
 def test_MarkupAny(self, mock_MarkupAny):
     Markup.new('description')
     mock_MarkupAny.assert_called_once_with('description')
示例#4
0
 def test_MarkupXML(self, mock_MarkupAny, mock_MarkupXML):
     mock_MarkupAny.side_effect = KiwiAnyMarkupPluginError('load-error')
     Markup.new('description')
     mock_MarkupXML.assert_called_once_with('description')
示例#5
0
class XMLDescription:
    """
    **Implements data management for the image description**

    Supported description markup languages are XML, YAML, JSON and INI.
    The provided input file is converted into XML, transformed to the
    current RelaxNG schema via XSLT and validated against this result.

    * XSLT Style Sheet processing to apply on this version of kiwi
    * Schema Validation based on RelaxNG schema
    * Loading XML data into internal data structures

    Attributes

    :param string description: path to description file
    :param string derived_from: path to base description file
    :param string xml_content: XML description data as content string
    """
    def __init__(self, description=None, derived_from=None, xml_content=None):
        if description and xml_content:
            raise KiwiDescriptionConflict(
                'description and xml_content are mutually exclusive')
        self.markup = Markup(description or xml_content)
        self.description = self.markup.get_xml_description()
        self.derived_from = derived_from
        self.description_origin = description
        self.extension_data = {}

    def load(self):  # noqa C901
        """
        Read XML description, validate it against the schema
        and the schematron rules and pass it to the
        autogenerated(generateDS) parser.

        :return: instance of XML toplevel domain (image)

        :rtype: object
        """
        try:
            schema_doc = etree.parse(Defaults.get_schema_file())
            relaxng = etree.RelaxNG(schema_doc)
            schematron = isoschematron.Schematron(schema_doc,
                                                  store_report=True)
        except Exception as issue:
            raise KiwiSchemaImportError(issue)
        try:
            description = etree.parse(self.description)
            validation_rng = relaxng.validate(description)
            validation_schematron = schematron.validate(description)
        except Exception as issue:
            raise KiwiValidationError(issue)
        if not validation_rng:
            self._get_relaxng_validation_details(Defaults.get_schema_file(),
                                                 self.description)
        if not validation_schematron:
            self._get_schematron_validation_details(
                schematron.validation_report)
        if not validation_rng or not validation_schematron:
            raise KiwiDescriptionInvalid(
                'Failed to validate schema and/or schematron rules')

        parse_result = self._parse()

        if parse_result.get_extension():
            extension_namespace_map = \
                description.getroot().xpath('extension')[0].nsmap

            for namespace_name in extension_namespace_map:
                extensions_for_namespace = description.getroot().xpath(
                    'extension/{namespace}:*'.format(namespace=namespace_name),
                    namespaces=extension_namespace_map)
                if extensions_for_namespace:
                    # one toplevel entry point per extension via xmlns
                    if len(extensions_for_namespace) > 1:
                        raise KiwiExtensionError(
                            'Multiple toplevel sections for "{0}" found'.
                            format(namespace_name))

                    # store extension xml data parse tree for this namespace
                    self.extension_data[namespace_name] = \
                        etree.ElementTree(extensions_for_namespace[0])

                    # validate extension xml data
                    try:
                        xml_catalog = Command.run([
                            'xmlcatalog', '/etc/xml/catalog',
                            extension_namespace_map[namespace_name]
                        ])
                        extension_schema = xml_catalog.output.rstrip().replace(
                            'file://', '')
                        extension_relaxng = etree.RelaxNG(
                            etree.parse(extension_schema))
                    except Exception as issue:
                        raise KiwiExtensionError(
                            'Extension schema error: {0}'.format(issue))
                    validation_result = extension_relaxng.validate(
                        self.extension_data[namespace_name])
                    if not validation_result:
                        xml_data_unformatted = etree.tostring(
                            self.extension_data[namespace_name],
                            encoding='utf-8')
                        xml_data_domtree = minidom.parseString(
                            xml_data_unformatted)
                        extension_file = NamedTemporaryFile()
                        with open(extension_file.name, 'w') as xml_data:
                            xml_data.write(xml_data_domtree.toprettyxml())
                        self._get_relaxng_validation_details(
                            extension_schema, extension_file.name)
                        raise KiwiExtensionError(
                            'Schema validation for extension XML data failed')

        return parse_result

    def get_extension_xml_data(self, namespace_name):
        """
        Return the xml etree parse result for the specified extension namespace

        :param string namespace_name: name of the extension namespace

        :return: result of etree.parse

        :rtype: object
        """
        if namespace_name in self.extension_data:
            return self.extension_data[namespace_name]

    def _get_relaxng_validation_details(self, schema_file, description_file):
        """
        Run jing program to validate description against the schema

        Jing provides detailed error information in case of a schema
        validation failure
        """
        try:
            cmd = Command.run(['jing', schema_file, description_file],
                              raise_on_error=False)
        except KiwiCommandNotFound as issue:
            log.info(
                'For detailed schema validation report, please install: jing')
            log.info('{0}: {1}: {2}'.format('jing',
                                            type(issue).__name__, issue))
            return
        log.info('RelaxNG validation failed. See jing report:')
        log.info('--> %s', cmd.output)

    def _get_schematron_validation_details(self, validation_report):
        """
        Extract error message form the schematron validation report

        :param etree validation_report: the schematron validation report
        """
        nspaces = validation_report.getroot().nsmap
        log.info('Schematron validation failed:')
        for msg in validation_report.xpath('//svrl:failed-assert/svrl:text',
                                           namespaces=nspaces):
            log.info('--> %s', msg.text)

    def _parse(self):
        try:
            parse = xml_parse.parse(self.description, True)
            parse.description_dir = self.description_origin and os.path.dirname(
                self.description_origin)
            parse.derived_description_dir = self.derived_from
            return parse
        except Exception as issue:
            raise KiwiDataStructureError(issue)