diff --git a/docs/changes/2662.bugfix.rst b/docs/changes/2662.bugfix.rst new file mode 100644 index 00000000000..5e806c361b9 --- /dev/null +++ b/docs/changes/2662.bugfix.rst @@ -0,0 +1,4 @@ +Fix order of finalizing ``Component`` instances using ``Tool.enter_context`` +and calling ``Provenance().finish_activity`` in ``ctapipe.core.Tool``. + +Fixes output provenance information missing for e.g. ``ctapipe-process``. diff --git a/src/ctapipe/core/tool.py b/src/ctapipe/core/tool.py index e484c6ef71d..8bdc0759668 100644 --- a/src/ctapipe/core/tool.py +++ b/src/ctapipe/core/tool.py @@ -405,6 +405,7 @@ def run(self, argv=None, raises=False): # return codes are taken from: # https://tldp.org/LDP/abs/html/exitcodes.html + status = "success" exit_status = 0 current_exception = None @@ -430,51 +431,42 @@ def run(self, argv=None, raises=False): self.start() self.finish() - self.log.info("Finished: %s", self.name) - Provenance().finish_activity(activity_name=self.name) except (ToolConfigurationError, TraitError) as err: current_exception = err self.log.error("%s", err) self.log.error("Use --help for more info") exit_status = 2 # wrong cmd line parameter - Provenance().finish_activity( - activity_name=self.name, status="error", exit_code=exit_status - ) + status = "error" except KeyboardInterrupt: self.log.warning("WAS INTERRUPTED BY CTRL-C") exit_status = 130 # Script terminated by Control-C - Provenance().finish_activity( - activity_name=self.name, status="interrupted", exit_code=exit_status - ) + status = "interrupted" except Exception as err: current_exception = err exit_status = getattr(err, "exit_code", 1) + status = "error" self.log.exception("Caught unexpected exception: %s", err) - Provenance().finish_activity( - activity_name=self.name, status="error", exit_code=exit_status - ) except SystemExit as err: exit_status = err.code - if exit_status == 0: - # Finish normally - Provenance().finish_activity(activity_name=self.name) - else: - # Finish with error + status = "error" + if exit_status != 0: current_exception = err self.log.critical( "Caught SystemExit with exit code %s", exit_status ) - Provenance().finish_activity( - activity_name=self.name, - status="error", - exit_code=exit_status, - ) finally: - if not {"-h", "--help", "--help-all"}.intersection(self.argv): - self.write_provenance() if raises and current_exception: + self.write_provenance() raise current_exception + Provenance().finish_activity( + activity_name=self.name, status=status, exit_code=exit_status + ) + + if not {"-h", "--help", "--help-all"}.intersection(self.argv): + self.write_provenance() + + self.log.info("Finished %s", self.name) self.exit(exit_status) def write_provenance(self):