Created
June 4, 2015 09:56
-
-
Save OrangeTux/d24b0ca0cd33fe6a52c2 to your computer and use it in GitHub Desktop.
Decorator which executes callback if wrapped Fabric task fails.
This file contains hidden or 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
def on_fail(func): | |
""" Return decorator which executes callback if wrapped tasks failes. | |
Usage: | |
def log_failure(): | |
print('Some failure.') | |
@task | |
@on_fail(log_failure): | |
def git_fetch(): | |
abort('Failure!') | |
:param func: A callable that is executed on failure of wrapped task. | |
:return: A decorator. | |
""" | |
def decorator(t): | |
@wraps(t) | |
def wrapper(*args, **kwargs): | |
try: | |
# warn_only must be False, so Fabric raises a SystemExit when | |
# tasks failes. | |
with settings(warn_only=False): | |
return t(*args, **kwargs) | |
except SystemExit: | |
print('Task \'%s\' failed!' % t.func_name) | |
func() | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment