Skip to main content
 首页 » 编程设计

python Fabric : Fail on a Series of Commands

2024年11月01日9pengyingh

在标准织物示例中,我们有

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 

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

请您参考如下方法:

你可以这样做:

╭─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 值来进行条件切换。