Skip to content

Commit

Permalink
Clean up the worktree and exit gracefully if any pre-build or make co…
Browse files Browse the repository at this point in the history
…mmands fail.

Without this, the script will fail to create a worktree when re-run due to the existence of the worktree from the previous run.

Signed-off-by: Aditya Deshpande <[email protected]>
  • Loading branch information
aditya-deshpande-arm committed Apr 25, 2023
1 parent 7b04013 commit 5439b4c
Showing 1 changed file with 52 additions and 21 deletions.
73 changes: 52 additions & 21 deletions scripts/code_size_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,36 @@ def _create_git_worktree(self, revision):
else:
print("Creating git worktree for", revision)
git_worktree_path = os.path.join(self.repo_path, "temp-" + revision)
subprocess.check_output(
[self.git_command, "worktree", "add", "--detach",
git_worktree_path, revision], cwd=self.repo_path,
stderr=subprocess.STDOUT
)
try:
subprocess.check_output(
[self.git_command, "worktree", "add", "--detach",
git_worktree_path, revision], cwd=self.repo_path,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
self._handle_CalledProcessError(e,git_worktree_path)

return git_worktree_path

def _build_libraries(self, git_worktree_path):
"""Build libraries in the specified worktree."""

my_environment = os.environ.copy()
if self.pre_build_commands != '':
try:
subprocess.check_output(
self.pre_build_commands, env=my_environment, shell=True,
cwd=git_worktree_path, stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e:
self._handle_CalledProcessError(e,git_worktree_path)
try:
subprocess.check_output(
self.pre_build_commands, env=my_environment, shell=True,
self.make_command, env=my_environment, shell=True,
cwd=git_worktree_path, stderr=subprocess.STDOUT,
)

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_CalledProcessError(e,git_worktree_path)

def _gen_code_size_report(self, revision, git_worktree_path):
"""Generate a code size report for each executable and store them
Expand All @@ -194,19 +203,28 @@ def _gen_code_size_report(self, revision, git_worktree_path):
print("Measuring code size for", revision)

# Size for libmbedcrypto.a
result = subprocess.check_output(
["size -t library/libmbedcrypto.a"], cwd=git_worktree_path, shell=True
)
try:
result = subprocess.check_output(
["size -t library/libmbedcrypto.a"], cwd=git_worktree_path, shell=True
)
except subprocess.CalledProcessError as e:
self._handle_CalledProcessError(e,git_worktree_path)
crypto_text = result.decode()
# Size for libmbedx509.a
result = subprocess.check_output(
["size -t library/libmbedx509.a"], cwd=git_worktree_path, shell=True
)
try:
result = subprocess.check_output(
["size -t library/libmbedx509.a"], cwd=git_worktree_path, shell=True
)
except subprocess.CalledProcessError as e:
self._handle_CalledProcessError(e,git_worktree_path)
x509_text = result.decode()
# Size for libmbedtls.a
result = subprocess.check_output(
["size -t library/libmbedtls.a"], cwd=git_worktree_path, shell=True
)
try:
result = subprocess.check_output(
["size -t library/libmbedtls.a"], cwd=git_worktree_path, shell=True
)
except subprocess.CalledProcessError as e:
self._handle_CalledProcessError(e,git_worktree_path)
tls_text = result.decode()

def size_text_to_dict(txt):
Expand Down Expand Up @@ -320,6 +338,19 @@ def get_comparision_results(self):
self._get_code_size_for_rev(self.new_rev)
return self.compare_code_size()

def _handle_CalledProcessError(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(e.output))

# Quit gracefully by removing the existing worktree
self._remove_worktree(git_worktree_path)
sys.exit(-1)

def main():
parser = argparse.ArgumentParser(
description=(
Expand Down

0 comments on commit 5439b4c

Please sign in to comment.