Mercurial > caja-test
comparison pavement.py @ 1:00d50391d378
Added runserver target to paver.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 07 Jun 2009 20:20:17 -0700 |
parents | 633c9cb05555 |
children | 6737bc744b46 |
comparison
equal
deleted
inserted
replaced
0:633c9cb05555 | 1:00d50391d378 |
---|---|
1 import md5 | |
2 import os | |
1 import subprocess | 3 import subprocess |
4 import wsgiref | |
5 import wsgiref.simple_server | |
2 from paver.easy import * | 6 from paver.easy import * |
3 | 7 |
8 DEFAULT_PORT = 8080 | |
9 DEFAULT_HOST = '127.0.0.1' | |
10 | |
11 class CajolerWebApp(object): | |
12 def __init__(self, cajoler_path): | |
13 self.cajoler_path = cajoler_path | |
14 self.cache = {} | |
15 | |
16 def cajole(self, filename): | |
17 contents = open(filename).read() | |
18 hash = md5.md5(contents).hexdigest() | |
19 if hash not in self.cache: | |
20 # TODO: This isn't threadsafe. | |
21 output_filename = "output.co.js" | |
22 retval = subprocess.call([self.cajoler_path, | |
23 "-i", filename, | |
24 "-o", output_filename]) | |
25 if retval == 0: | |
26 self.cache[hash] = open(output_filename).read() | |
27 os.remove(output_filename) | |
28 else: | |
29 self.cache[hash] = None | |
30 return self.cache[hash] | |
31 | |
32 def app(self, env, start_response): | |
33 path = env['PATH_INFO'] | |
34 parts = path.split('/')[1:] | |
35 if len(parts) == 1: | |
36 filename = os.path.join('caja-js', parts[0]) | |
37 if os.path.exists(filename) and not os.path.isdir(filename): | |
38 cajoled = self.cajole(filename) | |
39 if cajoled is None: | |
40 start_response('500 Internal Server Error', | |
41 [('Content-type', 'text/plain')]) | |
42 return ["Cajoling failed."] | |
43 start_response('200 OK', | |
44 [('Content-type', 'text/javascript')]) | |
45 return [cajoled] | |
46 start_response('404 Not Found', | |
47 [('Content-type', 'text/plain')]) | |
48 return ['Not found: %s' % path] | |
49 | |
4 @task | 50 @task |
5 def auto(options): | 51 @cmdopts((['port=', 'p', 'port to bind to'], |
6 subprocess.call( | 52 ['host=', 'o', 'host to bind to'], |
7 ["../google-caja-read-only/bin/cajole_html", | 53 ['cajoler-path=', 'c', 'path to cajole_html'])) |
8 "-i", "js/my-caja-module.js", | 54 def runserver(options): |
9 "-o", "js/my-caja-module.co.js"] | 55 port = int(options.get('port', DEFAULT_PORT)) |
10 ) | 56 host = options.get('host', DEFAULT_HOST) |
57 if not options.get('cajoler_path'): | |
58 raise Exception('Required option: --cajoler-path.') | |
59 print "Running server at %s port %d, press Ctrl-C to stop." % (host, | |
60 port) | |
61 app = CajolerWebApp(options.cajoler_path) | |
62 server = wsgiref.simple_server.make_server(host, port, app.app) | |
63 server.serve_forever() |