From 2265e6b910d8549d5de616010119050d36fb9b55 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 24 Aug 2012 13:59:06 +0900 Subject: Add an example of how the Gerrit client class is used Change-Id: Ia6df5a588e4789fdf24825a8492f6c11b35e899b --- example.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 example.py (limited to 'example.py') diff --git a/example.py b/example.py new file mode 100755 index 0000000..95c7c21 --- /dev/null +++ b/example.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +""" Example of using the Gerrit client class. """ + +import logging +import optparse +import sys +import time + +from pygerrit.client import GerritClient +from pygerrit.stream import GerritStreamErrorEvent + + +def _main(): + usage = "usage: %prog [options]" + parser = optparse.OptionParser(usage=usage) + parser.add_option('-g', '--gerrit-hostname', dest='hostname', + default='review', + help='gerrit server hostname (default: %default)') + parser.add_option('-b', '--blocking', dest='blocking', + action='store_true', + help='block on event get (default: False)') + parser.add_option('-t', '--timeout', dest='timeout', + default=None, type='int', + help='timeout (seconds) for blocking event get ' + '(default: None)') + + (options, _args) = parser.parse_args() + if options.timeout and not options.blocking: + parser.error('Can only use -t with -b') + + logging.basicConfig(format='%(message)s', level=logging.INFO) + + gerrit = GerritClient(host=options.hostname) + gerrit.start_event_stream() + try: + while True: + event = gerrit.get_event(block=options.blocking, + timeout=options.timeout) + if event: + logging.info("Event: %s", event.name) + if isinstance(event, GerritStreamErrorEvent): + logging.error(event.error) + break + else: + logging.info("No event") + if not options.blocking: + time.sleep(1) + except KeyboardInterrupt: + logging.info("Terminated by user") + gerrit.stop_event_stream() + + +if __name__ == "__main__": + sys.exit(_main()) -- cgit v1.2.1