Mercurial > caja-test
view pavement.py @ 3:8ca68c2a7217
caja-test.html now asks the server for cajoled js.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 07 Jun 2009 20:27:26 -0700 |
parents | 6737bc744b46 |
children | cf673c093b61 |
line wrap: on
line source
import md5 import os import subprocess import wsgiref import wsgiref.simple_server from paver.easy import * DEFAULT_PORT = 8080 DEFAULT_HOST = '127.0.0.1' class CajolerWebApp(object): def __init__(self, cajoler_path): self.cajoler_path = cajoler_path self.cache = {} def cajole(self, filename): contents = open(filename).read() hash = md5.md5(contents).hexdigest() if hash not in self.cache: # TODO: This isn't threadsafe. # TODO: There's no way to evict entries from the cache. output_filename = "output.co.js" retval = subprocess.call([self.cajoler_path, "-i", filename, "-o", output_filename]) if retval == 0: self.cache[hash] = open(output_filename).read() os.remove(output_filename) else: self.cache[hash] = None return self.cache[hash] def app(self, env, start_response): path = env['PATH_INFO'] parts = path.split('/')[1:] if len(parts) == 1: filename = os.path.join('caja-js', parts[0]) if os.path.exists(filename) and not os.path.isdir(filename): cajoled = self.cajole(filename) if cajoled is None: start_response('500 Internal Server Error', [('Content-type', 'text/plain')]) return ["Cajoling failed."] start_response('200 OK', [('Content-type', 'text/javascript')]) return [cajoled] start_response('404 Not Found', [('Content-type', 'text/plain')]) return ['Not found: %s' % path] @task @cmdopts((['port=', 'p', 'port to bind to'], ['host=', 'o', 'host to bind to'], ['cajoler-path=', 'c', 'path to cajole_html'])) def runserver(options): port = int(options.get('port', DEFAULT_PORT)) host = options.get('host', DEFAULT_HOST) if not options.get('cajoler_path'): raise Exception('Required option: --cajoler-path.') print "Running server at %s port %d, press Ctrl-C to stop." % (host, port) app = CajolerWebApp(options.cajoler_path) server = wsgiref.simple_server.make_server(host, port, app.app) server.serve_forever()