diff options
| author | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-08-02 17:47:24 +0900 |
|---|---|---|
| committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-08-05 17:51:27 +0900 |
| commit | 7762f04080d98b6dc0f4f16cdd8cb918cdac8941 (patch) | |
| tree | 5ef87f232a2a67df231203b421641933f52774bf /pygerrit/ssh.py | |
| parent | ce01e40a6f653a8862456ef735411084bc992b3b (diff) | |
| download | pygerrit-7762f04080d98b6dc0f4f16cdd8cb918cdac8941.tar.gz | |
Don't open ssh connection until needed
Instead of opening the ssh connection in the constructor, only
connect when actually needed.
This improves performance of the unit tests. Running the test
suite now takes less than 1 second rather than 22 seconds.
Change-Id: I03a49d74db8c045740a85201c02c492d175e0d6c
Diffstat (limited to 'pygerrit/ssh.py')
| -rw-r--r-- | pygerrit/ssh.py | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py index 0436165..d508d56 100644 --- a/pygerrit/ssh.py +++ b/pygerrit/ssh.py @@ -65,9 +65,14 @@ class GerritSSHClient(SSHClient): def __init__(self, hostname): """ Initialise and connect to SSH. """ super(GerritSSHClient, self).__init__() - self.load_system_host_keys() self.remote_version = None + self.hostname = hostname + self.connected = False + def _connect(self): + if self.connected: + return + self.load_system_host_keys() configfile = expanduser("~/.ssh/config") if not isfile(configfile): raise GerritError("ssh config file '%s' does not exist" % @@ -75,9 +80,9 @@ class GerritSSHClient(SSHClient): config = SSHConfig() config.parse(open(configfile)) - data = config.lookup(hostname) + data = config.lookup(self.hostname) if not data: - raise GerritError("No ssh config for host %s" % hostname) + 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 @@ -95,27 +100,35 @@ class GerritSSHClient(SSHClient): port=port, username=data['user'], key_filename=key_filename) + self.connected = True except socket.error as e: raise GerritError("Failed to connect to server: %s" % e) - def get_remote_version(self): - """ Return the version of the remote Gerrit server. """ - if self.remote_version is not None: - return self.remote_version - try: version_string = self._transport.remote_version pattern = re.compile(r'^.*GerritCodeReview_([a-z0-9-\.]*) .*$') self.remote_version = _extract_version(version_string, pattern) except AttributeError: - try: - result = self.run_gerrit_command("version") - except GerritError: - self.remote_version = "" - else: - version_string = result.stdout.read() - pattern = re.compile(r'^gerrit version (.*)$') - self.remote_version = _extract_version(version_string, pattern) + self.remote_version = None + + def exec_command(self, command, bufsize=1): + """ Execute the command. + + Make sure we're connected and then execute the command. + + Return a tuple of stdin, stdout, stderr. + + """ + self._connect() + return super(GerritSSHClient, self).exec_command(command, bufsize) + + def get_remote_version(self): + """ Return the version of the remote Gerrit server. """ + if self.remote_version is None: + result = self.run_gerrit_command("version") + version_string = result.stdout.read() + pattern = re.compile(r'^gerrit version (.*)$') + self.remote_version = _extract_version(version_string, pattern) return self.remote_version def run_gerrit_command(self, command): @@ -130,7 +143,8 @@ class GerritSSHClient(SSHClient): else: gerrit_command.append(command) try: - stdin, stdout, stderr = self.exec_command(" ".join(gerrit_command)) + c = " ".join(gerrit_command) + stdin, stdout, stderr = self.exec_command(c) except SSHException as err: raise GerritError("Command execution error: %s" % err) return GerritSSHCommandResult(stdin, stdout, stderr) |
