-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmkdocs_macros.py
300 lines (246 loc) · 10.3 KB
/
mkdocs_macros.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""
The main mkdocs-macros-plugin file.
It defines a hook function (`define_env`) which is called at the beginning of mkdocs processing.
See: https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/
It's used here mainly to:
- Get source-code from all repositories by
(a) downloading from github or
(b) copying from local filesystem
- Edit mkdocs configuration (defined initially in mkdocs) programatically:
1. Define navigation based on fetched files
2. Configure mkdocstrings plugin to find the source code
"""
import json
import logging
import shutil
import tempfile
import time
from pathlib import Path
import rich
from pulp_docs.cli import Config
from pulp_docs.constants import RESTAPI_URL_TEMPLATE
from pulp_docs.navigation import get_navigation
from pulp_docs.repository import Repo, Repos, SubPackage
# the name of the docs in the source repositories
SRC_DOCS_DIRNAME = "staging_docs"
# the dir to lookup for local repo checkouts
CHECKOUT_WORKDIR = Path().absolute().parent
log = logging.getLogger("mkdocs")
def create_clean_tmpdir(use_cache: bool = True):
tmpdir_basepath = Path(tempfile.gettempdir()).absolute()
tmpdir = tmpdir_basepath / "pulp-docs-tmp"
# Clean tmpdir only if not using cache
if tmpdir.exists() and use_cache is False:
shutil.rmtree(tmpdir)
tmpdir.mkdir()
return tmpdir
def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
"""
Download repositories into tmpdir and organize them in a convenient way
to mkdocs and its plugins.
Notes:
- repo_docs:
The directory where each repository documentation files are going.
These are *md files under each repo /docs folder.
- repo_sources:
The directory where each **repository source** is downloaded.
Contains the whole source tree of that repository.
- repo_docs/pulp-docs:
The common assets used to configure the whole mkdocs site (lives in pulp-docs package).
Returns: (Path(repo_docs), Path(repo_sources))
Example:
The final structure will be something like:
```
tmpdir/
repo_sources/
core/{full-repo-source}
repo1/{full-repo-source}
...
repo_docs/
pulp-docs/
assets/...
tags.md
{for-each-repo}/
README.md
CHANGELOG.md
docs/
```
"""
# Download/copy source code to tmpdir
repo_sources = TMPDIR / "repo_sources"
repo_docs = TMPDIR / "repo_docs"
shutil.rmtree(repo_sources, ignore_errors=True)
shutil.rmtree(repo_docs, ignore_errors=True)
for repo_or_pkg in repos.all:
start = time.perf_counter()
# handle subpcakges nested under repositories
this_docs_dir = repo_docs / repo_or_pkg.name
if not isinstance(repo_or_pkg, SubPackage):
this_src_dir = repo_sources / repo_or_pkg.name
repo_or_pkg.download(dest_dir=this_src_dir, clear_cache=config.clear_cache)
else:
this_src_dir = repo_sources / repo_or_pkg.subpackage_of / repo_or_pkg.name
# install and post-process
_place_doc_files(this_src_dir, this_docs_dir, repo_or_pkg)
if repo_or_pkg.type == "content":
_generate_rest_api_page(this_docs_dir, repo_or_pkg.name, repo_or_pkg.title)
end = time.perf_counter()
log.info(f"{repo_or_pkg.name} completed in {end - start:.2} sec")
# Copy core-files (shipped with pulp-docs) to tmpdir
shutil.copy(
repo_sources / repos.core_repo.name / SRC_DOCS_DIRNAME / "index.md",
repo_docs / "index.md",
)
# Log
log.info("[pulp-docs] Done downloading sources. Here are the sources used:")
for repo_or_pkg in repos.all:
log.info({repo_or_pkg.name: str(repo_or_pkg.status)})
return (repo_docs, repo_sources)
def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
"""Copy only doc-related files from src_dir to doc_dir"""
log.info(f"Moving doc files:\nfrom '{src_dir}'\nto '{docs_dir}'")
try:
shutil.copytree(src_dir / SRC_DOCS_DIRNAME, docs_dir / "docs")
except FileNotFoundError:
Path(docs_dir / "docs").mkdir(parents=True)
repo.status.has_staging_docs = False
# Get CHANGELOG
# TODO: remove reading .rst (plugins should provide markdown CHANGELOG)
repo.status.has_changelog = False
changes_dir = Path(docs_dir / "changes")
changes_dir.mkdir(exist_ok=True)
for changelog_name in ("CHANGELOG.md", "CHANGES.md", "CHANGES.rst"):
changelog_path = Path(src_dir / changelog_name)
if changelog_path.exists():
shutil.copy(changelog_path, changes_dir / "changelog.md")
repo.status.has_changelog = True
return
# Create placeholder, case it was not possible to fetch one
empty_changelog = changes_dir / "changelog.md"
empty_changelog.write_text(
"# Changelog\n\nThe repository does not provide a changelog or there was a problem fetching it."
)
def _generate_rest_api_page(docs_dir: Path, repo_name: str, repo_title: str):
"""Create page that contain a link to the rest api, based on the project url template"""
log.info("Generating REST_API page")
rest_api_page = docs_dir / "docs" / "rest_api.md"
rest_api_page.touch()
restapi_url = RESTAPI_URL_TEMPLATE.format(repo_name)
md_title = f"# {repo_title} REST Api"
md_body = f"[{restapi_url}]({restapi_url})"
rest_api_page.write_text(f"{md_title}\n\n{md_body}")
def print_user_repo(repos: Repos, config: Config):
"""Emit report about local checkout being used or warn if none."""
print("*" * 79)
log.info("[pulp-docs] Summary info about the build. Use -v for more info")
local_checkouts = []
cached_repos = []
downloaded_repos = []
warn_msgs = []
# prepare data for user report
for repo in repos.all:
record = {
"name": repo.name,
"download_source": repo.status.download_source,
"refs": repo.branch_in_use,
}
if repo.status.use_local_checkout is True:
local_checkouts.append(record)
elif repo.status.using_cache is True:
cached_repos.append(record)
else:
downloaded_repos.append(record)
# TODO: improve this refspec comparision heuristics
if repo.branch not in repo.branch_in_use:
warn_msgs.append(
f"[pulp-docs] Original {repo.name!r} ref is {repo.branch!r}, but local one is '{repo.branch_in_use}'."
)
if len(local_checkouts) == 0:
warn_msgs.append(
"[pulp-docs] No local checkouts found. Serving in read-only mode."
)
if config.verbose:
report = {
"config": config.get_environ_dict(),
"cached_repos": cached_repos,
"downloaded_repos": downloaded_repos,
"local_checkouts": local_checkouts,
}
else:
report = {
"cached_repos": [repo["name"] for repo in cached_repos],
"downloaded_repos": [repo["name"] for repo in downloaded_repos],
"local_checkouts": [repo["name"] for repo in local_checkouts],
}
rich.print_json(json.dumps(report, indent=4))
for msg in warn_msgs:
log.warning(msg)
print("*" * 79)
def define_env(env):
"""The mkdocs-marcros 'on_configuration' hook. Used to setup the project."""
# Load configuration from environment
log.info("[pulp-docs] Loading configuration from environ")
config = Config(from_environ=True)
if config.repolist:
repos = Repos.from_yaml(config.repolist)
repos.update_local_checkouts()
else:
repos = (
Repos.test_fixtures()
) # try to use fixtures if there is no BASE_REPOLIST
log.info(f"Repository configurations loaded: {[repo.name for repo in repos.all]}")
# Download and organize repository files
log.info("[pulp-docs] Preparing repositories")
TMPDIR = create_clean_tmpdir()
docs_dir, source_dir = prepare_repositories(TMPDIR, repos, config)
# Configure mkdocstrings
log.info("[pulp-docs] Configuring mkdocstrings")
code_sources = [str(source_dir / repo.name) for repo in repos.all]
env.conf["plugins"]["mkdocstrings"].config["handlers"]["python"][
"paths"
] = code_sources
# Configure navigation
log.info("[pulp-docs] Configuring navigation")
env.conf["docs_dir"] = docs_dir
env.conf["nav"] = get_navigation(docs_dir, repos)
log.info("[pulp-docs] Done with pulp-docs.")
env.conf["pulp_repos"] = repos
env.config["pulp_repos"] = repos
env.conf["pulp_config"] = config
# Extra config
@env.macro
def get_repos(repo_type="content"):
"Return repo names by type"
_repo_type = [repo_type] if repo_type else None
repos_list = sorted(
repos.get_repos(repo_types=_repo_type), key=lambda x: x.title
)
repos_data = [
{
"title": repo.title,
"version": "3.12.1",
"rest_api_url": f"https://docs.pulpproject.org/{repo.name}/restapi.html",
"codebase_url": f"https://github.com/{repo.owner}/{repo.name}",
"changelog_url": f"site:{repo.name}/changes/changelog/",
}
for repo in repos_list
]
return repos_data
def on_pre_page_macros(env):
"""The mkdocs-macros hook just before an inidvidual page render."""
repos: Repos = env.conf["pulp_repos"] # type: ignore
# Configure the edit_url with correct repository and path
src_uri = env.page.file.src_uri.replace("/docs/", f"/{SRC_DOCS_DIRNAME}/")
if src_uri != "index.md":
repo, _, path = src_uri.partition("/")
else:
repo = "pulpcore"
path = f"{SRC_DOCS_DIRNAME}/index.md"
repo_obj = repos.get(repo)
repo_branch = getattr(repo_obj, "branch", "main")
edit_url = f"https://github.com/pulp/{repo}/edit/{repo_branch}/{path}"
env.page.edit_url = edit_url
def on_post_build(env):
"""The mkdocs-marcros 'on_post_build' hook. Used to print summary report for end user."""
# Log relevant most useful information for end-user
print_user_repo(repos=env.conf["pulp_repos"], config=env.conf["pulp_config"])