This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43474c9
commit 55dcb7d
Showing
1 changed file
with
35 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
inventory builder | ||
~~~~~~~~~~~~~~~~~ | ||
A customized HTML builder which only generates intersphinx "object.inv" | ||
inventory files and pickle files. The documentation files are not written. | ||
Inventory builder | ||
A customized builder which only generates intersphinx "object.inv" | ||
inventory files. The documentation files are not written. | ||
""" | ||
from sphinx.builders.html import StandaloneHTMLBuilder | ||
from __future__ import annotations | ||
|
||
from os import path | ||
import shutil | ||
from typing import Any, Iterable | ||
from urllib.parse import quote | ||
|
||
from sphinx.application import Sphinx | ||
from sphinx.builders.dummy import DummyBuilder | ||
from sphinx.util.inventory import InventoryFile | ||
|
||
INVENTORY_FILENAME = "objects.inv" | ||
|
||
class InventoryBuilder(StandaloneHTMLBuilder): | ||
|
||
class InventoryBuilder(DummyBuilder): | ||
""" | ||
A customized HTML builder which only generates intersphinx "object.inv" | ||
inventory files and pickle files. The documentation files are not written. | ||
A customized builder which only generates intersphinx "object.inv" | ||
inventory files. The documentation files are not written. | ||
""" | ||
|
||
name = "inventory" | ||
format = "inventory" | ||
epilog = "The inventory files are in %(outdir)s." | ||
|
||
def get_outdated_docs(self): | ||
from sphinx.builders.html import BuildInfo | ||
old = BuildInfo() | ||
try: | ||
with open(path.join(self.outdir, '.buildinfo')) as fp: | ||
old = BuildInfo.load(fp) | ||
except ValueError: | ||
self.warn('unsupported build info format in %r, building all' % | ||
path.join(self.outdir, '.buildinfo')) | ||
except Exception: | ||
pass | ||
if self.build_info != old: | ||
for docname in self.env.found_docs: | ||
yield docname | ||
return | ||
def get_outdated_docs(self) -> Iterable[str]: | ||
""" | ||
Return an iterable of output files that are outdated. | ||
""" | ||
assert self.env is not None | ||
|
||
if self.templates: | ||
template_mtime = self.templates.newest_template_mtime() | ||
else: | ||
template_mtime = 0 | ||
for docname in self.env.found_docs: | ||
if docname not in self.env.all_docs: | ||
yield docname | ||
continue | ||
targetname = path.join(self.outdir, 'objects.inv') | ||
targetname = path.join(self.outdir, INVENTORY_FILENAME) | ||
try: | ||
targetmtime = path.getmtime(targetname) | ||
except Exception: | ||
targetmtime = 0 | ||
try: | ||
srcmtime = max(path.getmtime(self.env.doc2path(docname)), | ||
template_mtime) | ||
srcmtime = path.getmtime(self.env.doc2path(docname)) | ||
if srcmtime > targetmtime: | ||
yield docname | ||
except EnvironmentError: | ||
# source doesn't exist anymore | ||
pass | ||
|
||
def write_doc(self, docname, doctree): | ||
def get_target_uri(self, docname: str, typ: str | None = None) -> str: | ||
""" | ||
Don't write any doc | ||
Return the target URI for a document name. | ||
""" | ||
return quote(docname) + ".html" | ||
|
||
def finish(self): | ||
def finish(self) -> None: | ||
""" | ||
Only write the inventory files. | ||
""" | ||
self.write_buildinfo() | ||
self.dump_inventory() | ||
|
||
def removed_method_error(self): | ||
""" | ||
Raise an error if this method is called. | ||
This is just for making sure that some writer methods are indeed | ||
deactivated. | ||
""" | ||
raise RuntimeError("This function shouldn't be called in \"%s\" builder"%(self.name)) | ||
|
||
def cleanup(self): | ||
""" | ||
Remove the '_static' directory. | ||
This directory is unnecessary for the inventory build, but it | ||
may be created by the graphviz extension. Its presence will | ||
break the docbuild later on, so remove it. | ||
""" | ||
if path.isdir(path.join(self.outdir, '_static')): | ||
shutil.rmtree(path.join(self.outdir, '_static')) | ||
|
||
assert self.env is not None | ||
|
||
copy_image_files = removed_method_error | ||
copy_download_files = removed_method_error | ||
copy_static_files = removed_method_error | ||
handle_finish = removed_method_error | ||
InventoryFile.dump( | ||
path.join(self.outdir, INVENTORY_FILENAME), self.env, self | ||
) | ||
|
||
|
||
def setup(app): | ||
def setup(app: Sphinx) -> dict[str, Any]: | ||
app.add_builder(InventoryBuilder) | ||
return {'parallel_read_safe': True} | ||
return {"parallel_read_safe": True} |