diff --git a/sphinx_autobuild/build.py b/sphinx_autobuild/build.py index 6fc01f4..3055b7c 100644 --- a/sphinx_autobuild/build.py +++ b/sphinx_autobuild/build.py @@ -2,9 +2,10 @@ import shlex import subprocess -import sys from colorama import Fore, Style +# This isn't public API, but we want to avoid a subprocess call +from sphinx.cmd.build import build_main def _log(text, *, colour): @@ -31,8 +32,6 @@ def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands): def __call__(self): """Generate the documentation using ``sphinx``.""" - sphinx_command = [sys.executable, "-m", "sphinx"] + self.sphinx_args - if self.watcher.filepath: show(context=f"Detected change: {self.watcher.filepath}") @@ -40,24 +39,30 @@ def __call__(self): for command in self.pre_build_commands: show(context="pre-build", command=command) subprocess.run(command, check=True) - - show(command=["sphinx-build"] + self.sphinx_args) - subprocess.run(sphinx_command, check=True) except subprocess.CalledProcessError as e: - self.cmd_exit(e.returncode) - finally: - # We present this information, so that the user does not need to keep track - # of the port being used. It is presented by livereload when starting the - # server, so don't present it in the initial build. - if self.watcher.filepath: - show(context=f"Serving on {self.uri}") - - @staticmethod - def cmd_exit(return_code): - print(f"Command exited with exit code: {return_code}") - print( - "The server will continue serving the build folder, but the contents " - "being served are no longer in sync with the documentation sources. " - "Please fix the cause of the error above or press Ctrl+C to stop the " - "server." - ) + print(f"Pre-build command exited with exit code: {e.returncode}") + print( + "Please fix the cause of the error above or press Ctrl+C to stop the " + "server." + ) + raise + + show(command=["sphinx-build"] + self.sphinx_args) + + # NOTE: + # sphinx.cmd.build.build_main is not considered to be public API, + # but as this is a first-party project, we can cheat a little bit. + return_code = build_main(self.sphinx_args) + if return_code: + print(f"Sphinx exited with exit code: {return_code}") + print( + "The server will continue serving the build folder, but the contents " + "being served are no longer in sync with the documentation sources. " + "Please fix the cause of the error above or press Ctrl+C to stop the " + "server." + ) + # We present this information, so that the user does not need to keep track + # of the port being used. It is presented by livereload when starting the + # server, so don't present it in the initial build. + if self.watcher.filepath: + show(context=f"Serving on {self.uri}")