From 6326de9a4779267ce8697a656fc46ec222ad233e Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Mon, 4 Nov 2013 16:48:05 +0200 Subject: [PATCH] Refactored common/gerrit Now Gerrit class raises GerritException when it gets non-zero return code from ssh gerrit interface. Previously errors were silently ignored. Change-Id: I68d48a010c3427e00daa1306127de5e3b9e17bbd Signed-off-by: Ed Bartosh --- common/gerrit.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/common/gerrit.py b/common/gerrit.py index 7a3c707..4d4f357 100644 --- a/common/gerrit.py +++ b/common/gerrit.py @@ -4,9 +4,10 @@ import getpass import os -import sys import json +from subprocess import Popen, PIPE + def is_ref_deleted(oldrev, newrev): """Check if ref is deleted in Gerrit using old and new refs from 'ref updated' Gerrit event. @@ -28,26 +29,24 @@ class Gerrit: self.port = port self.silent_mode = silent_mode - if not 'Welcome to Gerrit Code Review' in ''.join(self._cmd_run('')): - raise GerritError("Init Error to Gerrit Service host: %s port: %s" - % (self.host, self.port)) + # Check connection + self._cmd_run('version') def _cmd_run(self, cmd, args=()): - """A cmd running wrapper""" - - args = list(args) - - print "ssh -p %s %s@%s gerrit %s %s" % (self.port, self.username, \ - self.host, cmd, ' '.join(args)) - if cmd: - sys.stdin, out = os.popen2("ssh -p %s %s@%s gerrit %s %s" - %(self.port, self.username, self.host, - cmd, ' '.join(args))) - return out.readlines() - else: - sys.stdin, out, err = os.popen3("ssh -p %s %s@%s" - %(self.port, self.username, self.host)) - return err.readlines() + """ + A cmd running wrapper. + Return: std output + Raises: GerritError if return code is not 0. + """ + + cmd = "ssh -p %s %s@%s gerrit %s %s" % (self.port, self.username, + self.host, cmd, ' '.join(args)) + pobj = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) + rcode = pobj.wait() + if rcode: + raise GerritError("Error running %s. Return code: %d, error: %s" % \ + (cmd, rcode, pobj.stderr.read())) + return pobj.stdout.readlines() def query(self, query): """Execute Gerrit query operation""" -- 2.7.4