Skip to content

Commit

Permalink
allow hooks to raise SkipNode
Browse files Browse the repository at this point in the history
closes #385
  • Loading branch information
trehn committed Feb 26, 2018
1 parent 0274ba0 commit e453846
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
19 changes: 14 additions & 5 deletions bundlewrap/cmdline/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from sys import exit

from ..concurrency import WorkerPool
from ..exceptions import SkipNode
from ..utils import SkipList
from ..utils.cmdline import get_target_nodes
from ..utils.table import ROW_SEPARATOR, render_table
Expand All @@ -34,11 +35,19 @@ def run_on_node(node, command, skip_list):
io.stdout(_("{x} {node} skipped by --resume-file").format(node=bold(node.name), x=yellow("»")))
return None

node.repo.hooks.node_run_start(
node.repo,
node,
command,
)
try:
node.repo.hooks.node_run_start(
node.repo,
node,
command,
)
except SkipNode as exc:
io.stdout(_("{x} {node} skipped by hook ({reason})").format(
node=bold(node.name),
reason=str(exc) or _("no reason given"),
x=yellow("»"),
))
return None

result = node.run(
command,
Expand Down
7 changes: 7 additions & 0 deletions bundlewrap/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ class PluginLocalConflict(PluginError):
pass


class SkipNode(UnicodeException):
"""
Can be raised by hooks to skip a node.
"""
pass


class TemplateError(RepositoryError):
"""
Raised when an error occurs while rendering a template.
Expand Down
21 changes: 15 additions & 6 deletions bundlewrap/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
NodeLockedException,
NoSuchBundle,
RepositoryError,
SkipNode,
)
from .group import GROUP_ATTR_DEFAULTS
from .itemqueue import ItemQueue
Expand Down Expand Up @@ -568,19 +569,27 @@ def apply(
))
return None

start = datetime.now()
try:
self.repo.hooks.node_apply_start(
self.repo,
self,
interactive=interactive,
)
except SkipNode as exc:
io.stdout(_("{x} {node} skipped by hook ({reason})").format(
node=bold(self.name),
reason=str(exc) or _("no reason given"),
x=yellow("»"),
))
return None

start = datetime.now()
io.stdout(_("{x} {node} {started} at {time}").format(
node=bold(self.name),
started=bold(_("started")),
time=start.strftime("%Y-%m-%d %H:%M:%S"),
x=blue("i"),
))
self.repo.hooks.node_apply_start(
self.repo,
self,
interactive=interactive,
)

try:
with NodeLock(self, interactive=interactive, ignore=force) as lock:
Expand Down
14 changes: 14 additions & 0 deletions docs/content/repo/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ Called each time a `bw apply` command reaches a new node.

`interactive` `True` if this is an interactive apply run.

To skip a node:

```
from bundlewrap.exceptions import SkipNode
raise SkipNode("reason goes here")
```

---

**`node_apply_end(repo, node, duration=None, interactive=False, result=None, **kwargs)`**
Expand Down Expand Up @@ -192,6 +199,13 @@ Called each time a `bw run` command reaches a new node.

`command` The command that will be run on the node.

To skip a node:

```
from bundlewrap.exceptions import SkipNode
raise SkipNode("reason goes here")
```

---

**`node_run_end(repo, node, command, duration=None, return_code=None, stdout="", stderr="", **kwargs)`**
Expand Down

0 comments on commit e453846

Please sign in to comment.