2
2
3
3
import shlex
4
4
import subprocess
5
- import sys
6
5
7
6
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
8
9
9
10
10
11
def _log (text , * , colour ):
@@ -31,33 +32,37 @@ def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands):
31
32
def __call__ (self ):
32
33
"""Generate the documentation using ``sphinx``."""
33
34
34
- sphinx_command = [sys .executable , "-m" , "sphinx" ] + self .sphinx_args
35
-
36
35
if self .watcher .filepath :
37
36
show (context = f"Detected change: { self .watcher .filepath } " )
38
37
39
38
try :
40
39
for command in self .pre_build_commands :
41
40
show (context = "pre-build" , command = command )
42
41
subprocess .run (command , check = True )
43
-
44
- show (command = ["sphinx-build" ] + self .sphinx_args )
45
- subprocess .run (sphinx_command , check = True )
46
42
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