Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpret (and use) the output from Fabric local command

Tags:

python

fabric

I would like to use a Fabric command to set up a local development environment, and as part of that I want to be able to set up a git remote. This works fine:

from fabric.api import local

def set_remote():
    """ Set up git remote for pushing to dev."""
    local('git remote add myremote [email protected]:myrepo.git')

The problem comes with running this a second time - when the local command bombs because the remote already exists. I'd like to prevent this by checking if the remote exists first:

In pseudocode, I'd like to do the following:

if 'myremote' in local('git remote'):
    print 'Remote \'myremote\' already exists.'
else:
    local('git remote add myremote [email protected]:myrepo.git')

How can I do this?

like image 857
Hugo Rodger-Brown Avatar asked Dec 04 '12 22:12

Hugo Rodger-Brown


1 Answers

You can use the settings context manager to warn_only:

from fabric.context_managers import settings

with settings(warn_only=True):
    # some command we are all right with having fail

Alternately, you can set the capture keyword arg on the local command to True:

if 'myremote' in local('git remote', capture=True):
    print 'Remote \'myremote\' already exists.'
else:
    local('git remote add myremote [email protected]:myrepo.git')
like image 163
Sean Vieira Avatar answered Sep 20 '22 01:09

Sean Vieira