From 5ff5fa3bc17beea39c9ff7949a7f61c3a1c493d6 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Mon, 21 Apr 2014 10:45:50 -0500
Subject: [PATCH 03/12] Add ability to reset Request and Chunk instances.

---
 blivet/partitioning.py | 68 ++++++++++++++++++++++++++------------------------
 1 file changed, 36 insertions(+), 32 deletions(-)

diff --git a/blivet/partitioning.py b/blivet/partitioning.py
index 77e55f6..eed5b65 100644
--- a/blivet/partitioning.py
+++ b/blivet/partitioning.py
@@ -1231,9 +1231,12 @@ class Request(object):
             :type device: :class:`~.devices.StorageDevice`
         """
         self.device = device
+        self.reset()
+
+    def reset(self):
         self.growth = 0                     # growth in sectors
         self.max_growth = 0                 # max growth in sectors
-        self.done = not getattr(device, "req_grow", True)  # can we grow this
+        self.done = not getattr(self.device, "req_grow", True)# can we grow this
                                                            # request more?
         self.base = 0                       # base sectors
 
@@ -1260,21 +1263,19 @@ class Request(object):
 
 
 class PartitionRequest(Request):
-    def __init__(self, partition):
-        """
-            :param partition: the partition being requested
-            :type partition: :class:`~.devices.PartitionDevice`
-        """
-        super(PartitionRequest, self).__init__(partition)
-        self.base = partition.partedPartition.geometry.length   # base sectors
+    def reset(self):
+        super(PartitionRequest, self).reset()
+        if self.device.partedPartition is None:
+            return
 
-        sector_size = Size(partition.partedPartition.disk.device.sectorSize)
+        self.base = self.device.partedPartition.geometry.length   # base sectors
 
-        if partition.req_grow:
+        if self.device.req_grow:
+            sector_size = Size(self.device.partedPartition.disk.device.sectorSize)
             limits = filter(lambda l: l > 0,
-                        [sizeToSectors(partition.req_max_size, sector_size),
-                         sizeToSectors(partition.format.maxSize, sector_size),
-                         partition.partedPartition.disk.maxPartitionLength])
+                        [sizeToSectors(self.device.req_max_size, sector_size),
+                         sizeToSectors(self.device.format.maxSize, sector_size),
+                         self.device.partedPartition.disk.maxPartitionLength])
 
             if limits:
                 max_sectors = min(limits)
@@ -1283,23 +1284,19 @@ class PartitionRequest(Request):
                     # max size is less than or equal to base, so we're done
                     self.done = True
 
-
 class LVRequest(Request):
-    def __init__(self, lv):
-        """
-            :param lv: the logical volume being requested
-            :type lv: :class:`~.devices.LVMLogicalVolumeDevice`
-        """
-        super(LVRequest, self).__init__(lv)
-
+    def reset(self):
+        super(LVRequest, self).reset()
         # Round up to nearest pe. For growable requests this will mean that
         # first growth is to fill the remainder of any unused extent.
-        self.base = int(lv.vg.align(lv.req_size, roundup=True) / lv.vg.peSize)
+        size = self.device.vg.align(self.device.req_size, roundup=True)
+        self.base = int(size / self.device.vg.peSize)
 
-        if lv.req_grow:
-            limits = [int(l / lv.vg.peSize) for l in
-                        [lv.vg.align(lv.req_max_size),
-                         lv.vg.align(lv.format.maxSize)] if l > 0]
+        if self.device.req_grow:
+            limits = [int(l / self.device.vg.peSize) for l in
+                        [self.device.vg.align(self.device.req_max_size),
+                         self.device.vg.align(self.device.format.maxSize)]
+                        if l > 0]
 
             if limits:
                 max_units = min(limits)
@@ -1308,7 +1305,6 @@ class LVRequest(Request):
                     # max size is less than or equal to base, so we're done
                     self.done = True
 
-
 class Chunk(object):
     """ A free region from which devices will be allocated """
     def __init__(self, length, requests=None):
@@ -1321,13 +1317,21 @@ class Chunk(object):
         if not hasattr(self, "path"):
             self.path = None
         self.length = length
-        self.pool = length                  # free unit count
+        self.requests = []                  # list of Request instances
+        self.reset(requests=requests)
+
+    def reset(self, requests=None):
+        requests = requests or []
+        self.pool = self.length             # free unit count
         self.base = 0                       # sum of growable requests' base
                                             # sizes
+
+        [r.reset() for r in self.requests]
+
         self.requests = []                  # list of Request instances
-        if isinstance(requests, list):
-            for req in requests:
-                self.addRequest(req)
+        for req in requests:
+            req.reset()
+            self.addRequest(req)
 
         self.skip_list = []
 
@@ -1560,7 +1564,7 @@ class DiskChunk(Chunk):
         return s
 
     def __str__(self):
-        s = "%d (%d-%d) on %s" % (self.length, self.geometry.start,
+        s = "%s / %d (%d-%d) on %s" % (self.lengthToSize(self.length), self.length, self.geometry.start,
                                   self.geometry.end, self.path)
         return s
 
-- 
1.9.3

