python - 如何在结构中指定有效的非零返回码?

标签 python unix fabric

我有一个 bash 命令,它发出非标准的返回码来指示不同类型的成功和失败。

我想将此命令作为结构文件的一部分运行,同时将所有有效返回代码列入白名单。

from fabric.api import local
local('exit 5') # This will cause an abort even though it is expected behaviour

我的愿望是做这样的事情:

from fabric.api import local, settings
with settigns(valid_exit_codes=[0,5]):
    local('exit 5')

我目前正在做一些事情:

with settings(warn_only=True):
    output = local('exit 5')

    if output.return_code in [0, 5]:
        return output.return_code
    else:
        abort("Non success return-code: %s" % output.return_code)

在寻找更简单的解决方法方面的任何帮助都会很棒。

最佳答案

自 Fabric 1.6 以来,env 中有 ok_ret_codes 设置:

http://docs.fabfile.org/en/1.13/usage/env.html#ok-ret-codes

所以你可以这样做:

    with fabric.api.settings(ok_ret_codes=[0, 2]):
        fabric.api.sudo('puppet agent -t')

关于python - 如何在结构中指定有效的非零返回码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12579454/

相关文章:

python - 在 python 的 scikit-learn 决策树中仅使用一个属性一次

postgresql - 无法从终端在 Psql 中执行命令

python - 为 EC2 配置结构

python - X 和 Y 坐标元组的简单加减法

python 类 __getitem__ 负索引处理

bash - cd 命令在 bash 中看不到该目录

python - 如何在fabric中并行执行多个任务

python - Conda activate env 适用于 bash 但不适用于结构

python - Flask handle_exception 不适用于 test_request_context

linux - 替换大文件中固定数量的文本