Skip to content

Commit

Permalink
Print the upgrade message as soon as possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
rblank committed Sep 12, 2024
1 parent 2e1bb66 commit cd874c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion tdoc/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sphinx.util import logging

__project__ = 't-doc-common'
__version__ = '0.7'
__version__ = '0.8.dev1'

_common = pathlib.Path(__file__).absolute().parent
_root = _common.parent.parent
Expand Down Expand Up @@ -94,6 +94,7 @@ class ExecBlock(nodes.literal_block): pass

class Exec(CodeBlock):
# TODO: :include:
# TODO: Validate the language against the list of supported languages

option_spec = CodeBlock.option_spec | {
'after': directives.unchanged,
Expand Down
17 changes: 9 additions & 8 deletions tdoc/common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def __init__(self, *args, cfg, **kwargs):
self.directory = self.build_dir(0) / 'html'
self.upgrade_msg = None
self.stop = False
self.building = False
self.builder = threading.Thread(target=self.watch_and_build)
self.builder.start()
self.checker = threading.Thread(target=self.check_upgrade, daemon=True)
Expand All @@ -159,14 +160,12 @@ def server_bind(self):
return super().server_bind()

def finish_request(self, request, client_addr):
with self.lock:
directory = self.directory
with self.lock: directory = self.directory
self.RequestHandlerClass(request, client_addr, self,
directory=directory)

def server_close(self):
with self.lock:
self.stop = True
with self.lock: self.stop = True
self.builder.join()
return super().server_close()

Expand All @@ -192,8 +191,7 @@ def watch_and_build(self):
"\nSource change detected, rebuilding\n")
prev_mtime = mtime
if build := self.build(mtime):
with self.lock:
self.directory = build / 'html'
with self.lock: self.directory = build / 'html'
self.print_serving()
if build_mtime is not None: self.remove_build_dir(build_mtime)
build_mtime = mtime
Expand Down Expand Up @@ -223,11 +221,14 @@ def build_dir(self, mtime):

def build(self, mtime):
build = self.build_dir(mtime)
with self.lock: self.building = True
try:
res = sphinx_build(self.cfg, 'html', build=build)
if res.returncode == 0: return build
except Exception as e:
self.cfg.stderr.write(f"Build: {e}\n")
finally:
with self.lock: self.building = False

def remove_build_dir(self, mtime):
build = self.build_dir(mtime)
Expand All @@ -241,8 +242,7 @@ def print_serving(self):
if ':' in host: host = f'[{host}]'
self.cfg.stdout.write(self.cfg.ansi("Serving at <@{LBLUE}%s@{NORM}>\n")
% f"http://{host}:{port}/")
with self.lock:
msg = self.upgrade_msg
with self.lock: msg = self.upgrade_msg
if msg: self.cfg.stdout.write(msg)

def check_upgrade(self):
Expand All @@ -258,6 +258,7 @@ def check_upgrade(self):
'development' if editable else 'install'))
with self.lock:
self.upgrade_msg = msg
if not self.building: self.cfg.stdout.write(msg)
except Exception:
if self.cfg.debug: raise

Expand Down

0 comments on commit cd874c9

Please sign in to comment.