summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2013-09-12 18:21:45 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2013-09-12 18:21:45 +0900
commit730b6b8bc3697f4294edbf3b73cec47b64c625c0 (patch)
tree6523edb2b9b74fa715351650efdb10fdf235daff
parent7fc93f60775f252278af535ade9a7c6cae17a3d1 (diff)
downloadpygerrit-730b6b8bc3697f4294edbf3b73cec47b64c625c0.tar.gz
Fix #10: Allow to manually specify ssh username and port
If the username and port are specified when constructing the client, they will be used instead of attempting to fetch from the ssh config file. In this case the full hostname of the server is required, rather than only the name as listed in the ssh config. Change-Id: I82b8638d99922c9b40a54a8275ffc085f505696f
-rwxr-xr-xexample.py9
-rw-r--r--pygerrit/client.py4
-rw-r--r--pygerrit/ssh.py30
3 files changed, 30 insertions, 13 deletions
diff --git a/example.py b/example.py
index cde021c..904f690 100755
--- a/example.py
+++ b/example.py
@@ -42,6 +42,11 @@ def _main():
parser.add_option('-g', '--gerrit-hostname', dest='hostname',
default='review',
help='gerrit server hostname (default: %default)')
+ parser.add_option('-p', '--port', dest='port',
+ type='int', default=29418,
+ help='port number (default: %default)')
+ parser.add_option('-u', '--username', dest='username',
+ help='username')
parser.add_option('-b', '--blocking', dest='blocking',
action='store_true',
help='block on event get (default: False)')
@@ -65,7 +70,9 @@ def _main():
level=level)
try:
- gerrit = GerritClient(host=options.hostname)
+ gerrit = GerritClient(host=options.hostname,
+ username=options.username,
+ port=options.port)
logging.info("Connected to Gerrit version [%s]",
gerrit.gerrit_version())
gerrit.start_event_stream()
diff --git a/pygerrit/client.py b/pygerrit/client.py
index 3c9de68..456ce2e 100644
--- a/pygerrit/client.py
+++ b/pygerrit/client.py
@@ -37,11 +37,11 @@ class GerritClient(object):
""" Gerrit client interface. """
- def __init__(self, host):
+ def __init__(self, host, username=None, port=None):
self._factory = GerritEventFactory()
self._events = Queue()
self._stream = None
- self._ssh_client = GerritSSHClient(host)
+ self._ssh_client = GerritSSHClient(host, username=username, port=port)
def gerrit_version(self):
""" Return the version of Gerrit that is connected to. """
diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py
index c235999..e779715 100644
--- a/pygerrit/ssh.py
+++ b/pygerrit/ssh.py
@@ -65,17 +65,19 @@ class GerritSSHClient(SSHClient):
""" Gerrit SSH Client, wrapping the paramiko SSH Client. """
- def __init__(self, hostname):
+ def __init__(self, hostname, username=None, port=None):
""" Initialise and connect to SSH. """
super(GerritSSHClient, self).__init__()
self.remote_version = None
self.hostname = hostname
+ self.username = username
+ self.key_filename = None
+ self.port = port
self.connected = Event()
self.lock = Lock()
- def _do_connect(self):
- """ Connect to the remote. """
- self.load_system_host_keys()
+ def _configure(self):
+ """ Configure the ssh parameters from the config file. """
configfile = expanduser("~/.ssh/config")
if not isfile(configfile):
raise GerritError("ssh config file '%s' does not exist" %
@@ -88,21 +90,29 @@ class GerritSSHClient(SSHClient):
raise GerritError("No ssh config for host %s" % self.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
+ self.hostname = data['hostname']
+ self.username = data['user']
if 'identityfile' in data:
key_filename = abspath(expanduser(data['identityfile'][0]))
if not isfile(key_filename):
raise GerritError("Identity file '%s' does not exist" %
key_filename)
+ self.key_filename = key_filename
try:
- port = int(data['port'])
+ self.port = int(data['port'])
except ValueError:
raise GerritError("Invalid port: %s" % data['port'])
+
+ def _do_connect(self):
+ """ Connect to the remote. """
+ self.load_system_host_keys()
+ if self.username is None or self.port is None:
+ self._configure()
try:
- self.connect(hostname=data['hostname'],
- port=port,
- username=data['user'],
- key_filename=key_filename)
+ self.connect(hostname=self.hostname,
+ port=self.port,
+ username=self.username,
+ key_filename=self.key_filename)
except socket.error as e:
raise GerritError("Failed to connect to server: %s" % e)