summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pygerrit/ssh.py53
-rwxr-xr-xsetup.py1
2 files changed, 54 insertions, 0 deletions
diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py
new file mode 100644
index 0000000..35de375
--- /dev/null
+++ b/pygerrit/ssh.py
@@ -0,0 +1,53 @@
+""" Gerrit SSH Client. """
+
+from os.path import abspath, expanduser, isfile
+
+from pygerrit.error import GerritError
+
+from paramiko import SSHClient, SSHConfig
+
+
+class GerritSSHClient(SSHClient):
+
+ """ Gerrit SSH Client, wrapping the paramiko SSH Client. """
+
+ def __init__(self, hostname):
+ """ Initialise and connect to SSH. """
+ super(GerritSSHClient, self).__init__()
+ self.load_system_host_keys()
+
+ configfile = expanduser("~/.ssh/config")
+ if not isfile(configfile):
+ raise GerritError("ssh config file '%s' does not exist" %
+ configfile)
+
+ config = SSHConfig()
+ config.parse(open(configfile))
+ data = config.lookup(hostname)
+ if not data:
+ raise GerritError("No ssh config for host %s" % hostname)
+ if not 'hostname' in data or not 'port' in data or not 'user' in data:
+ raise GerritError("Missing configuration data in %s" % configfile)
+ key_filename = None
+ if 'identityfile' in data:
+ key_filename = abspath(expanduser(data['identityfile']))
+ if not isfile(key_filename):
+ raise GerritError("Identity file '%s' does not exist" %
+ key_filename)
+ self.connect(hostname=data['hostname'],
+ port=int(data['port']),
+ username=data['user'],
+ key_filename=key_filename)
+
+ def run_gerrit_command(self, command):
+ """ Run the given command.
+
+ Run `command` and return a tuple of stdin, stdout, and stderr.
+
+ """
+ gerrit_command = ["gerrit"]
+ if isinstance(command, list):
+ gerrit_command += command
+ else:
+ gerrit_command.append(command)
+ return self.exec_command(" ".join(gerrit_command))
diff --git a/setup.py b/setup.py
index 1237f97..36f0deb 100755
--- a/setup.py
+++ b/setup.py
@@ -13,4 +13,5 @@ setup(
url="http://sonymobile.com",
packages=['pygerrit'],
keywords='gerrit, json',
+ install_requires=['paramiko'],
)