4 from flup.server.fcgi import WSGIServer
11 LOG_FILENAME = 'logs/zcommit.log'
13 # Set up a specific logger with our desired output level
14 logger = logging.getLogger(__name__)
15 logger.setLevel(logging.DEBUG)
17 # Add the log message handler to the logger
18 handler = logging.FileHandler(LOG_FILENAME)
19 logger.addHandler(handler)
21 def zephyr(klass, instance, zsig, msg):
22 # TODO: spoof the sender
23 logger.info("""About to send zephyr:
25 instance: %(instance)s
27 msg: %(msg)s""" % {'klass' : klass, 'instance' : instance,
28 'zsig' : zsig, 'msg' : msg})
29 cmd = ['zwrite', '-c', klass, '-i', instance,
30 '-s', zsig, '-d', '-m', msg]
31 subprocess.check_call(cmd)
33 class Application(object):
36 logger.debug('Hello world app reached')
41 def default(self, *args, **query):
43 return self._default(*args, **query)
45 logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc()))
48 def _default(self, *args, **query):
49 logger.info('A %s request with args: %r and query: %r' %
50 (cherrypy.request.method, args, query))
53 raise cherrypy.HTTPError(400, 'Invalid submission URL')
54 logger.debug('Passed validation')
55 for i in xrange(0, len(args), 2):
56 opts[args[i]] = args[i + 1]
57 logger.debug('Set opts')
58 if 'class' not in opts:
59 raise cherrypy.HTTPError(400, 'Must specify a zephyr class name')
60 logger.debug('Specified a class')
61 if cherrypy.request.method == 'POST':
62 logger.debug('About to load data')
63 payload = json.loads(query['payload'])
64 logger.debug('Loaded payload data')
67 zsig = '%s: %s' % (opts['zsig'], zsig)
68 logger.debug('Set zsig')
69 for c in reversed(payload['commits']):
70 inst = opts.get('instance', c['id'][:8])
73 actions.append('Added: %s\n' % c['added'])
75 actions.append('Removed: %s\n' % c['removed'])
77 actions.append('Modified: %s\n' % c['modified'])
79 actions = 'Something weird happened... could not figure out what action to take'
80 info = {'name' : c['author']['name'],
81 'email' : c['author']['email'],
82 'message' : c['message'],
83 'timestamp' : c['timestamp'],
86 msg = """%(name)s <%(email)s>
91 zephyr(opts['class'], inst, zsig, msg)
92 msg = 'Thanks for posting!'
94 msg = ('If you had sent a POST request to this URL, would have sent'
95 ' a zepyhr to -c %s' % opts['class'])
101 app = cherrypy.tree.mount(Application(), '/zcommit')
102 cherrypy.server.unsubscribe()
103 cherrypy.engine.start()
105 WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run()
107 cherrypy.engine.stop()
109 if __name__ == '__main__':