config-tools: add new extractors helpers

Add 2 helplers:
1. get_realpath: this method returns the realpath of paramenter
if it's a valid path string
2. get_bdf_from_realpath: this method returns the bus, device, function
number if the parameter is a path to /sys/devices

Tracked-On: #7970
Signed-off-by: Yang,Yu-chu <yu-chu.yang@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Yang,Yu-chu 2022-09-29 16:17:31 -07:00 committed by acrnsi-robot
parent 34e5061418
commit b235e88f24
1 changed files with 15 additions and 1 deletions

View File

@ -3,7 +3,8 @@
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
# #
import lxml import lxml, re
from pathlib import Path
def add_child(element, tag, text=None, **kwargs): def add_child(element, tag, text=None, **kwargs):
child = lxml.etree.Element(tag) child = lxml.etree.Element(tag)
@ -20,3 +21,16 @@ def get_node(etree, xpath):
"Rerun the Board Inspector with `--loglevel debug`. If this issue persists, " \ "Rerun the Board Inspector with `--loglevel debug`. If this issue persists, " \
"log a new issue at https://github.com/projectacrn/acrn-hypervisor/issues and attach the full logs." "log a new issue at https://github.com/projectacrn/acrn-hypervisor/issues and attach the full logs."
return result[0] if len(result) == 1 else None return result[0] if len(result) == 1 else None
def get_realpath(pathstr):
assert isinstance(pathstr, str), f"Internal error: pathstr must be a str: {type(pathstr)}"
path = Path(pathstr)
assert path.exists(), f"Internal error: {path} does not exist"
return str(path.resolve())
def get_bdf_from_realpath(pathstr):
realpath = get_realpath(pathstr)
bdf_regex = re.compile(r"^([0-9a-f]{4}):([0-9a-f]{2}):([0-9a-f]{2}).([0-7]{1})$")
m = bdf_regex.match(realpath.split('/')[-1])
assert m, f"Internal error: {realpath} contains no matched pattern: {bdf_regex}"
return int(m.group(2), base=16), int(m.group(3), base=16), int(m.group(4), base=16)