From 41e62563f507b4a2542c80c98aa878ffe6da00da Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Wed, 27 Jun 2012 09:20:48 -0500
Subject: [PATCH 10/14] Move shouldClear into Storage.

---
 pyanaconda/kickstart.py             |    6 +--
 pyanaconda/storage/__init__.py      |   73 ++++++++++++++++++++++++++++++++---
 pyanaconda/storage/devicetree.py    |   16 ++-----
 pyanaconda/storage/partitioning.py  |   60 +---------------------------
 pyanaconda/ui/gui/spokes/storage.py |    1 -
 5 files changed, 75 insertions(+), 81 deletions(-)

diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 78a1763..9b7124e 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -25,7 +25,6 @@ from storage.devicelibs.lvm import getPossiblePhysicalExtents
 from storage.devicelibs.mpath import MultipathConfigWriter, MultipathTopology
 from storage.formats import getFormat
 from storage.partitioning import clearPartitions
-from storage.partitioning import shouldClear
 import storage.iscsi
 import storage.fcoe
 import storage.zfcp
@@ -937,10 +936,7 @@ class PartitionData(commands.partition.F17_PartData):
                 if not disk.partitionable:
                     raise KickstartValueError, formatErrorMsg(self.lineno, msg="Cannot install to read-only media %s." % n)
 
-                should_clear = shouldClear(disk,
-                                           storage.config.clearPartType,
-                                           storage.config.clearPartDisks,
-                                           storage.config.clearPartDevices)
+                should_clear = storage.shouldClear(disk)
                 if disk and (disk.partitioned or should_clear):
                     kwargs["disks"] = [disk]
                     break
diff --git a/pyanaconda/storage/__init__.py b/pyanaconda/storage/__init__.py
index e05732f..79782a2 100644
--- a/pyanaconda/storage/__init__.py
+++ b/pyanaconda/storage/__init__.py
@@ -394,7 +394,8 @@ class Storage(object):
                                      passphrase=self.encryptionPassphrase,
                                      luksDict=self.__luksDevs,
                                      iscsi=self.iscsi,
-                                     dasd=self.dasd)
+                                     dasd=self.dasd,
+                                     shouldClear=self.shouldClear)
         self.fsset = FSSet(self.devicetree)
         self.roots = {}
         self.services = set()
@@ -501,7 +502,8 @@ class Storage(object):
                               passphrase=self.encryptionPassphrase,
                               luksDict=self.__luksDevs,
                               iscsi=self.iscsi,
-                              dasd=self.dasd)
+                              dasd=self.dasd,
+                              shouldClear=self.shouldClear)
         self.devicetree.populate(cleanupOnly=cleanupOnly)
         self.config.clearPartType = clearPartType # set it back
         self.fsset = FSSet(self.devicetree)
@@ -750,6 +752,64 @@ class Storage(object):
         """ The OS image used by live installs. """
         return None
 
+    def shouldClear(self, device, **kwargs):
+        clearPartType = kwargs.get("clearPartType", self.config.clearPartType)
+        clearPartDisks = kwargs.get("clearPartDisks",
+                                    self.config.clearPartDisks)
+        clearPartDevices = kwargs.get("clearPartDevices",
+                                      self.config.clearPartDevices)
+
+        if clearPartType in [CLEARPART_TYPE_NONE, None]:
+            return False
+
+        if isinstance(device, PartitionDevice):
+            # Never clear the special first partition on a Mac disk label, as
+            # that holds the partition table itself.
+            # Something similar for the third partition on a Sun disklabel.
+            if device.disk.format.labelType == "mac" and \
+               device.partedPartition.number == 1:
+                return False
+            elif device.disk.format.labelType == "sun" and \
+                 device.partedPartition.number == 3:
+                return False
+
+            # If we got a list of disks to clear, make sure this one's on it
+            if clearPartDisks and device.disk.name not in clearPartDisks:
+                return False
+
+            # We don't want to fool with extended partitions, freespace, &c
+            if not device.isPrimary and not device.isLogical:
+                return False
+
+            if clearPartType == CLEARPART_TYPE_LINUX and \
+               not device.format.linuxNative and \
+               not device.getFlag(parted.PARTITION_LVM) and \
+               not device.getFlag(parted.PARTITION_RAID) and \
+               not device.getFlag(parted.PARTITION_SWAP):
+                return False
+        elif device.isDisk and not device.partitioned:
+            # If we got a list of disks to clear, make sure this one's on it
+            if clearPartDisks and device.name not in clearPartDisks:
+                return False
+
+            # Never clear disks with hidden formats
+            if device.format.hidden:
+                return False
+
+            if clearPartType == CLEARPART_TYPE_LINUX and \
+               not device.format.linuxNative:
+                return False
+
+        # Don't clear devices holding install media.
+        if device.protected:
+            return False
+
+        if clearPartType == CLEARPART_TYPE_LIST and \
+           device.name not in clearPartDevices:
+            return False
+
+        return True
+
     def getFreeSpace(self, disks=None, clearPartType=None):
         """ Return a dict with free space info for each disk.
 
@@ -761,7 +821,6 @@ class Storage(object):
             self.disks and a clearPartType value other than
             self.config.clearPartType.
         """
