Skip to content

Commit

Permalink
Refactor execute to use wrapper for logging
Browse files Browse the repository at this point in the history
Avoid each implementation of Command to call logging and use
a wrapped to log executions of each subclass.
  • Loading branch information
ssbarnea committed Nov 17, 2020
1 parent 4cf4abd commit 2c17297
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 17 deletions.
23 changes: 22 additions & 1 deletion src/molecule/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def execute(self):
:return: None
"""
self.print_info()
self._config.provisioner.check()


Expand Down
2 changes: 0 additions & 2 deletions src/molecule/command/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/converge.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def execute(self):
:return: None
"""
self.print_info()
self._config.provisioner.converge()
self._config.state.change_state("converged", True)

Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def execute(self):
:return: None
"""
self.print_info()
self._config.dependency.execute()


Expand Down
2 changes: 0 additions & 2 deletions src/molecule/command/destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/idempotence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/molecule/command/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ def execute(self):
:return: None
"""
self.print_info()

# v3 migration code:
cmd = self._config.lint
if not cmd:
Expand Down
2 changes: 0 additions & 2 deletions src/molecule/command/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/side_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def execute(self):
:return: None
"""
self.print_info()
self._config.provisioner.syntax()


Expand Down
1 change: 0 additions & 1 deletion src/molecule/command/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def execute(self):
:return: None
"""
self.print_info()
self._config.verifier.execute()


Expand Down

0 comments on commit 2c17297

Please sign in to comment.