Python Fabric:一系列命令失败

时间:2013-06-26 20:42:46

标签: python fabric

在标准结构示例中,我们有

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.")

有没有办法检查整个方法的状态?

例如,

def method1():
   run_this_as_sudo
   run_this_as_sudo

如何检查整个方法是否在结构中失败而不是查看每个单独的方法调用?处理此问题的唯一方法是在由多个shell命令组成的每个方法上添加某种try catch吗?

1 个答案:

答案 0 :(得分:2)

您可以这样做:

╭─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"

我只是在条件中将调用链接在一起并使用他们返回的bool来进行条件切换。