1#!/usr/bin/env python3
2#
3# Copyright (C) 2022 Intel Corporation.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import sys, os
9import argparse
10from defusedxml.lxml import parse
11import logging
12import xmlschema
13
14logging_fn = {
15    "error": logging.error,
16    "warning": logging.warning,
17    "info": logging.info,
18}
19
20def validate_board(xsd_path, board_etree):
21    schema_etree = parse(xsd_path)
22    schema_etree.xinclude()
23    schema = xmlschema.XMLSchema11(schema_etree)
24
25    it = schema.iter_errors(board_etree)
26    count = 0
27    for error in it:
28        anno = error.validator.annotation
29        severity = anno.elem.get("{https://projectacrn.org}severity")
30        description = anno.elem.find("{http://www.w3.org/2001/XMLSchema}documentation").text
31        logging_fn[severity](description)
32        if severity in ["error", "warning"]:
33            count += 1
34
35    return count
36