Created
February 14, 2012 23:43
-
-
Save lost-theory/1831706 to your computer and use it in GitHub Desktop.
fabric elocal - "local" command that raises an exception
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
#!/usr/bin/env python | |
from fabric.api import task, local, settings | |
class CommandFailed(Exception): | |
def __init__(self, message, result): | |
Exception.__init__(self, message) | |
self.result = result | |
def elocal(*args, **kwargs): | |
with settings(warn_only=True): | |
result = local(*args, **kwargs) | |
if result.failed: | |
raise CommandFailed("args: %r, kwargs: %r, error code: %r" % (args, kwargs, result.return_code), result) | |
return result | |
############################################################################### | |
@task | |
def main(): | |
"""Do the thing, with elocal.""" | |
try: | |
elocal("ls -al yabbadababa", capture=True) | |
except CommandFailed, e: | |
print "command failed yo" | |
print repr(e) | |
print str(e) | |
print e.result.stderr | |
if __name__ == "__main__": | |
import fabric.main | |
import sys | |
sys.argv.pop(0) | |
if not sys.argv or '--help' in sys.argv: | |
sys.argv = ['--list'] | |
new_argv = ['fab', '-f', __file__] + sys.argv | |
sys.argv = new_argv | |
fabric.main.main() | |
""" | |
$ ./fab.py main | |
[localhost] local: ls -al yabbadababa | |
Warning: local() encountered an error (return code 2) while executing 'ls -al yabbadababa' | |
command failed yo | |
CommandFailed("args: ('ls -al yabbadababa',), kwargs: {'capture': True}, error code: 2",) | |
args: ('ls -al yabbadababa',), kwargs: {'capture': True}, error code: 2 | |
ls: cannot access yabbadababa: No such file or directory | |
Done. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment