Skip to content

Commit 90a4ce1

Browse files
author
Andrey Cheptsov
committed
Fix generated OpenAPI docs file
1 parent 9754b07 commit 90a4ce1

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

‎scripts/docs/gen_openapi_reference.py‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
from pathlib import Path
1010
from typing import Any
1111

12+
import mkdocs_gen_files
13+
1214
from dstack._internal.server.main import app
1315
from dstack._internal.settings import DSTACK_VERSION
1416

1517
logger = logging.getLogger("mkdocs.plugins.dstack.openapi")
1618
disable_env = "DSTACK_DOCS_DISABLE_OPENAPI_REFERENCE"
17-
output_dir = Path("mkdocs/docs/reference/http")
19+
output_dir = Path("docs/reference/http")
20+
source_output_dir = Path("mkdocs") / output_dir
1821
openapi_path = output_dir / "openapi.json"
1922

2023
TAG_LIST_BEGIN = "<!-- BEGIN GENERATED HTTP API TAGS -->"
@@ -39,9 +42,10 @@ def _write_tag_references(tags: list[str]) -> None:
3942

4043
def _update_index(tags: list[str]) -> None:
4144
index_path = output_dir / "index.md"
42-
if not index_path.exists():
45+
try:
46+
text = _read_text(index_path)
47+
except FileNotFoundError:
4348
return
44-
text = index_path.read_text()
4549
tag_links = "\n".join(f"- [{_tag_title(tag)}]({_tag_page_filename(tag)})" for tag in tags)
4650
generated = f"{TAG_LIST_BEGIN}\n{tag_links}\n{TAG_LIST_END}"
4751
pattern = re.compile(f"{re.escape(TAG_LIST_BEGIN)}.*?{re.escape(TAG_LIST_END)}", re.S)
@@ -53,12 +57,12 @@ def _update_index(tags: list[str]) -> None:
5357

5458

5559
def _remove_stale_openapi_files() -> None:
56-
for path in output_dir.glob("*.openapi.json"):
60+
for path in source_output_dir.glob("*.openapi.json"):
5761
path.unlink()
5862

5963

6064
def _remove_stale_tag_pages(page_filenames: set[str]) -> None:
61-
for path in output_dir.glob("*.md"):
65+
for path in source_output_dir.glob("*.md"):
6266
if path.name != "index.md" and path.name not in page_filenames:
6367
path.unlink()
6468

@@ -89,8 +93,13 @@ def _write_json(path: Path, data: dict[str, Any]) -> None:
8993

9094

9195
def _write_text(path: Path, content: str) -> None:
92-
if not path.exists() or path.read_text() != content:
93-
path.write_text(content)
96+
with mkdocs_gen_files.open(path.as_posix(), "w") as f:
97+
f.write(content)
98+
99+
100+
def _read_text(path: Path) -> str:
101+
with mkdocs_gen_files.open(path.as_posix(), "r") as f:
102+
return f.read()
94103

95104

96105
def main() -> None:
@@ -105,7 +114,8 @@ def main() -> None:
105114
schema = app.openapi()
106115
tags = _get_tags(schema)
107116

108-
output_dir.mkdir(parents=True, exist_ok=True)
117+
logger.info("Generating OpenAPI reference...")
118+
source_output_dir.mkdir(parents=True, exist_ok=True)
109119
_write_json(openapi_path, schema)
110120
_write_tag_references(tags)
111121
_update_index(tags)

0 commit comments

Comments
 (0)