1- import json
21import logging
32import os
43import shutil
54import sys
6- import time
75from sphinx_runpython .github_link import make_linkcode_resolve
86import yobx
97
108# Suppress TensorFlow C++ and Python logging before any TF import occurs.
119os .environ ["TF_CPP_MIN_LOG_LEVEL" ] = "3"
1210logging .getLogger ("tensorflow" ).setLevel (logging .ERROR )
1311
14- # ---------------------------------------------------------------------------
15- # Per-page build duration tracking (writes top-5 to a JSON file so that the
16- # doc_build_durations.rst page can display it in the generated documentation).
17- # ---------------------------------------------------------------------------
18-
19- _PAGE_TIMINGS_START : dict [str , float ] = {}
20- _PAGE_DURATIONS : dict [str , float ] = {}
21- _DOC_BUILD_DURATIONS_JSON = os .path .join (
22- os .path .dirname (__file__ ), "_static" , "doc_build_durations.json"
23- )
24- os .environ ["YOBX_DOC_BUILD_DURATIONS_JSON" ] = _DOC_BUILD_DURATIONS_JSON
25-
26-
27- def _on_source_read (app , docname , source ):
28- """Records the start time when a source file begins to be read."""
29- _PAGE_TIMINGS_START [docname ] = time .monotonic ()
30-
31-
32- def _on_doctree_read (app , doctree ):
33- """Records the elapsed time after a doctree has been parsed."""
34- docname = app .env .docname
35- start = _PAGE_TIMINGS_START .pop (docname , None )
36- if start is not None :
37- _PAGE_DURATIONS [docname ] = time .monotonic () - start
38-
39-
40- def _on_build_finished (app , exception ):
41- """Writes the 5 slowest pages (by build duration) to a JSON file."""
42- if exception or not _PAGE_DURATIONS :
43- return
44- top5 = sorted (_PAGE_DURATIONS .items (), key = lambda kv : kv [1 ], reverse = True )[:5 ]
45- static_dir = os .path .dirname (_DOC_BUILD_DURATIONS_JSON )
46- if os .path .exists (static_dir ) and not os .path .isdir (static_dir ):
47- os .remove (static_dir )
48- os .makedirs (static_dir , exist_ok = True )
49- with open (_DOC_BUILD_DURATIONS_JSON , "w" , encoding = "utf-8" ) as fh :
50- json .dump (
51- [{"docname" : name , "duration_s" : round (dur , 3 )} for name , dur in top5 ], fh , indent = 2
52- )
53-
5412
5513def _on_builder_inited (app ):
5614 """Removes any non-directory file named '_static' in the output directory.
@@ -66,11 +24,8 @@ def _on_builder_inited(app):
6624
6725
6826def setup (app ):
69- """Connects duration-tracking hooks to Sphinx events."""
27+ """Connects hooks to Sphinx events."""
7028 app .connect ("builder-inited" , _on_builder_inited )
71- app .connect ("source-read" , _on_source_read )
72- app .connect ("doctree-read" , _on_doctree_read )
73- app .connect ("build-finished" , _on_build_finished )
7429
7530
7631project = "yet-another-onnx-builder"
0 commit comments