diff mbox series

[v1,6/8] dts: add config parser module

Message ID 20220622121448.3304251-7-juraj.linkes@pantheon.tech (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers show
Series dts: ssh connection to a node | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Juraj Linkeš June 22, 2022, 12:14 p.m. UTC
The module uses Python's configparser module, which supports an ini-like
format with sections.

The configuration is split into two parts, one defining the parameters
of the test run and the other defining the topology to be used.

Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/conf/topology.cfg      |  9 +++++
 dts/execution.cfg          |  2 +
 dts/framework/config.py    | 81 ++++++++++++++++++++++++++++++++++++++
 dts/framework/exception.py | 13 ++++++
 dts/framework/settings.py  | 34 ++++++++++++++++
 5 files changed, 139 insertions(+)
 create mode 100644 dts/conf/topology.cfg
 create mode 100644 dts/execution.cfg
 create mode 100644 dts/framework/config.py
diff mbox series

Patch

diff --git a/dts/conf/topology.cfg b/dts/conf/topology.cfg
new file mode 100644
index 0000000000..f5406cc6b9
--- /dev/null
+++ b/dts/conf/topology.cfg
@@ -0,0 +1,9 @@ 
+#Topology Configuration
+#[SUT IP]
+#  sut_ip: SUT ip address
+#  sut_user: SUT username
+#  sut_passwd: SUT password
+[SUT IP1]
+sut_ip=xxx.xxx.xxx.xxx
+sut_user=root
+sut_passwd=
diff --git a/dts/execution.cfg b/dts/execution.cfg
new file mode 100644
index 0000000000..ef671aa394
--- /dev/null
+++ b/dts/execution.cfg
@@ -0,0 +1,2 @@ 
+[Execution1]
+sut=<SUT IP Address>
diff --git a/dts/framework/config.py b/dts/framework/config.py
new file mode 100644
index 0000000000..765132a2a0
--- /dev/null
+++ b/dts/framework/config.py
@@ -0,0 +1,81 @@ 
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2021 Intel Corporation
+#
+
+"""
+Generic port and topology nodes configuration file load function
+"""
+import configparser  # config parse module
+
+from .exception import ConfigParseException
+from .settings import CONFIG_ROOT_PATH
+
+TOPOCONF = "%s/topology.cfg" % CONFIG_ROOT_PATH
+
+
+class UserConf:
+    def __init__(self, config):
+        self.conf = configparser.SafeConfigParser()
+        load_files = self.conf.read(config)
+        if load_files == []:
+            self.conf = None
+            raise ConfigParseException(config)
+
+    def get_sections(self):
+        if self.conf is None:
+            return []
+
+        return self.conf.sections()
+
+    def load_section(self, section):
+        if self.conf is None:
+            return None
+
+        items = None
+        for conf_sect in self.conf.sections():
+            if conf_sect == section:
+                items = self.conf.items(section)
+
+        return items
+
+
+class TopologyConf(UserConf):
+    TOPO_DEFAULTS = {
+        "IP": "",
+        "user": "",
+        "pass": "",
+    }
+
+    def __init__(self, topo_conf=TOPOCONF):
+        self.config_file = topo_conf
+        self.nodes = []
+        try:
+            self.topo_conf = UserConf(self.config_file)
+        except ConfigParseException:
+            self.topo_conf = None
+            raise ConfigParseException
+
+    def load_topo_config(self):
+        sections = self.topo_conf.get_sections()
+        if not sections:
+            return self.nodes
+
+        for node_name in sections:
+            node = self.TOPO_DEFAULTS.copy()
+            node["section"] = node_name
+            node_conf = self.topo_conf.load_section(node_name)
+            if not node_conf:
+                continue
+
+            # convert file configuration to dts node configuration
+            for key, value in node_conf:
+                if key == "sut_ip":
+                    node["IP"] = value
+                elif key == "sut_user":
+                    node["user"] = value
+                elif key == "sut_passwd":
+                    node["pass"] = value
+
+            self.nodes.append(node)
+        return self.nodes
+
diff --git a/dts/framework/exception.py b/dts/framework/exception.py
index a109dd1fb8..a094fcce78 100644
--- a/dts/framework/exception.py
+++ b/dts/framework/exception.py
@@ -46,3 +46,16 @@  def __init__(self, host):
 
     def __str__(self):
         return "SSH session with %s has been dead" % self.host
+
+
+class ConfigParseException(Exception):
+
+    """
+    Configuration file parse failure exception.
+    """
+
+    def __init__(self, conf_file):
+        self.config = conf_file
+
+    def __str__(self):
+        return "Failed to parse config file [%s]" % (self.config)
diff --git a/dts/framework/settings.py b/dts/framework/settings.py
index d62083969e..c033a77fec 100644
--- a/dts/framework/settings.py
+++ b/dts/framework/settings.py
@@ -2,7 +2,41 @@ 
 # Copyright(c) 2010-2021 Intel Corporation
 #
 
+import os
+import re
+
 """
 Default session timeout.
 """
 TIMEOUT = 15
+
+"""
+DTS global environment variables
+"""
+DTS_ENV_PAT = r"DTS_*"
+DTS_CFG_FOLDER = "DTS_CFG_FOLDER"
+
+
+def load_global_setting(key):
+    """
+    Load DTS global setting
+    """
+    if re.match(DTS_ENV_PAT, key):
+        env_key = key
+    else:
+        env_key = "DTS_" + key
+
+    if env_key in list(os.environ.keys()):
+        return os.environ[env_key]
+    else:
+        return ""
+
+
+"""
+The root path of framework configs.
+"""
+dts_cfg_folder = load_global_setting(DTS_CFG_FOLDER)
+if dts_cfg_folder != "":
+    CONFIG_ROOT_PATH = dts_cfg_folder
+else:
+    CONFIG_ROOT_PATH = "./conf"