changeset 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
files caja-js/my-caja-module.js js/my-caja-module.co.js js/my-caja-module.js pavement.py
diffstat 4 files changed, 65 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/caja-js/my-caja-module.js	Sun Jun 07 20:20:17 2009 -0700
@@ -0,0 +1,5 @@
+function blarg(x) {
+  return x + 1;
+}
+
+register({blarg: blarg});
--- a/js/my-caja-module.co.js	Sun Jun 07 19:29:10 2009 -0700
+++ b/js/my-caja-module.co.js	Sun Jun 07 20:20:17 2009 -0700
@@ -13,6 +13,6 @@
                    },
                    'cajolerName': 'com.google.caja',
                    'cajolerVersion': '3532',
-                   'cajoledDate': 1244334865384
+                   'cajoledDate': 1244428337358
                  });
 }
--- a/js/my-caja-module.js	Sun Jun 07 19:29:10 2009 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-function blarg(x) {
-  return x + 1;
-}
-
-register({blarg: blarg});
--- a/pavement.py	Sun Jun 07 19:29:10 2009 -0700
+++ b/pavement.py	Sun Jun 07 20:20:17 2009 -0700
@@ -1,10 +1,63 @@
+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.
+            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
-def auto(options):
-    subprocess.call(
-        ["../google-caja-read-only/bin/cajole_html",
-         "-i", "js/my-caja-module.js",
-         "-o", "js/my-caja-module.co.js"]
-        )
+@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()