diff options
-rw-r--r-- | pygerrit/ssh.py | 53 | ||||
-rwxr-xr-x | setup.py | 1 |
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)) @@ -13,4 +13,5 @@ setup( url="http://sonymobile.com", packages=['pygerrit'], keywords='gerrit, json', + install_requires=['paramiko'], ) |