在远程 shell 中使用 Fabric 进行 run() 调用时,我可以捕获错误代码吗?

发布于 2024-10-15 21:27:44 字数 381 浏览 1 评论 0原文

通常,一旦 run() 调用返回非零退出代码,Fabric 就会退出。然而,对于某些呼叫来说,这是预期的。例如,当 PNGOut 无法压缩文件时,它会返回错误代码 2。

目前,我只能通过使用 shell 逻辑(do_something_that_fails || truedo_something_that_fails || do_something_else)来规避此限制,但我宁愿能够将我的逻辑保留在普通 Python(正如 Fabric 的承诺)。

有没有办法检查错误代码并对其做出反应,而不是让 Fabric 恐慌并死掉?我仍然想要其他调用的默认行为,因此通过修改环境来改变它的行为似乎不是一个好的选择(据我记得,你只能用它来告诉它警告而不是死亡)。

Normally Fabric quits as soon as a run() call returns a non-zero exit code. For some calls, however, this is expected. For example, PNGOut returns an error code of 2 when it is unable to compress a file.

Currently I can only circumvent this limitation by either using shell logic (do_something_that_fails || true or do_something_that_fails || do_something_else), but I'd rather be able to keep my logic in plain Python (as is the Fabric promise).

Is there a way to check for an error code and react to it rather than having Fabric panic and die? I still want the default behaviours for other calls, so changing its behaviour by modifying the environment doesn't seem like a good option (and as far as I recall, you can only use that to tell it to warn instead of dying anyway).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

动次打次papapa 2024-10-22 21:27:44

您可以使用 settings 上下文管理器和 warn_only 设置来防止非零退出代码中止:

from fabric.api import settings

with settings(warn_only=True):
    result = run('pngout old.png new.png')
    if result.return_code == 0: 
        do something
    elif result.return_code == 2: 
        do something else 
    else: #print error to user
        print result
        raise SystemExit()

更新: 我的答案已过时。请参阅下面的评论。

You can prevent aborting on non-zero exit codes by using the settings context manager and the warn_only setting:

from fabric.api import settings

with settings(warn_only=True):
    result = run('pngout old.png new.png')
    if result.return_code == 0: 
        do something
    elif result.return_code == 2: 
        do something else 
    else: #print error to user
        print result
        raise SystemExit()

Update: My answer is outdated. See comments below.

蒲公英的约定 2024-10-22 21:27:44

是的,你可以。只需更改环境的 abort_exception 即可。例如:

from fabric.api import settings

class FabricException(Exception):
    pass

with settings(abort_exception = FabricException):
    try:
        run(<something that might fail>)
    except FabricException:
        <handle the exception>

关于 abort_exception 的文档是 在这里

Yes, you can. Just change the environment's abort_exception. For example:

from fabric.api import settings

class FabricException(Exception):
    pass

with settings(abort_exception = FabricException):
    try:
        run(<something that might fail>)
    except FabricException:
        <handle the exception>

The documentation on abort_exception is here.

你与昨日 2024-10-22 21:27:44

显然,破坏环境就是答案。

fabric.api.settings 可以用作上下文管理器(使用 with)将其应用到各个语句。 run()local()sudo() 调用的返回值不仅仅是 shell 命令的输出,还包括具有允许对错误做出反应的特殊属性(return_codefailed)。

我想我正在寻找更接近 subprocess.Popen 或 Python 通常的异常处理行为的东西。

Apparently messing with the environment is the answer.

fabric.api.settings can be used as a context manager (with with) to apply it to individual statements. The return value of run(), local() and sudo() calls isn't just the output of the shell command, but also has special properties (return_code and failed) that allow reacting to the errors.

I guess I was looking for something closer to the behaviour of subprocess.Popen or Python's usual exception handling.

嘦怹 2024-10-22 21:27:44

试试这个

from fabric.api import run, env
env.warn_only = True # if you want to ignore exceptions and handle them yurself

command = "your command"
x = run(command, capture=True) # run or local or sudo
if(x.stderr != ""):
    error = "On %s: %s" %(command, x.stderr)
    print error
    print x.return_code # which may be 1 or 2
    # do what you want or
    raise Exception(error) #optional
else:
    print "the output of %s is: %s" %(command, x)
    print x.return_code # which is 0

try this

from fabric.api import run, env
env.warn_only = True # if you want to ignore exceptions and handle them yurself

command = "your command"
x = run(command, capture=True) # run or local or sudo
if(x.stderr != ""):
    error = "On %s: %s" %(command, x.stderr)
    print error
    print x.return_code # which may be 1 or 2
    # do what you want or
    raise Exception(error) #optional
else:
    print "the output of %s is: %s" %(command, x)
    print x.return_code # which is 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文