vinstall / vinstall / backend /

# -*- coding: utf8 -*-

#    This file is part of vinstall.
#    vinstall is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#    vinstall is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with vinstall.  If not, see <>.

""" Convinience methods for working with partitions for the vectorlinux installer


import utils
import parted

__author__ = "Moises Henriquez"
__email__ = "moc.liamg@xnl.e0m"[::-1]
__version__ = "0.1"
__date__ = "2012-06-05"

class DiskPartitioner(object):
    """ Class used to modify the partition table on the given disk

    def __init__(self, disk):
        """ Arguments:

                disk - a media.Disk object

        #self.disk = disk._disk
        self.disk = disk

    def create_partition_table(self, table_type="msdos"):
        """ Create the partition table on the disk.
        This is only necessary when using a fresh disk that has
        never been partitioned before.

        assert self.disk._disk is None, "Partition table already exists?"
#        assert self.disk._disk._has_partition_table is False, "Partition table already exists?"
#        assert not hasattr(self.disk, "_disk"), ".disk attribute already here.  Do we have a partition table already?"
#        assert self.disk._disk is not None, "We already have .disk attribute.  Do we have a partition table already?"
#        assert self.disk.type is None, "Disk already has a partition table"
        self.disk._disk = parted.freshDisk(self.disk._device, "msdos")
        self.disk._disk._has_partition_table = True

    def delete_all_partitions(self):
        """ Delete all partitions from drive

        assert self.disk._has_partition_table is True, "No partition table on disk."

    def add_partition(self,  size=0, units='MiB'):
        """Add a partition to the disk.  
        Args:  size = partition size
               units = partition units (MB, GB, MiB, GiB), (defaults to MiB)

        assert self.disk.has_partition_table is False, "No partition table on disk.  Cannot add partitions"
        mypsize = parted.sizeToSectors(size, units, self.disk._device.sectorSize)
        # first partition starts at sector 1
        # find out where the free space is
        myconst = parted.Constraint(device = self.disk._device)
        for i in self.disk._disk.getFreeSpaceRegions():
            if i > mypsize:  # partition fits here
                start_marker = i.start
        print "Creating new partition @ block %s and extending to %s"% (start_marker, mypsize)
        mygeom = parted.Geometry(device = self.disk._device,
                                 start = start_marker,
                                 length = mypsize)
        myfs = parted.FileSystem(type='ext2', geometry=mygeom)
        mypartition = parted.Partition(disk=self.disk._disk,
                                       fs = myfs,
                                       geometry = mygeom)
        myconst = parted.Constraint(exactGeom = mygeom)
        # Add the partition to the disk
        self.disk._disk.addPartition(partition = mypartition,
                                     constraint = myconst)


        # OLD CODE ----
        if self.disk.type_name is None:

        psize = reparted.Size(size,  units)
        partition = reparted.Partition(self.disk,  size=psize)

    def write_changes(self):
        """Finalize changes to the disk.  This needs to be called
        after creating partitions or deleting partitions to make sure
        the changes are actually written to the disk."""