在远程 shell 中使用 Fabric 进行 run() 调用时,我可以捕获错误代码吗?
通常,一旦 run() 调用返回非零退出代码,Fabric 就会退出。然而,对于某些呼叫来说,这是预期的。例如,当 PNGOut 无法压缩文件时,它会返回错误代码 2。
目前,我只能通过使用 shell 逻辑(do_something_that_fails || true
或 do_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
settings
上下文管理器和warn_only
设置来防止非零退出代码中止:更新: 我的答案已过时。请参阅下面的评论。
You can prevent aborting on non-zero exit codes by using the
settings
context manager and thewarn_only
setting:Update: My answer is outdated. See comments below.
是的,你可以。只需更改环境的
abort_exception
即可。例如:关于
abort_exception
的文档是 在这里。Yes, you can. Just change the environment's
abort_exception
. For example:The documentation on
abort_exception
is here.显然,破坏环境就是答案。
fabric.api.settings
可以用作上下文管理器(使用with
)将其应用到各个语句。run()
、local()
和sudo()
调用的返回值不仅仅是 shell 命令的输出,还包括具有允许对错误做出反应的特殊属性(return_code
和failed
)。我想我正在寻找更接近 subprocess.Popen 或 Python 通常的异常处理行为的东西。
Apparently messing with the environment is the answer.
fabric.api.settings
can be used as a context manager (withwith
) to apply it to individual statements. The return value ofrun()
,local()
andsudo()
calls isn't just the output of the shell command, but also has special properties (return_code
andfailed
) 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.试试这个
try this