Antoni Segura Puimedon has uploaded a new change for review.

Change subject: py3k: make netinfo work in python 2.6-7,3.3
......................................................................

py3k: make netinfo work in python 2.6-7,3.3

Change-Id: I2ae9450fc072cef9fc908cdd790728a1d1c8b9f7
Signed-off-by: Antoni S. Puimedon <[email protected]>
---
M lib/vdsm/netinfo.py
M lib/vdsm/utils.py
2 files changed, 40 insertions(+), 27 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/49/17649/1

diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py
index 6c18a1c..a3db639 100644
--- a/lib/vdsm/netinfo.py
+++ b/lib/vdsm/netinfo.py
@@ -17,26 +17,27 @@
 #
 # Refer to the README and COPYING files for full details of the license
 #
+from __future__ import print_function
 
 from collections import namedtuple
-import errno
 from fnmatch import fnmatch
 from glob import iglob
 from itertools import chain
+from xml.dom import minidom
+import errno
+import ethtool
 import logging
 import os
 import shlex
 import socket
 import struct
-from xml.dom import minidom
 
-import ethtool
-
-from config import config
-import constants
-from ipwrapper import Route
-from ipwrapper import routeShowAllDefaultGateways
-import libvirtconnection
+from .config import config
+from . import constants
+from . import libvirtconnection
+from .ipwrapper import Route
+from .ipwrapper import routeShowAllDefaultGateways
+from .utils import iteritems
 
 NET_CONF_DIR = '/etc/sysconfig/network-scripts/'
 NET_CONF_BACK_DIR = constants.P_VDSM_LIB + 'netconfback/'
@@ -326,14 +327,14 @@
 
 def graph():
     for bridge in bridges():
-        print bridge
+        print(bridge)
         for iface in ports(bridge):
-            print '\t' + iface
+            print('\t' + iface)
             if iface in vlans():
                 iface = iface.split('.')[0]
             if iface in bondings():
                 for slave in slaves(iface):
-                    print '\t\t' + slave
+                    print('\t\t' + slave)
 
 
 def getVlanBondingNic(bridge):
@@ -358,7 +359,7 @@
     "Convert an integer to the corresponding ip address in the dot-notation"
     ip_address = []
 
-    for i in xrange(4):
+    for i in range(4):
         ip_num, ip_val = divmod(ip_num, 256)
         ip_address.append(str(ip_val))
 
@@ -388,7 +389,7 @@
 
     try:
         with open("/proc/net/ipv6_route") as route_file:
-            for route_line in route_file.xreadlines():
+            for route_line in route_file:
                 route_parm = route_line.rstrip().split(' ')
                 dest = route_parm[0]
                 prefix = route_parm[1]
@@ -516,7 +517,7 @@
     paddr = permAddr()
     d['networks'] = {}
 
-    for net, netAttr in networks().iteritems():
+    for net, netAttr in iteritems(networks()):
         try:
             d['networks'][net] = _getNetInfo(netAttr.get('iface', net),
                                              netAttr['bridged'], routes,
@@ -566,7 +567,8 @@
 
 def getIpAddresses():
     "Return a list of the host's IP addresses"
-    return filter(None, [getaddr(i) for i in ethtool.get_active_devices()])
+    return [addr for addr in
+            (getaddr(i) for i in ethtool.get_active_devices()) if addr]
 
 
 def IPv4toMapped(ip):
@@ -613,7 +615,7 @@
 
     def getBridgedNetworksAndVlansForIface(self, iface):
         """ Returns tuples of (bridge, vlan) connected to nic/bond """
-        for network, netdict in self.networks.iteritems():
+        for network, netdict in iteritems(self.networks):
             if netdict['bridged']:
                 for interface in netdict['ports']:
                     if iface == interface:
@@ -623,7 +625,7 @@
 
     def getBridgelessNetworksAndVlansForIface(self, iface):
         """ Returns tuples of (network, vlan) connected to nic/bond """
-        for network, netdict in self.networks.iteritems():
+        for network, netdict in iteritems(self.networks):
             if not netdict['bridged']:
                 if iface == netdict['iface']:
                     yield (network, None)
@@ -635,37 +637,37 @@
             yield vlanDevName.split('.', 1)[1]
 
     def getVlanDevsForIface(self, iface):
-        for v, vdict in self.vlans.iteritems():
+        for v, vdict in iteritems(self.vlans):
             if iface == vdict['iface']:
                 yield v
 
     def getNetworkForIface(self, iface):
         """ Return the network attached to nic/bond """
-        for network, netdict in self.networks.iteritems():
+        for network, netdict in iteritems(self.networks):
             if ('ports' in netdict and iface in netdict['ports'] or
                     iface == netdict['iface']):
                 return network
 
     def getBridgelessNetworks(self):
         """ Return all bridgless networks."""
-        for network, netdict in self.networks.iteritems():
+        for network, netdict in iteritems(self.networks):
             if not netdict['bridged']:
                 yield network
 
     def getBridgelessNetworkForIface(self, iface):
         """ Return the bridgeless network attached to nic/bond """
-        for network, netdict in self.networks.iteritems():
+        for network, netdict in iteritems(self.networks):
             if not netdict['bridged'] and iface == netdict['iface']:
                 return network
 
     def getBridgedNetworkForIface(self, iface):
         """ Return all bridged networks attached to nic/bond """
-        for bridge, netdict in self.networks.iteritems():
+        for bridge, netdict in iteritems(self.networks):
             if netdict['bridged'] and iface in netdict['ports']:
                 return bridge
 
     def getBondingsForNic(self, nic):
-        for b, bdict in self.bondings.iteritems():
+        for b, bdict in iteritems(self.bondings):
             if nic in bdict['slaves']:
                 yield b
 
@@ -711,15 +713,15 @@
     def ifaceUsers(self, iface):
         "Returns a list of entities using the interface"
         users = set()
-        for n, ndict in self.networks.iteritems():
+        for n, ndict in iteritems(self.networks):
             if ndict['bridged'] and iface in ndict['ports']:
                 users.add(n)
             elif not ndict['bridged'] and iface == ndict['iface']:
                 users.add(n)
-        for b, bdict in self.bondings.iteritems():
+        for b, bdict in iteritems(self.bondings):
             if iface in bdict['slaves']:
                 users.add(b)
-        for v, vdict in self.vlans.iteritems():
+        for v, vdict in iteritems(self.vlans):
             if iface == vdict['iface']:
                 users.add(v)
         return users
diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py
index 538498f..cedd322 100644
--- a/lib/vdsm/utils.py
+++ b/lib/vdsm/utils.py
@@ -53,6 +53,17 @@
 from config import config
 import constants
 
+PY2 = sys.version_info[0] == 2
+if PY2:
+    iterkeys = lambda d: d.iterkeys()
+    itervalues = lambda d: d.itervalues()
+    iteritems = lambda d: d.iteritems()
+else:
+    iterkeys = lambda d: iter(d.keys())
+    itervalues = lambda d: iter(d.values())
+    iteritems = lambda d: iter(d.items())
+
+
 # Buffsize is 1K because I tested it on some use cases and 1k was fastets. If
 # you find this number to be a bottleneck in any way you are welcome to change
 # it


-- 
To view, visit http://gerrit.ovirt.org/17649
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2ae9450fc072cef9fc908cdd790728a1d1c8b9f7
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to