From 605ceec45e75d1e008131e1fec051e29ef71cc50 Mon Sep 17 00:00:00 2001 From: Sam Hague Date: Mon, 1 Oct 2018 13:04:59 -0400 Subject: [PATCH] Ignore any non gerrit lines The latest ssh infra changes are now returning output like: "Warning: Permanently added the RSA host key for IP address" to the output and that is being parsed incorrectly. So drop any lines that are not gerrit lines. Those lines will have "grantedOn" in them. Also added a testcase to test the condition. Change-Id: Iefc109f28eed491aad9fac7bd9093685cba2348d Signed-off-by: Sam Hague --- tools/distchanges/gerritquery.py | 30 ++++++++++-------- tools/distchanges/logg.py | 35 +++++++++++++++++++++ tools/distchanges/tests/test_gerritquery.py | 33 +++++++++++++++++++ 3 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 tools/distchanges/logg.py create mode 100644 tools/distchanges/tests/test_gerritquery.py diff --git a/tools/distchanges/gerritquery.py b/tools/distchanges/gerritquery.py index 4e2c6cdd42..b6cddc5cdb 100644 --- a/tools/distchanges/gerritquery.py +++ b/tools/distchanges/gerritquery.py @@ -74,12 +74,15 @@ class GerritQuery: @staticmethod def print_safe_encoding(string): - if type(string) == unicode: - encoding = 'utf-8' - if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: - encoding = sys.stdout.encoding - return string.encode(encoding or 'utf-8', 'replace') - else: + try: + if type(string) == unicode: + encoding = 'utf-8' + if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding: + encoding = sys.stdout.encoding + return string.encode(encoding or 'utf-8', 'replace') + else: + return str(string) + except: return str(string) def run_command_status(self, *argv, **kwargs): @@ -281,13 +284,14 @@ class GerritQuery: :return list: Lines of the JSON """ lines = [] - for line in changes.split("\n"): - if line.find('"type":"error","message"') != -1: - logger.warn("there was a query error") - continue - if line.find('stats') == -1: + skipped = 0 + for i, line in enumerate(changes.split("\n")): + if line.find('"grantedOn":') != -1: lines.append(line) - logger.debug("get_gerrit_lines: found %d lines", len(lines)) + else: + logger.debug("skipping: {}".format(line)) + skipped += 1 + logger.debug("get_gerrit_lines: found {} lines, skipped: {}".format(len(lines), skipped)) return lines def get_gerrits(self, project, changeid=None, limit=1, msg=None, status=None, comments=False, commitid=None): @@ -326,6 +330,6 @@ class GerritQuery: return gerrits try: sorted_gerrits = sorted(gerrits, key=itemgetter('grantedOn'), reverse=True) - except KeyError, e: + except KeyError as e: logger.warn("KeyError exception in %s, %s", project, str(e)) return sorted_gerrits diff --git a/tools/distchanges/logg.py b/tools/distchanges/logg.py new file mode 100644 index 0000000000..b6762c8db1 --- /dev/null +++ b/tools/distchanges/logg.py @@ -0,0 +1,35 @@ +# Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved. +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License v1.0 which accompanies this distribution, +# and is available at http://www.eclipse.org/legal/epl-v10.html + +import logging + +logger = None +ch = None +fh = None + + +def debug(): + ch.setLevel(logging.DEBUG) + # logger.setLevel(min([ch.level, fh.level])) + + +class Logger: + def __init__(self, console_level=logging.INFO, file_level=logging.DEBUG): + global logger + global ch + global fh + + logger = logging.getLogger() + formatter = logging.Formatter('%(asctime)s | %(levelname).3s | %(name)-20s | %(lineno)04d | %(message)s') + ch = logging.StreamHandler() + ch.setLevel(console_level) + ch.setFormatter(formatter) + logger.addHandler(ch) + fh = logging.FileHandler("/tmp/odltools.txt", "w") + fh.setLevel(file_level) + fh.setFormatter(formatter) + logger.addHandler(fh) + logger.setLevel(min([ch.level, fh.level])) diff --git a/tools/distchanges/tests/test_gerritquery.py b/tools/distchanges/tests/test_gerritquery.py new file mode 100644 index 0000000000..86daf3ae23 --- /dev/null +++ b/tools/distchanges/tests/test_gerritquery.py @@ -0,0 +1,33 @@ +# Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved. +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License v1.0 which accompanies this distribution, +# and is available at http://www.eclipse.org/legal/epl-v10.html + +import logging +import unittest +from gerritquery import GerritQuery +import logg + + +REMOTE_URL = GerritQuery.remote_url +BRANCH = 'stable/oxygen' +LIMIT = 10 +QLIMIT = 50 +VERBOSE = 0 +PROJECT = "controller" + + +class TestRequest(unittest.TestCase): + def setUp(self): + logg.Logger(logging.DEBUG, logging.INFO) + self.gerritquery = GerritQuery(REMOTE_URL, BRANCH, QLIMIT, VERBOSE) + + def test_get_gerrits(self): + changeid = "I41232350532e56340c1fe9853ef7e74e3aa03359" + gerrits = self.gerritquery.get_gerrits(PROJECT, changeid, 1, status="merged") + print("{}".format(gerrits)) + + +if __name__ == '__main__': + unittest.main() -- 2.36.6