mirror of
https://github.com/flatcar/scripts.git
synced 2026-05-05 12:16:41 +02:00
cgpt: touch up style
No functional changes here. BUG=None TEST=build_image worked Change-Id: I4dc7968c7417cefcb6576e9452dcf71847101c56 Reviewed-on: https://gerrit.chromium.org/gerrit/34706 Reviewed-by: Liam McLoughlin <lmcloughlin@chromium.org> Tested-by: Liam McLoughlin <lmcloughlin@chromium.org> Tested-by: Mike Frysinger <vapier@chromium.org> Commit-Ready: Mike Frysinger <vapier@chromium.org>
This commit is contained in:
parent
e824585f80
commit
624071a08b
@ -29,25 +29,25 @@ def LoadPartitionConfig(filename):
|
||||
"""
|
||||
|
||||
if not os.path.exists(filename):
|
||||
raise ConfigNotFound("Partition config %s was not found!" % filename)
|
||||
raise ConfigNotFound('Partition config %s was not found!' % filename)
|
||||
with open(filename) as f:
|
||||
config = json.load(f)
|
||||
f.close()
|
||||
|
||||
metadata = config["metadata"]
|
||||
metadata["block_size"] = int(metadata["block_size"])
|
||||
metadata = config['metadata']
|
||||
metadata['block_size'] = int(metadata['block_size'])
|
||||
|
||||
for layout_name, layout in config["layouts"].items():
|
||||
for layout_name, layout in config['layouts'].items():
|
||||
for part in layout:
|
||||
part["blocks"] = int(part["blocks"])
|
||||
part["bytes"] = part["blocks"] * metadata["block_size"]
|
||||
part['blocks'] = int(part['blocks'])
|
||||
part['bytes'] = part['blocks'] * metadata['block_size']
|
||||
|
||||
if "fs_blocks" in part:
|
||||
part["fs_blocks"] = int(part["fs_blocks"])
|
||||
part["fs_bytes"] = part["fs_blocks"] * metadata["fs_block_size"]
|
||||
if 'fs_blocks' in part:
|
||||
part['fs_blocks'] = int(part['fs_blocks'])
|
||||
part['fs_bytes'] = part['fs_blocks'] * metadata['fs_block_size']
|
||||
|
||||
if part["fs_bytes"] > part["bytes"]:
|
||||
raise InvalidLayout("Filesystem may not be larger than partition")
|
||||
if part['fs_bytes'] > part['bytes']:
|
||||
raise InvalidLayout('Filesystem may not be larger than partition')
|
||||
|
||||
return config
|
||||
|
||||
@ -63,28 +63,28 @@ def GetTableTotals(config, partitions):
|
||||
"""
|
||||
|
||||
ret = {
|
||||
"expand_count": 0,
|
||||
"expand_min": 0,
|
||||
"block_count": START_SECTOR * config["metadata"]["block_size"]
|
||||
'expand_count': 0,
|
||||
'expand_min': 0,
|
||||
'block_count': START_SECTOR * config['metadata']['block_size']
|
||||
}
|
||||
|
||||
# Total up the size of all non-expanding partitions to get the minimum
|
||||
# required disk size.
|
||||
for partition in partitions:
|
||||
if "features" in partition and "expand" in partition["features"]:
|
||||
ret["expand_count"] += 1
|
||||
ret["expand_min"] += partition["blocks"]
|
||||
if 'features' in partition and 'expand' in partition['features']:
|
||||
ret['expand_count'] += 1
|
||||
ret['expand_min'] += partition['blocks']
|
||||
else:
|
||||
ret["block_count"] += partition["blocks"]
|
||||
ret['block_count'] += partition['blocks']
|
||||
|
||||
# At present, only one expanding partition is permitted.
|
||||
# Whilst it'd be possible to have two, we don't need this yet
|
||||
# and it complicates things, so it's been left out for now.
|
||||
if ret["expand_count"] > 1:
|
||||
raise InvalidLayout("1 expand partition allowed, %d requested"
|
||||
% ret["expand_count"])
|
||||
if ret['expand_count'] > 1:
|
||||
raise InvalidLayout('1 expand partition allowed, %d requested'
|
||||
% ret['expand_count'])
|
||||
|
||||
ret["min_disk_size"] = ret["block_count"] + ret["expand_min"]
|
||||
ret['min_disk_size'] = ret['block_count'] + ret['expand_min']
|
||||
|
||||
return ret
|
||||
|
||||
@ -102,14 +102,14 @@ def GetPartitionTable(config, image_type):
|
||||
Object representing a selected partition table
|
||||
"""
|
||||
|
||||
partitions = config["layouts"]["base"]
|
||||
partitions = config['layouts']['base']
|
||||
|
||||
if image_type != "base":
|
||||
for partition_t in config["layouts"][image_type]:
|
||||
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":
|
||||
if partition['type'] == 'blank' or partition_t['type'] == 'blank':
|
||||
continue
|
||||
if partition_t["num"] == partition["num"]:
|
||||
if partition_t['num'] == partition['num']:
|
||||
for k, v in partition_t.items():
|
||||
partition[k] = v
|
||||
|
||||
@ -123,14 +123,14 @@ def GetScriptShell():
|
||||
A string containg the skeleton script
|
||||
"""
|
||||
|
||||
script_shell_path = os.path.join(os.path.dirname(__file__), "cgpt_shell.sh")
|
||||
with open(script_shell_path, "r") as f:
|
||||
script_shell_path = os.path.join(os.path.dirname(__file__), 'cgpt_shell.sh')
|
||||
with open(script_shell_path, 'r') as f:
|
||||
script_shell = "".join(f.readlines())
|
||||
f.close()
|
||||
|
||||
# Before we return, insert the path to this tool so somebody reading the
|
||||
# script later can tell where it was generated.
|
||||
script_shell = script_shell.replace("@SCRIPT_GENERATOR@", script_shell_path)
|
||||
script_shell = script_shell.replace('@SCRIPT_GENERATOR@', script_shell_path)
|
||||
|
||||
return script_shell
|
||||
|
||||
@ -148,50 +148,49 @@ def WriteLayoutFunction(sfile, func_name, image_type, config):
|
||||
partitions = GetPartitionTable(config, image_type)
|
||||
partition_totals = GetTableTotals(config, partitions)
|
||||
|
||||
sfile.write("%s() {\ncreate_image $1 %d %s\n" % (
|
||||
func_name, partition_totals["min_disk_size"],
|
||||
config["metadata"]["block_size"]))
|
||||
sfile.write('%s() {\ncreate_image $1 %d %s\n' % (
|
||||
func_name, partition_totals['min_disk_size'],
|
||||
config['metadata']['block_size']))
|
||||
|
||||
sfile.write("CURR=%d\n" % START_SECTOR)
|
||||
sfile.write("$GPT create $1\n")
|
||||
sfile.write('CURR=%d\n' % START_SECTOR)
|
||||
sfile.write('$GPT create $1\n')
|
||||
|
||||
# Pass 1: Set up the expanding partition size.
|
||||
for partition in partitions:
|
||||
partition["var"] = partition["blocks"]
|
||||
if partition["type"] != "blank":
|
||||
partition['var'] = partition['blocks']
|
||||
if partition['type'] != 'blank':
|
||||
|
||||
if partition["num"] == 1:
|
||||
if "features" in partition and "expand" in partition["features"]:
|
||||
sfile.write("if [ -b $1 ]; then\n")
|
||||
sfile.write("STATEFUL_SIZE=$(( $(numsectors $1) - %d))\n" %
|
||||
partition_totals["block_count"])
|
||||
sfile.write("else\n")
|
||||
sfile.write("STATEFUL_SIZE=%s\n" % partition["blocks"])
|
||||
sfile.write("fi\n")
|
||||
partition["var"] = "$STATEFUL_SIZE"
|
||||
if partition['num'] == 1:
|
||||
if 'features' in partition and 'expand' in partition['features']:
|
||||
sfile.write('if [ -b $1 ]; then\n')
|
||||
sfile.write('STATEFUL_SIZE=$(( $(numsectors $1) - %d))\n' %
|
||||
partition_totals['block_count'])
|
||||
sfile.write('else\n')
|
||||
sfile.write('STATEFUL_SIZE=%s\n' % partition['blocks'])
|
||||
sfile.write('fi\n')
|
||||
partition['var'] = '$STATEFUL_SIZE'
|
||||
|
||||
|
||||
sfile.write("STATEFUL_SIZE=$((STATEFUL_SIZE-(STATEFUL_SIZE %% %d)))\n" %
|
||||
config["metadata"]["fs_block_size"])
|
||||
sfile.write('STATEFUL_SIZE=$((STATEFUL_SIZE-(STATEFUL_SIZE %% %d)))\n' %
|
||||
config['metadata']['fs_block_size'])
|
||||
|
||||
# Pass 2: Write out all the cgpt add commands.
|
||||
for partition in partitions:
|
||||
if partition["type"] != "blank":
|
||||
sfile.write("$GPT add -i %d -b $CURR -s %s -t %s -l %s $1 && " % (
|
||||
partition["num"], str(partition["var"]), partition["type"],
|
||||
partition["label"]))
|
||||
if partition['type'] != 'blank':
|
||||
sfile.write('$GPT add -i %d -b $CURR -s %s -t %s -l %s $1 && ' % (
|
||||
partition['num'], str(partition['var']), partition['type'],
|
||||
partition['label']))
|
||||
|
||||
# Increment the CURR counter ready for the next partition.
|
||||
sfile.write("CURR=$(( $CURR + %s ))\n" % partition["var"])
|
||||
sfile.write('CURR=$(( $CURR + %s ))\n' % partition['var'])
|
||||
|
||||
# Set default priorities on kernel partitions
|
||||
sfile.write("$GPT add -i 2 -S 0 -T 15 -P 15 $1\n")
|
||||
sfile.write("$GPT add -i 4 -S 0 -T 15 -P 0 $1\n")
|
||||
sfile.write("$GPT add -i 6 -S 0 -T 15 -P 0 $1\n")
|
||||
sfile.write('$GPT add -i 2 -S 0 -T 15 -P 15 $1\n')
|
||||
sfile.write('$GPT add -i 4 -S 0 -T 15 -P 0 $1\n')
|
||||
sfile.write('$GPT add -i 6 -S 0 -T 15 -P 0 $1\n')
|
||||
|
||||
sfile.write("$GPT boot -p -b $2 -i 12 $1\n")
|
||||
sfile.write("$GPT show $1\n")
|
||||
sfile.write("}\n")
|
||||
sfile.write('$GPT boot -p -b $2 -i 12 $1\n')
|
||||
sfile.write('$GPT show $1\n')
|
||||
sfile.write('}\n')
|
||||
|
||||
|
||||
def GetPartitionByNumber(partitions, num):
|
||||
@ -204,12 +203,12 @@ def GetPartitionByNumber(partitions, num):
|
||||
An object for the selected partition
|
||||
"""
|
||||
for partition in partitions:
|
||||
if partition["type"] == "blank":
|
||||
if partition['type'] == 'blank':
|
||||
continue
|
||||
if partition["num"] == int(num):
|
||||
if partition['num'] == int(num):
|
||||
return partition
|
||||
|
||||
raise PartitionNotFound("Partition not found")
|
||||
raise PartitionNotFound('Partition not found')
|
||||
|
||||
|
||||
def WritePartitionScript(image_type, layout_filename, sfilename):
|
||||
@ -223,12 +222,12 @@ def WritePartitionScript(image_type, layout_filename, sfilename):
|
||||
|
||||
config = LoadPartitionConfig(layout_filename)
|
||||
|
||||
sfile = open(sfilename, "w")
|
||||
sfile = open(sfilename, 'w')
|
||||
script_shell = GetScriptShell()
|
||||
sfile.write(script_shell)
|
||||
|
||||
WriteLayoutFunction(sfile, "write_base_table", "base", config)
|
||||
WriteLayoutFunction(sfile, "write_partition_table", image_type, config)
|
||||
WriteLayoutFunction(sfile, 'write_base_table', 'base', config)
|
||||
WriteLayoutFunction(sfile, 'write_partition_table', image_type, config)
|
||||
|
||||
sfile.close()
|
||||
|
||||
@ -243,7 +242,7 @@ def GetBlockSize(layout_filename):
|
||||
"""
|
||||
|
||||
config = LoadPartitionConfig(layout_filename)
|
||||
return config["metadata"]["block_size"]
|
||||
return config['metadata']['block_size']
|
||||
|
||||
|
||||
def GetFilesystemBlockSize(layout_filename):
|
||||
@ -258,7 +257,7 @@ def GetFilesystemBlockSize(layout_filename):
|
||||
"""
|
||||
|
||||
config = LoadPartitionConfig(layout_filename)
|
||||
return config["metadata"]["fs_block_size"]
|
||||
return config['metadata']['fs_block_size']
|
||||
|
||||
|
||||
def GetPartitionSize(image_type, layout_filename, num):
|
||||
@ -276,7 +275,7 @@ def GetPartitionSize(image_type, layout_filename, num):
|
||||
partitions = GetPartitionTable(config, image_type)
|
||||
partition = GetPartitionByNumber(partitions, num)
|
||||
|
||||
return partition["bytes"]
|
||||
return partition['bytes']
|
||||
|
||||
|
||||
def GetFilesystemSize(image_type, layout_filename, num):
|
||||
@ -296,10 +295,10 @@ def GetFilesystemSize(image_type, layout_filename, num):
|
||||
partitions = GetPartitionTable(config, image_type)
|
||||
partition = GetPartitionByNumber(partitions, num)
|
||||
|
||||
if "fs_bytes" in partition:
|
||||
return partition["fs_bytes"]
|
||||
if 'fs_bytes' in partition:
|
||||
return partition['fs_bytes']
|
||||
else:
|
||||
return partition["bytes"]
|
||||
return partition['bytes']
|
||||
|
||||
|
||||
def GetLabel(image_type, layout_filename, num):
|
||||
@ -310,17 +309,17 @@ def GetLabel(image_type, layout_filename, num):
|
||||
layout_filename: Path to partition configuration file
|
||||
num: Number of the partition you want to read from
|
||||
Returns:
|
||||
Label of selected partition, or "UNTITLED" if none specified
|
||||
Label of selected partition, or 'UNTITLED' if none specified
|
||||
"""
|
||||
|
||||
config = LoadPartitionConfig(layout_filename)
|
||||
partitions = GetPartitionTable(config, image_type)
|
||||
partition = GetPartitionByNumber(partitions, num)
|
||||
|
||||
if "label" in partition:
|
||||
return partition["label"]
|
||||
if 'label' in partition:
|
||||
return partition['label']
|
||||
else:
|
||||
return "UNTITLED"
|
||||
return 'UNTITLED'
|
||||
|
||||
|
||||
def DoDebugOutput(image_type, layout_filename):
|
||||
@ -337,75 +336,76 @@ def DoDebugOutput(image_type, layout_filename):
|
||||
partitions = GetPartitionTable(config, image_type)
|
||||
|
||||
for partition in partitions:
|
||||
if partition["bytes"] < 1024 * 1024:
|
||||
size = "%d bytes" % partition["bytes"]
|
||||
if partition['bytes'] < 1024 * 1024:
|
||||
size = '%d bytes' % partition['bytes']
|
||||
else:
|
||||
size = "%d MB" % (partition["bytes"] / 1024 / 1024)
|
||||
if "label" in partition:
|
||||
if "fs_bytes" in partition:
|
||||
if partition["fs_bytes"] < 1024 * 1024:
|
||||
fs_size = "%d bytes" % partition["fs_bytes"]
|
||||
size = '%d MB' % (partition['bytes'] / 1024 / 1024)
|
||||
if 'label' in partition:
|
||||
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)
|
||||
fs_size = '%d MB' % (partition['fs_bytes'] / 1024 / 1024)
|
||||
print '%s - %s/%s' % (partition['label'], fs_size, size)
|
||||
else:
|
||||
print "%s - %s" % (partition["label"], size)
|
||||
print '%s - %s' % (partition['label'], size)
|
||||
else:
|
||||
print "blank - %s" % size
|
||||
print 'blank - %s' % size
|
||||
|
||||
|
||||
def main(argv):
|
||||
action_map = {
|
||||
"write": {
|
||||
"argc": 4,
|
||||
"usage": "<image_type> <partition_config_file> <script_file>",
|
||||
"func": WritePartitionScript
|
||||
'write': {
|
||||
'argc': 4,
|
||||
'usage': '<image_type> <partition_config_file> <script_file>',
|
||||
'func': WritePartitionScript,
|
||||
},
|
||||
"readblocksize": {
|
||||
"argc": 2,
|
||||
"usage": "<partition_config_file>",
|
||||
"func": GetBlockSize
|
||||
'readblocksize': {
|
||||
'argc': 2,
|
||||
'usage': '<partition_config_file>',
|
||||
'func': GetBlockSize,
|
||||
},
|
||||
"readfsblocksize": {
|
||||
"argc": 2,
|
||||
"usage": "<partition_config_file>",
|
||||
"func": GetFilesystemBlockSize
|
||||
'readfsblocksize': {
|
||||
'argc': 2,
|
||||
'usage': '<partition_config_file>',
|
||||
'func': GetFilesystemBlockSize,
|
||||
},
|
||||
"readpartsize": {
|
||||
"argc": 4,
|
||||
"usage": "<image_type> <partition_config_file> <partition_num>",
|
||||
"func": GetPartitionSize
|
||||
'readpartsize': {
|
||||
'argc': 4,
|
||||
'usage': '<image_type> <partition_config_file> <partition_num>',
|
||||
'func': GetPartitionSize,
|
||||
},
|
||||
"readfssize": {
|
||||
"argc": 4,
|
||||
"usage": "<image_type> <partition_config_file> <partition_num>",
|
||||
"func": GetFilesystemSize
|
||||
'readfssize': {
|
||||
'argc': 4,
|
||||
'usage': '<image_type> <partition_config_file> <partition_num>',
|
||||
'func': GetFilesystemSize,
|
||||
},
|
||||
"readlabel": {
|
||||
"argc": 4,
|
||||
"usage": "<image_type> <partition_config_file> <partition_num>",
|
||||
"func": GetLabel
|
||||
'readlabel': {
|
||||
'argc': 4,
|
||||
'usage': '<image_type> <partition_config_file> <partition_num>',
|
||||
'func': GetLabel,
|
||||
},
|
||||
"debug": {
|
||||
"argc": 3,
|
||||
"usage": "<image_type> <partition_config_file>",
|
||||
"func": DoDebugOutput
|
||||
'debug': {
|
||||
'argc': 3,
|
||||
'usage': '<image_type> <partition_config_file>',
|
||||
'func': DoDebugOutput,
|
||||
}
|
||||
}
|
||||
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in action_map:
|
||||
print "Usage: %s <action>\n" % sys.argv[0]
|
||||
print "Valid actions are:"
|
||||
print 'Usage: %s <action>\n' % sys.argv[0]
|
||||
print 'Valid actions are:'
|
||||
for action in action_map:
|
||||
print " %s %s" % (action, action_map[action]["usage"])
|
||||
print ' %s %s' % (action, action_map[action]['usage'])
|
||||
sys.exit(1)
|
||||
else:
|
||||
action_name = sys.argv[1]
|
||||
action = action_map[action_name]
|
||||
if action["argc"] == len(sys.argv) - 1:
|
||||
print action["func"](*sys.argv[2:])
|
||||
if action['argc'] == len(sys.argv) - 1:
|
||||
print action['func'](*sys.argv[2:])
|
||||
else:
|
||||
sys.exit("Usage: %s %s %s" % (sys.argv[0], sys.argv[1], action["usage"]))
|
||||
sys.exit('Usage: %s %s %s' % (sys.argv[0], sys.argv[1], action['usage']))
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user