From f3fc62c8a0b35e9aad5538bc8eb10cdc6db334b4 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 25 Oct 2018 12:23:12 -0700 Subject: [PATCH] releasetools: Fix the error message. This avoids showing a message as below: raise BuildImageError("Failed to get tune2fs usage:\n{}".format(output)) UnboundLocalError: local variable 'output' referenced before assignment The `output` won't be set under such a case, which unfortunately hides the real cause. This CL addresses the issue by letting it throw implicitly, which contains the command and outputs. Test: `m dist` Test: `python -m unittest test_build_image` Change-Id: Icf015bab8869d150516246ed73552f6502127cdf --- tools/releasetools/build_image.py | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py index d7318427b..b88171f28 100755 --- a/tools/releasetools/build_image.py +++ b/tools/releasetools/build_image.py @@ -58,15 +58,9 @@ def GetDiskUsage(path): Returns: The number of bytes based on a 1K block_size. - - Raises: - BuildImageError: On error. """ cmd = ["du", "-k", "-s", path] - try: - output = common.RunAndCheckOutput(cmd, verbose=False) - except common.ExternalError: - raise BuildImageError("Failed to get disk usage:\n{}".format(output)) + output = common.RunAndCheckOutput(cmd, verbose=False) return int(output.split()[0]) * 1024 @@ -78,15 +72,9 @@ def GetInodeUsage(path): Returns: The number of inodes used. - - Raises: - BuildImageError: On error. """ cmd = ["find", path, "-print"] - try: - output = common.RunAndCheckOutput(cmd, verbose=False) - except common.ExternalError: - raise BuildImageError("Failed to get disk inode usage:\n{}".format(output)) + output = common.RunAndCheckOutput(cmd, verbose=False) # increase by > 4% as number of files and directories is not whole picture. return output.count('\n') * 25 // 24 @@ -99,19 +87,15 @@ def GetFilesystemCharacteristics(sparse_image_path): Returns: The characteristics dictionary. - - Raises: - BuildImageError: On error. """ unsparse_image_path = UnsparseImage(sparse_image_path, replace=False) cmd = ["tune2fs", "-l", unsparse_image_path] try: output = common.RunAndCheckOutput(cmd, verbose=False) - except common.ExternalError: - raise BuildImageError("Failed to get tune2fs usage:\n{}".format(output)) - os.remove(unsparse_image_path) - fs_dict = { } + finally: + os.remove(unsparse_image_path) + fs_dict = {} for line in output.splitlines(): fields = line.split(":") if len(fields) == 2: