1#!/usr/bin/env python3
2#
3# Copyright (C) 2022 Intel Corporation.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8__package__ = 'configurator.pyodide'
9
10from pathlib import Path
11from tempfile import TemporaryDirectory
12from defusedxml.ElementTree import tostring
13
14from scenario_config.pipeline import PipelineObject, PipelineEngine
15from scenario_config.xml_loader import XMLLoadStage
16from scenario_config.default_populator import DefaultValuePopulatingStage
17
18from .pyodide import write_temp_file, nuc11_scenario, scenario_xml_schema_path, convert_result
19from .loadScenario import load_scenario_xml
20
21
22def main(scenario):
23    pipeline = PipelineEngine(["schema_path", "scenario_path"])
24    pipeline.add_stages([
25        XMLLoadStage("schema"),
26        XMLLoadStage("scenario"),
27        DefaultValuePopulatingStage(),
28    ])
29    with TemporaryDirectory() as tmpdir:
30        write_temp_file(tmpdir, {
31            'scenario.xml': scenario
32        })
33        scenario_file_path = Path(tmpdir) / 'scenario.xml'
34
35        obj = PipelineObject(
36            scenario_path=scenario_file_path,
37            schema_path=scenario_xml_schema_path,
38        )
39        pipeline.run(obj)
40
41        # Clean up the VM_NAME and/or vm_name which does not exist
42        etree = obj.get("scenario_etree").getroot()
43        vmNames = [name.text for name in etree.findall(".//vm/name")]
44        for name in etree.findall(".//IVSHMEM_VM/VM_NAME") + \
45            etree.findall(".//vuart_connection/endpoint/vm_name"):
46            if name.text not in vmNames:
47                name.text = ""
48
49        own_pcpus = etree.findall(".//vm[vm_type = 'RTVM']/own_pcpu")
50        for o in own_pcpus:
51            o.text = 'y'
52
53        result = tostring(obj.get("scenario_etree").getroot())
54        result = result.decode()
55    result = convert_result({
56        'xml': result,
57        'json': load_scenario_xml(result)
58    })
59    return result
60
61
62def test():
63    main(nuc11_scenario)
64
65
66if __name__ == '__main__':
67    test()
68