Exemplo n.º 1
0
    def create_image(self):
        """ """
        device = Device(self.device_path)
        disk = Disk(device)

        if device.is_disk():
            mbr = Mbr(self.target_path)
            mbr.save_to_file(self.device_path)
            dlm = DiskLayoutManager(self.target_path)
            dlm.save_to_file(disk)
        
        partition_list = disk.get_valid_partitions()
        
        total_bytes = 0
        for p in partition_list:
            total_bytes += p.filesystem.get_used_size()
       
        total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))
        self._print_informations(total_bytes)

        information = Information(self.target_path)
        information.set_image_is_disk(device.is_disk())
        information.set_image_name(self.image_name)
        information.set_image_total_bytes(total_bytes)
        information.set_image_compressor_level(self.compressor_level)

        compressor = Compressor(self.compressor_level)
        progress = Progress(total_blocks)
        progress.start()
        for p in partition_list:
            number = p.get_number()
            uuid = p.filesystem.uuid()
            type = p.filesystem.type
            information.add_partition(number, uuid, type)

            p.filesystem.open_to_read()

            file_name = FILE_PATTERN % (self.image_name, number)
            with open(self.target_path + file_name, 'wb') as f:
                # TODO: Work with parallelism
                while True: # Ugly
                    data = p.filesystem.read(BLOCK_SIZE)
                    if not len(data):
                        break
                    cdata = compressor.compact(data)
                    f.write(cdata)
                    progress.increment(1)
            p.filesystem.close()

        swap = disk.get_swap_partition()
        if swap is not None:
            number = swap.get_number()
            uuid = swap.filesystem.uuid()
            type = swap.filesystem.type
            information.add_partition(number, uuid, type)

        information.save()
        progress.stop()
        print "completed."
Exemplo n.º 2
0
class CompressedWriter(GenericWriter):

    def __init__(self, path, compressor_level):
        GenericWriter.__init__(self, path)
        self.compressor = Compressor(compressor_level)

    def write(self, data):
        if not self._check_fd():
            data = self.compressor.compact(data)
            self._fd.write(data)
            return len(data)
        return 0