Change in vdsm[master]: Dan Kenikgsberg: In several places, and in particular tests/...

mmirecki at redhat.com mmirecki at redhat.com
Thu May 14 10:02:12 UTC 2015


Marcin Mirecki has uploaded a new change for review.

Change subject: Dan Kenikgsberg: In several places, and in particular tests/netconfTests.py, we still use xml.dom.minidom, which is a slow module for parsing and handling xml. As a tiny first task, change this to use xml.dom.ElementTree. 
......................................................................

Dan Kenikgsberg:
In several places, and in particular
tests/netconfTests.py, we still use xml.dom.minidom,
which is a slow module for parsing and handling xml. As a tiny first
task, change this to use xml.dom.ElementTree. 

Change-Id: Ie713533c47f26cdabac5e63ea14a3a9ab9d8d222
Signed-off-by: mmirecki <mmirecki at redhat.com>
---
M tests/netconfTests.py
M vdsm/caps.py
2 files changed, 50 insertions(+), 43 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/09/40909/1

diff --git a/tests/netconfTests.py b/tests/netconfTests.py
index 13d8c18..4817dff 100644
--- a/tests/netconfTests.py
+++ b/tests/netconfTests.py
@@ -26,7 +26,7 @@
 import shutil
 import subprocess
 import tempfile
-from xml.dom.minidom import parseString
+import xml.etree.ElementTree as ET
 
 from vdsm import netinfo
 from network.configurators import ifcfg, libvirt
