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