Only dynamically compile actually changed coffeescript

This commit is contained in:
Kovid Goyal 2014-09-21 21:19:48 +05:30
parent 9529fa26dc
commit ef39b9b584

View File

@ -77,12 +77,15 @@ def get_image_path(path, data=False, allow_user_override=True):
return get_path('images', allow_user_override=allow_user_override) return get_path('images', allow_user_override=allow_user_override)
return get_path('images/'+path, data=data, allow_user_override=allow_user_override) return get_path('images/'+path, data=data, allow_user_override=allow_user_override)
def _compile_coffeescript(name): def js_name_to_path(name, ext='.coffee'):
from calibre.utils.serve_coffee import compile_coffeescript path = (u'/'.join(name.split('.'))) + ext
path = (u'/'.join(name.split('.'))) + '.coffee'
d = os.path.dirname d = os.path.dirname
base = d(d(os.path.abspath(__file__))) base = d(d(os.path.abspath(__file__)))
src = os.path.join(base, path) return os.path.join(base, path)
def _compile_coffeescript(name):
from calibre.utils.serve_coffee import compile_coffeescript
src = js_name_to_path(name)
with open(src, 'rb') as f: with open(src, 'rb') as f:
cs, errors = compile_coffeescript(f.read(), src) cs, errors = compile_coffeescript(f.read(), src)
if errors: if errors:
@ -93,12 +96,19 @@ def _compile_coffeescript(name):
return cs return cs
def compiled_coffeescript(name, dynamic=False): def compiled_coffeescript(name, dynamic=False):
if dynamic: import zipfile
return _compile_coffeescript(name) zipf = get_path('compiled_coffeescript.zip', allow_user_override=False)
else: with zipfile.ZipFile(zipf, 'r') as zf:
import zipfile if dynamic:
zipf = get_path('compiled_coffeescript.zip', allow_user_override=False) import json
with zipfile.ZipFile(zipf, 'r') as zf: existing_hash = json.loads(zf.comment or '{}').get(name + '.js')
if existing_hash is not None:
import hashlib
with open(js_name_to_path(name), 'rb') as f:
if existing_hash == hashlib.sha1(f.read()).hexdigest():
return zf.read(name + '.js')
return _compile_coffeescript(name)
else:
return zf.read(name+'.js') return zf.read(name+'.js')
__builtin__.__dict__['P'] = get_path __builtin__.__dict__['P'] = get_path