[PATCH v1 1/2] config-tools: add new extrators helplers


Junjie Mao
 

-----Original Message-----
From: Yang, Yu-chu <yu-chu.yang@...>
Sent: Wednesday, October 5, 2022 2:26 AM
To: acrn-dev@...
Cc: Mao, Junjie <junjie.mao@...>
Subject: [PATCH v1 1/2] config-tools: add new extrators helplers

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

Tracked-On: #7970
Signed-off-by: Yang,Yu-chu <yu-chu.yang@...>
Reviewed-by: Junjie Mao <junjie.mao@...>

---
Best Regards
Junjie Mao

---
.../board_inspector/extractors/helpers.py | 18 +++++++++++++++++-
1 file changed, 17 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..c64130db2 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,18 @@ 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):
+ if not isinstance(pathstr, str):
+ raise TypeError(f"pathstr must be a str: {type(pathstr)}")
+ path = Path(pathstr)
+ if not path.exists():
+ raise FileNotFoundError(f"{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


Yang, Yu-chu
 

Typos in title, it shall be "config-tools: add new extractors helpers"
Will fix it in the commit.

Thanks,
Yu-chu

-----Original Message-----
From: acrn-dev@... <acrn-dev@...> On Behalf Of Yang, Yu-chu
Sent: Tuesday, October 4, 2022 11:26 AM
To: acrn-dev@...
Cc: Mao, Junjie <junjie.mao@...>
Subject: [acrn-dev] [PATCH v1 1/2] config-tools: add new extrators helplers

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

Tracked-On: #7970
Signed-off-by: Yang,Yu-chu <yu-chu.yang@...>
---
.../board_inspector/extractors/helpers.py | 18 +++++++++++++++++-
1 file changed, 17 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..c64130db2 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,18 @@ 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):
+ if not isinstance(pathstr, str):
+ raise TypeError(f"pathstr must be a str: {type(pathstr)}")
+ path = Path(pathstr)
+ if not path.exists():
+ raise FileNotFoundError(f"{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


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

Tracked-On: #7970
Signed-off-by: Yang,Yu-chu <yu-chu.yang@...>
---
.../board_inspector/extractors/helpers.py | 18 +++++++++++++++++-
1 file changed, 17 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..c64130db2 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,18 @@ 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):
+ if not isinstance(pathstr, str):
+ raise TypeError(f"pathstr must be a str: {type(pathstr)}")
+ path = Path(pathstr)
+ if not path.exists():
+ raise FileNotFoundError(f"{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