diff options
| author | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-10-23 18:56:32 +0900 |
|---|---|---|
| committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-10-24 10:22:07 +0900 |
| commit | c8ad56f37edb69f46fdc1e3cecc14a760f34f0a1 (patch) | |
| tree | 10c30670fd04dd54ff1381eb5c343a68f278c840 /pygerrit/ssh.py | |
| parent | d309677f9dd631b6a956659dd45ef91e4944656d (diff) | |
| download | pygerrit-c8ad56f37edb69f46fdc1e3cecc14a760f34f0a1.tar.gz | |
Refactor getting the Gerrit version
Move the functionality to get the Gerrit version from the main
client into the SSH client and reimplement it.
First attempt to get the version from the underlying Paramiko SSH
client. If that fails, fall back to using the `gerrit version`
command as before.
Get and print the version in the example script.
Change-Id: Ia79853f80f898b43c301fcecee61fdfdb75e9811
Diffstat (limited to 'pygerrit/ssh.py')
| -rw-r--r-- | pygerrit/ssh.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py index 06154c4..52817c2 100644 --- a/pygerrit/ssh.py +++ b/pygerrit/ssh.py @@ -25,6 +25,7 @@ THE SOFTWARE. """ from os.path import abspath, expanduser, isfile +import re from threading import Lock from pygerrit.error import GerritError @@ -33,6 +34,15 @@ from paramiko import SSHClient, SSHConfig from paramiko.ssh_exception import SSHException +def _extract_version(version_string, pattern): + """ Extract the version from `version_string` using `pattern`. """ + if version_string: + match = pattern.match(version_string.strip()) + if match: + return match.group(1) + return "" + + class GerritSSHClient(SSHClient): """ Gerrit SSH Client, wrapping the paramiko SSH Client. """ @@ -42,6 +52,7 @@ class GerritSSHClient(SSHClient): super(GerritSSHClient, self).__init__() self.load_system_host_keys() self.lock = Lock() + self.remote_version = None configfile = expanduser("~/.ssh/config") if not isfile(configfile): @@ -70,6 +81,26 @@ class GerritSSHClient(SSHClient): username=data['user'], key_filename=key_filename) + 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: + _stdin, stdout, _stderr = self.run_gerrit_command("version") + except GerritError: + self.remote_version = "" + else: + version_string = 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): """ Run the given command. |
