diff --git a/src/calibre/srv/embedded.py b/src/calibre/srv/embedded.py index ecabba3f62..2f3bfc9333 100644 --- a/src/calibre/srv/embedded.py +++ b/src/calibre/srv/embedded.py @@ -46,10 +46,6 @@ class Server(object): self.opts = opts self.log, self.access_log = log, access_log self.handler.set_log(self.log) - _df = os.environ.get('CALIBRE_DEVELOP_FROM', None) - if _df and os.path.exists(_df): - from calibre.utils.rapydscript import compile_srv - compile_srv() @property def user_manager(self): @@ -91,6 +87,10 @@ class Server(object): pass reset_caches() # we reset the cache as the server tdir has changed try: + _df = os.environ.get('CALIBRE_DEVELOP_FROM', None) + if _df and os.path.exists(_df): + from calibre.utils.rapydscript import compile_srv + compile_srv() self.loop.serve_forever() except BaseException as e: self.exception = e diff --git a/src/calibre/utils/rapydscript.py b/src/calibre/utils/rapydscript.py index 61c9405d6c..799e1126e6 100644 --- a/src/calibre/utils/rapydscript.py +++ b/src/calibre/utils/rapydscript.py @@ -19,6 +19,7 @@ from threading import Thread, local from calibre import force_unicode from calibre.constants import __appname__, __version__, cache_dir +from calibre.utils.filenames import atomic_rename from calibre.utils.terminal import ANSIStream from duktape import Context, JSError, to_python from lzma.xz import compress, decompress @@ -223,10 +224,16 @@ def compile_srv(): html = f.read().replace(b'RESET_STYLES', reset, 1).replace(b'ICONS', icons, 1).replace(b'MAIN_JS', js, 1) manifest = create_manifest(html) - with lopen(os.path.join(base, 'index-generated.html'), 'wb') as f: - f.write(html) - with lopen(os.path.join(base, 'calibre.appcache'), 'wb') as f: - f.write(manifest) + + def atomic_write(name, content): + name = os.path.join(base, name) + tname = name + '.tmp' + with lopen(tname, 'wb') as f: + f.write(content) + atomic_rename(tname, name) + + atomic_write('index-generated.html', html) + atomic_write('calibre.appcache', manifest) # }}}