From 41a0aad78dddcb43813f5cdea61ee6b8bbc51433 Mon Sep 17 00:00:00 2001 From: Aditya Deshpande Date: Thu, 13 Apr 2023 16:32:21 +0100 Subject: [PATCH] code_size_compare.py: clean up worktree in errors of subprocess With this change, the program cleans up worktree in errors of execution of subprocess.checkout. Additionally, the error log is printed out for users. Signed-off-by: Aditya Deshpande Signed-off-by: Yanray Wang --- scripts/code_size_compare.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/scripts/code_size_compare.py b/scripts/code_size_compare.py index 5b93b1a30f..0145349ae3 100755 --- a/scripts/code_size_compare.py +++ b/scripts/code_size_compare.py @@ -141,16 +141,20 @@ class CodeSizeComparison: git_worktree_path, revision], cwd=self.repo_path, stderr=subprocess.STDOUT ) + return git_worktree_path def _build_libraries(self, git_worktree_path): """Build libraries in the specified worktree.""" my_environment = os.environ.copy() - subprocess.check_output( - self.make_command, env=my_environment, shell=True, - cwd=git_worktree_path, stderr=subprocess.STDOUT, - ) + try: + subprocess.check_output( + self.make_command, env=my_environment, shell=True, + cwd=git_worktree_path, stderr=subprocess.STDOUT, + ) + except subprocess.CalledProcessError as e: + self._handle_called_process_error(e, git_worktree_path) def _gen_code_size_csv(self, revision, git_worktree_path): """Generate code size csv file.""" @@ -241,6 +245,20 @@ class CodeSizeComparison: self._get_code_size_for_rev(self.new_rev) return self.compare_code_size() + def _handle_called_process_error(self, e: subprocess.CalledProcessError, + git_worktree_path): + """Handle a CalledProcessError and quit the program gracefully. + Remove any extra worktrees so that the script may be called again.""" + + # Tell the user what went wrong + print("The following command: {} failed and exited with code {}" + .format(e.cmd, e.returncode)) + print("Process output:\n {}".format(str(e.output, "utf-8"))) + + # Quit gracefully by removing the existing worktree + self._remove_worktree(git_worktree_path) + sys.exit(-1) + def main(): parser = argparse.ArgumentParser( description=(