[PATCH v2 1/2] config-tools: add new extractors helpers


Yang, Yu-chu
 

From: "Yang,Yu-chu" <yu-chu.yang@...>

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

v1->v2
1. refine the exception

Tracked-On: #7970
Signed-off-by: Yang,Yu-chu <yu-chu.yang@...>
Reviewed-by: Junjie Mao <junjie.mao@...>
---
.../board_inspector/extractors/helpers.py | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/misc/config_tools/board_inspector/extractors/helpers.py b/misc/config_tools/board_inspector/extractors/helpers.py
index de22b9b41..8e8db38dc 100644
--- a/misc/config_tools/board_inspector/extractors/helpers.py
+++ b/misc/config_tools/board_inspector/extractors/helpers.py
@@ -3,7 +3,8 @@
# 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)
--
2.25.1


Junjie Mao
 

-----Original Message-----
From: acrn-dev@... <acrn-dev@...> On Behalf Of Yang,
Yu-chu
Sent: Thursday, October 13, 2022 5:18 AM
To: acrn-dev@...
Cc: Mao, Junjie <junjie.mao@...>
Subject: [acrn-dev] [PATCH v2 1/2] config-tools: add new extractors helpers

From: "Yang,Yu-chu" <yu-chu.yang@...>

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

v1->v2
1. refine the exception

Tracked-On: #7970
Signed-off-by: Yang,Yu-chu <yu-chu.yang@...>
Reviewed-by: Junjie Mao <junjie.mao@...>
---
.../board_inspector/extractors/helpers.py | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/misc/config_tools/board_inspector/extractors/helpers.py
b/misc/config_tools/board_inspector/extractors/helpers.py
index de22b9b41..8e8db38dc 100644
--- a/misc/config_tools/board_inspector/extractors/helpers.py
+++ b/misc/config_tools/board_inspector/extractors/helpers.py
@@ -3,7 +3,8 @@
# 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"
Reviewed-by: Junjie Mao <junjie.mao@...>

---
Best Regards
Junjie Mao

+ 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)
--
2.25.1