[PATCH 3/3] Transform our compare functions into key functions and use these instead

Vratislav Podzimek vpodzime at redhat.com
Wed Feb 18 16:15:16 UTC 2015


Python 3's sorting stuff doesn't support the 'cmp=...' argument.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 blivet/blivet.py       | 14 ++++++++++++--
 blivet/partitioning.py | 18 ++++++++++++++----
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/blivet/blivet.py b/blivet/blivet.py
index c49a380..7023cbe 100644
--- a/blivet/blivet.py
+++ b/blivet/blivet.py
@@ -26,6 +26,7 @@ import shelve
 import contextlib
 import time
 import parted
+import functools
 
 
 from pykickstart.constants import AUTOPART_TYPE_LVM, CLEARPART_TYPE_ALL, CLEARPART_TYPE_LINUX, CLEARPART_TYPE_LIST, CLEARPART_TYPE_NONE
@@ -345,7 +346,7 @@ class Blivet(object):
                     log.info("Skipping disk: %s: No media present", device.name)
                     continue
                 disks.append(device)
-        disks.sort(key=lambda d: d.name, cmp=self.compareDisks)
+        disks.sort(key=self.compareDisksKey)
         return disks
 
     @property
@@ -1430,7 +1431,7 @@ class Blivet(object):
             return
 
         boot_disks = [d for d in self.disks if d.partitioned]
-        boot_disks.sort(cmp=self.compareDisks, key=lambda d: d.name)
+        boot_disks.sort(key=self.compareDisksKey)
         self.bootloader.set_disk_list(boot_disks)
 
     def setUpBootLoader(self, early=False):
@@ -1563,6 +1564,11 @@ class Blivet(object):
         os.symlink(target, path)
 
     def compareDisks(self, first, second):
+        if not isinstance(first, str):
+            first = first.name
+        if not isinstance(second, str):
+            second = second.name
+
         if first in self.eddDict and second in self.eddDict:
             one = self.eddDict[first]
             two = self.eddDict[second]
@@ -1615,6 +1621,10 @@ class Blivet(object):
 
         return 0
 
+    @property
+    def compareDisksKey(self):
+        return functools.cmp_to_key(self.compareDisks)
+
     def getFSType(self, mountpoint=None):
         """ Return the default filesystem type based on mountpoint. """
         fstype = self.defaultFSType
diff --git a/blivet/partitioning.py b/blivet/partitioning.py
index 9c3f47d..5f131a8 100644
--- a/blivet/partitioning.py
+++ b/blivet/partitioning.py
@@ -23,12 +23,13 @@
 from operator import gt, lt
 from decimal import Decimal
 from gi.repository import BlockDev as blockdev
+import functools
 
 import parted
 
 from .errors import DeviceError, PartitioningError
 from .flags import flags
-from .devices import PartitionDevice, LUKSDevice, devicePathToName
+from .devices import Device, PartitionDevice, LUKSDevice, devicePathToName
 from .size import Size
 from .i18n import _
 from .util import stringize, unicodeize
@@ -107,6 +108,8 @@ def partitionCompare(part1, part2):
 
     return ret
 
+_partitionCompareKey = functools.cmp_to_key(partitionCompare)
+
 def getNextPartitionType(disk, no_primary=None):
     """ Return the type of partition to create next on a disk.
 
@@ -580,7 +583,7 @@ def allocatePartitions(storage, disks, partitions, freespace):
                 ["%s(id %d)" % (p.name, p.id) for p in partitions])
 
     new_partitions = [p for p in partitions if not p.exists]
-    new_partitions.sort(cmp=partitionCompare)
+    new_partitions.sort(key=_partitionCompareKey)
 
     # the following dicts all use device path strings as keys
     disklabels = {}     # DiskLabel instances for each disk
@@ -607,7 +610,7 @@ def allocatePartitions(storage, disks, partitions, freespace):
             req_disks = disks
 
         # sort the disks, making sure the boot disk is first
-        req_disks.sort(key=lambda d: d.name, cmp=storage.compareDisks)
+        req_disks.sort(key=storage.compareDisksKey)
         for disk in req_disks:
             if storage.bootDisk and disk == storage.bootDisk:
                 boot_index = req_disks.index(disk)
@@ -1347,7 +1350,7 @@ class VGChunk(Chunk):
 
     def sortRequests(self):
         # sort the partitions by start sector
-        self.requests.sort(key=lambda r: r.device, cmp=lvCompare)
+        self.requests.sort(key=_lvCompareKey)
 
 
 class ThinPoolChunk(VGChunk):
@@ -1755,6 +1758,11 @@ def lvCompare(lv1, lv2):
           0 => x == y
         > 1 => x > y
     """
+    if not isinstance(lv1, Device):
+        lv1 = lv1.device
+    if not isinstance(lv2, Device):
+        lv2 = lv2.device
+
     ret = 0
 
     # larger requests go to the front of the list
@@ -1779,6 +1787,8 @@ def lvCompare(lv1, lv2):
 
     return ret
 
+_lvCompareKey = functools.cmp_to_key(lvCompare)
+
 def _apply_chunk_growth(chunk):
     """ grow the lvs by the amounts the VGChunk calculated """
     for req in chunk.requests:
-- 
2.1.0



More information about the anaconda-patches mailing list