-        from partitioning import shouldClear
         from size import Size
         if disks is None:
             disks = self.disks
@@ -771,7 +830,8 @@ class Storage(object):
 
         free = {}
         for disk in disks:
-            should_clear = shouldClear(disk, clearPartType, [disk.name])
+            should_clear = self.shouldClear(disk, clearPartType=clearPartType,
+                                            clearPartDisks=[disk.name])
             if should_clear:
                 free[disk.name] = (Size(spec="%f mb" % disk.size), 0)
                 continue
@@ -784,8 +844,9 @@ class Storage(object):
                     # only check actual filesystems since lvm &c require a bunch of
                     # operations to translate free filesystem space into free disk
                     # space
-                    should_clear = shouldClear(partition, clearPartType,
-                                               [disk.name])
+                    should_clear = self.shouldClear(partition,
+                                                    clearPartType=clearPartType,
+                                                    clearPartDisks=[disk.name])
                     if should_clear:
                         disk_free += partition.size
                     elif hasattr(partition.format, "free"):
diff --git a/pyanaconda/storage/devicetree.py b/pyanaconda/storage/devicetree.py
index 0070a87..5d50986 100644
--- a/pyanaconda/storage/devicetree.py
+++ b/pyanaconda/storage/devicetree.py
@@ -30,7 +30,6 @@ import pprint
 from errors import *
 from devices import *
 from deviceaction import *
-from partitioning import shouldClear
 from pykickstart.constants import *
 import formats
 import devicelibs.mdraid
