Put all compiled coffeescript into a zip file in resources

This commit is contained in:
Kovid Goyal 2012-05-06 00:01:15 +05:30
parent 1481f62758
commit 8a76489706
4 changed files with 94 additions and 53 deletions

View File

@ -2,6 +2,7 @@
.check-cache.pickle .check-cache.pickle
src/calibre/plugins src/calibre/plugins
resources/images.qrc resources/images.qrc
resources/compiled_coffeescript.zip
src/calibre/ebooks/oeb/display/test/*.js src/calibre/ebooks/oeb/display/test/*.js
src/calibre/manual/.build/ src/calibre/manual/.build/
src/calibre/manual/cli/ src/calibre/manual/cli/
@ -16,7 +17,6 @@ resources/ebook-convert-complete.pickle
resources/builtin_recipes.xml resources/builtin_recipes.xml
resources/builtin_recipes.zip resources/builtin_recipes.zip
resources/template-functions.json resources/template-functions.json
resources/display/*.js
setup/installer/windows/calibre/build.log setup/installer/windows/calibre/build.log
src/calibre/translations/.errors src/calibre/translations/.errors
src/cssutils/.svn/ src/cssutils/.svn/

View File

@ -26,7 +26,7 @@ def get_opts_from_parser(parser):
class Coffee(Command): # {{{ class Coffee(Command): # {{{
description = 'Compile coffeescript files into javascript' description = 'Compile coffeescript files into javascript'
COFFEE_DIRS = {'ebooks/oeb/display': 'display'} COFFEE_DIRS = ('ebooks/oeb/display',)
def add_options(self, parser): def add_options(self, parser):
parser.add_option('--watch', '-w', action='store_true', default=False, parser.add_option('--watch', '-w', action='store_true', default=False,
@ -47,49 +47,69 @@ class Coffee(Command): # {{{
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
def show_js(self, jsfile): def show_js(self, raw):
from pygments.lexers import JavascriptLexer from pygments.lexers import JavascriptLexer
from pygments.formatters import TerminalFormatter from pygments.formatters import TerminalFormatter
from pygments import highlight from pygments import highlight
with open(jsfile, 'rb') as f:
raw = f.read()
print highlight(raw, JavascriptLexer(), TerminalFormatter()) print highlight(raw, JavascriptLexer(), TerminalFormatter())
def do_coffee_compile(self, opts, timestamp=False, ignore_errors=False): def do_coffee_compile(self, opts, timestamp=False, ignore_errors=False):
for toplevel, dest in self.COFFEE_DIRS.iteritems(): src_files = {}
dest = self.j(self.RESOURCES, dest) for src in self.COFFEE_DIRS:
for x in glob.glob(self.j(self.SRC, __appname__, toplevel, '*.coffee')): for f in glob.glob(self.j(self.SRC, __appname__, src,
js = self.j(dest, os.path.basename(x.rpartition('.')[0]+'.js')) '*.coffee')):
if self.newer(js, x): bn = os.path.basename(f).rpartition('.')[0]
print ('\t%sCompiling %s'%(time.strftime('[%H:%M:%S] ') if arcname = src.replace('/', '.') + '.' + bn + '.js'
timestamp else '', os.path.basename(x))) src_files[arcname] = (f, os.stat(f).st_mtime)
try:
cs = subprocess.check_output(self.compiler + existing = {}
[x]).decode('utf-8') dest = self.j(self.RESOURCES, 'compiled_coffeescript.zip')
except Exception as e: if os.path.exists(dest):
print ('\n\tCompilation of %s failed'%os.path.basename(x)) with zipfile.ZipFile(dest, 'r') as zf:
print (e) for info in zf.infolist():
if ignore_errors: mtime = time.mktime(info.date_time + (0, 0, -1))
with open(js, 'wb') as f: arcname = info.filename
f.write('# Compilation from coffeescript failed') if (arcname in src_files and src_files[arcname][1] <
else: mtime):
raise SystemExit(1) existing[arcname] = (zf.read(info), info)
else:
with open(js, 'wb') as f: todo = set(src_files) - set(existing)
f.write(cs.encode('utf-8')) updated = {}
if opts.show_js: for arcname in todo:
self.show_js(js) name = arcname.rpartition('.')[0]
print ('#'*80) print ('\t%sCompiling %s'%(time.strftime('[%H:%M:%S] ') if
print ('#'*80) timestamp else '', name))
src = src_files[arcname][0]
try:
js = subprocess.check_output(self.compiler +
[src]).decode('utf-8')
except Exception as e:
print ('\n\tCompilation of %s failed'%name)
print (e)
if ignore_errors:
js = u'# Compilation from coffeescript failed'
else:
raise SystemExit(1)
else:
if opts.show_js:
self.show_js(js)
print ('#'*80)
print ('#'*80)
zi = zipfile.ZipInfo()
zi.filename = arcname
zi.date_time = time.localtime()[:6]
updated[arcname] = (js.encode('utf-8'), zi)
if updated:
with zipfile.ZipFile(dest, 'w', zipfile.ZIP_STORED) as zf:
for raw, zi in updated.itervalues():
zf.writestr(zi, raw)
for raw, zi in existing.itervalues():
zf.writestr(zi, raw)
def clean(self): def clean(self):
for toplevel, dest in self.COFFEE_DIRS.iteritems(): x = self.j(self.RESOURCES, 'compiled_coffeescript.zip')
dest = self.j(self.RESOURCES, dest) if os.path.exists(x):
for x in glob.glob(self.j(self.SRC, __appname__, toplevel, '*.coffee')): os.remove(x)
x = x.rpartition('.')[0] + '.js'
x = self.j(dest, os.path.basename(x))
if os.path.exists(x):
os.remove(x)
# }}} # }}}
class Kakasi(Command): # {{{ class Kakasi(Command): # {{{

View File

@ -11,6 +11,7 @@ import os, zipfile
import calibre import calibre
from calibre.utils.localization import lang_as_iso639_1 from calibre.utils.localization import lang_as_iso639_1
from calibre.utils.resources import compiled_coffeescript
class JavaScriptLoader(object): class JavaScriptLoader(object):
@ -27,7 +28,7 @@ class JavaScriptLoader(object):
}.iteritems()} }.iteritems()}
CS = { CS = {
'cfi':('ebooks/oeb/display/cfi.coffee', 'display/cfi.js'), 'cfi':'ebooks.oeb.display.cfi',
} }
ORDER = ('jquery', 'jquery_scrollTo', 'bookmarks', 'referencing', 'images', ORDER = ('jquery', 'jquery_scrollTo', 'bookmarks', 'referencing', 'images',
@ -59,21 +60,9 @@ class JavaScriptLoader(object):
ans = P(src, data=True, ans = P(src, data=True,
allow_user_override=False).decode('utf-8') allow_user_override=False).decode('utf-8')
else: else:
f = getattr(calibre, '__file__', None) dynamic = (self._dynamic_coffeescript and
if self._dynamic_coffeescript and f and os.path.exists(f): os.path.exists(calibre.__file__))
src = src[0] ans = compiled_coffeescript(src, dynamic=dynamic).decode('utf-8')
src = os.path.join(os.path.dirname(f), *(src.split('/')))
from calibre.utils.serve_coffee import compile_coffeescript
with open(src, 'rb') as f:
cs, errors = compile_coffeescript(f.read(), src)
if errors:
for line in errors:
print (line)
raise Exception('Failed to compile coffeescript'
': %s'%src)
ans = cs
else:
ans = P(src[1], data=True, allow_user_override=False)
self._cache[name] = ans self._cache[name] = ans
return ans return ans

View File

@ -72,5 +72,37 @@ def get_path(path, data=False, allow_user_override=True):
def get_image_path(path, data=False, allow_user_override=True): def get_image_path(path, data=False, allow_user_override=True):
return get_path('images/'+path, data=data) return get_path('images/'+path, data=data)
def _compile_coffeescript(name):
from calibre.utils.serve_coffee import compile_coffeescript
path = (u'/'.join(name.split('.'))) + '.coffee'
d = os.path.dirname
base = d(d(os.path.abspath(__file__)))
src = os.path.join(base, path)
with open(src, 'rb') as f:
cs, errors = compile_coffeescript(f.read(), src)
if errors:
for line in errors:
print (line)
raise Exception('Failed to compile coffeescript'
': %s'%src)
return cs
def compiled_coffeescript(name, dynamic=False):
if dynamic:
return _compile_coffeescript(name)
else:
import zipfile
zipf = get_path('compiled_coffeescript.zip', allow_user_override=False)
try:
with zipfile.ZipFile(zipf, 'r') as zf:
return zf.read(name+'.js')
except EnvironmentError:
# zipfile does not exist, probably someone running with
# CALIBRE_DEVELOP_FROM and an outdated binary install, so try to
# compile from source
if os.path.exists(zipf): raise
return _compile_coffeescript(name)
__builtin__.__dict__['P'] = get_path __builtin__.__dict__['P'] = get_path
__builtin__.__dict__['I'] = get_image_path __builtin__.__dict__['I'] = get_image_path