[master 7/16] Transform our compare functions into key functions and use these instead
M4rtinK
installerbot-noreply at redhat.com
Fri Apr 10 13:38:13 UTC 2015
From: Vratislav Podzimek <vpodzime at redhat.com>
Python 3's sorting stuff doesn't support the 'cmp=...' argument.
---
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 3a7e76d..8fc3c96 100644
--- a/blivet/blivet.py
+++ b/blivet/blivet.py
@@ -26,6 +26,7 @@
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 @@ def disks(self):
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
@@ -1410,7 +1411,7 @@ def updateBootLoaderDiskList(self):
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):
@@ -1543,6 +1544,11 @@ def makeMtab(self):
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]
@@ -1595,6 +1601,10 @@ def compareDisks(self, first, second):
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 @@ def sizeToLength(self, size):
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:
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/f6e02cf09091debf56b0e6138e2b6231da66170d
More information about the anaconda-patches
mailing list