[PATCH 00/16] remove Network(): remove unused functions
Radek Vykydal
rvykydal at redhat.com
Thu Jul 19 10:27:55 UTC 2012
Please ignore this message.
On 07/19/2012 12:25 PM, Radek Vykydal wrote:
> move some methods out of Network object
> still keeping old ui working
> ---
> pyanaconda/gui.py | 4 +-
> pyanaconda/iw/network_gui.py | 3 +-
> pyanaconda/kickstart.py | 13 ++-
> pyanaconda/network.py | 210 +++++++++++++--------------------
> pyanaconda/textw/netconfig_text.py | 4 +-
> tests/pyanaconda_test/network_test.py | 107 ++++-------------
> 6 files changed, 123 insertions(+), 218 deletions(-)
>
> diff --git a/pyanaconda/gui.py b/pyanaconda/gui.py
> index d8360a9..308ee86 100755
> --- a/pyanaconda/gui.py
> +++ b/pyanaconda/gui.py
> @@ -934,7 +934,7 @@ class InstallInterface(InstallInterfaceBase):
> w = self.anaconda.intf.waitWindow(_("Wireless setup"),
> _("Scanning access points for wireless devices"))
> # get available wireless APs
> - dev_all_ssids = self.anaconda.network.getSSIDs()
> + dev_all_ssids = network.getSSIDs()
> w.pop()
> # select wireless APs
> dev_ssids = selectSSIDsDialog(dev_all_ssids) or dev_all_ssids
> @@ -961,7 +961,7 @@ class InstallInterface(InstallInterfaceBase):
> w = WaitWindow(_("Waiting for NetworkManager"),
> _("Waiting for NetworkManager to activate "
> "these devices: %s") % ",".join(waited_devs))
> - failed_devs = self.anaconda.network.waitForDevicesActivation(waited_devs)
> + failed_devs = network.waitForDevicesActivation(waited_devs)
> w.pop()
>
> if just_setup:
> diff --git a/pyanaconda/iw/network_gui.py b/pyanaconda/iw/network_gui.py
> index 1521db0..f1c19c0 100644
> --- a/pyanaconda/iw/network_gui.py
> +++ b/pyanaconda/iw/network_gui.py
> @@ -95,7 +95,8 @@ class NetworkWindow(InstallWindow):
> custom_icon="error")
> self.hostnameError()
>
> - self.anaconda.network.setHostname(hostname)
> + self.anaconda.network.hostname = hostname
> + network.setHostname(hostname)
> return None
>
> def NMCEExited(pid, condition, anaconda):
> diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
> index 8a74713..8238c88 100644
> --- a/pyanaconda/kickstart.py
> +++ b/pyanaconda/kickstart.py
> @@ -645,7 +645,8 @@ class NetworkData(commands.network.F16_NetworkData):
> def execute(self):
> if flags.imageInstall:
> if self.hostname != "":
> - self.anaconda.network.setHostname(self.hostname)
> + self.anaconda.network.hostname = self.hostname
> + network.setHostname(self.hostname)
>
> # Only set hostname
> return
> @@ -654,7 +655,8 @@ class NetworkData(commands.network.F16_NetworkData):
> # only set hostname
> if self.essid:
> if self.hostname != "":
> - self.anaconda.network.setHostname(self.hostname)
> + self.anaconda.network.hostname = self.hostname
> + network.setHostname(self.hostname)
> return
>
> devices = self.anaconda.network.netdevices
> @@ -706,7 +708,8 @@ class NetworkData(commands.network.F16_NetworkData):
> break
>
> if self.hostname != "":
> - self.anaconda.network.setHostname(self.hostname)
> + self.anaconda.network.hostname = self.hostname
> + network.setHostname(self.hostname)
> if not dev:
> # Only set hostname
> return
> @@ -757,10 +760,10 @@ class NetworkData(commands.network.F16_NetworkData):
> dev.set(("ETHTOOL_OPTS", self.ethtool))
>
> if self.nameserver != "":
> - self.anaconda.network.setDNS(self.nameserver, dev.iface)
> + dev.setDNS(self.nameserver)
>
> if self.gateway != "":
> - self.anaconda.network.setGateway(self.gateway, dev.iface)
> + dev.setGateway(self.gateway)
>
> if self.nodefroute:
> dev.set (("DEFROUTE", "no"))
> diff --git a/pyanaconda/network.py b/pyanaconda/network.py
> index 859c368..0a5ad61 100644
> --- a/pyanaconda/network.py
> +++ b/pyanaconda/network.py
> @@ -362,6 +362,31 @@ class NetworkDevice(IfcfgFile):
>
> return False
>
> + def setGateway(self, gw):
> + if ':' in gw:
> + self.set(('IPV6_DEFAULTGW', gw))
> + else:
> + self.set(('GATEWAY', gw))
> +
> + def unsetDNS(self):
> + """Unset all DNS* ifcfg parameters."""
> + i = 1
> + while True:
> + if self.get("DNS%d" % i):
> + self.unset("DNS%d" %i)
> + else:
> + break
> + i += 1
> +
> + def setDNS(self, ns):
> + dns = ns.split(',')
> + i = 1
> + for addr in dns:
> + addr = addr.strip()
> + dnslabel = "DNS%d" % (i,)
> + self.set((dnslabel, addr))
> + i += 1
> +
> class WirelessNetworkDevice(NetworkDevice):
>
> """
> @@ -469,11 +494,6 @@ class Network:
> self.ksdevice = dev
> break
>
> -
> -
> - def getDevice(self, device):
> - return self.netdevices[device]
> -
> def getKSDevice(self):
> if self.ksdevice is None:
> return None
> @@ -483,42 +503,6 @@ class Network:
> except:
> return None
>
> - def setHostname(self, hn):
> - self.hostname = hn
> - if flags.imageInstall:
> - log.info("image install -- not setting hostname")
> - return
> -
> - log.info("setting installation environment hostname to %s" % hn)
> - iutil.execWithRedirect("hostname", ["-v", hn ],
> - stdout="/dev/tty5", stderr="/dev/tty5")
> -
> - def unsetDNS(self, devname):
> - """Unset all DNS* ifcfg parameters."""
> - i = 1
> - dev = self.netdevices[devname]
> - while True:
> - if dev.get("DNS%d" % i):
> - dev.unset("DNS%d" %i)
> - else:
> - break
> - i += 1
> -
> - def setDNS(self, ns, device):
> - dns = ns.split(',')
> - i = 1
> - for addr in dns:
> - addr = addr.strip()
> - dnslabel = "DNS%d" % (i,)
> - self.netdevices[device].set((dnslabel, addr))
> - i += 1
> -
> - def setGateway(self, gw, device):
> - if ':' in gw:
> - self.netdevices[device].set(('IPV6_DEFAULTGW', gw))
> - else:
> - self.netdevices[device].set(('GATEWAY', gw))
> -
> @property
> def gateway(self):
> """GATEWAY - last device in list wins"""
> @@ -537,29 +521,6 @@ class Network:
> return dev.get('IPV6_DEFAULTGW')
> return ""
>
> - def lookupHostname(self):
> - # can't look things up if they don't exist!
> - if not self.hostname or self.hostname == "localhost.localdomain":
> - return None
> -
> - if not hasActiveNetDev():
> - log.warning("no network devices were available to look up host name")
> - return None
> -
> - try:
> - (family, socktype, proto, canonname, sockaddr) = \
> - socket.getaddrinfo(self.hostname, None, socket.AF_INET)[0]
> - (ip, port) = sockaddr
> - except:
> - try:
> - (family, socktype, proto, canonname, sockaddr) = \
> - socket.getaddrinfo(self.hostname, None, socket.AF_INET6)[0]
> - (ip, port, flowinfo, scopeid) = sockaddr
> - except:
> - return None
> -
> - return ip
> -
> # Note that the file is written-out only if there is a value
> # that has changed.
> def writeIfcfgFiles(self):
> @@ -595,10 +556,6 @@ class Network:
> "NM_CONTROLLED=yes\n")
> ifcfgfile.close()
>
> -
> - def getSSIDs(self):
> - return getSSIDs()
> -
> def writeKS(self, f):
> devNames = self.netdevices.keys()
> devNames.sort()
> @@ -611,16 +568,6 @@ class Network:
> line = "%s" % kickstartNetworkData(dev, self.hostname)
> f.write(line)
>
> - def hasNameServers(self, hash):
> - if hash.keys() == []:
> - return False
> -
> - for key in hash.keys():
> - if key.upper().startswith('DNS'):
> - return True
> -
> - return False
> -
> def hasWirelessDev(self):
> for dev in self.netdevices:
> if isys.isWirelessDevice(dev):
> @@ -768,60 +715,12 @@ class Network:
> f.write("options ipv6 disable=1\n")
> f.close()
>
> - def waitForDevicesActivation(self, devices):
> - waited_devs_props = {}
> -
> - bus = dbus.SystemBus()
> - nm = bus.get_object(isys.NM_SERVICE, isys.NM_MANAGER_PATH)
> - device_paths = nm.get_dbus_method("GetDevices")()
> - for device_path in device_paths:
> - device = bus.get_object(isys.NM_SERVICE, device_path)
> - device_props_iface = dbus.Interface(device, isys.DBUS_PROPS_IFACE)
> - iface = str(device_props_iface.Get(isys.NM_DEVICE_IFACE, "Interface"))
> - if iface in devices:
> - waited_devs_props[iface] = device_props_iface
> -
> - i = 0
> - while True:
> - for dev, device_props_iface in waited_devs_props.items():
> - state = device_props_iface.Get(isys.NM_DEVICE_IFACE, "State")
> - if state == isys.NM_DEVICE_STATE_ACTIVATED:
> - waited_devs_props.pop(dev)
> - if len(waited_devs_props) == 0 or i >= CONNECTION_TIMEOUT:
> - break
> - i += 1
> - time.sleep(1)
> -
> - return waited_devs_props.keys()
> -
> - # write out current configuration state and wait for NetworkManager
> - # to bring the device up, watch NM state and return to the caller
> - # once we have a state
> - def waitForConnection(self):
> - bus = dbus.SystemBus()
> - nm = bus.get_object(isys.NM_SERVICE, isys.NM_MANAGER_PATH)
> - props = dbus.Interface(nm, isys.DBUS_PROPS_IFACE)
> -
> - i = 0
> - while i < CONNECTION_TIMEOUT:
> - state = props.Get(isys.NM_SERVICE, "State")
> - if nmIsConnected(state):
> - return True
> - i += 1
> - time.sleep(1)
> -
> - state = props.Get(isys.NM_SERVICE, "State")
> - if nmIsConnected(state):
> - return True
> -
> - return False
> -
> # write out current configuration state and wait for NetworkManager
> # to bring the device up, watch NM state and return to the caller
> # once we have a state
> def bringUp(self):
> self.write()
> - if self.waitForConnection():
> + if waitForConnection():
> resetResolver()
> return True
> else:
> @@ -901,6 +800,55 @@ class Network:
>
> return netargs
>
> +def waitForDevicesActivation(devices):
> + waited_devs_props = {}
> +
> + bus = dbus.SystemBus()
> + nm = bus.get_object(isys.NM_SERVICE, isys.NM_MANAGER_PATH)
> + device_paths = nm.get_dbus_method("GetDevices")()
> + for device_path in device_paths:
> + device = bus.get_object(isys.NM_SERVICE, device_path)
> + device_props_iface = dbus.Interface(device, isys.DBUS_PROPS_IFACE)
> + iface = str(device_props_iface.Get(isys.NM_DEVICE_IFACE, "Interface"))
> + if iface in devices:
> + waited_devs_props[iface] = device_props_iface
> +
> + i = 0
> + while True:
> + for dev, device_props_iface in waited_devs_props.items():
> + state = device_props_iface.Get(isys.NM_DEVICE_IFACE, "State")
> + if state == isys.NM_DEVICE_STATE_ACTIVATED:
> + waited_devs_props.pop(dev)
> + if len(waited_devs_props) == 0 or i >= CONNECTION_TIMEOUT:
> + break
> + i += 1
> + time.sleep(1)
> +
> + return waited_devs_props.keys()
> +
> +# write out current configuration state and wait for NetworkManager
> +# to bring the device up, watch NM state and return to the caller
> +# once we have a state
> +def waitForConnection():
> + bus = dbus.SystemBus()
> + nm = bus.get_object(isys.NM_SERVICE, isys.NM_MANAGER_PATH)
> + props = dbus.Interface(nm, isys.DBUS_PROPS_IFACE)
> +
> + i = 0
> + while i < CONNECTION_TIMEOUT:
> + state = props.Get(isys.NM_SERVICE, "State")
> + if nmIsConnected(state):
> + return True
> + i += 1
> + time.sleep(1)
> +
> + state = props.Get(isys.NM_SERVICE, "State")
> + if nmIsConnected(state):
> + return True
> +
> + return False
> +
> +
> def kickstartNetworkData(ifcfg, hostname=None):
>
> from pyanaconda.kickstart import NetworkData
> @@ -1037,3 +985,11 @@ def resetResolver():
> isys.resetResolv()
> urlgrabber.grabber.reset_curl_obj()
>
> +def setHostname(hn):
> + if flags.imageInstall:
> + log.info("image install -- not setting hostname")
> + return
> +
> + log.info("setting installation environment hostname to %s" % hn)
> + iutil.execWithRedirect("hostname", ["-v", hn ],
> + stdout="/dev/tty5", stderr="/dev/tty5")
> diff --git a/pyanaconda/textw/netconfig_text.py b/pyanaconda/textw/netconfig_text.py
> index aabd7d5..ed1fff2 100644
> --- a/pyanaconda/textw/netconfig_text.py
> +++ b/pyanaconda/textw/netconfig_text.py
> @@ -390,9 +390,9 @@ class NetworkConfiguratorText:
> else:
> dev.set(("IPV6INIT", "no"))
>
> - self.anaconda.network.unsetDNS(devname)
> + self.netdevs[devname].unsetDNS()
> if nameservers:
> - self.anaconda.network.setDNS(nameservers, devname)
> + self.netdevs[devname].setDNS(nameservers)
>
> dev.set(('ONBOOT', 'yes'))
>
> diff --git a/tests/pyanaconda_test/network_test.py b/tests/pyanaconda_test/network_test.py
> index 561ddab..7ad58cf 100644
> --- a/tests/pyanaconda_test/network_test.py
> +++ b/tests/pyanaconda_test/network_test.py
> @@ -262,6 +262,28 @@ class NetworkTest(mock.TestCase):
> self.assertEqual(nd.info, {'KEY': 'other_value'})
> self.assertTrue(nd._dirty)
>
> + def networkdevice_set_gateway_test(self):
> + import pyanaconda.network
> + nd = pyanaconda.network.NetworkDevice(self.NETSCRIPTSDIR, self.DEVICE)
> + nd.setGateway('10.0.0.1')
> + self.assertEqual(nd.info, {'GATEWAY': '10.0.0.1'})
> + self.assertTrue(nd._dirty)
> +
> + def networkdevice_set_gateway_ipv6_test(self):
> + import pyanaconda.network
> + nd = pyanaconda.network.NetworkDevice(self.NETSCRIPTSDIR, self.DEVICE)
> + nd.setGateway('fe80::5675:d0ff:feac:4d3f')
> + self.assertEqual(nd.info, {'IPV6_DEFAULTGW': 'fe80::5675:d0ff:feac:4d3f'})
> + self.assertTrue(nd._dirty)
> +
> + def networkdevice_set_dns_test(self):
> + import pyanaconda.network
> + nd = pyanaconda.network.NetworkDevice(self.NETSCRIPTSDIR, self.DEVICE)
> + nd.setDNS('10.0.0.1, 10.0.0.2')
> + self.assertEqual(nd.info, {'DNS1': '10.0.0.1'})
> + self.assertEqual(nd.info, {'DNS2': '10.0.0.2'})
> + self.assertTrue(nd._dirty)
> +
> def networkdevice_keyfile_path_test(self):
> import pyanaconda.network
> nd = pyanaconda.network.NetworkDevice(self.NETSCRIPTSDIR, self.DEVICE)
> @@ -294,14 +316,6 @@ class NetworkTest(mock.TestCase):
> self.assertEqual(pyanaconda.network.shutil.move.call_args[0],
> (TMP_FILE, '%s/keys-%s' % (TMP_DIR, self.DEVICE)))
>
> - def network_get_device_test(self):
> - import pyanaconda.network
> -
> - nw = pyanaconda.network.Network()
> - nw.netdevices['dev'] = 'device'
> - ret = nw.getDevice('dev')
> - self.assertEqual(ret, 'device')
> -
> def network_get_ks_device_1_test(self):
> import pyanaconda.network
> nw = pyanaconda.network.Network()
> @@ -324,57 +338,6 @@ class NetworkTest(mock.TestCase):
> ret = nw.getKSDevice()
> self.assertEqual(ret, 'device')
>
> - def network_set_hostname_test(self):
> - import pyanaconda.network
> - pyanaconda.network.iutil.execWithRedirect = mock.Mock()
> - nw = pyanaconda.network.Network()
> - nw.setHostname('DESKTOP')
> - self.assertEqual(nw.hostname, 'DESKTOP')
> -
> - def network_set_dns_test(self):
> - import pyanaconda.network
> - nw = pyanaconda.network.Network()
> - nw.netdevices['dev'] = mock.Mock()
> - nw.setDNS('10.0.0.1, 10.0.0.2', 'dev')
> - self.assertEqual(nw.netdevices['dev'].method_calls,
> - [('set', (('DNS1', '10.0.0.1'),), {}),
> - ('set', (('DNS2', '10.0.0.2'),), {})]
> - )
> -
> - def network_set_gateway_test(self):
> - import pyanaconda.network
> - nw = pyanaconda.network.Network()
> - nw.netdevices['eth0'] = mock.Mock()
> - nw.setGateway('10.0.0.1', 'eth0')
> - self.assertEqual(pyanaconda.network.Network.netdevices['eth0'].method_calls,
> - [('set', (('GATEWAY', '10.0.0.1'),), {})])
> -
> - def network_lookup_hostname_1_test(self):
> - import pyanaconda.network
> - nw = pyanaconda.network.Network()
> - nw.hostname = None
> - ret = nw.lookupHostname()
> - self.assertEqual(ret, None)
> -
> - def network_lookup_hostname_2_test(self):
> - import pyanaconda.network
> - nw = pyanaconda.network.Network()
> - nw.hostname = 'desktop'
> - pyanaconda.network.hasActiveNetDev = mock.Mock(return_value=False)
> - ret = nw.lookupHostname()
> - self.assertEqual(ret, None)
> -
> - def network_lookup_hostname_3_test(self):
> - import pyanaconda.network
> - pyanaconda.network.socket.getaddrinfo.return_value = \
> - [(0, 0, 0, 0, ('10.1.1.1', 0))]
> -
> - nw = pyanaconda.network.Network()
> - nw.hostname = 'desktop'
> - pyanaconda.network.hasActiveNetDev = mock.Mock(return_value=True)
> - ret = nw.lookupHostname()
> - self.assertEqual(ret, '10.1.1.1')
> -
> def network_write_ifcfg_files_test(self):
> import pyanaconda.network
> nw = pyanaconda.network.Network()
> @@ -458,22 +421,6 @@ class NetworkTest(mock.TestCase):
> self.assertEqual(self.fs[TMPFILE],
> 'network --device eth0 --bootproto dhcp --noipv6\n')
>
> - def network_has_name_server_1_test(self):
> - import pyanaconda.network
> - hash = {'foo':'', 'bar':''}
> -
> - nw = pyanaconda.network.Network()
> - ret = nw.hasNameServers(hash)
> - self.assertFalse(ret)
> -
> - def network_has_name_server_2_test(self):
> - import pyanaconda.network
> - hash = {'foo':'', 'bar':'', 'dnsserver':''}
> -
> - nw = pyanaconda.network.Network()
> - ret = nw.hasNameServers(hash)
> - self.assertTrue(ret)
> -
> def network_has_wireless_dev_1_test(self):
> import pyanaconda.network
> pyanaconda.network.isys = mock.Mock()
> @@ -556,8 +503,7 @@ class NetworkTest(mock.TestCase):
> pyanaconda.network.dbus.Interface().Get.return_value = \
> pyanaconda.network.isys.NM_STATE_CONNECTED_GLOBAL
>
> - nw = pyanaconda.network.Network()
> - ret = nw.waitForConnection()
> + ret = pyanaconda.network.waitForConnection()
> self.assertTrue(ret)
>
> def network_wait_for_connection_2_test(self):
> @@ -568,19 +514,18 @@ class NetworkTest(mock.TestCase):
> pyanaconda.network.isys.NM_STATE_CONNECTED = self.OK
> pyanaconda.network.time.sleep = mock.Mock()
>
> - nw = pyanaconda.network.Network()
> - ret = nw.waitForConnection()
> + ret = pyanaconda.network.waitForConnection()
> self.assertFalse(ret)
>
> def network_bring_up_test(self):
> import pyanaconda.network
> pyanaconda.network.Network.write = mock.Mock()
> - pyanaconda.network.Network.waitForConnection = mock.Mock()
> + pyanaconda.network.waitForConnection = mock.Mock()
>
> nw = pyanaconda.network.Network()
> nw.bringUp()
> self.assertTrue(pyanaconda.network.Network.write.called)
> - self.assertTrue(pyanaconda.network.Network.waitForConnection.called)
> + self.assertTrue(pyanaconda.network.waitForConnection.called)
>
> def iface_for_host_ip_test(self):
> import pyanaconda.network
More information about the anaconda-patches
mailing list