diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2014-12-10 18:05:24 +0000 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2015-03-11 12:48:44 +0000 |
commit | 36cf89491a48e20002898a07b348e6f8c6064f54 (patch) | |
tree | e62818d66d13af833a13911c2abb2a869ec8794e /lorrycontroller/gerrit.py | |
parent | ce97e140f61cfc3781c3082febeacd0e5fb145cd (diff) | |
download | lorry-controller-sam/support-gerrit.tar.gz |
Add initial support for mirroring to a Gerrit instancesam/support-gerrit
Diffstat (limited to 'lorrycontroller/gerrit.py')
-rw-r--r-- | lorrycontroller/gerrit.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lorrycontroller/gerrit.py b/lorrycontroller/gerrit.py new file mode 100644 index 0000000..042a621 --- /dev/null +++ b/lorrycontroller/gerrit.py @@ -0,0 +1,53 @@ +# Copyright (C) 2015 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +import cliapp + + +class Gerrit(object): + + '''Run commands on a Gerrit instance. + + This uses the SSH API to Gerrit. The REST API is actually much nicer to + use, but it requires that the account doing stuff has set an HTTP password. + Use of the SSH API requires only the SSH key that we also use for push + access. + + ''' + + def __init__(self, host, user, port=29418): + self._ssh_command_args = [ + 'ssh', '-oStrictHostKeyChecking=no', '-oBatchMode=yes', '-p%i' % port, + '%s@%s' % (user, host)] + + def _ssh_command(self, command): + return cliapp.runcmd(self._ssh_command_args + command) + + def has_project(self, name): + # There's no 'does this project exist' command in Gerrit 2.9.4; 'list + # all projects with this prefix' is as close we can get. + + output = self._ssh_command([ + 'gerrit', 'ls-projects', '--type=ALL', '--prefix=%s' % name]) + projects = output.strip().split('\n') + + if name in projects: + return True + else: + return False + + def create_project(self, name): + self._ssh_command(['gerrit', 'create-project', name]) |