Commits

Rodrigo Bistolfi committed 98be70f

media.py: Added partitions() method to Disk(). Fix parted api changes

Comments (0)

Files changed (1)

vinstall/backend/media.py

 
     def __init__(self, device_path):
         """Initializes a Media object fro the device path."""
-	mntpoint = device_path.split("/")[2]
-	mntpoint =  "/mnt/" + mntpoint
+        mntpoint = device_path.split("/")[2]
+        mntpoint =  "/mnt/" + mntpoint
         if not os.path.exists(mntpoint):
             os.mkdir(mntpoint)
-	self.mountpoint = mntpoint
+        self.mountpoint = mntpoint
         self.device_path = device_path
         if self.is_mounted():
            for line in open("/etc/mtab"):
 
     def size(self, unit="GB"):
         """Return the partition size in the specified unit.  Defaults to GB"""
-        su = round(self._parted_partition.getLength(unit), 1)
+        su = round(self._parted_partition.getSize(unit), 1)
         return "%s %s"% (su, unit)
 
-
     def query_filesystem(self):
         """Retrieve the current filesystem type from the system."""
         try:
         """ Returns disk size, by default in GB, unit can be GB or MB
         This method is used internally when the disk is not initialized.
 
-	"""
+        """
         pow = 2 if unit == "MB" else 3
         cylinders,  heads,  sectors = self._device.hardwareGeometry
         sector_size = self._device.physicalSectorSize #self._device.sector_size
     def has_partition_table(self):
         """Return True if the disk has a partition table.  False otherwise
 
-	"""
+        """
         return self._disk is not None
 
+    def partitions(self):
+        """Return iterable with partitions from this disk
+
+        """
+        for part in self._disk.partitions:
+            if part.type in (parted.PARTITION_NORMAL,
+                             parted.PARTITION_LOGICAL,):
+                p = Partition(part.path)
+                p._parted_partition = part
+                yield p
+
+
     def size(self, unit="GB"):
         """ Returns disk size, by default in GB, unit can be GB or MB
 
         """
-        return self._device.getLength(unit)
-        #return self._device.getSize(unit)
+        # fix parted api change
+        try:
+            l = self._device.getSize(unit)
+        except AttributeError:
+            l = self._device.getLength(unit)
+        return l
 
     def path(self):
         """Return the node path for this device"""