From c17cf6aff0ce0c3bc7dc824b5b82f219b0a37538 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 4 Oct 2012 17:25:57 -0400 Subject: [PATCH] cgpt: fix up "with open" handling There is no need to call .close() ourself in a with block. We can also use a with block in the WritePartitionScript func. BUG=None TEST=build_image still works Change-Id: I53b31ba96c94e885b1d4415889b5d2a9691ccda1 Reviewed-on: https://gerrit.chromium.org/gerrit/34707 Reviewed-by: Liam McLoughlin Tested-by: Liam McLoughlin Tested-by: Mike Frysinger Commit-Ready: Mike Frysinger --- build_library/cgpt.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/build_library/cgpt.py b/build_library/cgpt.py index 31ef360f35..850569bf72 100755 --- a/build_library/cgpt.py +++ b/build_library/cgpt.py @@ -32,7 +32,6 @@ def LoadPartitionConfig(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']) @@ -126,7 +125,6 @@ def GetScriptShell(): 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. @@ -222,14 +220,12 @@ def WritePartitionScript(image_type, layout_filename, sfilename): config = LoadPartitionConfig(layout_filename) - sfile = open(sfilename, 'w') - script_shell = GetScriptShell() - sfile.write(script_shell) + with open(sfilename, 'w') as f: + script_shell = GetScriptShell() + f.write(script_shell) - WriteLayoutFunction(sfile, 'write_base_table', 'base', config) - WriteLayoutFunction(sfile, 'write_partition_table', image_type, config) - - sfile.close() + WriteLayoutFunction(f, 'write_base_table', 'base', config) + WriteLayoutFunction(f, 'write_partition_table', image_type, config) def GetBlockSize(layout_filename):