4 from flup.server.fcgi import WSGIServer
12 HERE = os.path.dirname(__file__)
13 ZWRITE = os.path.join(HERE, 'bin', 'zsend')
14 LOG_FILENAME = 'logs/zcommit.log'
16 # Set up a specific logger with our desired output level
17 logger = logging.getLogger(__name__)
18 logger.setLevel(logging.DEBUG)
20 # Add the log message handler to the logger
21 handler = logging.FileHandler(LOG_FILENAME)
22 logger.addHandler(handler)
24 def zephyr(sender, klass, instance, zsig, msg):
25 # TODO: spoof the sender
26 logger.info("""About to send zephyr:
29 instance: %(instance)s
31 msg: %(msg)s""" % {'sender' : sender,
33 'instance' : instance,
36 cmd = [ZWRITE, '-S', sender, '-c', klass, '-i', instance,
37 '-s', zsig, '-d', '-m', msg]
38 subprocess.check_call(cmd)
40 class Application(object):
43 logger.debug('Hello world app reached')
48 def default(self, *args, **query):
50 return self._default(*args, **query)
52 logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc()))
55 def _default(self, *args, **query):
56 logger.info('A %s request with args: %r and query: %r' %
57 (cherrypy.request.method, args, query))
60 raise cherrypy.HTTPError(400, 'Invalid submission URL')
61 logger.debug('Passed validation')
62 for i in xrange(0, len(args), 2):
63 opts[args[i]] = args[i + 1]
64 logger.debug('Set opts')
65 if 'class' not in opts:
66 raise cherrypy.HTTPError(400, 'Must specify a zephyr class name')
67 logger.debug('Specified a class')
68 if cherrypy.request.method == 'POST':
69 logger.debug('About to load data')
70 payload = json.loads(query['payload'])
71 logger.debug('Loaded payload data')
74 zsig = '%s: %s' % (opts['zsig'], zsig)
75 sender = opts.get('sender', 'daemon.zcommit')
76 logger.debug('Set zsig')
77 for c in reversed(payload['commits']):
78 inst = opts.get('instance', c['id'][:8])
81 actions.append('Added: %s\n' % ', '.join(c['added']))
83 actions.append('Removed: %s\n' % ', '.join(c['removed']))
85 actions.append('Modified: %s\n' % ', '.join(c['modified']))
87 actions.append('Something weird happened... could not figure out what action to take')
88 info = {'name' : c['author']['name'],
89 'email' : c['author']['email'],
90 'message' : c['message'],
91 'timestamp' : c['timestamp'],
92 'actions' : '--\n'.join(actions)}
94 msg = """%(name)s <%(email)s> (%(timestamp)s)
98 zephyr(sender, opts['class'], inst, zsig, msg)
99 msg = 'Thanks for posting!'
101 msg = ('If you had sent a POST request to this URL, would have sent'
102 ' a zepyhr to -c %s' % opts['class'])
108 app = cherrypy.tree.mount(Application(), '/zcommit')
109 cherrypy.server.unsubscribe()
110 cherrypy.engine.start()
112 WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run()
114 cherrypy.engine.stop()
116 if __name__ == '__main__':