forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_services_markdown.py
executable file
·134 lines (106 loc) · 3.84 KB
/
echo_services_markdown.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
#!/bin/env python
""" Usage
cd osparc-simcore
./scripts/echo_services_markdown.py >services.md
"""
import itertools
import sys
from collections.abc import Iterable
from datetime import datetime
from operator import attrgetter
from pathlib import Path
from typing import Final, NamedTuple
CURRENT_FILE = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve()
CURRENT_DIR = CURRENT_FILE.parent
_URL_PREFIX: Final[
str
] = "https://raw.githubusercontent.com/ITISFoundation/osparc-simcore/refs/heads/master"
_REDOC_URL_PREFIX: Final[str] = f"https://redocly.github.io/redoc/?url={_URL_PREFIX}"
_SWAGGER_URL_PREFIX: Final[str] = f"https://petstore.swagger.io/?url={_URL_PREFIX}"
class CaptureTuple(NamedTuple):
service_name: str
file_path: Path
_service_names_aliases: dict[str, str] = {
"web": "webserver",
}
def generate_markdown_table(
*captured_files: Iterable[CaptureTuple],
) -> str:
title = ("Name", "Files", " ")
num_cols = len(title)
lines = ["-" * 10] * num_cols
def _to_row_data(values: Iterable) -> list[str]:
row = list(map(str, values))
assert len(row) == num_cols, f"len({row=}) != {num_cols=}"
return row
rows = [
_to_row_data(title),
_to_row_data(lines),
]
found = itertools.groupby(
sorted(itertools.chain(*captured_files), key=attrgetter("service_name")),
key=attrgetter("service_name"),
)
for name, service_files in found:
rows.append(
_to_row_data(
(
f"**{name.upper()}**",
"",
"",
)
)
)
for _, file_path in service_files:
linked_path = f"[{file_path}](./{file_path})"
# SEE https://shields.io/badges
badges = []
if file_path.stem.lower() == "dockerfile":
repo = _service_names_aliases.get(f"{name}") or name
badges = [
f"[![Docker Image Size](https://img.shields.io/docker/image-size/itisfoundation/{repo})](https://hub.docker.com/r/itisfoundation/{repo}/tags)"
]
elif file_path.stem.lower() == "openapi":
badges = [
f"[![ReDoc](https://img.shields.io/badge/OpenAPI-ReDoc-85ea2d?logo=openapiinitiative)]({_REDOC_URL_PREFIX}/{file_path}) "
f"[![Swagger UI](https://img.shields.io/badge/OpenAPI-Swagger_UI-85ea2d?logo=swagger)]({_SWAGGER_URL_PREFIX}/{file_path})",
]
rows.append(
_to_row_data(
(
"",
linked_path,
" ".join(badges),
)
)
)
rows.append(_to_row_data(["" * 10] * num_cols))
# converts to markdown table
return "\n".join(f"| {'|'.join(r)} |" for r in rows)
if __name__ == "__main__":
repo_base_path = CURRENT_DIR.parent.resolve()
services_path = repo_base_path / "services"
def _to_tuple(file: Path):
return CaptureTuple(
f"{file.relative_to(services_path).parents[-2]}",
file.relative_to(repo_base_path),
)
dockerfiles_found = (_to_tuple(file) for file in services_path.rglob("Dockerfile"))
openapi_files_found = (
_to_tuple(file)
for file in services_path.rglob("openapi.*")
if file.suffix in {".json", ".yaml", ".yml"}
)
markdown_table = generate_markdown_table(
openapi_files_found,
dockerfiles_found,
)
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print("# services")
print(">")
print(f"> Auto generated on `{now}` using ")
print("```cmd")
print("cd osparc-simcore")
print(f"python ./{CURRENT_FILE.relative_to(repo_base_path)}")
print("```")
print(markdown_table)