Skip to content

Commit 34e1cde

Browse files
committed
cleanup
1 parent 3420416 commit 34e1cde

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

sphinxdocs/private/sphinx.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def sphinx_docs(
143143
tools: {type}`list[label]` Additional tools that are used by Sphinx and its plugins.
144144
This just makes the tools available during Sphinx execution. To locate
145145
them, use {obj}`extra_opts` and `$(location)`.
146+
use_persistent_workers: {type}`bool` (experimental) If enable, use a persistent
147+
worker to run Sphinx for improved incremental, caching, and startup performance.
146148
**kwargs: {type}`dict` Common attributes to pass onto rules.
147149
"""
148150
add_tag(kwargs, "@rules_python//sphinxdocs:sphinx_docs")
@@ -243,7 +245,7 @@ _sphinx_docs = rule(
243245
),
244246
"sphinx": attr.label(
245247
executable = True,
246-
cfg = "host",
248+
cfg = "exec",
247249
mandatory = True,
248250
doc = "Sphinx binary to generate documentation.",
249251
),

sphinxdocs/private/sphinx_build.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
from pathlib import Path
2-
3-
import shutil
4-
import contextlib
51
import argparse
2+
import contextlib
3+
import io
64
import json
75
import logging
86
import os
97
import pathlib
8+
import shutil
109
import sys
1110
import time
1211
import traceback
1312
import typing
14-
import io
15-
import sphinx.application
13+
from pathlib import Path
1614

15+
import sphinx.application
1716
from sphinx.cmd.build import main
1817

19-
2018
WorkRequest = object
2119
WorkResponse = object
2220

23-
logger = logging.getLogger('sphinxdocs-build')
21+
logger = logging.getLogger("sphinxdocs_build")
2422

2523
_WORKER_SPHINX_EXT_MODULE_NAME = "bazel_worker_sphinx_ext"
2624

25+
2726
class Worker:
2827

29-
def __init__(self, instream: "typing.TextIO", outstream: "typing.TextIO", exec_root: str):
28+
def __init__(
29+
self, instream: "typing.TextIO", outstream: "typing.TextIO", exec_root: str
30+
):
3031
# NOTE: Sphinx performs its own logging re-configuration, so any
3132
# logging config we do isn't respected by Sphinx. Controlling where
3233
# stdout and stderr goes are the main mechanisms. Recall that
@@ -109,10 +110,7 @@ def _prepare_sphinx(self, request):
109110
incoming_digests = {}
110111
current_digests = self._digests.setdefault(srcdir, {})
111112
changed_paths = []
112-
request_info = {
113-
"exec_root": self._exec_root,
114-
"inputs": request["inputs"]
115-
}
113+
request_info = {"exec_root": self._exec_root, "inputs": request["inputs"]}
116114
for entry in request["inputs"]:
117115
path = entry["path"]
118116
digest = entry["digest"]
@@ -174,11 +172,12 @@ def _process_request(self, request: "WorkRequest") -> "WorkResponse | None":
174172

175173
if exit_code:
176174
raise Exception(
177-
"Sphinx main() returned failure: " +
178-
f" exit code: {exit_code}\n" +
179-
"========== STDOUT START ==========\n" +
180-
stdout.getvalue().rstrip("\n") + "\n" +
181-
"========== STDOUT END ==========\n"
175+
"Sphinx main() returned failure: "
176+
+ f" exit code: {exit_code}\n"
177+
+ "========== STDOUT START ==========\n"
178+
+ stdout.getvalue().rstrip("\n")
179+
+ "\n"
180+
+ "========== STDOUT END ==========\n"
182181
)
183182

184183
# Copying is unfortunately necessary because Bazel doesn't know to
@@ -193,32 +192,31 @@ def _process_request(self, request: "WorkRequest") -> "WorkResponse | None":
193192
return response
194193

195194

196-
197-
# todo: make this parallel-safe
198195
class BazelWorkerExtension:
196+
"""A Sphinx extension implemented as a class acting like a module."""
197+
199198
def __init__(self):
199+
# Make it look like a Module object
200200
self.__name__ = _WORKER_SPHINX_EXT_MODULE_NAME
201201
# set[str] of src-dir relative path names
202202
self.changed_paths = set()
203203

204204
def setup(self, app):
205-
app.connect('env-get-outdated', self._handle_env_get_outdated)
206-
return {
207-
"parallel_read_safe": True,
208-
"parallel_write_safe": True
209-
}
205+
app.connect("env-get-outdated", self._handle_env_get_outdated)
206+
return {"parallel_read_safe": True, "parallel_write_safe": True}
210207

211208
def _handle_env_get_outdated(self, app, env, added, changed, removed):
212209
changed = {
213210
# NOTE: path2doc returns None if it's not a doc path
214-
env.path2doc(p) for p in self.changed_paths
211+
env.path2doc(p)
212+
for p in self.changed_paths
215213
}
216214
logger.info("changed docs: %s", changed)
217215
return changed
218216

219217

220218
if __name__ == "__main__":
221-
if '--persistent_worker' in sys.argv:
219+
if "--persistent_worker" in sys.argv:
222220
with Worker(sys.stdin, sys.stdout, os.getcwd()) as worker:
223221
sys.exit(worker.run())
224222
else:

0 commit comments

Comments
 (0)