Skip to content

Commit f9c92ef

Browse files
committed
Build director: refactor how build.jobs are executed
Put all the job steps under a helper called `run_build_job` to simplify the code.
1 parent 50980c7 commit f9c92ef

File tree

1 file changed

+35
-104
lines changed

1 file changed

+35
-104
lines changed

readthedocs/doc_builder/director.py

Lines changed: 35 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,18 @@ def setup_vcs(self):
8989
version_type=self.data.version.type,
9090
)
9191

92-
self.pre_checkout()
92+
# We can't do too much on ``pre_checkout`` because we haven't
93+
# cloned the repository yet and we don't know what the user wrote
94+
# in the `.readthedocs.yaml` yet.
95+
#
96+
# We could implement something different in the future if we download
97+
# the `.readthedocs.yaml` file without cloning.
98+
# See https://github.com/readthedocs/readthedocs.org/issues/8935
99+
#
100+
# self.run_build_job('pre_checkout')
101+
93102
self.checkout()
94-
self.post_checkout()
103+
self.run_build_job("post_checkout")
95104

96105
commit = self.data.build_commit or self.vcs_repository.commit
97106
if commit:
@@ -139,21 +148,21 @@ def setup_environment(self):
139148
environment=self.build_environment,
140149
)
141150

142-
self.pre_system_dependencies()
151+
self.run_build_job("pre_system_dependencies")
143152
self.system_dependencies()
144-
self.post_system_dependencies()
153+
self.run_build_job("post_system_dependencies")
145154

146155
# Install all ``build.tools`` specified by the user
147156
if self.data.config.using_build_tools:
148157
self.language_environment.install_build_tools()
149158

150-
self.pre_create_environment()
159+
self.run_build_job("pre_create_environment")
151160
self.create_environment()
152-
self.post_create_environment()
161+
self.run_build_job("post_create_environment")
153162

154-
self.pre_install()
163+
self.run_build_job("pre_install")
155164
self.install()
156-
self.post_install()
165+
self.run_build_job("post_install")
157166

