Example #1
0
 def test_load_schema_import_error(self, mock_xslt, mock_relax):
     mock_relax.side_effect = KiwiSchemaImportError('ImportError')
     self.description_from_file.load()
Example #2
0
    def load(self):  # noqa: C901
        """
        Read XML description, pass it along to the XSLT processor,
        validate it against the schema and finally pass it to the
        autogenerated(generateDS) parser.

        :return: instance of XML toplevel domain (image)

        :rtype: object
        """
        try:
            self._xsltproc()
            schema_doc = etree.parse(Defaults.get_schema_file())
            relaxng = etree.RelaxNG(schema_doc)
            schematron = isoschematron.Schematron(schema_doc,
                                                  store_report=True)
        except Exception as e:
            raise KiwiSchemaImportError('%s: %s' %
                                        (type(e).__name__, format(e)))
        try:
            description = etree.parse(self.description_xslt_processed.name)
            validation_rng = relaxng.validate(description)
            validation_sch = schematron.validate(description)
        except Exception as e:
            raise KiwiValidationError('%s: %s' % (type(e).__name__, format(e)))
        if not validation_rng:
            self._get_relaxng_validation_details(
                Defaults.get_schema_file(),
                self.description_xslt_processed.name)
        if not validation_sch:
            self._get_schematron_validation_details(
                schematron.validation_report)
        if not validation_rng or not validation_sch:
            if self.description:
                message = 'Schema validation for {description} failed'.format(
                    description=self.description)
            else:
                message = 'Schema validation for XML content failed'
            raise KiwiDescriptionInvalid(message)

        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 e:
                        raise KiwiExtensionError(
                            'Extension schema error: {0}: {1}'.format(
                                type(e).__name__, e))
                    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