|
16 | 16 | """
|
17 | 17 | import json
|
18 | 18 | import logging
|
| 19 | +import os |
19 | 20 | import shutil
|
20 | 21 | import tempfile
|
21 | 22 | import time
|
@@ -123,11 +124,43 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
|
123 | 124 |
|
124 | 125 |
|
125 | 126 | def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
|
126 |
| - """Copy only doc-related files from src_dir to doc_dir""" |
| 127 | + """ |
| 128 | + Copy only doc-related files from src_dir to doc_dir. |
| 129 | +
|
| 130 | + Examples: |
| 131 | + ``` |
| 132 | + # src_dir |
| 133 | + $WORKSPACE/pulpcore/ |
| 134 | + $WORKSPACE/pulpcore/pulp_glue |
| 135 | +
|
| 136 | + # docs_dir |
| 137 | + $TMPDIR/repo_docs/pulpcore/docs |
| 138 | + $TMPDIR/repo_docs/pulp_glue/docs |
| 139 | + ``` |
| 140 | +
|
| 141 | + """ |
127 | 142 | log.info(f"Moving doc files:\nfrom '{src_dir}'\nto '{docs_dir}'")
|
128 | 143 |
|
129 | 144 | try:
|
130 | 145 | shutil.copytree(src_dir / SRC_DOCS_DIRNAME, docs_dir / "docs")
|
| 146 | + # [WORKAROUND] for allowing mkdocstrings to find "unexpected packages". |
| 147 | + # Also injects __init__.py so references are |
| 148 | + # Example: having pulp-cli/pulpcore/cli |
| 149 | + # Mostly usefull for pulp-cli, where this kind of organization makes sense. |
| 150 | + # It may be worth considering generalizing this treatment, so repos can define |
| 151 | + # the packages to be included in mkdocstrings path. |
| 152 | + # See: https://mkdocstrings.github.io/python/usage/#using-the-paths-option |
| 153 | + if repo.name != "pulpcore" and "pulpcore" in os.listdir(src_dir): |
| 154 | + repo_source_path = src_dir.parent |
| 155 | + for child in Path(src_dir / "pulpcore").glob("*"): |
| 156 | + if child.is_dir(): |
| 157 | + Path(child / "__init__.py").touch(exist_ok=True) |
| 158 | + shutil.copytree( |
| 159 | + src_dir / "pulpcore", |
| 160 | + repo_source_path / "pulpcore" / "pulpcore", |
| 161 | + dirs_exist_ok=True, |
| 162 | + ) |
| 163 | + |
131 | 164 | except FileNotFoundError:
|
132 | 165 | Path(docs_dir / "docs").mkdir(parents=True)
|
133 | 166 | repo.status.has_staging_docs = False
|
@@ -240,7 +273,19 @@ def define_env(env):
|
240 | 273 |
|
241 | 274 | # Configure mkdocstrings
|
242 | 275 | log.info("[pulp-docs] Configuring mkdocstrings")
|
243 |
| - code_sources = [str(source_dir / repo.name) for repo in repos.all] |
| 276 | + code_sources = [] |
| 277 | + for repo_or_package in repos.all: |
| 278 | + # Handle subpackages or repos |
| 279 | + if isinstance(repo_or_package, SubPackage): |
| 280 | + repo_or_package_path = ( |
| 281 | + repo_or_package.subpackage_of + "/" + repo_or_package.name |
| 282 | + ) |
| 283 | + else: |
| 284 | + repo_or_package_path = repo_or_package.name |
| 285 | + |
| 286 | + # Add to mkdocstring pythonpath |
| 287 | + code_sources.append(str(source_dir / repo_or_package_path)) |
| 288 | + |
244 | 289 | env.conf["plugins"]["mkdocstrings"].config["handlers"]["python"][
|
245 | 290 | "paths"
|
246 | 291 | ] = code_sources
|
|
0 commit comments