|
27 | 27 | # add these directories to sys.path here. If the directory is relative to the |
28 | 28 | # documentation root, use os.path.abspath to make it absolute, like shown here. |
29 | 29 |
|
| 30 | +import glob |
30 | 31 | import os |
31 | 32 | import shutil |
32 | 33 | import subprocess |
33 | 34 | import typing |
| 35 | +from pathlib import Path |
34 | 36 |
|
35 | 37 | if typing.TYPE_CHECKING: |
36 | 38 | from autoapi._objects import PythonObject |
37 | 39 |
|
38 | | -CUR_DIR = os.path.dirname(os.path.abspath(__file__)) |
39 | | -DOC_DIR = os.path.dirname(CUR_DIR) |
40 | | -ROOT_DIR = os.path.dirname(os.path.dirname(CUR_DIR)) |
41 | | -NAT_DIR = os.path.join(ROOT_DIR, "src", "nat") |
42 | 40 |
|
43 | | -# Work-around for https://github.com/readthedocs/sphinx-autoapi/issues/298 |
44 | | -# AutoAPI support for implicit namespaces is broken, so we need to manually |
45 | | -# construct an nat package with an __init__.py file |
46 | | -BUILD_DIR = os.path.join(DOC_DIR, "build") |
47 | | -API_TREE = os.path.join(BUILD_DIR, "_api_tree") |
| 41 | +def _build_api_tree() -> Path: |
| 42 | + # Work-around for https://github.com/readthedocs/sphinx-autoapi/issues/298 |
| 43 | + # AutoAPI support for implicit namespaces is broken, so we need to manually |
48 | 44 |
|
49 | | -if os.path.exists(API_TREE): |
50 | | - shutil.rmtree(API_TREE) |
| 45 | + cur_dir = Path(os.path.abspath(__file__)).parent |
| 46 | + docs_dir = cur_dir.parent |
| 47 | + root_dir = docs_dir.parent |
| 48 | + nat_dir = root_dir / "src" / "nat" |
| 49 | + plugins_dir = root_dir / "packages" |
51 | 50 |
|
52 | | -os.makedirs(API_TREE) |
53 | | -shutil.copytree(NAT_DIR, os.path.join(API_TREE, "nat")) |
54 | | -with open(os.path.join(API_TREE, "nat", "__init__.py"), "w") as f: |
55 | | - f.write("") |
| 51 | + build_dir = docs_dir / "build" |
| 52 | + api_tree = build_dir / "_api_tree" |
| 53 | + dest_dir = api_tree / "nat" |
| 54 | + |
| 55 | + if api_tree.exists(): |
| 56 | + shutil.rmtree(api_tree.absolute()) |
| 57 | + |
| 58 | + os.makedirs(api_tree.absolute()) |
| 59 | + shutil.copytree(nat_dir, dest_dir) |
| 60 | + dest_plugins_dir = dest_dir / "plugins" |
| 61 | + |
| 62 | + for sub_dir in (dest_dir, dest_plugins_dir): |
| 63 | + with open(sub_dir / "__init__.py", "w", encoding="utf-8") as f: |
| 64 | + f.write("") |
| 65 | + |
| 66 | + plugin_dirs = [Path(p) for p in glob.glob(f'{plugins_dir}/nvidia_nat_*')] |
| 67 | + for plugin_dir in plugin_dirs: |
| 68 | + src_dir = plugin_dir / 'src/nat/plugins' |
| 69 | + if src_dir.exists(): |
| 70 | + for plugin_subdir in src_dir.iterdir(): |
| 71 | + if plugin_subdir.is_dir(): |
| 72 | + dest_subdir = dest_plugins_dir / plugin_subdir.name |
| 73 | + shutil.copytree(plugin_subdir, dest_subdir) |
| 74 | + package_file = dest_subdir / "__init__.py" |
| 75 | + if not package_file.exists(): |
| 76 | + with open(package_file, "w", encoding="utf-8") as f: |
| 77 | + f.write("") |
| 78 | + |
| 79 | + return api_tree |
| 80 | + |
| 81 | + |
| 82 | +API_TREE = _build_api_tree() |
| 83 | +print(f"API tree built at {API_TREE}") |
56 | 84 |
|
57 | 85 | # -- Project information ----------------------------------------------------- |
58 | 86 |
|
|
87 | 115 | "sphinxmermaid" |
88 | 116 | ] |
89 | 117 |
|
90 | | -autoapi_dirs = [API_TREE] |
| 118 | +autoapi_dirs = [str(API_TREE.absolute())] |
91 | 119 |
|
92 | 120 | autoapi_root = "api" |
93 | 121 | autoapi_python_class_content = "both" |
|
0 commit comments