| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 | |
| 3 """Implements a sample server to receive status_push notifications. | |
| 4 | |
| 5 It is mainly for testing. | |
| 6 Use with buildbot.status.status_push.StatusPush to receive all the buildbot | |
| 7 events. | |
| 8 """ | |
| 9 | |
| 10 import logging | |
| 11 import optparse | |
| 12 import sys | |
| 13 | |
| 14 try: | |
| 15 from urlparse import parse_qs | |
| 16 except ImportError: | |
| 17 from cgi import parse_qs | |
| 18 | |
| 19 import BaseHTTPServer | |
| 20 | |
| 21 try: | |
| 22 import simplejson as json | |
| 23 except ImportError: | |
| 24 try: | |
| 25 import json | |
| 26 except ImportError: | |
| 27 # We can live without it. | |
| 28 json = None | |
| 29 | |
| 30 | |
| 31 OPTIONS = None | |
| 32 | |
| 33 | |
| 34 class EventsHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
| 35 def do_POST(self): | |
| 36 try: | |
| 37 length = int(self.headers['Content-Length']) | |
| 38 except (ValueError, KeyError): | |
| 39 self.send_response(411) | |
| 40 return | |
| 41 | |
| 42 try: | |
| 43 if (self.headers['Content-Type'] != | |
| 44 'application/x-www-form-urlencoded'): | |
| 45 raise KeyError() | |
| 46 except KeyError: | |
| 47 self.send_response(406) | |
| 48 return | |
| 49 | |
| 50 data = self.rfile.read(length) | |
| 51 remaining = length - len(data) | |
| 52 while remaining: | |
| 53 data += self.rfile.read(remaining) | |
| 54 remaining = length - len(data) | |
| 55 | |
| 56 data_dict = parse_qs(data, True) | |
| 57 for packet in data_dict['packets']: | |
| 58 if json != None: | |
| 59 for p in json.loads(packet): | |
| 60 if OPTIONS.long: | |
| 61 print p | |
| 62 else: | |
| 63 print p['event'] | |
| 64 else: | |
| 65 if OPTIONS.long: | |
| 66 print packet | |
| 67 else: | |
| 68 print packet[:90] + '...' | |
| 69 self.send_response(200, 'OK') | |
| 70 self.send_header('Content-Type', 'text/plan') | |
| 71 self.end_headers() | |
| 72 self.wfile.write('OK') | |
| 73 | |
| 74 | |
| 75 def main(argv): | |
| 76 parser = optparse.OptionParser(usage='%prog [options]\n\n' + __doc__) | |
| 77 parser.add_option('-v', '--verbose', default=0, action='count', | |
| 78 help='Use multiple times to increase logging') | |
| 79 parser.add_option('-p', '--port', type='int', default=8000, | |
| 80 help='HTTP port to bind to; default=%default') | |
| 81 parser.add_option('-b', '--binding', default='', | |
| 82 help='IP address to bind, default=all') | |
| 83 parser.add_option('-l', '--long', action='store_true', | |
| 84 help='Prints the whole packet') | |
| 85 options, args = parser.parse_args(argv) | |
| 86 | |
| 87 if options.verbose == 0: | |
| 88 logging.basicConfig(level=logging.ERROR) | |
| 89 elif options.verbose == 1: | |
| 90 logging.basicConfig(level=logging.WARNING) | |
| 91 elif options.verbose == 2: | |
| 92 logging.basicConfig(level=logging.INFO) | |
| 93 else: | |
| 94 logging.basicConfig(level=logging.DEBUG) | |
| 95 | |
| 96 global OPTIONS | |
| 97 OPTIONS = options | |
| 98 | |
| 99 httpd = BaseHTTPServer.HTTPServer((options.binding, options.port), | |
| 100 EventsHandler) | |
| 101 if options.port == 0: | |
| 102 options.port = httpd.server_port | |
| 103 print 'Listening on port %d' % options.port | |
| 104 sys.stdout.flush() | |
| 105 httpd.serve_forever() | |
| 106 | |
| 107 | |
| 108 if __name__ == '__main__': | |
| 109 sys.exit(main(sys.argv)) | |
| 110 | |
| 111 # vim: set ts=4 sts=4 sw=4 et: | |
| OLD | NEW |