[dts] [PATCH v2 09/19] Change and add some functions to support virtual test

Jiajia, Sun sunx.jiajia at intel.com
Fri May 22 11:04:02 CEST 2015


From: sjiajiax <sunx.jiajia at intel.com>

Signed-off-by: sjiajiax <sunx.jiajia at intel.com>
---
 framework/config.py | 170 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 135 insertions(+), 35 deletions(-)
 mode change 100755 => 100644 framework/config.py

diff --git a/framework/config.py b/framework/config.py
old mode 100755
new mode 100644
index d2548e8..7e2436a
--- a/framework/config.py
+++ b/framework/config.py
@@ -37,45 +37,133 @@ import re
 import ConfigParser  # config parse module
 import argparse      # prase arguments module
 
-portconf = "conf/ports.cfg"
-crbconf = "conf/crbs.cfg"
+PORTCONF = "conf/ports.cfg"
+CRBCONF = "conf/crbs.cfg"
+VIRTCONF = "conf/virt_global.cfg"
 
 
 class UserConf():
 
-    def __init__(self, port_conf=portconf, crb_conf=crbconf):
-        self.port_config = port_conf
-        self.crb_config = crb_conf
+    def __init__(self, config):
+        self.conf = ConfigParser.SafeConfigParser()
+        load_files = self.conf.read(config)
+        if load_files == []:
+            print "FAILED LOADING %s!!!" % config
+            self.conf = None
+            raise
+
+    def get_sections(self):
+        if self.conf is None:
+            return None
+
+        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
+
+    def load_config(self, item):
+        confs = [conf.strip() for conf in item.split(';')]
+        if '' in confs:
+            confs.remove('')
+        return confs
+
+    def load_param(self, conf):
+        paramDict = dict()
+
+        for param in conf.split(','):
+            (key, _, value) = param.partition('=')
+            paramDict[key] = value
+        return paramDict
+
+
+class VirtConf(UserConf):
+
+    def __init__(self, virt_conf=VIRTCONF):
+        self.config_file = virt_conf
+        self.virt_cfg = {}
+        try:
+            self.virt_conf = UserConf(self.config_file)
+        except Exception as e:
+            print "FAILED LOADING VIRT CONFIG!!!"
+            self.virt_conf = None
+
+    def load_virt_config(self, name):
+        self.virt_cfgs = []
+
+        try:
+            virt_confs = self.virt_conf.load_section(name)
+        except:
+            print "FAILED FIND SECTION %s!!!" % name
+            return
+
+        for virt_conf in virt_confs:
+            virt_cfg = {}
+            virt_params = []
+            key, config = virt_conf
+            confs = self.virt_conf.load_config(config)
+            for config in confs:
+                virt_params.append(self.load_virt_param(config))
+            virt_cfg[key] = virt_params
+            self.virt_cfgs.append(virt_cfg)
+
+    def get_virt_config(self):
+        return self.virt_cfgs
+
+    def load_virt_param(self, config):
+        cfg_params = self.virt_conf.load_param(config)
+        return cfg_params
+
+
+class PortConf(UserConf):
+
+    def __init__(self, port_conf=PORTCONF):
+        self.config_file = port_conf
         self.ports_cfg = {}
         self.pci_regex = "([\da-f]{2}:[\da-f]{2}.\d{1})$"
         try:
-            self.port_conf = ConfigParser.SafeConfigParser()
-            self.port_conf.read(self.port_config)
+            self.port_conf = UserConf(self.config_file)
         except Exception as e:
             print "FAILED LOADING PORT CONFIG!!!"
+            self.port_conf = None
 
     def load_ports_config(self, crbIP):
-        ports = []
-        for crb in self.port_conf.sections():
-            if crb != crbIP:
-                continue
-            ports = [port.strip()
-                     for port in self.port_conf.get(crb, 'ports').split(';')]
+        self.ports_cfg = {}
+        if self.port_conf is None:
+            return
+
+        ports = self.port_conf.load_section(crbIP)
+        if ports is None:
+            return
+        key, config = ports[0]
+        confs = self.port_conf.load_config(config)
+
+        for config in confs:
+            port_param = self.port_conf.load_param(config)
 
-        for port in ports:
-            port_cfg = self.__parse_port_param(port)
             # check pci BDF validity
-            if 'pci' not in port_cfg:
+            if 'pci' not in port_param:
                 print "NOT FOUND CONFIG FOR NO PCI ADDRESS!!!"
                 continue
-            m = re.match(self.pci_regex, port_cfg['pci'])
+            m = re.match(self.pci_regex, port_param['pci'])
             if m is None:
                 print "INVALID CONFIG FOR NO PCI ADDRESS!!!"
                 continue
 
-            keys = port_cfg.keys()
+            keys = port_param.keys()
             keys.remove('pci')
-            self.ports_cfg[port_cfg['pci']] = {key: port_cfg[key] for key in keys}
+            self.ports_cfg[port_param['pci']] = {
+                key: port_param[key] for key in keys}
+            if 'numa' in self.ports_cfg[port_param['pci']]:
+                numa_str = self.ports_cfg[port_param['pci']]['numa']
+                self.ports_cfg[port_param['pci']]['numa'] = int(numa_str)
 
     def get_ports_config(self):
         return self.ports_cfg
@@ -86,23 +174,35 @@ class UserConf():
         else:
             return False
 
-    def __parse_port_param(self, port):
-        portDict = dict()
-
-        for param in port.split(','):
-            (key, _, value) = param.partition('=')
-            if key == 'numa':
-                portDict[key] = int(value)
-            else:
-                portDict[key] = value
-        return portDict
 
 
 if __name__ == '__main__':
-    parser = argparse.ArgumentParser(description="Load DTS configuration files")
-    parser.add_argument("-p", "--portconf", default=portconf)
-    parser.add_argument("-c", "--crbconf", default=crbconf)
+    parser = argparse.ArgumentParser(
+        description="Load DTS configuration files")
+    parser.add_argument("-p", "--portconf", default=PORTCONF)
+    parser.add_argument("-c", "--crbconf", default=CRBCONF)
+    parser.add_argument("-v", "--virtconf", default=VIRTCONF)
     args = parser.parse_args()
-    conf = UserConf()
-    conf.load_ports_config('192.168.1.1')
-    conf.check_port_available('0000:86:00.0')
+
+    # not existed configuration file
+    VirtConf('/tmp/not-existed.cfg')
+
+    # example for basic use configuration file
+    conf = UserConf(PORTCONF)
+    for section in conf.get_sections():
+        items = conf.load_section(section)
+        key, value = items[0]
+        confs = conf.load_config(value)
+        for config in confs:
+            conf.load_param(config)
+
+    # example for port configuration file
+    portconf = PortConf(PORTCONF)
+    portconf.load_ports_config('DUT IP')
+    print portconf.get_ports_config()
+    portconf.check_port_available('86:00.0')
+
+    # example for global virtualization configuration file
+    virtconf = VirtConf(VIRTCONF)
+    virtconf.load_virt_config('LIBVIRT')
+    print virtconf.get_virt_config()
-- 
1.9.3



More information about the dts mailing list