[blivet][master/rhel7-branch][PATCH 2/3] Remove DeviceTree.getDevicesByType. (#1155984)

Samantha N. Bueno sbueno+anaconda at redhat.com
Thu Jun 4 16:04:24 UTC 2015


It does very little to simplify the caller's life other than add the
overhead of logging the return value.

Patch originally by Dave Lehman <dlehman at redhat.com>

Resolves: rhbz#1155984
---
 blivet/__init__.py   | 68 ++++++++++++++++++++--------------------------------
 blivet/devicetree.py | 15 ------------
 blivet/iscsi.py      |  3 +--
 tests/action_test.py |  6 ++---
 4 files changed, 30 insertions(+), 62 deletions(-)

diff --git a/blivet/__init__.py b/blivet/__init__.py
index 83a65bc..c165a69 100644
--- a/blivet/__init__.py
+++ b/blivet/__init__.py
@@ -597,9 +597,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        vgs = self.devicetree.getDevicesByType("lvmvg")
-        vgs.sort(key=lambda d: d.name)
-        return vgs
+        vgs = (d for d in self.devices if d.type == "lvmvg")
+        return sorted(vgs, key=lambda d: d.name)
 
     @property
     def lvs(self):
@@ -609,9 +608,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        lvs = self.devicetree.getDevicesByType("lvmlv")
-        lvs.sort(key=lambda d: d.name)
-        return lvs
+        lvs = (d for d in self.devices if d.type == "lvmlv")
+        return sorted(lvs, key=lambda d: d.name)
 
     @property
     def thinlvs(self):
@@ -621,9 +619,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        thin = self.devicetree.getDevicesByType("lvmthinlv")
-        thin.sort(key=lambda d: d.name)
-        return thin
+        thin = (d for d in self.devices if d.type == "lvmthinlv")
+        return sorted(thin, key=lambda d: d.name)
 
     @property
     def thinpools(self):
@@ -633,9 +630,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        pools = self.devicetree.getDevicesByType("lvmthinpool")
-        pools.sort(key=lambda d: d.name)
-        return pools
+        pools = (d for d in self.devices if d.type == "lvmthinpool")
+        return sorted(pools, key=lambda d: d.name)
 
     @property
     def pvs(self):
@@ -645,10 +641,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        devices = self.devicetree.devices
-        pvs = [d for d in devices if d.format.type == "lvmpv"]
-        pvs.sort(key=lambda d: d.name)
-        return pvs
+        pvs = (d for d in self.devices if d.format.type == "lvmpv")
+        return sorted(pvs, key=lambda d: d.name)
 
     @property
     def mdarrays(self):
@@ -658,16 +652,14 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        arrays = self.devicetree.getDevicesByType("mdarray")
-        arrays.sort(key=lambda d: d.name)
-        return arrays
+        arrays = (d for d in self.devices if d.type == "mdarray")
+        return sorted(arrays, key=lambda d: d.name)
 
     @property
     def mdcontainers(self):
         """ A list of the MD containers in the device tree. """
-        arrays = self.devicetree.getDevicesByType("mdcontainer")
-        arrays.sort(key=lambda d: d.name)
-        return arrays
+        arrays = (d for d in self.devices if d.type == "mdcontainer")
+        return sorted(arrays, key=lambda d: d.name)
 
     @property
     def mdmembers(self):
@@ -677,10 +669,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        devices = self.devicetree.devices
-        members = [d for d in devices if d.format.type == "mdmember"]
-        members.sort(key=lambda d: d.name)
-        return members
+        members = (d for d in self.devices if d.format.type == "mdmember")
+        return sorted(members, key=lambda d: d.name)
 
     @property
     def btrfsVolumes(self):
@@ -690,8 +680,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        return sorted(self.devicetree.getDevicesByType("btrfs volume"),
-                      key=lambda d: d.name)
+        volumes = (d for d in self.devices if d.type == "btrfs volume")
+        return sorted(volumes, key=lambda d: d.name)
 
     @property
     def swaps(self):
@@ -701,10 +691,8 @@ class Blivet(object):
             does not necessarily reflect the actual on-disk state of the
             system's disks.
         """
-        devices = self.devicetree.devices
-        swaps = [d for d in devices if d.format.type == "swap"]
-        swaps.sort(key=lambda d: d.name)
-        return swaps
+        swaps = (d for d in self.devices if d.format.type == "swap")
+        return sorted(swaps, key=lambda d: d.name)
 
     def shouldClear(self, device, **kwargs):
         """ Return True if a clearpart settings say a device should be cleared.
@@ -2706,7 +2694,7 @@ class FSSet(object):
             open(mdadm_path, "w").write(mdadm_conf)
 
         # /etc/multipath.conf
-        if self.devicetree.getDevicesByType("dm-multipath"):
+        if any(d.type == "dm-multipath" for d in self.devices):
             util.copy_to_system("/etc/multipath.conf")
             util.copy_to_system("/etc/multipath/wwids")
             util.copy_to_system("/etc/multipath/bindings")
@@ -2740,15 +2728,11 @@ class FSSet(object):
 
     def mdadmConf(self):
         """ Return the contents of mdadm.conf. """
-        arrays = self.devicetree.getDevicesByType("mdarray")
-        arrays.extend(self.devicetree.getDevicesByType("mdbiosraidarray"))
-        arrays.extend(self.devicetree.getDevicesByType("mdcontainer"))
-        # Sort it, this not only looks nicer, but this will also put
-        # containers (which get md0, md1, etc.) before their members
-        # (which get md127, md126, etc.). and lame as it is mdadm will not
-        # assemble the whole stack in one go unless listed in the proper order
-        # in mdadm.conf
-        arrays.sort(key=lambda d: d.path)
+        # mdadm will not assemble the whole stack in one go unless listed in the
+        # proper order in mdadm.conf
+        arrays = [d for d in self.devices if d.type == "mdcontainer"]
+        arrays.extend([d for d in self.devices if d.type == "mdbiosraidarray"])
+        arrays.extend([d for d in self.devices if d.type == "mdarray"])
         if not arrays:
             return ""
 
diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index b6f94a4..321b431 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -2368,21 +2368,6 @@ class DeviceTree(object):
         log_method_return(self, result)
         return result
 
-    def getDevicesByType(self, device_type, incomplete=False, hidden=False):
-        """ Return a list of devices with a matching device type.
-
-            :param str device_type: the type to match
-            :param bool incomplete: include incomplete devices in search
-            :param bool hidden: include hidden devices in search
-            :returns: all matching device found
-            :rtype: list of :class:`~.devices.Device`
-        """
-        log_method_call(self, device_type=device_type, incomplete=incomplete, hidden=hidden)
-        devices = self._filterDevices(incomplete=incomplete, hidden=hidden)
-        result = [d for d in devices if d.type == device_type]
-        log_method_return(self, [r.name for r in result])
-        return result
-
     def getDeviceByID(self, id_num, incomplete=False, hidden=False):
         """ Return a device with specified device id.
 
diff --git a/blivet/iscsi.py b/blivet/iscsi.py
index cc557cb..42fca12 100644
--- a/blivet/iscsi.py
+++ b/blivet/iscsi.py
@@ -430,8 +430,7 @@ class iscsi(object):
 
     def getNodeDisks(self, node, storage):
         nodeDisks = []
-        iscsiDisks = storage.devicetree.getDevicesByType("iscsi")
-        for disk in iscsiDisks:
+        for disk in (d for d in storage.disks if d.type == "iscsi"):
             if disk.node == node:
                 nodeDisks.append(disk)
 
diff --git a/tests/action_test.py b/tests/action_test.py
index 6440832..d0f21c3 100644
--- a/tests/action_test.py
+++ b/tests/action_test.py
@@ -118,9 +118,9 @@ class DeviceActionTestCase(StorageTestCase):
 
         # clear the disks
         self.destroyAllDevices()
-        self.assertEqual(devicetree.getDevicesByType("lvmlv"), [])
-        self.assertEqual(devicetree.getDevicesByType("lvmvg"), [])
-        self.assertEqual(devicetree.getDevicesByType("partition"), [])
+        self.assertEqual(self.storage.lvs, [])
+        self.assertEqual(self.storage.vgs, [])
+        self.assertEqual(self.storage.partitions, [])
 
         sda = devicetree.getDeviceByName("sda")
         self.assertNotEqual(sda, None, "failed to find disk 'sda'")
-- 
1.9.3



More information about the anaconda-patches mailing list