@@ -67,10 +67,15 @@
         Compare two xml strings for equality.
         """
 
-        aXml = parseString(a).toprettyxml()
-        bXml = parseString(b).toprettyxml()
+        aXml = ET.tostring(ET.fromstring(a))
+        bXml = ET.tostring(ET.fromstring(b))
+
         aXmlNrml = re.sub('\n\s*', ' ', aXml).strip()
         bXmlNrml = re.sub('\n\s*', ' ', bXml).strip()
+
+        aXmlNrml = re.sub('>\s*', '>', aXml)
+        bXmlNrml = re.sub('>\s*', '>', bXml)
+
         self.assertEqual(aXmlNrml, bXmlNrml, msg)
 
     def setUp(self):
diff --git a/vdsm/caps.py b/vdsm/caps.py
index 7f8027f..4cc246f 100644
--- a/vdsm/caps.py
+++ b/vdsm/caps.py
@@ -24,7 +24,6 @@
 import os
 import platform
 from collections import defaultdict
-from xml.dom import minidom
 import logging
 import time
 import linecache
@@ -44,6 +43,8 @@
 import storage.hba
 from network.configurators import qos
 from network import tc
+import xml.etree.ElementTree as ET
+
 
 # For debian systems we can use python-apt if available
 try:
@@ -207,19 +208,20 @@
     if capabilities is None:
         capabilities = _getFreshCapsXMLStr()
 
-    caps = minidom.parseString(capabilities)
-    host = caps.getElementsByTagName('host')[0]
-    cells = host.getElementsByTagName('cells')[0]
+    caps = ET.fromstring(capabilities)
+    host = caps.iter(tag='host').next()
+    cells = host.iter(tag='cells').next()
 
     sockets = set()
     siblings = set()
     onlineCpus = []
 
-    for cpu in cells.getElementsByTagName('cpu'):
-        if cpu.hasAttribute('socket_id') and cpu.hasAttribute('siblings'):
-            onlineCpus.append(cpu.getAttribute('id'))
-            sockets.add(cpu.getAttribute('socket_id'))
-            siblings.add(cpu.getAttribute('siblings'))
+    for cpu in cells.iter(tag='cpu'):
+        if cpu.get('socket_id') is not None and \
+           cpu.get('siblings') is not None:
+            onlineCpus.append(cpu.get('id'))
+            sockets.add(cpu.get('socket_id'))
+            siblings.add(cpu.get('siblings'))
 
     topology = {'sockets': len(sockets),
                 'cores': len(siblings),
@@ -241,13 +243,13 @@
     None if libvirt does not report the live
     snapshot support (as in version <= 1.2.2),
     '''
-    features = guest.getElementsByTagName('features')
+    features = guest.iter(tag='features')
     if not features:
         return None
 
-    for feature in features[0].childNodes:
-        if feature.nodeName == 'disksnapshot':
-            value = feature.getAttribute('default')
+    for feature in list(features.next()):
+        if feature.tag == 'disksnapshot':
+            value = feature.get('default')
             if value.lower() == 'on':
                 return True
             else:
@@ -260,11 +262,11 @@
 def _getLiveSnapshotSupport(arch, capabilities=None):
     if capabilities is None:
         capabilities = _getCapsXMLStr()
-    caps = minidom.parseString(capabilities)
+    caps = ET.fromstring(capabilities)
 
-    for guestTag in caps.getElementsByTagName('guest'):
-        archTag = guestTag.getElementsByTagName('arch')[0]
-        if archTag.getAttribute('name') == arch:
+    for guestTag in caps.iter(tag='guest'):
+        archTag = guestTag.iter(tag='arch').next()
+        if archTag.get('name') == arch:
             return _findLiveSnapshotSupport(guestTag)
 
     if not config.getboolean('vars', 'fake_kvm_support'):
@@ -300,19 +302,19 @@
 @utils.memoized
 def getNumaTopology():
     capabilities = _getCapsXMLStr()
-    caps = minidom.parseString(capabilities)
-    host = caps.getElementsByTagName('host')[0]
-    cells = host.getElementsByTagName('cells')[0]
+    caps = ET.fromstring(capabilities)
+    host = caps.iter(tag='host').next()
+    cells = host.iter(tag='cells').next()
     cellsInfo = {}
-    cellSets = cells.getElementsByTagName('cell')
+    cellSets = cells.findall('cell')
     for cell in cellSets:
         cellInfo = {}
         cpus = []
-        for cpu in cell.getElementsByTagName('cpu'):
-            cpus.append(int(cpu.getAttribute('id')))
+        for cpu in cell.iter(tag='cpu'):
+            cpus.append(int(cpu.get('id')))
         cellInfo['cpus'] = cpus
-        cellIndex = cell.getAttribute('id')
-        if cellSets.length < 2:
+        cellIndex = cell.get('id')
+        if len(cellSets) < 2:
             memInfo = getUMAHostMemoryStats()
         else:
             memInfo = getMemoryStatsByNumaCell(int(cellIndex))
@@ -382,18 +384,18 @@
 def _getEmulatedMachines(arch, capabilities=None):
     if capabilities is None:
         capabilities = _getCapsXMLStr()
-    caps = minidom.parseString(capabilities)
+    caps = ET.fromstring(capabilities)
 
-    for archTag in caps.getElementsByTagName('arch'):
-        if archTag.getAttribute('name') == arch:
-            return [m.firstChild.data for m in archTag.childNodes
-                    if m.nodeName == 'machine']
+    for archTag in caps.iter(tag='arch'):
+        if archTag.get('name') == arch:
+            return [m.text for m in archTag.iterfind('machine')]
+
     return []
 
 
 def _getAllCpuModels():
     with open('/usr/share/libvirt/cpu_map.xml') as xml:
-        cpu_map = minidom.parseString(xml.read())
+        cpu_map = ET.fromstring(xml.read())
 
     arch = platform.machine()
 
@@ -406,11 +408,11 @@
 
     architectureElement = None
 
-    architectureElements = cpu_map.getElementsByTagName('arch')
+    architectureElements = cpu_map.iter(tag='arch')
 
     if architectureElements:
         for a in architectureElements:
-            if a.getAttribute('name') == arch:
+            if a.get('name') == arch:
                 architectureElement = a
 
     if architectureElement is None:
@@ -420,22 +422,22 @@
 
     allModels = dict()
 
-    for m in architectureElement.childNodes:
-        if m.nodeName != 'model':
+    for m in list(architectureElement):
+        if m.tag != 'model':
             continue
-        element = m.getElementsByTagName('vendor')
+        element = m.iter(tag='vendor')
         if element:
-            vendor = element[0].getAttribute('name')
+            vendor = element.next().get('name')
         else:
             # If current model doesn't have a vendor, check if it has a model
             # that it is based on. The models in the cpu_map.xml file are
             # sorted in a way that the base model is always defined before.
-            element = m.getElementsByTagName('model')
+            element = m.iter(tag='model')
             if element:
-                vendor = allModels.get(element[0].getAttribute('name'), None)
+                vendor = allModels.get(element.next().get('name'), None)
             else:
                 vendor = None
-        allModels[m.getAttribute('name')] = vendor
+        allModels[m.get('name')] = vendor
 
     return allModels
 


-- 
To view, visit https://gerrit.ovirt.org/40909
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie713533c47f26cdabac5e63ea14a3a9ab9d8d222
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Marcin Mirecki <mmirecki at redhat.com>


More information about the vdsm-patches mailing list