1515
1616
1717class Builder :
18- def __init__ (self , sphinx_args , * , url_host , pre_build_commands ):
18+ def __init__ (
19+ self , sphinx_args , * , url_host , pre_build_commands , post_build_commands
20+ ):
1921 self .sphinx_args = sphinx_args
2022 self .pre_build_commands = pre_build_commands
23+ self .post_build_commands = post_build_commands
2124 self .uri = f"http://{ url_host } "
2225
2326 def __call__ (self , * , changed_paths : Sequence [Path ]):
@@ -35,24 +38,7 @@ def __call__(self, *, changed_paths: Sequence[Path]):
3538 show_message (f"Detected changes ({ ', ' .join (rel_paths )} )" )
3639 show_message ("Rebuilding..." )
3740
38- try :
39- for command in self .pre_build_commands :
40- show_message ("pre-build" )
41- show_command (command )
42- subprocess .run (command , check = True )
43- except subprocess .CalledProcessError as e :
44- print (f"Pre-build command exited with exit code: { e .returncode } " )
45- print (
46- "Please fix the cause of the error above or press Ctrl+C to stop the "
47- "server."
48- )
49- print (
50- "The server will continue serving the build folder, but the contents "
51- "being served are no longer in sync with the documentation sources. "
52- "Please fix the cause of the error above or press Ctrl+C to stop the "
53- "server."
54- )
55- traceback .print_exception (e )
41+ if self ._run_commands (self .pre_build_commands , "pre-build" ) != 0 :
5642 return
5743
5844 if sphinx .version_info [:3 ] >= (7 , 2 , 3 ):
@@ -70,5 +56,33 @@ def __call__(self, *, changed_paths: Sequence[Path]):
7056 "Please fix the cause of the error above or press Ctrl+C to stop the "
7157 "server."
7258 )
59+ else :
60+ # Run the post-build commands only if the build was successful
61+ self ._run_commands (self .post_build_commands , "post-build" )
62+
7363 # Remind the user of the server URL for convenience.
7464 show_message (f"Serving on { self .uri } " )
65+
66+ def _run_commands (self , commands , log_context ):
67+ try :
68+ for command in commands :
69+ show_message (log_context )
70+ show_command (command )
71+ subprocess .run (command , check = True )
72+ except subprocess .CalledProcessError as e :
73+ print (
74+ f"{ log_context .title ()} command exited with exit code: { e .returncode } "
75+ )
76+ print (
77+ "Please fix the cause of the error above or press Ctrl+C to stop the "
78+ "server."
79+ )
80+ print (
81+ "The server will continue serving the build folder, but the contents "
82+ "being served are no longer in sync with the documentation sources. "
83+ "Please fix the cause of the error above or press Ctrl+C to stop the "
84+ "server."
85+ )
86+ traceback .print_exception (e )
87+ return e .returncode
88+ return 0
0 commit comments