158167
# TODO: remove this and document how to do it on `build.jobs.post_install`
159168
if self.data.project.has_feature(Feature.LIST_PACKAGES_INSTALLED_ENV):
@@ -169,7 +178,7 @@ def build(self):
169178
4. build ePub
170179
"""
171180

172-
self.pre_build()
181+
self.run_build_job("pre_build")
173182

174183
self.data.outcomes = defaultdict(lambda: False)
175184
self.data.outcomes["html"] = self.build_html()
@@ -178,23 +187,13 @@ def build(self):
178187
self.data.outcomes["pdf"] = self.build_pdf()
179188
self.data.outcomes["epub"] = self.build_epub()
180189

181-
self.post_build()
190+
self.run_build_job("post_build")
182191

183192
after_build.send(
184193
sender=self.data.version,
185194
)
186195

187196
# VCS checkout
188-
def pre_checkout(self):
189-
# We can't do too much here because we haven't cloned the repository
190-
# yet and we don't know what the user wrote in the `.readthedocs.yaml`
191-
# yet.
192-
#
193-
# We could implement something different in the future if we download
194-
# the `.readthedocs.yaml` file without cloning.
195-
# See https://github.com/readthedocs/readthedocs.org/issues/8935
196-
pass
197-
198197
def checkout(self):
199198
log.info(
200199
"Clonning repository.",
@@ -211,17 +210,7 @@ def checkout(self):
211210
if self.vcs_repository.supports_submodules:
212211
self.vcs_repository.update_submodules(self.data.config)
213212

214-
def post_checkout(self):
215-
commands = self.data.config.build.jobs.post_checkout
216-
for command in commands:
217-
self.run_user_cmd(command)
218-
219213
# System dependencies (``build.apt_packages``)
220-
def pre_system_dependencies(self):
221-
commands = self.data.config.build.jobs.pre_system_dependencies
222-
for command in commands:
223-
self.run_user_cmd(command)
224-
225214
# NOTE: `system_dependencies` should not be possible to override by the
226215
# user because it's executed as ``RTD_DOCKER_USER`` (e.g. ``root``) user.
227216
def system_dependencies(self):
@@ -257,75 +246,23 @@ def system_dependencies(self):
257246
user=settings.RTD_DOCKER_SUPER_USER,
258247
)
259248

260-
def post_system_dependencies(self):
261-
commands = self.data.config.build.jobs.post_system_dependencies
262-
for command in commands:
263-
self.run_user_cmd(command)
264-
265249
# Language environment
266-
def pre_create_environment(self):
267-
commands = self.data.config.build.jobs.pre_create_environment
268-
for command in commands:
269-
self.run_user_cmd(command)
270-
271250
def create_environment(self):
272-
commands = [] # self.data.config.build.jobs.create_environment
273-
for command in commands:
274-
self.run_user_cmd(command)
275-
276-
if not commands:
277-
self.language_environment.setup_base()
278-
279-
def post_create_environment(self):
280-
commands = self.data.config.build.jobs.post_create_environment
281-
for command in commands:
282-
self.run_user_cmd(command)
251+
self.language_environment.setup_base()
283252

284253
# Install
285-
def pre_install(self):
286-
commands = self.data.config.build.jobs.pre_install
287-
for command in commands:
288-
self.run_user_cmd(command)
289-
290254
def install(self):
291-
commands = [] # self.data.config.build.jobs.install
292-
for command in commands:
293-
self.run_user_cmd(command)
294-
295-
if not commands:
296-
self.language_environment.install_core_requirements()
297-
self.language_environment.install_requirements()
298-
299-
def post_install(self):
300-
commands = self.data.config.build.jobs.post_install
301-
for command in commands:
302-
self.run_user_cmd(command)
255+
self.language_environment.install_core_requirements()
256+
self.language_environment.install_requirements()
303257

304258
# Build
305-
def pre_build(self):
306-
commands = self.data.config.build.jobs.pre_build
307-
for command in commands:
308-
self.run_user_cmd(command)
309-
310259
def build_html(self):
311-
commands = [] # self.data.config.build.jobs.build.html
312-
if commands:
313-
for command in commands:
314-
self.run_user_cmd(command)
315-
return True
316-
317260
return self.build_docs_class(self.data.config.doctype)
318261

319262
def build_pdf(self):
320263
if "pdf" not in self.data.config.formats or self.data.version.type == EXTERNAL:
321264
return False
322265

323-
commands = [] # self.data.config.build.jobs.build.pdf
324-
if commands:
325-
for command in commands:
326-
self.run_user_cmd(command)
327-
return True
328-
329266
# Mkdocs has no pdf generation currently.
330267
if self.is_type_sphinx():
331268
return self.build_docs_class("sphinx_pdf")
@@ -339,12 +276,6 @@ def build_htmlzip(self):
339276
):
340277
return False
341278

342-
commands = [] # self.data.config.build.jobs.build.htmlzip
343-
if commands:
344-
for command in commands:
345-
self.run_user_cmd(command)
346-
return True
347-
348279
# We don't generate a zip for mkdocs currently.
349280
if self.is_type_sphinx():
350281
return self.build_docs_class("sphinx_singlehtmllocalmedia")
@@ -354,30 +285,30 @@ def build_epub(self):
354285
if "epub" not in self.data.config.formats or self.data.version.type == EXTERNAL:
355286
return False
356287

357-
commands = [] # self.data.config.build.jobs.build.epub
358-
if commands:
359-
for command in commands:
360-
self.run_user_cmd(command)
361-
return True
362-
363288
# Mkdocs has no epub generation currently.
364289
if self.is_type_sphinx():
365290
return self.build_docs_class("sphinx_epub")
366291
return False
367292

368-
def post_build(self):
369-
commands = self.data.config.build.jobs.post_build
293+
def run_build_job(self, job):
294+
if (
295+
getattr(self.data.config.build, "jobs", None) is None
296+
or getattr(self.data.config.build.jobs, job, None) is None
297+
):
298+
return
299+
300+
cwd = self.data.project.checkout_path(self.data.version.slug)
301+
commands = getattr(self.data.config.build.jobs, job, [])
370302
for command in commands:
371-
self.run_user_cmd(command)
303+
environment = self.vcs_environment
304+
if job not in ("pre_checkout", "post_checkout"):
305+
environment = self.build_environment
306+
environment.run(*command.split(), escape_command=False, cwd=cwd)
372307

373308
# Helpers
374309
#
375310
# TODO: move somewhere or change names to make them private or something to
376311
# easily differentiate them from the normal flow.
377-
def run_user_cmd(self, cmd):
378-
cwd = self.data.project.checkout_path(self.data.version.slug)
379-
self.build_environment.run(cmd.split(), escape_command=False, cwd=cwd)
380-
381312
def build_docs_class(self, builder_class):
382313
"""
383314
Build docs with additional doc backends.

0 commit comments

Comments
 (0)