def runTest(self):
     self.assertRaises(SyntaxError, parted.formatBytes, 57, "GIB")
     self.assertEqual(1e-24, parted.formatBytes(1, "YB"))
     self.assertEqual(1/2**80, parted.formatBytes(1, "YiB"))
     self.assertEqual(1, parted.formatBytes(1, 'B'))
     self.assertEqual(1, parted.formatBytes(1e24, 'YB'))
     self.assertEqual(1, parted.formatBytes(2**80, 'YiB'))
Exemple #2
0
 def runTest(self):
     self.assertRaises(SyntaxError, parted.formatBytes, 57, "GIB")
     self.assertEqual(1e-24, parted.formatBytes(1, "YB"))
     self.assertEqual(1 / 2**80, parted.formatBytes(1, "YiB"))
     self.assertEqual(1, parted.formatBytes(1, 'B'))
     self.assertEqual(1, parted.formatBytes(1e24, 'YB'))
     self.assertEqual(1, parted.formatBytes(2**80, 'YiB'))
Exemple #3
0
    def print_partitions(self):
        """print the partition table"""
        # Shortcuts
        device, disk = self.device, self.disk
        unit = device.sectorSize
        size = device.length * device.sectorSize
        cylinders, heads, sectors = device.hardwareGeometry
        data = {
            'path': device.path,
            'size': size,
            'size_mbytes': int(parted.formatBytes(size, 'MB')),
            'heads': heads,
            'sectors': sectors,
            'cylinders': cylinders,
            'sectors_total': device.length,
            'unit': unit,
            'sector_size': device.sectorSize,
            'physical_sector_size': device.physicalSectorSize,
            # Try to guess minimum_io_size and optimal_io_size, should work under Linux
            'minimum_io_size': device.minimumAlignment.grainSize * device.sectorSize,
            'optimal_io_size': device.optimumAlignment.grainSize * device.sectorSize,
        }

        # TODO: Alignment offset: disk.startAlignment if disk.startAlignment != 0
        print """
Disk {path}: {size_mbytes:d} MB, {size:d} bytes
{heads:d} heads, {sectors:d} sectors/track, {cylinders:d} cylinders, total {sectors_total:d} sectors
Units = 1 * sectors of {unit:d} = {unit:d} bytes
Sector size (logical/physical): {sector_size:d} bytes / {physical_sector_size:d} bytes
I/O size (minimum/optimal): {minimum_io_size:d} bytes / {optimal_io_size:d} bytes
""".format(**data)

        # Calculate first column width: if there is something in it, there should be enough space
        # If not, give it the minimum to fit the caption nicely
        width = len(disk.partitions[0].path) if disk.partitions else len('Device') + 1
        print "{0:>{width}} Boot      Start         End      Blocks   Id  System".format('Device', width=width)

        for p in disk.partitions:
            boot = '*' if p.getFlag(parted.PARTITION_BOOT) else ''
            # Assume default 1K-blocks
            blocks = int(parted.formatBytes(p.geometry.length * device.sectorSize, 'KiB'))
            data = {
                'path': p.path,
                'boot': boot,
                'start': p.geometry.start,
                'end': p.geometry.end,
                'blocks': blocks,
                'id': p.number,
                'system': self._guess_system(p),
            }
            print "{path:>}{boot:>4}{start:>12d}{end:>12d}{blocks:>12d}{id:>5d}  {system}".format(**data)

        return device, disk
Exemple #4
0
 def getLength(self, unit='sectors'):
     """Return the length of the geometry in sectors. Optionally, a SI or
        IEC prefix followed by a 'B' may be given in order to convert the
        length into bytes. The allowed values include B, kB, MB, GB, TB, KiB,
        MiB, GiB, and TiB."""
     sectors = self.length
     if unit == "sectors":
         return sectors
     return parted.formatBytes(sectors * self.device.sectorSize, unit)
Exemple #5
0
 def _format_partition(self, p):
     size_kib = int(parted.formatBytes(
         p.geometry.length * self.device.sectorSize, 'KiB'))
     return {
         'path': p.path,
         'boot': p.getFlag(parted.PARTITION_BOOT),
         'raid': p.getFlag(parted.PARTITION_RAID),
         'start': p.geometry.start,
         'end': p.geometry.end,
         'blocks': size_kib,
         'id': p.number,
         'system': self._guess_system(p),
         'system_type': p.fileSystem.type if p.fileSystem else None,
         'size': humanize_sizeof(p.geometry.length * self.device.sectorSize),
         #'system_raw': p.fileSystem
     }
def print_disks_to_json_format(disks,free):
    """Return the stat of disks given with json."""
    data = []
    devpaths = disks.keys()
    for devpath in devpaths:
        dev = parted.getDevice(devpath)
        disksize = parted.formatBytes(dev.getLength()*512,'GB')
        dev_data = {
            "model": dev.model,
            "path":  dev.path,
            "size": disksize,
            "type": "unknow",
            "unit": 'GB',
            "table": [],
            }
        if(disks[devpath]):
            disk = disks[devpath]
            dev_data["type"] = disk.type
            dev_data["table"] = print_disk_to_json_format(disk,free)
        data.append(dev_data)

    return json.dumps(data)