6

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')

これを 2 回実行すると問題が発生します。これは、リモートが既に存在するためにローカル コマンドが爆撃したときです。最初にリモートが存在するかどうかを確認して、これを防ぎたいと思います:

疑似コードでは、次のことを行いたいと思います。

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

これどうやってするの?

4

1 に答える 1

10

settingsコンテキスト マネージャーを使用して、次のことができますwarn_only

from fabric.context_managers import settings

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

または、コマンドのcaptureキーワード arg を次のように設定できます。localTrue

if 'myremote' in local('git remote', capture=True):
    print 'Remote \'myremote\' already exists.'
else:
    local('git remote add myremote git@myremote.com:myrepo.git')
于 2012-12-04T22:32:37.550 に答える