]> andersk Git - zcommit.git/blame - zcommit.py
Should not try to join on a string... heh
[zcommit.git] / zcommit.py
CommitLineData
2e3d139c
GB
1#!/usr/bin/python
2
3import cherrypy
4from flup.server.fcgi import WSGIServer
08b967d5
GB
5import logging
6import json
2e3d139c
GB
7import subprocess
8import sys
08b967d5
GB
9import traceback
10
11LOG_FILENAME = 'logs/zcommit.log'
12
13# Set up a specific logger with our desired output level
14logger = logging.getLogger(__name__)
15logger.setLevel(logging.DEBUG)
16
17# Add the log message handler to the logger
18handler = logging.FileHandler(LOG_FILENAME)
19logger.addHandler(handler)
2e3d139c
GB
20
21def zephyr(klass, instance, zsig, msg):
22 # TODO: spoof the sender
08b967d5
GB
23 logger.info("""About to send zephyr:
24class: %(klass)s
25instance: %(instance)s
26zsig: %(zsig)s
27msg: %(msg)s""" % {'klass' : klass, 'instance' : instance,
28 'zsig' : zsig, 'msg' : msg})
2e3d139c
GB
29 cmd = ['zwrite', '-c', klass, '-i', instance,
30 '-s', zsig, '-d', '-m', msg]
31 subprocess.check_call(cmd)
32
33class Application(object):
34 @cherrypy.expose
35 def index(self):
08b967d5 36 logger.debug('Hello world app reached')
2e3d139c
GB
37 return 'Hello world!'
38
2e3d139c
GB
39 class Github(object):
40 @cherrypy.expose
41 def default(self, *args, **query):
08b967d5
GB
42 try:
43 return self._default(*args, **query)
44 except Exception, e:
45 logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc()))
46 raise
47
48 def _default(self, *args, **query):
49 logger.info('A %s request with args: %r and query: %r' %
50 (cherrypy.request.method, args, query))
2e3d139c
GB
51 opts = {}
52 if len(args) % 2:
53 raise cherrypy.HTTPError(400, 'Invalid submission URL')
08b967d5 54 logger.debug('Passed validation')
2e3d139c
GB
55 for i in xrange(0, len(args), 2):
56 opts[args[i]] = args[i + 1]
08b967d5 57 logger.debug('Set opts')
2e3d139c
GB
58 if 'class' not in opts:
59 raise cherrypy.HTTPError(400, 'Must specify a zephyr class name')
08b967d5 60 logger.debug('Specified a class')
2e3d139c 61 if cherrypy.request.method == 'POST':
08b967d5 62 logger.debug('About to load data')
2e3d139c 63 payload = json.loads(query['payload'])
08b967d5 64 logger.debug('Loaded payload data')
2e3d139c
GB
65 zsig = payload['ref']
66 if 'zsig' in opts:
67 zsig = '%s: %s' % (opts['zsig'], zsig)
08b967d5 68 logger.debug('Set zsig')
2e3d139c
GB
69 for c in reversed(payload['commits']):
70 inst = opts.get('instance', c['id'][:8])
8ab83150
GB
71 actions = []
72 if c.get('added'):
92ce0c18 73 actions.append('Added: %s\n' % ', '.join(c['added']))
8ab83150 74 if c.get('removed'):
92ce0c18 75 actions.append('Removed: %s\n' % ', '.join(c['removed']))
8ab83150 76 if c.get('modified'):
92ce0c18 77 actions.append('Modified: %s\n' % ', '.join(c['modified']))
8ab83150 78 if not actions:
f0fbfde0 79 actions.append('Something weird happened... could not figure out what action to take')
08b967d5
GB
80 info = {'name' : c['author']['name'],
81 'email' : c['author']['email'],
82 'message' : c['message'],
83 'timestamp' : c['timestamp'],
92ce0c18 84 'actions' : '\n--\n'.join(actions)}
8ab83150 85
2e3d139c
GB
86 msg = """%(name)s <%(email)s>
87%(message)s
88%(timestamp)s
89--
8ab83150 90%(actions)s""" % info
2e3d139c 91 zephyr(opts['class'], inst, zsig, msg)
08b967d5 92 msg = 'Thanks for posting!'
2e3d139c
GB
93 else:
94 msg = ('If you had sent a POST request to this URL, would have sent'
95 ' a zepyhr to -c %s' % opts['class'])
96 return msg
97
98 github = Github()
99
100def main():
101 app = cherrypy.tree.mount(Application(), '/zcommit')
102 cherrypy.server.unsubscribe()
103 cherrypy.engine.start()
104 try:
105 WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run()
106 finally:
107 cherrypy.engine.stop()
108
109if __name__ == '__main__':
110 sys.exit(main())
This page took 0.056443 seconds and 5 git commands to generate.