Skip to content

Commit ec4bac6

Browse files
committed
migrate: Fix mkdocs paths for api repos
The mkdocstrings-python v2 migration added in v0.14.0 moved the `paths` setting out of `handlers.python.options` assuming `src`, but api repositories use `py`, so they were not fixed automatically. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 182c1a6 commit ec4bac6

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ But you might still need to adapt your code:
3434

3535
### Cookiecutter template
3636

37-
<!-- Here bug fixes for cookiecutter specifically -->
37+
- Added a migration step for api repositories to fix `mkdocs.yml` when the previous `mkdocstrings-python` v2 migration moved only `paths: ["src"]` under `handlers.python.options` but not `paths: ["py"]`.

cookiecutter/migrate.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def main() -> None:
3838
"""Run the migration steps."""
3939
# Add a separation line like this one after each migration step.
4040
print("=" * 72)
41+
print("Fixing mkdocstrings-python v2 paths for api repos...")
42+
migrate_api_mkdocs_mkdocstrings_paths()
43+
print("=" * 72)
4144
print()
4245

4346
if _manual_steps:
@@ -60,6 +63,54 @@ def main() -> None:
6063
print()
6164

6265

66+
def migrate_api_mkdocs_mkdocstrings_paths() -> None:
67+
"""Fix the mkdocstrings paths migration for api repositories."""
68+
project_type = read_cookiecutter_str_var("type")
69+
if project_type is None:
70+
manual_step(
71+
"Unable to detect the cookiecutter project type from "
72+
".cookiecutter-replay.json; if this is an api project and "
73+
'`mkdocs.yml` still has `paths: ["py"]` nested under '
74+
"`handlers.python.options`, move it out of `options`."
75+
)
76+
return
77+
78+
if project_type != "api":
79+
print(" Skipping mkdocs.yml (not an api project)")
80+
return
81+
82+
filepath = Path("mkdocs.yml")
83+
if not filepath.exists():
84+
manual_step(
85+
"Unable to find mkdocs.yml; if this project uses mkdocs, "
86+
'make sure the `paths: ["py"]` config is under '
87+
"`handlers.python`, not `handlers.python.options`."
88+
)
89+
return
90+
91+
old = ' options:\n paths: ["py"]'
92+
new = ' paths: ["py"]\n options:'
93+
current_template = (
94+
' handlers:\n paths: ["py"]\n python:\n options:'
95+
)
96+
content = filepath.read_text(encoding="utf-8")
97+
98+
if old in content:
99+
replace_file_contents_atomically(filepath, old, new, count=1)
100+
print(f" Updated {filepath}: moved mkdocstrings api paths out of options")
101+
return
102+
103+
if new in content or current_template in content:
104+
print(f" Skipped {filepath}: mkdocstrings api paths already updated")
105+
return
106+
107+
manual_step(
108+
f"Could not find the api mkdocstrings path pattern in {filepath}. "
109+
'If `paths: ["py"]` is still nested under `handlers.python.options`, '
110+
"move it out of `options` according to the latest template."
111+
)
112+
113+
63114
def apply_patch(patch_content: str) -> None:
64115
"""Apply a patch using the patch utility."""
65116
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)

0 commit comments

Comments
 (0)