2

In the standard fabric example, we have

def test():
    with settings(warn_only=True):
        result = local('./manage.py test my_app', capture=True)
    if result.failed and not confirm("Tests failed. Continue anyway?"):
        abort("Aborting at user request.")

Is there any way to check the status of an entire method?

For instance,

def method1():
   run_this_as_sudo
   run_this_as_sudo

How do I check to see if the entire method failed in fabric as opposed to looking at each individual method call? Is the only way to handle this to add some sort of try catch on every method that is composed of multiple shell commands?

user1431282
  • 6,535
  • 13
  • 51
  • 68
  • You may be able to catch a `SystemExit` exception for this. See: http://stackoverflow.com/questions/5481742/how-to-catch-auth-errors-in-fabric-and-retry – Roshambo Jun 26 '13 at 23:36

1 Answers1

2

You can do something like this:

╭─mgoose@Macintosh  ~
╰─$ fab -f tmp.py test
Ok
Something failed

Done.
╭─mgoose@Macintosh  ~
╰─$ cat tmp.py
from fabric.api import local, task, quiet

@task
def test():
    with quiet():
        if local("whoami").succeeded and local("echo good").succeeded:
            print "Ok"
        else:
            print "Something failed"


        if local("exit 1").succeeded and local("echo good").succeeded:
            print "Ok"
        else:
            print "Something failed"

I'm just chaining the calls together in the conditional and use the bools they return to make the conditional toggle.

Morgan
  • 4,143
  • 27
  • 35