From 2c1729792b9db903f64223eff8c83a72ed67630f Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 17 Nov 2020 18:09:04 +0000 Subject: [PATCH] Refactor execute to use wrapper for logging Avoid each implementation of Command to call logging and use a wrapped to log executions of each subclass. --- src/molecule/command/base.py | 23 ++++++++++++++++++++++- src/molecule/command/check.py | 1 - src/molecule/command/cleanup.py | 2 -- src/molecule/command/converge.py | 1 - src/molecule/command/create.py | 1 - src/molecule/command/dependency.py | 1 - src/molecule/command/destroy.py | 2 -- src/molecule/command/idempotence.py | 1 - src/molecule/command/lint.py | 2 -- src/molecule/command/prepare.py | 2 -- src/molecule/command/side_effect.py | 1 - src/molecule/command/syntax.py | 1 - src/molecule/command/verify.py | 1 - 13 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/molecule/command/base.py b/src/molecule/command/base.py index 0891899401..b0f59ce5bc 100644 --- a/src/molecule/command/base.py +++ b/src/molecule/command/base.py @@ -37,6 +37,20 @@ MOLECULE_DEFAULT_SCENARIO_NAME = "default" +def section_logger(func: Callable) -> Callable: + """Wrap effective execution of a method.""" + + def wrapper(*args, **kwargs): + # LOG.error("start %s %s", dir(args[0]), kwargs) + args[0].print_info() + rt = func(*args, **kwargs) + # LOG.error("stop") + return rt + + # wrapper.inherit_decorator = section_logger + return wrapper + + class Base(object, metaclass=abc.ABCMeta): """An abstract base class used to define the command interface.""" @@ -50,7 +64,13 @@ def __init__(self, c): self._config = c self._setup() + def __init_subclass__(cls) -> None: + """Decorate execute from all subclasses.""" + super().__init_subclass__() + setattr(cls, "execute", section_logger(cls.execute)) + @abc.abstractmethod + @section_logger def execute(self): # pragma: no cover pass @@ -141,7 +161,8 @@ def execute_subcommand(config, subcommand): # and is also used for reporting in execute_cmdline_scenarios config.action = subcommand - return command(config).execute() + result = command(config).execute() + return result def execute_scenario(scenario): diff --git a/src/molecule/command/check.py b/src/molecule/command/check.py index fd6aa89d65..57f6396024 100644 --- a/src/molecule/command/check.py +++ b/src/molecule/command/check.py @@ -79,7 +79,6 @@ def execute(self): :return: None """ - self.print_info() self._config.provisioner.check() diff --git a/src/molecule/command/cleanup.py b/src/molecule/command/cleanup.py index 33b137d5e1..d310b79643 100644 --- a/src/molecule/command/cleanup.py +++ b/src/molecule/command/cleanup.py @@ -73,8 +73,6 @@ def execute(self): :return: None """ - self.print_info() - if not self._config.provisioner.playbooks.cleanup: msg = "Skipping, cleanup playbook not configured." LOG.warning(msg) diff --git a/src/molecule/command/converge.py b/src/molecule/command/converge.py index 0b595ff294..7e680e2c23 100644 --- a/src/molecule/command/converge.py +++ b/src/molecule/command/converge.py @@ -79,7 +79,6 @@ def execute(self): :return: None """ - self.print_info() self._config.provisioner.converge() self._config.state.change_state("converged", True) diff --git a/src/molecule/command/create.py b/src/molecule/command/create.py index c31c91b2e8..6b44c29d4b 100644 --- a/src/molecule/command/create.py +++ b/src/molecule/command/create.py @@ -78,7 +78,6 @@ def execute(self): :return: None """ - self.print_info() self._config.state.change_state("driver", self._config.driver.name) if self._config.driver.delegated and not self._config.driver.managed: diff --git a/src/molecule/command/dependency.py b/src/molecule/command/dependency.py index f34375e945..6932ae3bec 100644 --- a/src/molecule/command/dependency.py +++ b/src/molecule/command/dependency.py @@ -70,7 +70,6 @@ def execute(self): :return: None """ - self.print_info() self._config.dependency.execute() diff --git a/src/molecule/command/destroy.py b/src/molecule/command/destroy.py index 06ad424727..90afa43461 100644 --- a/src/molecule/command/destroy.py +++ b/src/molecule/command/destroy.py @@ -93,8 +93,6 @@ def execute(self): :return: None """ - self.print_info() - if self._config.command_args.get("destroy") == "never": msg = "Skipping, '--destroy=never' requested." LOG.warning(msg) diff --git a/src/molecule/command/idempotence.py b/src/molecule/command/idempotence.py index 0439af6a6d..3fbff5ecf0 100644 --- a/src/molecule/command/idempotence.py +++ b/src/molecule/command/idempotence.py @@ -74,7 +74,6 @@ def execute(self): :return: None """ - self.print_info() if not self._config.state.converged: msg = "Instances not converged. Please converge instances first." util.sysexit_with_message(msg) diff --git a/src/molecule/command/lint.py b/src/molecule/command/lint.py index 2666d136bb..3d050b619d 100644 --- a/src/molecule/command/lint.py +++ b/src/molecule/command/lint.py @@ -80,8 +80,6 @@ def execute(self): :return: None """ - self.print_info() - # v3 migration code: cmd = self._config.lint if not cmd: diff --git a/src/molecule/command/prepare.py b/src/molecule/command/prepare.py index f69bf31bfc..81d7c52afd 100644 --- a/src/molecule/command/prepare.py +++ b/src/molecule/command/prepare.py @@ -89,8 +89,6 @@ def execute(self): :return: None """ - self.print_info() - if self._config.state.prepared and not self._config.command_args.get("force"): msg = "Skipping, instances already prepared." LOG.warning(msg) diff --git a/src/molecule/command/side_effect.py b/src/molecule/command/side_effect.py index 72d31feb09..dc19cbe71f 100644 --- a/src/molecule/command/side_effect.py +++ b/src/molecule/command/side_effect.py @@ -72,7 +72,6 @@ def execute(self): :return: None """ - self.print_info() if not self._config.provisioner.playbooks.side_effect: msg = "Skipping, side effect playbook not configured." LOG.warning(msg) diff --git a/src/molecule/command/syntax.py b/src/molecule/command/syntax.py index dca2fc304f..9b59b2b8cc 100644 --- a/src/molecule/command/syntax.py +++ b/src/molecule/command/syntax.py @@ -69,7 +69,6 @@ def execute(self): :return: None """ - self.print_info() self._config.provisioner.syntax() diff --git a/src/molecule/command/verify.py b/src/molecule/command/verify.py index 7e732ee263..fcf428bba6 100644 --- a/src/molecule/command/verify.py +++ b/src/molecule/command/verify.py @@ -70,7 +70,6 @@ def execute(self): :return: None """ - self.print_info() self._config.verifier.execute()