Skip to content

Commit cc58cc1

Browse files
SilverRainZe-rouxAA-Turner
authored
Add a --post-build argument (#190)
Co-authored-by: Emmanuel Roux <[email protected]> Originally-authored-by: Emmanuel Roux <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent 92bcf10 commit cc58cc1

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

README.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ which can seen by running ``sphinx-autobuild --help``:
6767
--delay DELAY how long to wait before opening the browser
6868
--watch DIR additional directories to watch
6969
--pre-build COMMAND additional command(s) to run prior to building the documentation
70+
--post-build COMMAND additional command(s) to run after building the documentation
7071
7172
Using with Makefile
7273
-------------------
@@ -140,6 +141,19 @@ to avoid needing to manually manage ports and opening browser windows
140141
sphinx-autobuild --port=0 --open-browser pikachu/docs pikachu/docs/_build/html &
141142
sphinx-autobuild --port=0 --open-browser magikarp/docs magickarp/docs/_build/html &
142143
144+
Notifications for build cycles
145+
------------------------------
146+
147+
As an example of using the ``--pre-build`` and ``--post-build`` arguments,
148+
the command below uses `notify-send` to send a notification when a build
149+
starts and finishes.
150+
151+
.. code-block:: bash
152+
153+
sphinx-autobuild docs docs/_build/html/ \
154+
--pre-build 'notify-send "sphinx-autobuild: build start"' \
155+
--post-build 'notify-send "sphinx-autobuild: build end"'
156+
143157
Relevant Sphinx Bugs
144158
====================
145159

sphinx_autobuild/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def main(argv=()):
4949
url_host = f"{host_name}:{port_num}"
5050

5151
pre_build_commands = list(map(shlex.split, args.pre_build))
52-
52+
post_build_commands = list(map(shlex.split, args.post_build))
5353
builder = Builder(
5454
build_args,
5555
url_host=url_host,
5656
pre_build_commands=pre_build_commands,
57+
post_build_commands=post_build_commands,
5758
)
5859

5960
watch_dirs = [src_dir] + args.additional_watched_dirs
@@ -233,6 +234,13 @@ def _add_autobuild_arguments(parser):
233234
default=[],
234235
help="additional command(s) to run prior to building the documentation",
235236
)
237+
group.add_argument(
238+
"--post-build",
239+
action="append",
240+
metavar="COMMAND",
241+
default=[],
242+
help="additional command(s) to run after building the documentation",
243+
)
236244
return group
237245

238246

sphinx_autobuild/build.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616

1717
class 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

tests/test_application.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def test_application(tmp_path):
2121
url_host = "127.0.0.1:7777"
2222
ignore_handler = IgnoreFilter([out_dir], [])
2323
builder = Builder(
24-
[str(src_dir), str(out_dir)], url_host=url_host, pre_build_commands=[]
24+
[str(src_dir), str(out_dir)],
25+
url_host=url_host,
26+
pre_build_commands=[],
27+
post_build_commands=[],
2528
)
2629
app = _create_app([src_dir], ignore_handler, builder, out_dir, url_host)
2730
client = TestClient(app)

0 commit comments

Comments
 (0)