22Build a subset of Python's documentation using Sphinx
33"""
44
5- import gc
65import io
76import os
87from pathlib import Path
98import shutil
10- import sys
119
1210import pyperf
1311from sphinx .cmd .build import main as sphinx_main
@@ -46,13 +44,14 @@ def open(
4644):
4745 if isinstance (file , Path ):
4846 file = str (file )
47+
4948 if isinstance (file , str ):
5049 if "r" in mode and file in preloaded_files :
5150 if "b" in mode :
5251 return io .BytesIO (preloaded_files [file ])
5352 else :
5453 return io .StringIO (preloaded_files [file ].decode (encoding or "utf-8" ))
55- elif "w" in mode :
54+ elif "w" in mode and DOC_ROOT in Path ( file ). parents :
5655 if "b" in mode :
5756 newfile = io .BytesIO ()
5857 else :
@@ -82,32 +81,36 @@ def replace(src, dst):
8281os .replace = replace
8382
8483
85- def build_html (doc_root ):
84+ def build_doc (doc_root ):
8685 # Make sure there is no caching going on
87- if (DOC_ROOT / "build" ).is_dir ():
88- shutil .rmtree (DOC_ROOT / "build" )
89-
90- args = [
91- "-b" ,
92- "html" ,
93- "-d" ,
94- str (doc_root / "build" / "doctrees" ),
95- "-j" ,
96- "1" ,
97- "-Q" ,
98- str (doc_root ),
99- str (doc_root / "build" / "html" ),
100- ]
101-
10286 t0 = pyperf .perf_counter ()
103- sphinx_main (args )
87+ sphinx_main (
88+ [
89+ "-b" ,
90+ "dummy" ,
91+ "-d" ,
92+ str (doc_root / "build" / "doctrees" ),
93+ "-j" ,
94+ "1" ,
95+ "-Q" ,
96+ str (doc_root ),
97+ str (doc_root / "build" / "html" ),
98+ ]
99+ )
104100 return pyperf .perf_counter () - t0
105101
106102
107103def bench_sphinx (loops , doc_root ):
104+ if (DOC_ROOT / "build" ).is_dir ():
105+ shutil .rmtree (DOC_ROOT / "build" )
106+ read_all_files ()
107+
108108 runs_total = 0
109109 for _ in range (loops ):
110- runs_total += build_html (doc_root )
110+ runs_total += build_doc (doc_root )
111+ if (DOC_ROOT / "build" ).is_dir ():
112+ shutil .rmtree (DOC_ROOT / "build" )
113+
111114 return runs_total
112115
113116
@@ -119,8 +122,4 @@ def bench_sphinx(loops, doc_root):
119122 )
120123 args = runner .parse_args ()
121124
122- if (DOC_ROOT / "build" ).is_dir ():
123- shutil .rmtree (DOC_ROOT / "build" )
124- read_all_files ()
125-
126- runner .bench_time_func ("sphinx" , bench_sphinx , DOC_ROOT , inner_loops = 1 )
125+ runner .bench_time_func ("sphinx" , bench_sphinx , DOC_ROOT )
0 commit comments