forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphinx_build.py
More file actions
164 lines (145 loc) · 5.29 KB
/
sphinx_build.py
File metadata and controls
164 lines (145 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from pathlib import Path
import argparse
import json
import logging
import os
import pathlib
import sys
import time
import traceback
import typing
from sphinx.cmd.build import main
WorkRequest = object
WorkResponse = object
parser = argparse.ArgumentParser(
fromfile_prefix_chars='@'
)
# parser.add_argument('srcdir')
# parser.add_argument('outdir')
parser.add_argument("--persistent_worker", action="store_true")
parser.add_argument("--doctree-dir")
class Worker:
def __init__(self, instream: "typing.TextIO", outstream: "typing.TextIO"):
self._instream = instream
self._outstream = outstream
self._logger = logging.getLogger("worker")
logging.basicConfig(filename='echo.log', encoding='utf-8', level=logging.DEBUG)
self._logger.info("starting worker")
self._current = {}
self._previous = {}
self._cache = {}
def run(self) -> None:
try:
while True:
request = None
try:
request = self._get_next_request()
if request is None:
self._logger.info("Empty request: exiting")
break
response = self._process_request(request)
if response:
self._send_response(response)
except Exception:
self._logger.exception("Unhandled error: request=%s", request)
output = (
f"Unhandled error:\nRequest: {request}\n"
+ traceback.format_exc()
)
request_id = 0 if not request else request.get("requestId", 0)
self._send_response(
{
"exitCode": 3,
"output": output,
"requestId": request_id,
}
)
finally:
self._logger.info("Worker shutting down")
def _get_next_request(self) -> "object | None":
line = self._instream.readline()
if not line:
return None
return json.loads(line)
@property
def inputs(self):
self._previous
self._current
return self._value
def _update_digest(self, request):
args, unknown = parser.parse_known_args(request["arguments"])
# Make room for the new build's data.
self._previous = self._current
# Rearrange the new data into a dict to make comparisons easier.
self._current = {}
for page in request["inputs"]:
path = page["path"]
self._current[path] = page["digest"]
# Compare the content hashes to determine what pages have changed.
tmp = []
for path in self._current:
if path not in self._previous:
tmp.append(path)
continue
if self._current[path] != self._previous[path]:
tmp.append(path)
continue
for path in self._previous:
if path not in self._current:
tmp.append(path)
continue
# Normalize the paths into docnames
digest = []
for path in tmp:
if not path.endswith(".rst"):
continue
srcdir = self.args[0]
docname = path.replace(srcdir + "/", "")
docname = docname.replace(".rst", "")
digest.append(docname)
args, unknown = parser.parse_known_args(self.args)
# Save the digest.
doctree_dir = Path(args.doctree_dir)
# On a fresh build, _restore_cache() does nothing, so this dir won't exist yet.
if not doctree_dir.is_dir():
doctree_dir.mkdir(parents=True)
with open(doctree_dir / Path("digest.json"), "w") as f:
json.dump(digest, f, indent=2)
def _restore_cache(self):
for filepath in self._cache:
data = self._cache[filepath]
parent = Path(os.path.dirname(filepath))
if not parent.is_dir():
parent.mkdir(parents=True)
with open(filepath, "wb") as f:
f.write(data)
def _update_cache(self):
args, unknown = parser.parse_known_args(self.args)
self._cache = {}
for root, _, files in os.walk(args.doctree_dir):
for filename in files:
filepath = Path(root) / Path(filename)
with open(filepath, "rb") as f:
self._cache[str(filepath)] = f.read()
def _process_request(self, request: "WorkRequest") -> "WorkResponse | None":
if request.get("cancel"):
return None
self.args = request["arguments"]
self._restore_cache()
self._update_digest(request)
main(self.args)
self._update_cache()
response = {
"requestId": request.get("requestId", 0),
"exitCode": 0,
}
return response
def _send_response(self, response: "WorkResponse") -> None:
self._outstream.write(json.dumps(response) + "\n")
self._outstream.flush()
if __name__ == "__main__":
args, unknown = parser.parse_known_args()
if args.persistent_worker:
Worker(sys.stdin, sys.stdout).run()
else:
sys.exit(main())