diff --git a/.gitignore b/.gitignore index 9044b51d7b..bb107c4a89 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ resources/builtin_recipes.zip resources/template-functions.json resources/editor-functions.json resources/user-manual-translation-stats.json +resources/content-server/main.js icons/icns/*.iconset setup/installer/windows/calibre/build.log tags diff --git a/resources/content-server/index.html b/resources/content-server/index.html new file mode 100644 index 0000000000..93c1afedee --- /dev/null +++ b/resources/content-server/index.html @@ -0,0 +1,10 @@ + + + + calibre + + + + + + diff --git a/resources/rapydscript/compiler.js.xz b/resources/rapydscript/compiler.js.xz index 526284e6d3..d12c66cb63 100644 Binary files a/resources/rapydscript/compiler.js.xz and b/resources/rapydscript/compiler.js.xz differ diff --git a/src/calibre/srv/auto_reload.py b/src/calibre/srv/auto_reload.py index 291fb2bfcd..09470efecb 100644 --- a/src/calibre/srv/auto_reload.py +++ b/src/calibre/srv/auto_reload.py @@ -61,7 +61,7 @@ if islinux: for fd in r: w = self.fd_map[fd] modified |= w() - self.handle_modified() + self.handle_modified(modified) def ignore_event(self, path, name): return not self.file_is_watched(name) @@ -176,6 +176,7 @@ def find_dirs_to_watch(fpath, dirs, add_default_dirs): base = d(d(d(srv))) add(os.path.join(base, 'resources', 'server')) add(os.path.join(base, 'src', 'calibre', 'db')) + add(os.path.join(base, 'src', 'pyj')) return dirs def join_process(p, timeout=5): @@ -211,7 +212,9 @@ class Worker(object): self.p = None def restart(self): + from calibre.utils.rapydscript import compile_srv self.clean_kill() + compile_srv() self.p = subprocess.Popen(self.cmd, creationflags=getattr(subprocess, 'CREATE_NEW_PROCESS_GROUP', 0)) def auto_reload(log, dirs=frozenset(), cmd=None, add_default_dirs=True): diff --git a/src/calibre/srv/code.py b/src/calibre/srv/code.py new file mode 100644 index 0000000000..6e14398aae --- /dev/null +++ b/src/calibre/srv/code.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2015, Kovid Goyal + +from __future__ import (unicode_literals, division, absolute_import, + print_function) +import errno + +from calibre.srv.errors import HTTPNotFound +from calibre.srv.routes import endpoint + +@endpoint('', auth_required=False) +def index(ctx, rd): + return open(P('content-server/index.html'), 'rb') + +@endpoint('/js/{which}', auth_required=False) +def js(ctx, rd, which): + try: + return open(P('content-server/' + which), 'rb') + except EnvironmentError as e: + if e.errno == errno.ENOENT: + raise HTTPNotFound('No js with name: %r' % which) + raise diff --git a/src/calibre/srv/handler.py b/src/calibre/srv/handler.py index c2793bc093..0b248e59c5 100644 --- a/src/calibre/srv/handler.py +++ b/src/calibre/srv/handler.py @@ -131,7 +131,7 @@ class Handler(object): def __init__(self, libraries, opts, testing=False): self.router = Router(ctx=Context(libraries, opts, testing=testing), url_prefix=opts.url_prefix) - for module in ('content', 'ajax'): + for module in ('content', 'ajax', 'code'): module = import_module('calibre.srv.' + module) self.router.load_routes(vars(module).itervalues()) self.router.finalize() diff --git a/src/calibre/srv/standalone.py b/src/calibre/srv/standalone.py index 92477103a3..e351b16248 100644 --- a/src/calibre/srv/standalone.py +++ b/src/calibre/srv/standalone.py @@ -69,6 +69,10 @@ class Server(object): self.handler.set_log(self.loop.log) self.serve_forever = self.loop.serve_forever self.stop = self.loop.stop + _df = os.environ.get('CALIBRE_DEVELOP_FROM', None) + if _df and os.path.exists(_df): + from calibre.utils.rapydscript import compile_srv + compile_srv() def create_option_parser(): diff --git a/src/calibre/utils/rapydscript.py b/src/calibre/utils/rapydscript.py index 2f6b32a32c..d426e65c71 100644 --- a/src/calibre/utils/rapydscript.py +++ b/src/calibre/utils/rapydscript.py @@ -78,6 +78,17 @@ def compile_pyj(data, filename='', beautify=True, private_scope=True, lib } c.g.rs_source_code = data return c.eval('exports["compile"](rs_source_code, %s, current_options)' % json.dumps(filename)) + +def compile_srv(): + d = os.path.dirname + base = d(d(d(d(os.path.abspath(__file__))))) + rapydscript_dir = os.path.join(base, 'src', 'pyj') + fname = os.path.join(rapydscript_dir, 'srv.pyj') + with open(fname, 'rb') as f: + raw = compile_pyj(f.read(), fname) + with open(P('content-server/main.js', allow_user_override=False), 'wb') as f: + f.write(raw.encode('utf-8')) + # }}} # Translations {{{ diff --git a/src/pyj/srv.pyj b/src/pyj/srv.pyj new file mode 100644 index 0000000000..b0435b393b --- /dev/null +++ b/src/pyj/srv.pyj @@ -0,0 +1,5 @@ +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2015, Kovid Goyal + +alert('hello world!') +