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,43 +211,51 @@ 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...
for idx, step in enumerate(steps, 1): try:
# Explain what we're doing. Use first line of the builder's docstring for idx, step in enumerate(steps, 1):
# as a summary, if present. # Explain what we're doing. Use first line of the builder's docstring
if hasattr(step, '__doc__'): # as a summary, if present.
short_description = step.__doc__.strip().split('\n')[0].rstrip('.') if hasattr(step, '__doc__'):
print(f"tzk: Step {idx}/{len(steps)}: {short_description}") short_description = step.__doc__.strip().split('\n')[0].rstrip('.')
else: print(f"tzk: Step {idx}/{len(steps)}: {short_description}")
print(f"tzk: Step {idx}/{len(steps)}") else:
print(f"tzk: Step {idx}/{len(steps)}")
# If the user asked to skip this builder on the command line, do so. # If the user asked to skip this builder on the command line, do so.
if step.__name__ in args.skip_builder: if step.__name__ in args.skip_builder:
print(f"tzk: Skipping step {idx} due to --skip-builder parameter.") print(f"tzk: Skipping step {idx} due to --skip-builder parameter.")
continue continue
# Execute step and handle any errors. # Execute step and handle any errors.
try:
step() step()
except BuildError as e:
print(f"tzk: ERROR: {str(e)}")
print(f"tzk: Build of product '{args.product}' failed on step {idx}, "
f"backed by builder '{step.__name__}'.")
sys.exit(1)
except Exception:
print(f"tzk: Build of product '{args.product}' failed on step {idx}: "
f"unhandled exception. "
f"The original error follows:")
traceback.print_exc()
sys.exit(1)
# TODO: This should run in a finally() block; leaving for now as it's convenient for development :) except BuildError as e:
for idx, step in enumerate(steps, 1): failed = True
if hasattr(step, 'cleaner'): print(f"tzk: ERROR: {str(e)}")
print(f"tzk: Running cleanup routine for step {idx}...") print(f"tzk: Build of product '{args.product}' failed on step {idx}, "
step.cleaner() f"backed by builder '{step.__name__}'. ")
print(f"tzk: Add '--skip-builder {step.__name__}' if you'd like "
f"to skip this step.")
print(f"tzk: Build of product '{args.product}' completed successfully.") except Exception:
failed = True
print(f"tzk: Build of product '{args.product}' failed on step {idx}: "
f"unhandled exception. "
f"The original error follows:")
traceback.print_exc()
finally:
for idx, step in enumerate(steps, 1):
if hasattr(step, 'cleaner'):
print(f"tzk: Running cleanup routine registered by step {idx}...")
step.cleaner()
if failed:
sys.exit(1)
else:
print(f"tzk: Build of product '{args.product}' completed successfully.")
def chdir_to_wiki(): def chdir_to_wiki():