Skip to content

Commit

Permalink
Refactor get_builder() into a Builder callable
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Feb 4, 2024
1 parent 0ffc86f commit bda901a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
4 changes: 2 additions & 2 deletions sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from livereload import Server

from sphinx_autobuild import __version__
from sphinx_autobuild.build import SPHINX_BUILD_OPTIONS, get_builder
from sphinx_autobuild.build import SPHINX_BUILD_OPTIONS, Builder
from sphinx_autobuild.ignore import get_ignore
from sphinx_autobuild.utils import find_free_port

Expand Down Expand Up @@ -176,7 +176,7 @@ def main():
server = Server()

build_args, pre_build_commands = _get_build_args(args)
builder = get_builder(
builder = Builder(
server.watcher,
build_args,
host=args.host,
Expand Down
48 changes: 26 additions & 22 deletions sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,43 @@ def show(*, context=None, command=None):
_log(msg, colour=Fore.BLUE)


def get_builder(watcher, sphinx_args, *, host, port, pre_build_commands):
"""Prepare the function that calls sphinx."""
sphinx_command = [sys.executable, "-m", "sphinx"] + sphinx_args
class Builder:
def __init__(self, watcher, sphinx_args, *, host, port, pre_build_commands):
self.watcher = watcher
self.sphinx_args = sphinx_args
self.pre_build_commands = pre_build_commands
self.uri = f"http://{host}:{port}"

def build():
def __call__(self):
"""Generate the documentation using ``sphinx``."""
if watcher.filepath:
show(context=f"Detected change: {watcher.filepath}")

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 pre_build_commands:
for command in self.pre_build_commands:
show(context="pre-build", command=command)
subprocess.run(command, check=True)

show(command=["sphinx-build"] + sphinx_args)
show(command=["sphinx-build"] + self.sphinx_args)
subprocess.run(sphinx_command, check=True)
except subprocess.CalledProcessError as e:
cmd_exit(e.returncode)
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 watcher.filepath:
show(context=f"Serving on http://{host}:{port}")

return build

if self.watcher.filepath:
show(context=f"Serving on {self.uri}")

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."
)
@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."
)

0 comments on commit bda901a

Please sign in to comment.