17
17
18
18
import json
19
19
import logging
20
- import os
21
20
import shutil
22
21
import tempfile
23
22
import time
24
23
from pathlib import Path
25
24
25
+ import httpx
26
26
import rich
27
27
28
28
from pulp_docs .cli import Config
@@ -89,6 +89,7 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
89
89
# Download/copy source code to tmpdir
90
90
repo_sources = TMPDIR / "repo_sources"
91
91
repo_docs = TMPDIR / "repo_docs"
92
+ api_src_dir = TMPDIR / "api_json"
92
93
shutil .rmtree (repo_sources , ignore_errors = True )
93
94
shutil .rmtree (repo_docs , ignore_errors = True )
94
95
@@ -102,10 +103,13 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
102
103
else :
103
104
this_src_dir = repo_sources / repo_or_pkg .subpackage_of / repo_or_pkg .name
104
105
106
+ # restapi
107
+ if repo_or_pkg .type in ("content" , "core" ):
108
+ _download_api_json (api_src_dir , repo_or_pkg .name )
109
+ _generate_rest_api_page (this_src_dir , repo_or_pkg .name , repo_or_pkg .title )
110
+
105
111
# install and post-process
106
- _place_doc_files (this_src_dir , this_docs_dir , repo_or_pkg )
107
- if repo_or_pkg .type == "content" :
108
- _generate_rest_api_page (this_docs_dir , repo_or_pkg .name , repo_or_pkg .title )
112
+ _place_doc_files (this_src_dir , this_docs_dir , repo_or_pkg , api_src_dir )
109
113
110
114
end = time .perf_counter ()
111
115
log .info (f"{ repo_or_pkg .name } completed in { end - start :.2} sec" )
@@ -124,7 +128,27 @@ def prepare_repositories(TMPDIR: Path, repos: Repos, config: Config):
124
128
return (repo_docs , repo_sources )
125
129
126
130
127
- def _place_doc_files (src_dir : Path , docs_dir : Path , repo : Repo ):
131
+ def _download_api_json (api_dir : Path , repo_name : str ):
132
+ api_json_path = api_dir / f"{ repo_name } /api.json"
133
+ if api_json_path .exists ():
134
+ log .info (f"{ repo_name } api.json already downloaded." )
135
+ return
136
+
137
+ log .info (f"Downloading api.json for { repo_name } " )
138
+ api_url_1 = "https://docs.pulpproject.org/{repo_name}/api.json"
139
+ api_url_2 = "https://docs.pulpproject.org/{repo_name}/_static/api.json"
140
+ response = httpx .get (api_url_1 .format (repo_name = repo_name ))
141
+ if response .is_error :
142
+ response = httpx .get (api_url_2 .format (repo_name = repo_name ))
143
+ if response .is_error :
144
+ raise Exception ("Couldnt get rest api page" )
145
+
146
+ api_json_path .parent .mkdir (parents = True , exist_ok = True )
147
+ api_json_path .write_text (json .dumps (response .json ()))
148
+ log .info ("Done." )
149
+
150
+
151
+ def _place_doc_files (src_dir : Path , docs_dir : Path , repo : Repo , api_src_dir : Path ):
128
152
"""
129
153
Copy only doc-related files from src_dir to doc_dir.
130
154
@@ -148,6 +172,11 @@ def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
148
172
Path (docs_dir / "docs" ).mkdir (parents = True )
149
173
repo .status .has_staging_docs = False
150
174
175
+ # Get restapi
176
+ if repo .type in ("content" , "core" ):
177
+ api_json = api_src_dir / f"{ repo .name } /api.json"
178
+ shutil .copy (api_json , docs_dir / "docs/api.json" )
179
+
151
180
# Get changelog
152
181
repo .status .has_changelog = False
153
182
changes_dir = Path (docs_dir )
@@ -175,15 +204,24 @@ def _place_doc_files(src_dir: Path, docs_dir: Path, repo: Repo):
175
204
)
176
205
177
206
207
+ RESTAPI_TEMPLATE = """\
208
+ ---
209
+ hide:
210
+ - toc
211
+ ---
212
+
213
+ # {repo_title} REST Api
214
+
215
+ <swagger-ui src="api.json"/>
216
+ """
217
+
218
+
178
219
def _generate_rest_api_page (docs_dir : Path , repo_name : str , repo_title : str ):
179
220
"""Create page that contain a link to the rest api, based on the project url template"""
180
221
log .info ("Generating REST_API page" )
181
- rest_api_page = docs_dir / "docs" / "rest_api.md"
182
- rest_api_page .touch ()
183
- restapi_url = RESTAPI_URL_TEMPLATE .format (repo_name )
184
- md_title = f"# { repo_title } REST Api"
185
- md_body = f"[{ restapi_url } ]({ restapi_url } )"
186
- rest_api_page .write_text (f"{ md_title } \n \n { md_body } " )
222
+ rest_api_page = docs_dir / "staging_docs" / "rest_api.md"
223
+ rest_api_page .parent .mkdir (parents = True , exist_ok = True )
224
+ rest_api_page .write_text (RESTAPI_TEMPLATE .format (repo_title = repo_title ))
187
225
188
226
189
227
def print_user_repo (repos : Repos , config : Config ):
0 commit comments