Skip to content

Commit

Permalink
Call Sphinx's build directly
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Feb 4, 2024
1 parent 6f9627e commit d57f117
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -31,33 +32,37 @@ 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}")

try:
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}")

0 comments on commit d57f117

Please sign in to comment.