From 3ed36ff02aad9d19766099e04fd52f81b9b0208f Mon Sep 17 00:00:00 2001 From: "Yang,Yu-chu" Date: Thu, 8 Apr 2021 16:27:00 -0700 Subject: [PATCH] config-tools: refine append_node and add get_node Refine the "append_node" which can add new node with an attribute and return the appended node. The method "get_node" finds the xpath value and return it if there is an unique node exists, otherwise it returns None. It is used to get an xpath element node or can determine the xpath existence. The "get_text" is replaced with "get_node". The only get_text in hv_ram.py is modified accordingly. Tracked-On: #5980 Signed-off-by: Yang,Yu-chu Reviewed-by: Junjie Mao --- misc/config_tools/library/common.py | 16 ++++++++++------ misc/config_tools/static_allocators/hv_ram.py | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/misc/config_tools/library/common.py b/misc/config_tools/library/common.py index 4a9143f9b..2f17201ab 100644 --- a/misc/config_tools/library/common.py +++ b/misc/config_tools/library/common.py @@ -191,10 +191,10 @@ def get_xml_attrib(config_file, attrib): def count_nodes(xpath, etree): return int(etree.xpath(f"count({xpath})")) -def get_text(xpath, etree): - result = etree.xpath(f"{xpath}/text()") - assert len(result) == 1, "Internal error: cannot get texts from multiple nodes at a time" - return result[0] +def get_node(xpath, etree): + result = etree.xpath(f"{xpath}") + assert len(result) <= 1, f"Internal error: multiple element nodes are found for {xpath}" + return result[0] if len(result) == 1 else None def update_text(xpath, value, etree, overwrite=False): result = etree.xpath(f"{xpath}") @@ -202,7 +202,7 @@ def update_text(xpath, value, etree, overwrite=False): if overwrite or not result[0].text: result[0].text = str(value) -def append_node(xpath, value, etree): +def append_node(xpath, value, etree, **attribute): # Look for an existing ancestor node parts = xpath.split("/") ancestor_level = 1 @@ -220,7 +220,11 @@ def append_node(xpath, value, etree): child = lxml.etree.Element(tag) ancestor.append(child) ancestor = child - child.text = str(value) + if value: + child.text = str(value) + for key, value in attribute.items(): + child.set(key, value) + return ancestor def get_board_name(): """ diff --git a/misc/config_tools/static_allocators/hv_ram.py b/misc/config_tools/static_allocators/hv_ram.py index d5392ae4d..eb7cee2bb 100644 --- a/misc/config_tools/static_allocators/hv_ram.py +++ b/misc/config_tools/static_allocators/hv_ram.py @@ -24,7 +24,7 @@ def fn(board_etree, scenario_etree, allocation_etree): post_launched_vm_num += 1 hv_ram_size = common.HV_BASE_RAM_SIZE + common.POST_LAUNCHED_VM_RAM_SIZE * post_launched_vm_num - ivshmem_enabled = common.get_text("//IVSHMEM_ENABLED", scenario_etree) + ivshmem_enabled = common.get_node("//IVSHMEM_ENABLED/text()", scenario_etree) total_shm_size = 0 if ivshmem_enabled == 'y': raw_shmem_regions = scenario_etree.xpath("//IVSHMEM_REGION/text()")