2022-01-16 17:33:05 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# This file is placed in the public domain or under the
|
|
|
|
# CC0-1.0-Universal license, whichever is more permissive.
|
|
|
|
|
2021-06-08 19:11:26 -04:00
|
|
|
"""Build script for Sphinx documentation"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from sphinx.application import Sphinx
|
|
|
|
|
|
|
|
|
|
|
|
def create_parser():
|
|
|
|
parser = argparse.ArgumentParser(description="Build PEP documents")
|
|
|
|
# alternative builders:
|
2022-01-16 17:33:05 -05:00
|
|
|
builders = parser.add_mutually_exclusive_group()
|
|
|
|
builders.add_argument("-l", "--check-links", action="store_const",
|
|
|
|
dest="builder", const="linkcheck",
|
|
|
|
help='Check validity of links within PEP sources. '
|
|
|
|
'Cannot be used with "-f" or "-d".')
|
|
|
|
builders.add_argument("-f", "--build-files", action="store_const",
|
|
|
|
dest="builder", const="html",
|
|
|
|
help='Render PEPs to "pep-NNNN.html" files (default). '
|
|
|
|
'Cannot be used with "-d" or "-l".')
|
|
|
|
builders.add_argument("-d", "--build-dirs", action="store_const",
|
|
|
|
dest="builder", const="dirhtml",
|
|
|
|
help='Render PEPs to "index.html" files within "pep-NNNN" directories. '
|
|
|
|
'Cannot be used with "-f" or "-l".')
|
2021-06-08 19:11:26 -04:00
|
|
|
|
|
|
|
# flags / options
|
2022-01-16 17:33:05 -05:00
|
|
|
parser.add_argument("-w", "--fail-on-warning", action="store_true",
|
|
|
|
help="Fail the Sphinx build on any warning.")
|
|
|
|
parser.add_argument("-n", "--nitpicky", action="store_true",
|
|
|
|
help="Run Sphinx in 'nitpicky' mode, "
|
|
|
|
"warning on every missing reference target.")
|
|
|
|
parser.add_argument("-j", "--jobs", type=int, default=1,
|
|
|
|
help="How many parallel jobs to run (if supported). "
|
|
|
|
"Integer, default 1.")
|
2022-07-29 09:07:03 -04:00
|
|
|
parser.add_argument(
|
|
|
|
"-o",
|
|
|
|
"--output-dir",
|
2023-08-05 08:24:46 -04:00
|
|
|
default="build",
|
2022-07-29 09:07:03 -04:00
|
|
|
help="Output directory, relative to root. Default 'build'.",
|
|
|
|
)
|
2021-06-08 19:11:26 -04:00
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2021-06-30 15:19:44 -04:00
|
|
|
def create_index_file(html_root: Path, builder: str) -> None:
|
2021-06-12 13:51:14 -04:00
|
|
|
"""Copies PEP 0 to the root index.html so that /peps/ works."""
|
2021-06-30 15:19:44 -04:00
|
|
|
pep_zero_file = "pep-0000.html" if builder == "html" else "pep-0000/index.html"
|
|
|
|
try:
|
|
|
|
pep_zero_text = html_root.joinpath(pep_zero_file).read_text(encoding="utf-8")
|
|
|
|
except FileNotFoundError:
|
|
|
|
return None
|
|
|
|
if builder == "dirhtml":
|
|
|
|
pep_zero_text = pep_zero_text.replace('="../', '="') # remove relative directory links
|
|
|
|
html_root.joinpath("index.html").write_text(pep_zero_text, encoding="utf-8")
|
2021-06-12 13:51:14 -04:00
|
|
|
|
|
|
|
|
2021-06-08 19:11:26 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
args = create_parser()
|
|
|
|
|
|
|
|
root_directory = Path(".").absolute()
|
|
|
|
source_directory = root_directory
|
2022-07-29 09:07:03 -04:00
|
|
|
build_directory = root_directory / args.output_dir
|
2021-06-08 19:11:26 -04:00
|
|
|
doctree_directory = build_directory / ".doctrees"
|
|
|
|
|
|
|
|
# builder configuration
|
2022-01-16 17:33:05 -05:00
|
|
|
if args.builder is not None:
|
|
|
|
sphinx_builder = args.builder
|
2021-06-09 09:02:16 -04:00
|
|
|
else:
|
|
|
|
# default builder
|
2022-01-16 17:33:05 -05:00
|
|
|
sphinx_builder = "html"
|
2021-06-08 19:11:26 -04:00
|
|
|
|
|
|
|
# other configuration
|
|
|
|
config_overrides = {}
|
|
|
|
if args.nitpicky:
|
|
|
|
config_overrides["nitpicky"] = True
|
|
|
|
|
|
|
|
app = Sphinx(
|
|
|
|
source_directory,
|
|
|
|
confdir=source_directory,
|
|
|
|
outdir=build_directory,
|
|
|
|
doctreedir=doctree_directory,
|
|
|
|
buildername=sphinx_builder,
|
|
|
|
confoverrides=config_overrides,
|
|
|
|
warningiserror=args.fail_on_warning,
|
|
|
|
parallel=args.jobs,
|
2022-03-11 10:34:31 -05:00
|
|
|
tags=["internal_builder"],
|
2021-06-08 19:11:26 -04:00
|
|
|
)
|
|
|
|
app.build()
|
2022-01-16 17:33:05 -05:00
|
|
|
|
|
|
|
create_index_file(build_directory, sphinx_builder)
|