@@ -155,11 +154,11 @@ class DeviceTree(object):
     """
 
     def __init__(self, conf=None, passphrase=None, luksDict=None,
-                 iscsi=None, dasd=None):
-        self.reset(conf, passphrase, luksDict, iscsi, dasd)
+                 iscsi=None, dasd=None, shouldClear=None):
+        self.reset(conf, passphrase, luksDict, iscsi, dasd, shouldClear)
 
     def reset(self, conf=None, passphrase=None, luksDict=None,
-              iscsi=None, dasd=None):
+              iscsi=None, dasd=None, shouldClear=None):
         # internal data members
         self._devices = []
         self._actions = []
@@ -171,11 +170,8 @@ class DeviceTree(object):
         self.populated = False
 
         self.exclusiveDisks = getattr(conf, "exclusiveDisks", [])
-        self.clearPartType = getattr(conf, "clearPartType", CLEARPART_TYPE_NONE)
-        self.clearPartDisks = getattr(conf, "clearPartDisks", [])
-        self.clearPartDevices = getattr(conf, "clearPartDevices", [])
         self.zeroMbr = getattr(conf, "zeroMbr", False)
-        self.reinitializeDisks = getattr(conf, "reinitializeDisks", False)
+        self.shouldClear = shouldClear or (lambda d: False)
         self.iscsi = iscsi
         self.dasd = dasd
         self.mpathFriendlyNames = getattr(conf, "mpathFriendlyNames", True)
@@ -1632,9 +1628,7 @@ class DeviceTree(object):
             device.format = formats.DeviceFormat()
             return
 
-        if shouldClear(device, self.clearPartType,
-                       clearPartDisks=self.clearPartDisks,
-                       clearPartDevices=self.clearPartDevices):
+        if self.shouldClear(device):
             # if this is a device that will be cleared by clearpart,
             # don't bother with format-specific processing
             return
diff --git a/pyanaconda/storage/partitioning.py b/pyanaconda/storage/partitioning.py
index 1cb9b94..43f77a7 100644
--- a/pyanaconda/storage/partitioning.py
+++ b/pyanaconda/storage/partitioning.py
@@ -341,63 +341,6 @@ def doAutoPartition(storage, data):
             storage.reset()
             raise exn
 
-def shouldClear(device, clearPartType, clearPartDisks=None, clearPartDevices=None):
-    if clearPartType in [CLEARPART_TYPE_NONE, None]:
-        return False
-    if not clearPartDisks:
-        clearPartDisks = []
-    if not clearPartDevices:
-        clearPartDevices = []
-
-    if isinstance(device, PartitionDevice):
-        # Never clear the special first partition on a Mac disk label, as that
-        # holds the partition table itself.
-        # Something similar for the third partition on a Sun disklabel.
-        if device.disk.format.partedDisk.type == "mac" and \
-           device.partedPartition.number == 1:
-            return False
-        elif device.disk.format.labelType == "sun" and \
-             device.partedPartition.number == 3:
-            return False
-
-        # If we got a list of disks to clear, make sure this one's on it
-        if device.disk.name not in clearPartDisks:
-            return False
-
-        # We don't want to fool with extended partitions, freespace, &c
-        if device.partType not in [parted.PARTITION_NORMAL,
-                                   parted.PARTITION_LOGICAL]:
-            return False
-
-        if clearPartType == CLEARPART_TYPE_LINUX and \
-           not device.format.linuxNative and \
-           not device.getFlag(parted.PARTITION_LVM) and \
-           not device.getFlag(parted.PARTITION_RAID) and \
-           not device.getFlag(parted.PARTITION_SWAP):
-            return False
-    elif device.isDisk and not device.partitioned:
-        # If we got a list of disks to clear, make sure this one's on it
-        if device.name not in clearPartDisks:
-            return False
-
-        # Never clear disks with hidden formats
-        if device.format.hidden:
-            return False
-
-        if clearPartType == CLEARPART_TYPE_LINUX and \
-           not device.format.linuxNative:
-            return False
-
-    # Don't clear devices holding install media.
-    if device.protected:
-        return False
-
-    if clearPartType == CLEARPART_TYPE_LIST and \
-       not clearPartDevices or device.name not in clearPartDevices:
-        return False
-
-    return True
-
 def recursiveRemove(storage, device):
     log.debug("clearing %s" % device.name)
 
@@ -449,7 +392,7 @@ def clearPartitions(storage):
     partitions.sort(key=lambda p: p.partedPartition.number, reverse=True)
     for part in partitions:
         log.debug("clearpart: looking at %s" % part.name)
-        if not shouldClear(part, storage.config.clearPartType, storage.config.clearPartDisks, storage.config.clearPartDevices):
+        if not storage.shouldClear(part):
             continue
 
         recursiveRemove(storage, part)
@@ -460,6 +403,7 @@ def clearPartitions(storage):
 
     # make sure that the the boot device has the correct disklabel type if
     # we're going to completely clear it.
+    # also handles clearpart --initlabel
     boot_disk = storage.bootDisk
     for disk in storage.partitioned:
         if not boot_disk and not storage.config.reinitializeDisks:
diff --git a/pyanaconda/ui/gui/spokes/storage.py b/pyanaconda/ui/gui/spokes/storage.py
index 4e597a7..055e3b6 100644
--- a/pyanaconda/ui/gui/spokes/storage.py
+++ b/pyanaconda/ui/gui/spokes/storage.py
@@ -48,7 +48,6 @@ from pyanaconda.ui.gui.categories.storage import StorageCategory
 from pyanaconda.ui.gui.utils import enlightbox, gdk_threaded
 
 from pyanaconda.storage.size import Size
-from pyanaconda.storage.partitioning import shouldClear
 from pyanaconda.product import productName
 from pyanaconda.flags import flags
 
-- 
1.7.7.6

