@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):
: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):
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
--- /dev/null
+# 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]))
--- /dev/null
+# 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()