user1431282
user1431282

Reputation: 6835

Python Fabric : Fail on a Series of Commands

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?

Upvotes: 2

Views: 1122

Answers (1)

Morgan
Morgan

Reputation: 4131

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.

Upvotes: 2

Related Questions