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
# SPDX-License-Identifier: BSD-3-Clause
#
-import lxml
+import lxml, re
+from pathlib import Path
def add_child(element, tag, text=None, **kwargs):
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, " \
"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
+
+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)
