Python 解释(并使用)Fabric local命令的输出

Python 解释(并使用)Fabric local命令的输出,python,fabric,Python,Fabric,我想使用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') 第二次运行此命令时会出现问题-当本地命令轰炸时,因为远程命令已经存在。我想先

我想使用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')
如何执行此操作?

您可以使用上下文管理器仅向您发出警告:

from fabric.context_managers import settings

with settings(warn_only=True):
    # some command we are all right with having fail
或者,您可以将命令上的
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
,尽管我认为让它失败可能更像恶作剧(更容易请求原谅…等等)。顺便说一句,我想你也不想回答这个问题,对吗-