【问题标题】:Interpret (and use) the output from Fabric local command解释(并使用)Fabric 本地命令的输出
【发布时间】:2012-12-04 22:28:09
【问题描述】:

我想使用 Fabric 命令来设置本地开发环境,作为其中的一部分,我希望能够设置 git 远程。这工作正常:

from fabric.api import local

def set_remote():
    """ Set up git remote for pushing to dev."""
    local('git remote add myremote git@myremote.com:myrepo.git')

问题出现在第二次运行时 - 当本地命令因为远程已经存在而爆炸时。我想通过先检查遥控器是否存在来防止这种情况发生:

在伪代码中,我想执行以下操作:

if 'myremote' in local('git remote'):
    print 'Remote \'myremote\' already exists.'
else:
    local('git remote add myremote git@myremote.com:myrepo.git')

我该怎么做?

【问题讨论】:

    标签: python fabric


    【解决方案1】:

    您可以使用settings 上下文管理器来warn_only

    from fabric.context_managers import settings
    
    with settings(warn_only=True):
        # some command we are all right with having fail
    

    或者,您可以将local 命令上的capture 关键字arg 设置为True

    if 'myremote' in local('git remote', capture=True):
        print 'Remote \'myremote\' already exists.'
    else:
        local('git remote add myremote git@myremote.com:myrepo.git')
    

    【讨论】:

    • 太棒了 - 非常感谢你 - 我会使用 capture=True,虽然我认为让它失败可能更 Pythonic(更容易请求宽恕......等等)
    • 顺便说一句 _ 我想你也不想回答这个问题 - stackoverflow.com/questions/13713156/…
    猜你喜欢
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多