4 from flup.server.fcgi import WSGIServer
12 HERE = os.path.dirname(__file__)
13 os.environ['KRBTKFILE'] = os.path.join(HERE, 'tkt')
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(klass, instance, zsig, msg):
25 # TODO: spoof the sender
26 logger.info("""About to send zephyr:
28 instance: %(instance)s
30 msg: %(msg)s""" % {'klass' : klass, 'instance' : instance,
31 'zsig' : zsig, 'msg' : msg})
32 cmd = ['zwrite', '-c', klass, '-i', instance,
33 '-s', zsig, '-d', '-m', msg]
34 subprocess.check_call(cmd)
36 class Application(object):
39 logger.debug('Hello world app reached')
44 def default(self, *args, **query):
46 return self._default(*args, **query)
48 logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc()))
51 def _default(self, *args, **query):
52 logger.info('A %s request with args: %r and query: %r' %
53 (cherrypy.request.method, args, query))
56 raise cherrypy.HTTPError(400, 'Invalid submission URL')
57 logger.debug('Passed validation')
58 for i in xrange(0, len(args), 2):
59 opts[args[i]] = args[i + 1]
60 logger.debug('Set opts')
61 if 'class' not in opts:
62 raise cherrypy.HTTPError(400, 'Must specify a zephyr class name')
63 logger.debug('Specified a class')
64 if cherrypy.request.method == 'POST':
65 logger.debug('About to load data')
66 payload = json.loads(query['payload'])
67 logger.debug('Loaded payload data')
70 zsig = '%s: %s' % (opts['zsig'], zsig)
71 logger.debug('Set zsig')
72 for c in reversed(payload['commits']):
73 inst = opts.get('instance', c['id'][:8])
76 actions.append('Added: %s\n' % ', '.join(c['added']))
78 actions.append('Removed: %s\n' % ', '.join(c['removed']))
80 actions.append('Modified: %s\n' % ', '.join(c['modified']))
82 actions.append('Something weird happened... could not figure out what action to take')
83 info = {'name' : c['author']['name'],
84 'email' : c['author']['email'],
85 'message' : c['message'],
86 'timestamp' : c['timestamp'],
87 'actions' : '\n--\n'.join(actions)}
89 msg = """%(name)s <%(email)s>
94 zephyr(opts['class'], inst, zsig, msg)
95 msg = 'Thanks for posting!'
97 msg = ('If you had sent a POST request to this URL, would have sent'
98 ' a zepyhr to -c %s' % opts['class'])
104 app = cherrypy.tree.mount(Application(), '/zcommit')
105 cherrypy.server.unsubscribe()
106 cherrypy.engine.start()
108 WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run()
110 cherrypy.engine.stop()
112 if __name__ == '__main__':