fix(disk_util): Convert partition list to an object.

Hey, 'num' is a primary key! Lets use it and avoid pointless looping.
This commit is contained in:
Michael Marineau 2013-12-20 12:00:01 -08:00
parent 576c8996f4
commit 0f4b52134a
2 changed files with 47 additions and 65 deletions

View File

@ -60,17 +60,20 @@ def LoadPartitionConfig(options):
if len(config['layouts']) <= 0:
raise InvalidLayout('Missing "layouts" entries')
for layout_name, layout in config['layouts'].items():
for part in layout:
for layout_name, layout in config['layouts'].iteritems():
for part_num, part in layout.iteritems():
unknown_keys = set(part.keys()) - valid_layout_keys
if unknown_keys:
raise InvalidLayout('Unknown items in layout %s: %r' %
(layout_name, unknown_keys))
if part['type'] != 'blank':
for s in ('num', 'label'):
if not s in part:
raise InvalidLayout('Layout "%s" missing "%s"' % (layout_name, s))
part['num'] = int(part_num)
if part['type'] == 'blank':
continue
if not part.get('label', None):
raise InvalidLayout('Layout "%s" missing "label" in partition %s' %
(layout_name, part_num))
part['alignment'] = int(part.get('alignment', metadata['alignment']))
part['blocks'] = int(part['blocks'])
@ -111,18 +114,9 @@ def GetPartitionTable(options, config):
Object representing a selected partition table
"""
partitions = config['layouts']['base']
metadata = config['metadata']
image_type = options.disk_layout
if image_type != 'base':
for partition_t in config['layouts'][image_type]:
for partition in partitions:
if partition['type'] == 'blank' or partition_t['type'] == 'blank':
continue
if partition_t['num'] == partition['num']:
for k, v in partition_t.items():
partition[k] = v
partitions = config['layouts']['base'].copy()
for part_num, part in config['layouts'][options.disk_layout].iteritems():
partitions[part_num].update(part)
return partitions
@ -174,7 +168,7 @@ def WritePartitionTable(options):
sector = GPT_RESERVED_SECTORS
esp_number = None
for partition in partitions:
for partition in partitions.itervalues():
sector = Align(sector, partition['alignment'])
if partition['type'] != 'blank':
Cgpt('add', '-i', partition['num'],
@ -211,13 +205,12 @@ def GetPartitionByNumber(partitions, num):
Returns:
An object for the selected partition
"""
for partition in partitions:
if partition['type'] == 'blank':
continue
if partition['num'] == int(num):
return partition
raise PartitionNotFound('Partition not found')
partition = partitions.get(str(num), None)
if not partition or partition['type'] == 'blank':
raise PartitionNotFound('Partition not found')
return partition
def GetPartitionByLabel(partitions, label):
@ -229,10 +222,10 @@ def GetPartitionByLabel(partitions, label):
Returns:
An object for the selected partition
"""
for partition in partitions:
if 'label' not in partition:
for partition in partitions.itervalues():
if partition['type'] == 'blank':
continue
if partition['label'] == label:
elif partition['label'] == label:
return partition
raise PartitionNotFound('Partition not found')
@ -349,22 +342,22 @@ def DoDebugOutput(options):
"""
partitions = GetPartitionTableFromConfig(options)
for partition in partitions:
if partition['bytes'] < 1024 * 1024:
size = '%d bytes' % partition['bytes']
else:
size = '%d MB' % (partition['bytes'] / 1024 / 1024)
if 'label' in partition:
for num, partition in sorted(partitions.iteritems()):
if partition['type'] != 'blank':
if partition['bytes'] < 1024 * 1024:
size = '%d bytes' % partition['bytes']
else:
size = '%d MB' % (partition['bytes'] / 1024 / 1024)
if 'fs_bytes' in partition:
if partition['fs_bytes'] < 1024 * 1024:
fs_size = '%d bytes' % partition['fs_bytes']
else:
fs_size = '%d MB' % (partition['fs_bytes'] / 1024 / 1024)
print '%s - %s/%s' % (partition['label'], fs_size, size)
print '%s: %s - %s/%s' % (num, partition['label'], fs_size, size)
else:
print '%s - %s' % (partition['label'], size)
print '%s: %s - %s' % (num, partition['label'], size)
else:
print 'blank - %s' % size
print '%s: blank' % num
def DoParseOnly(options):

View File

@ -6,82 +6,71 @@
"fs_block_size": 4096
},
"layouts":{
"base":[
{
"num": 1,
"base":{
"1":{
"label":"EFI-SYSTEM",
"type":"efi",
"blocks":"262144"
},
{
"num": 2,
"2":{
"label":"BOOT-B",
"type":"coreos-reserved",
"blocks":"131072"
},
{
"num": 3,
"3":{
"label":"ROOT-A",
"uuid":"7130c94a-213a-4e5a-8e26-6cce9662f132",
"type":"coreos-rootfs",
"blocks":"2097152",
"fs_blocks":"262144"
},
{
"num": 4,
"4":{
"label":"ROOT-B",
"uuid":"e03dd35c-7c2d-4a47-b3fe-27f15780a57c",
"type":"coreos-rootfs",
"blocks":"2097152",
"fs_blocks":"262144"
},
{
"num": 5,
"5":{
"label":"ROOT-C",
"uuid":"d82521b4-07ac-4f1c-8840-ddefedc332f3",
"type":"blank",
"blocks":"0"
},
{
"num": 6,
"6":{
"label":"OEM",
"type":"data",
"blocks":"262144"
},
{
"num": 7,
"7":{
"type":"blank",
"label":"coreos-reserved",
"blocks":"0"
},
{
"num": 8,
"8":{
"type":"blank",
"label":"coreos-reserved",
"blocks":"0"
},
{
"num": 9,
"9":{
"label":"STATE",
"type":"data",
"blocks":"1048576"
}
],
"vm": [
{
"num": 9,
},
"vm":{
"9":{
"label":"STATE",
"type":"data",
"blocks":"6291456"
}
],
"vagrant": [
{
"num": 9,
},
"vagrant":{
"9":{
"label":"STATE",
"type":"data",
"blocks":"33587200"
}
]
}
}
}