summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-31 14:37:06 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2014-03-31 14:37:06 +0000
commitf423549ddbe4186ab86d26ca0e8475b68c853445 (patch)
tree92d0848bb0cdce22bda30f5e650e2ab4d6234439
parentdb036f586f9dcfd2a8fb3dd31b1d2df641332430 (diff)
downloadlorry-controller-f423549ddbe4186ab86d26ca0e8475b68c853445.tar.gz
Refactor GitanoCommand to avoid repetitive code
-rw-r--r--lorrycontroller/gitano.py44
1 files changed, 21 insertions, 23 deletions
diff --git a/lorrycontroller/gitano.py b/lorrycontroller/gitano.py
index f2f33b0..c0cccba 100644
--- a/lorrycontroller/gitano.py
+++ b/lorrycontroller/gitano.py
@@ -14,6 +14,8 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+import logging
+
import cliapp
import lorrycontroller
@@ -21,9 +23,11 @@ import lorrycontroller
class GitanoCommandFailure(Exception):
- def __init__(self, trovehost, command):
+ def __init__(self, trovehost, command, stderr):
Exception.__init__(
- self, 'Failed to run "%s" on Gitano on %s' % (command, trovehost))
+ self,
+ 'Failed to run "%s" on Gitano on %s\n%s' %
+ (command, trovehost, stderr))
class GitanoCommand(object):
@@ -34,18 +38,10 @@ class GitanoCommand(object):
self.trovehost = trovehost
def create(self, repo_path):
- exit, stdout, stderr = cliapp.runcmd_unchecked(
- ['ssh', 'git@%s' % self.trovehost, 'create', repo_path])
- if exit:
- raise GitanoCommandFailure(self.trovehost, 'create %s' % repo_path)
+ self._ssh_command(['create', repo_path])
def get_gitano_config_for_repo(self, repo_path):
- exit, stdout, stderr = cliapp.runcmd_unchecked(
- ['ssh', 'git@%s' % self.trovehost,
- 'config', cliapp.shell_quote(repo_path), 'show'])
-
- if exit:
- raise GitanoCommandFailure(self.trovehost, 'config show')
+ stdout = self._ssh_command(['config', repo_path, 'show'])
# "config REPO show" outputs a sequence of lines of the form "key: value".
# Extract those into a collections.defaultdict.
@@ -59,22 +55,24 @@ class GitanoCommand(object):
return result
def set_gitano_config_for_repo(self, path, key, value):
- exit, stdout, stderr = cliapp.runcmd_unchecked(
- ['ssh', 'git@%s' % self.trovehost,
- 'config', cliapp.shell_quote(repo_path),
- 'set', cliapp.shell_quote(key), cliapp.shell_quote(value)])
-
- if exit:
- raise GitanoCommandFailure(self.trovehost, 'config set')
+ self._ssh_command(['config', path, 'set', key, value])
def ls(self):
+ return self._ssh_command(['ls'])
+
+ def _ssh_command(self, gitano_args):
+ quoted_args = [cliapp.shell_quote(x) for x in gitano_args]
+
exit, stdout, stderr = cliapp.runcmd_unchecked(
- ['ssh', 'git@%s' % self.trovehost, 'ls'])
+ ['ssh', 'git@%s' % self.trovehost] + quoted_args)
if exit != 0:
logging.error(
- 'Failed to run "gitano ls" for %s:\n%s',
+ 'Failed to run "%s" for %s:\n%s',
self.trovehost, stdout + stderr)
- raise GitanoLsError(self.trovehost, stdout + stderr)
-
+ raise GitanoCommandFailure(
+ self.trovehost,
+ ' '.join(gitano_args),
+ stdout + stderr)
+
return stdout