Skip to content

Commit d57f117

Browse files
committed
Call Sphinx's build directly
1 parent 6f9627e commit d57f117

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

sphinx_autobuild/build.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import shlex
44
import subprocess
5-
import sys
65

76
from colorama import Fore, Style
7+
# This isn't public API, but we want to avoid a subprocess call
8+
from sphinx.cmd.build import build_main
89

910

1011
def _log(text, *, colour):
@@ -31,33 +32,37 @@ def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands):
3132
def __call__(self):
3233
"""Generate the documentation using ``sphinx``."""
3334

34-
sphinx_command = [sys.executable, "-m", "sphinx"] + self.sphinx_args
35-
3635
if self.watcher.filepath:
3736
show(context=f"Detected change: {self.watcher.filepath}")
3837

3938
try:
4039
for command in self.pre_build_commands:
4140
show(context="pre-build", command=command)
4241
subprocess.run(command, check=True)
43-
44-
show(command=["sphinx-build"] + self.sphinx_args)
45-
subprocess.run(sphinx_command, check=True)
4642
except subprocess.CalledProcessError as e:
47-
self.cmd_exit(e.returncode)
48-
finally:
49-
# We present this information, so that the user does not need to keep track
50-
# of the port being used. It is presented by livereload when starting the
51-
# server, so don't present it in the initial build.
52-
if self.watcher.filepath:
53-
show(context=f"Serving on {self.uri}")
54-
55-
@staticmethod
56-
def cmd_exit(return_code):
57-
print(f"Command exited with exit code: {return_code}")
58-
print(
59-
"The server will continue serving the build folder, but the contents "
60-
"being served are no longer in sync with the documentation sources. "
61-
"Please fix the cause of the error above or press Ctrl+C to stop the "
62-
"server."
63-
)
43+
print(f"Pre-build command exited with exit code: {e.returncode}")
44+
print(
45+
"Please fix the cause of the error above or press Ctrl+C to stop the "
46+
"server."
47+
)
48+
raise
49+
50+
show(command=["sphinx-build"] + self.sphinx_args)
51+
52+
# NOTE:
53+
# sphinx.cmd.build.build_main is not considered to be public API,
54+
# but as this is a first-party project, we can cheat a little bit.
55+
return_code = build_main(self.sphinx_args)
56+
if return_code:
57+
print(f"Sphinx exited with exit code: {return_code}")
58+
print(
59+
"The server will continue serving the build folder, but the contents "
60+
"being served are no longer in sync with the documentation sources. "
61+
"Please fix the cause of the error above or press Ctrl+C to stop the "
62+
"server."
63+
)
64+
# We present this information, so that the user does not need to keep track
65+
# of the port being used. It is presented by livereload when starting the
66+
# server, so don't present it in the initial build.
67+
if self.watcher.filepath:
68+
show(context=f"Serving on {self.uri}")

0 commit comments

Comments
 (0)