run cleanup steps in a finally block

This commit is contained in:
Soren I. Bjornstad 2021-08-27 12:51:47 -05:00
parent 4f44143331
commit 85b836144c

View File

@ -211,7 +211,9 @@ class BuildCommand(CliCommand):
print(f"tzk: Starting build of product '{args.product}'.") print(f"tzk: Starting build of product '{args.product}'.")
print(f"tzk: Found {len(steps)} build {numerize(len(steps), 'step')}.") print(f"tzk: Found {len(steps)} build {numerize(len(steps), 'step')}.")
failed = False
# For each build step... # For each build step...
try:
for idx, step in enumerate(steps, 1): for idx, step in enumerate(steps, 1):
# Explain what we're doing. Use first line of the builder's docstring # Explain what we're doing. Use first line of the builder's docstring
# as a summary, if present. # as a summary, if present.
@ -227,26 +229,32 @@ class BuildCommand(CliCommand):
continue continue
# Execute step and handle any errors. # Execute step and handle any errors.
try:
step() step()
except BuildError as e: except BuildError as e:
failed = True
print(f"tzk: ERROR: {str(e)}") print(f"tzk: ERROR: {str(e)}")
print(f"tzk: Build of product '{args.product}' failed on step {idx}, " print(f"tzk: Build of product '{args.product}' failed on step {idx}, "
f"backed by builder '{step.__name__}'.") f"backed by builder '{step.__name__}'. ")
sys.exit(1) print(f"tzk: Add '--skip-builder {step.__name__}' if you'd like "
f"to skip this step.")
except Exception: except Exception:
failed = True
print(f"tzk: Build of product '{args.product}' failed on step {idx}: " print(f"tzk: Build of product '{args.product}' failed on step {idx}: "
f"unhandled exception. " f"unhandled exception. "
f"The original error follows:") f"The original error follows:")
traceback.print_exc() traceback.print_exc()
sys.exit(1)
# TODO: This should run in a finally() block; leaving for now as it's convenient for development :) finally:
for idx, step in enumerate(steps, 1): for idx, step in enumerate(steps, 1):
if hasattr(step, 'cleaner'): if hasattr(step, 'cleaner'):
print(f"tzk: Running cleanup routine for step {idx}...") print(f"tzk: Running cleanup routine registered by step {idx}...")
step.cleaner() step.cleaner()
if failed:
sys.exit(1)
else:
print(f"tzk: Build of product '{args.product}' completed successfully.") print(f"tzk: Build of product '{args.product}' completed successfully.")