Skip to content

Commit c4a6798

Browse files
committed
Add workaround for mkdocstring source-find subpackages
This enables mkdocstrings to find: - pulp-cli/pulp-glue/ - pulp-cli/pulpcore/ This should work also if those code-sources do not use explicit __init__.py to declare they are modules. [noissue]
1 parent 11ea5f1 commit c4a6798

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

src/pulp_docs/data/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ plugins:
5252
show_symbol_type_toc: true
5353
docstring_section_style: table # table, list, spacy
5454
# heading_level: 2
55-
# show_root_heading: false
56-
# show_root_toc_entry: false
55+
show_root_heading: true
56+
show_root_toc_entry: true
5757
show_if_no_docstring: false
5858
show_signature_annotations: false
5959
separate_signature: false

src/pulp_docs/data/repolist.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ repos:
6565
owner: pulp
6666
title: Pulp CLI
6767
branch: main
68-
# - name: pulp_glue
69-
# nested_under: pulp/pulp-cli/pulp_glue
70-
# title: Pulp Glue
71-
# branch: main
7268
- name: pulp-openapi-generator
7369
owner: pulp
7470
title: OpenAPI Generator
@@ -77,3 +73,11 @@ repos:
7773
owner: pulp
7874
title: Selinux
7975
branch: main
76+
- name: oci_env
77+
owner: pulp
78+
title: OCI Env
79+
branch: main
80+
# subpackages
81+
- name: pulp-glue
82+
title: Pulp Glue
83+
subpackage_of: pulp-cli

src/pulp_docs/mkdocs_macros.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717
import json
1818
import logging
19+
import os
1920
import shutil
2021
import tempfile
2122
import time
@@ -123,11 +124,43 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
123124

124125

125126
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+
"""
127142
log.info(f"Moving doc files:\nfrom '{src_dir}'\nto '{docs_dir}'")
128143

129144
try:
130145
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+
131164
except FileNotFoundError:
132165
Path(docs_dir / "docs").mkdir(parents=True)
133166
repo.status.has_staging_docs = False
@@ -240,7 +273,19 @@ def define_env(env):
240273

241274
# Configure mkdocstrings
242275
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+
244289
env.conf["plugins"]["mkdocstrings"].config["handlers"]["python"][
245290
"paths"
246291
] = code_sources

0 commit comments

Comments
 (0)