-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generic conditional container plugin. #326
Open
gnfzdz
wants to merge
1
commit into
anishathalye:master
Choose a base branch
from
gnfzdz:conditionalplugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import dotbot | ||
import dotbot.util | ||
|
||
from .messenger import Messenger | ||
|
||
class Condition(object): | ||
|
||
""" | ||
Abstract base class for conditions that test whether dotbot should execute tasks/actions | ||
""" | ||
|
||
def __init__(self, context): | ||
self._context = context | ||
self._log = Messenger() | ||
|
||
def can_handle(self, directive): | ||
""" | ||
Returns true if the Condition can handle the directive. | ||
""" | ||
raise NotImplementedError | ||
|
||
def handle(self, directive, data): | ||
""" | ||
Executes the test. | ||
Returns the boolean value returned by the test | ||
""" | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .shell import ShellCondition | ||
from .tty import TtyCondition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ..condition import Condition | ||
import dotbot.util | ||
|
||
class ShellCondition(Condition): | ||
|
||
""" | ||
Condition testing an arbitrary shell command and evaluating to true if the return code is zero | ||
""" | ||
|
||
_directive = "shell" | ||
|
||
def can_handle(self, directive): | ||
return directive == self._directive | ||
|
||
def handle(self, directive, command): | ||
if directive != self._directive: | ||
raise ValueError("ShellCondition cannot handle directive %s" % directive) | ||
|
||
ret = dotbot.util.shell_command(command, cwd=self._context.base_directory()) | ||
return ret == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ..condition import Condition | ||
|
||
import sys | ||
|
||
class TtyCondition(Condition): | ||
|
||
""" | ||
Condition testing if stdin is a TTY (allowing to request input from the user) | ||
""" | ||
|
||
_directive = "tty" | ||
|
||
def can_handle(self, directive): | ||
return directive == self._directive | ||
|
||
def handle(self, directive, data=True): | ||
if directive != self._directive: | ||
raise ValueError("Tty cannot handle directive %s" % directive) | ||
expected = data if data is not None else True | ||
return expected == (sys.stdin.isatty() and sys.stdout.isatty() and sys.stderr.isatty()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .clean import Clean | ||
from .conditional import Conditional | ||
from .create import Create | ||
from .link import Link | ||
from .shell import Shell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import dotbot | ||
from dotbot.dispatcher import Dispatcher | ||
from dotbot.tester import Tester | ||
|
||
class Conditional(dotbot.Plugin): | ||
|
||
''' | ||
Conditionally execute nested commands based on the result of configured test(s) | ||
''' | ||
|
||
_directive = "conditional" | ||
|
||
def can_handle(self, directive): | ||
return directive == self._directive | ||
|
||
def handle(self, directive, data): | ||
if directive != self._directive: | ||
raise ValueError("Conditional cannot handle directive %s" % directive) | ||
return self._process_conditional(data) | ||
|
||
def _process_conditional(self, data): | ||
success = True | ||
tests = data.get("if") | ||
test_result = Tester(self._context).evaluate(tests) | ||
|
||
tasks = data.get("then") if test_result else data.get("else") | ||
self._log.info("executing sub-commands") | ||
# TODO prepend/extract defaults if scope_defaults is False | ||
if tasks is not None: | ||
return self._execute_tasks(tasks) | ||
else: | ||
return True | ||
|
||
def _execute_tasks(self, data): | ||
# TODO improve handling of defaults either by reusing context/dispatcher -OR- prepend defaults & extract at end | ||
dispatcher = Dispatcher(self._context.base_directory(), | ||
only=self._context.options().only, | ||
skip=self._context.options().skip, | ||
options=self._context.options()) | ||
# if the data is a dictionary, wrap it in a list | ||
data = data if type(data) is list else [ data ] | ||
return dispatcher.dispatch(data) | ||
# return self._context._dispatcher.dispatch(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from dotbot.condition import Condition | ||
from dotbot.messenger import Messenger | ||
|
||
class Tester(object): | ||
|
||
def __init__(self, context): | ||
self._context = context | ||
self._log = Messenger() | ||
self.__load_conditions() | ||
|
||
def __load_conditions(self): | ||
self._plugins = [plugin(self._context) for plugin in Condition.__subclasses__()] | ||
|
||
def evaluate(self, tests): | ||
normalized = self.normalize_tests(tests) | ||
|
||
for task in normalized: | ||
for action in task: | ||
for plugin in self._plugins: | ||
if plugin.can_handle(action): | ||
try: | ||
local_success = plugin.handle(action, task[action]) | ||
if not local_success: | ||
return False | ||
except Exception as err: | ||
self._log.error("An error was encountered while testing condition %s" % action) | ||
self._log.debug(err) | ||
return False | ||
return True | ||
|
||
def normalize_tests(self, tests): | ||
if isinstance(tests, str): | ||
return [ { 'shell': tests } ] | ||
elif isinstance(tests, dict): | ||
return [ tests ] | ||
elif isinstance(tests, list): | ||
return map(lambda test: { 'shell': test } if isinstance(test, str) else test, tests) | ||
else: | ||
# TODO error | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
- defaults: | ||
link: | ||
relink: true | ||
create: true | ||
|
||
- clean: ['~'] | ||
|
||
- conditional: | ||
if: | ||
tty: true | ||
then: | ||
- shell: | ||
- 'echo "Running from a TTY"' | ||
|
||
- conditional: | ||
if: | ||
shell: "command -v python" | ||
then: | ||
- shell: | ||
- 'echo "python is available"' | ||
|
||
- conditional: | ||
if: "command -v foo" | ||
else: | ||
- shell: | ||
- 'echo "foo is not available"' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note this currently has the same issue as the similar third-party plugins (scoping defaults to the child tasks). I think the best approach would be to update dispatcher constructor to allow passing in (and therefore reusing) a context.
Other options considered:
In either case, I also think it's worth adding a configuration option to this plugin to control whether defaults are scoped to the child tasks or not.