Skip to content

Commit

Permalink
Add hooks action_run_{start,end}
Browse files Browse the repository at this point in the history
  • Loading branch information
vain committed Jul 15, 2014
1 parent aebe649 commit 0b8df3c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
24 changes: 24 additions & 0 deletions doc/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ This is a list of all functions a hook file may implement.

|
.. py:function:: action_run_start(repo, node, action, **kwargs)
Called each time a :command:`bw apply` command reaches a new action.

:param Repository repo: The current repository (instance of :py:class:`blockwart.repo.Repository`).
:param Node node: The current node (instance of :py:class:`blockwart.node.Node`).
:param Item item: The current action.

|
|
.. py:function:: action_run_end(repo, node, action, duration=None, status=None, **kwargs)
Called each time a :command:`bw apply` command completes processing an action.

:param Repository repo: The current repository (instance of :py:class:`blockwart.repo.Repository`).
:param Node node: The current node (instance of :py:class:`blockwart.node.Node`).
:param Item item: The current action.
:param timedelta duration: How long the action was running.
:param ItemStatus status: An object with these attributes: ``correct``, ``skipped``.

|
|
.. py:function:: apply_start(repo, target, nodes, interactive=False, **kwargs)
Called when you start a :command:`bw apply` command.
Expand Down
31 changes: 29 additions & 2 deletions src/blockwart/items/actions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from blockwart.exceptions import ActionFailure, BundleError
from blockwart.items import Item
from blockwart.items import Item, ItemStatus
from blockwart.utils import LOG
from blockwart.utils.ui import ask_interactively
from blockwart.utils.text import mark_for_translation as _
from blockwart.utils.text import bold, wrap_question
from datetime import datetime


class Action(Item):
Expand All @@ -21,7 +22,7 @@ class Action(Item):
ITEM_TYPE_NAME = 'action'
REQUIRED_ATTRIBUTES = ['command']

def get_result(self, interactive=False, interactive_default=True):
def _get_result(self, interactive=False, interactive_default=True):
if interactive is False and self.attributes['interactive'] is True:
return self.STATUS_SKIPPED

Expand Down Expand Up @@ -63,6 +64,32 @@ def get_result(self, interactive=False, interactive_default=True):
except ActionFailure:
return self.STATUS_FAILED

def get_result(self, interactive=False, interactive_default=True):
self.node.repo.hooks.action_run_start(
self.node.repo,
self.node,
self,
)
start_time = datetime.now()

status_code = self._get_result(interactive, interactive_default)
if status_code == Item.STATUS_SKIPPED:
status = ItemStatus(correct=False, skipped=True)
elif status_code == Item.STATUS_ACTION_SUCCEEDED:
status = ItemStatus()
else:
status = ItemStatus(correct=False)

self.node.repo.hooks.action_run_end(
self.node.repo,
self.node,
self,
duration=datetime.now() - start_time,
status=status,
)

return status_code

def run(self, interactive=False):
result = self.bundle.node.run(
self.attributes['command'],
Expand Down
2 changes: 2 additions & 0 deletions src/blockwart/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
FILENAME_NODES = "nodes.py"

HOOK_EVENTS = (
'action_run_start',
'action_run_end',
'apply_start',
'apply_end',
'item_apply_start',
Expand Down

0 comments on commit 0b8df3c

Please sign in to comment.