On demand compilation of coffeescript in the test server

This commit is contained in:
Kovid Goyal 2011-12-19 12:43:06 +05:30
parent 0ad159b8b1
commit cef64ff0e7
3 changed files with 34 additions and 73 deletions

View File

@ -2,9 +2,9 @@
<html> <html>
<head> <head>
<title>Testing CFI functionality</title> <title>Testing CFI functionality</title>
<script type="text/javascript" src="cfi.js"></script> <script type="text/javascript" src="../cfi.coffee"></script>
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="cfi-test.js"></script> <script type="text/javascript" src="cfi-test.coffee"></script>
</head> </head>
<body> <body>
<h1 id="first-h1" style="border: solid 1px red">Testing CFI functionality</h1> <h1 id="first-h1" style="border: solid 1px red">Testing CFI functionality</h1>

View File

@ -18,8 +18,8 @@ except ImportError:
def run_devel_server(): def run_devel_server():
os.chdir(os.path.dirname(__file__)) os.chdir(os.path.dirname(os.path.abspath(__file__)))
serve(['../cfi.coffee', 'cfi-test.coffee']) serve()
if __name__ == '__main__': if __name__ == '__main__':
run_devel_server() run_devel_server()

View File

@ -10,51 +10,21 @@ __docformat__ = 'restructuredtext en'
''' '''
Utilities to help with developing coffeescript based apps Utilities to help with developing coffeescript based apps
''' '''
import time, SimpleHTTPServer, SocketServer, threading, os, subprocess import time, SimpleHTTPServer, SocketServer, os, subprocess
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
generated_files = set()
def translate_path(self, path): def translate_path(self, path):
if path.endswith('jquery.js'): if path.endswith('jquery.js'):
return P('content_server/jquery.js') return P('content_server/jquery.js')
if path.endswith('.coffee'):
return self.compile_coffeescript(path[1:])
return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self, return SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self,
path) path)
class Server(threading.Thread):
def __init__(self, port=8000):
threading.Thread.__init__(self)
self.port = port
self.daemon = True
self.httpd = SocketServer.TCPServer(("localhost", port), Handler)
def run(self):
print('serving at localhost:%d'%self.port)
self.httpd.serve_forever()
def end(self):
self.httpd.shutdown()
self.join()
class Compiler(threading.Thread):
def __init__(self, coffee_files):
threading.Thread.__init__(self)
self.daemon = True
if not isinstance(coffee_files, dict):
coffee_files = {x:os.path.splitext(os.path.basename(x))[0]+'.js'
for x in coffee_files}
a = os.path.abspath
self.src_map = {a(x):a(y) for x, y in coffee_files.iteritems()}
self.keep_going = True
def run(self):
while self.keep_going:
for src, dest in self.src_map.iteritems():
if self.newer(src, dest):
self.compile(src, dest)
time.sleep(0.1)
def newer(self, src, dest): def newer(self, src, dest):
try: try:
sstat = os.stat(src) sstat = os.stat(src)
@ -64,42 +34,33 @@ class Compiler(threading.Thread):
return (not os.access(dest, os.R_OK) or sstat.st_mtime > return (not os.access(dest, os.R_OK) or sstat.st_mtime >
os.stat(dest).st_mtime) os.stat(dest).st_mtime)
def compile(self, src, dest): def compile_coffeescript(self, src):
with open(dest, 'wb') as f: dest = os.path.splitext(src)[0] + '.js'
try: self.generated_files.add(dest)
subprocess.check_call(['coffee', '-c', '-p', src], stdout=f) if self.newer(src, dest):
except: with open(dest, 'wb') as f:
print('Compilation of %s failed'%src) try:
f.seek(0) subprocess.check_call(['coffee', '-c', '-p', src], stdout=f)
f.truncate() except:
f.write('// Compilation of cofeescript failed') print('Compilation of %s failed'%src)
f.seek(0)
f.truncate()
f.write('// Compilation of coffeescript failed')
f.write('alert("Compilation of %s failed");'%src)
return dest
def end(self): def serve(port=8000):
self.keep_going = False httpd = SocketServer.TCPServer(('localhost', port), Handler)
self.join() print('serving at localhost:%d'%port)
for x in self.src_map.itervalues(): try:
try:
httpd.serve_forever()
except KeyboardInterrupt:
raise SystemExit(0)
finally:
for x in Handler.generated_files:
try: try:
os.remove(x) os.remove(x)
except: except:
pass pass
def serve(coffee_files, port=8000):
ws = Server(port=port)
comp = Compiler(coffee_files)
comp.start()
ws.start()
try:
while True:
time.sleep(1)
if not comp.is_alive() or not ws.is_alive():
print ('Worker failed')
raise SystemExit(1)
except KeyboardInterrupt:
pass
finally:
try:
comp.end()
except:
pass
ws.end()