4 from flup.server.fcgi import WSGIServer
11 import dateutil.parser
13 HERE = os.path.abspath(os.path.dirname(__file__))
14 ZWRITE = os.path.join(HERE, 'bin', 'zsend')
15 LOG_FILENAME = 'logs/zcommit.log'
17 # Set up a specific logger with our desired output level
18 logger = logging.getLogger(__name__)
19 logger.setLevel(logging.DEBUG)
21 # Add the log message handler to the logger
22 handler = logging.FileHandler(LOG_FILENAME)
23 logger.addHandler(handler)
25 def zephyr(sender, klass, instance, zsig, msg):
26 # TODO: spoof the sender
27 logger.info("""About to send zephyr:
30 instance: %(instance)s
32 msg: %(msg)s""" % {'sender' : sender,
34 'instance' : instance,
37 cmd = [ZWRITE, '-S', sender, '-c', klass, '-i', instance,
38 '-s', zsig, '-d', '-m', msg]
39 subprocess.check_call([p.encode('utf-8') for p in cmd])
41 class Application(object):
44 logger.debug('Hello world app reached')
46 <p> <i>Welcome to zcommit.</i> </p>
48 <p> zcommit allows you to send zephyr notifications by sending an HTTP
49 POST request to a URL. Currently zcommit supports POST-backs from
50 github. If you would like it to support another form of POST-back,
51 please let us know (zcommit@mit.edu). </p>
53 <h1> URL structure </h1>
55 The URL you post to is structured as follows:
56 <tt>http://zcommit.mit.edu/$type/$key1/$value1/$key2/$value2/...</tt>.
57 So for example, the URL
58 <tt>http://zcommit.mit.edu/github/class/zcommit/instance/commit</tt>
59 is parsed as having type <tt>github</tt>, class <tt>zcommit</tt>, and
60 instance <tt>commit</tt>. Using this information, zcommit figures out
61 how to form a useful message which is then sends as a zephyr.
67 Set your POST-back URL to
68 <tt>http://zcommit.mit.edu/github/class/$classname</tt>, followed by
69 any of the following optional key/value parameters:
72 <li> <tt>/instance/$instance</tt> </li>
73 <li> <tt>/zsig/$zsig</tt> (sets the prefix of the zsig; the postfix is always the branch name) </li>
74 <li> <tt>/sender/$sender</tt> </li>
80 def default(self, *args, **query):
82 return self._default(*args, **query)
84 logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc()))
87 def _default(self, *args, **query):
88 logger.info('A %s request with args: %r and query: %r' %
89 (cherrypy.request.method, args, query))
92 raise cherrypy.HTTPError(400, 'Invalid submission URL')
93 logger.debug('Passed validation')
94 for i in xrange(0, len(args), 2):
95 opts[args[i]] = unicode(args[i + 1], 'utf-8', 'replace')
96 logger.debug('Set opts')
97 if 'class' not in opts:
98 raise cherrypy.HTTPError(400, 'Must specify a zephyr class name')
99 logger.debug('Specified a class')
100 if cherrypy.request.method == 'POST':
101 logger.debug('About to load data')
102 payload = json.loads(query['payload'])
103 logger.debug('Loaded payload data')
104 zsig = payload['ref']
106 zsig = '%s: %s' % (opts['zsig'], zsig)
107 sender = opts.get('sender', 'daemon.zcommit')
108 logger.debug('Set zsig')
109 for c in payload['commits']:
110 inst = opts.get('instance', c['id'][:8])
113 actions.extend(' A %s\n' % f for f in c['added'])
115 actions.extend(' D %s\n' % f for f in c['removed'])
116 if c.get('modified'):
117 actions.extend(' M %s\n' % f for f in c['modified'])
119 actions.append('Did not add/remove/modify any nonempty files.')
120 info = {'name' : c['author']['name'],
121 'email' : c['author']['email'],
122 'message' : c['message'],
123 'timestamp' : dateutil.parser.parse(c['timestamp']).strftime('%F %T %z'),
124 'actions' : ''.join(actions),
128 Author: %(name)s <%(email)s>
133 %(actions)s""" % info
134 zephyr(sender, opts['class'], inst, zsig, msg)
135 msg = 'Thanks for posting!'
137 msg = ('If you had sent a POST request to this URL, would have sent'
138 ' a zepyhr to -c %s' % opts['class'])
144 app = cherrypy.tree.mount(Application(), '/zcommit')
145 cherrypy.server.unsubscribe()
146 cherrypy.engine.start()
148 WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run()
150 cherrypy.engine.stop()
152 if __name__ == '__main__':