Skip to content

Commit f0dc823

Browse files
authored
[app] Allow extensions to detect freshenv builds (#12358)
Some extensions cache data outside of the `BuildEnvironment` (usually for memory/performance reasons). For these extensions, it would be helpful to detect when `sphinx-build -E` has been called, which in essence suggests the user wishes to remove cached data before building. This commit adds the `Sphinx.fresh_env_used` public property, exposing `Sphinx._fresh_env_used` in an immutable manner.
1 parent 53c31f7 commit f0dc823

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

sphinx/application.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,30 @@ def __init__(self, srcdir: str | os.PathLike[str], confdir: str | os.PathLike[st
143143
tags: list[str] | None = None,
144144
verbosity: int = 0, parallel: int = 0, keep_going: bool = False,
145145
pdb: bool = False) -> None:
146+
"""Initialize the Sphinx application.
147+
148+
:param srcdir: The path to the source directory.
149+
:param confdir: The path to the configuration directory.
150+
If not given, it is assumed to be the same as ``srcdir``.
151+
:param outdir: Directory for storing build documents.
152+
:param doctreedir: Directory for caching pickled doctrees.
153+
:param buildername: The name of the builder to use.
154+
:param confoverrides: A dictionary of configuration settings that override the
155+
settings in the configuration file.
156+
:param status: A file-like object to write status messages to.
157+
:param warning: A file-like object to write warnings to.
158+
:param freshenv: If true, clear the cached environment.
159+
:param warningiserror: If true, warnings become errors.
160+
:param tags: A list of tags to apply.
161+
:param verbosity: The verbosity level.
162+
:param parallel: The maximum number of parallel jobs to use
163+
when reading/writing documents.
164+
:param keep_going: If true, continue processing when an error occurs.
165+
:param pdb: If true, enable the Python debugger on an exception.
166+
"""
146167
self.phase = BuildPhase.INITIALIZATION
147168
self.verbosity = verbosity
169+
self._fresh_env_used: bool | None = None
148170
self.extensions: dict[str, Extension] = {}
149171
self.registry = SphinxComponentRegistry()
150172

@@ -267,6 +289,13 @@ def __init__(self, srcdir: str | os.PathLike[str], confdir: str | os.PathLike[st
267289
# set up the builder
268290
self._init_builder()
269291

292+
@property
293+
def fresh_env_used(self) -> bool | None:
294+
"""True/False as to whether a new environment was created for this build,
295+
or None if the environment has not been initialised yet.
296+
"""
297+
return self._fresh_env_used
298+
270299
def _init_i18n(self) -> None:
271300
"""Load translated strings from the configured localedirs if enabled in
272301
the configuration.
@@ -319,7 +348,6 @@ def _load_existing_env(self, filename: str) -> BuildEnvironment:
319348
def _post_init_env(self) -> None:
320349
if self._fresh_env_used:
321350
self.env.find_files(self.config, self.builder)
322-
del self._fresh_env_used
323351

324352
def preload_builder(self, name: str) -> None:
325353
self.registry.preload_builder(self, name)

0 commit comments

Comments
 (0)