-
Notifications
You must be signed in to change notification settings - Fork 65
/
build_doc.py
188 lines (149 loc) · 5.54 KB
/
build_doc.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
"""
This file builds the documentation for APICheck
"""
import os
import re
import json
import hashlib
import configparser
HERE = os.path.abspath(os.path.dirname(__file__))
URL_PREFIX = "/apicheck"
DOC_PATH = os.path.join(HERE, "docs")
TOOLS_PATH = os.path.join(HERE, "tools")
EDGE_TOOLS_PATH = os.path.join(HERE, "tools-edge")
STATIC_PATH = os.path.join(HERE, "docs", "assets")
META_KEYS = ("name", "version", "description",
"home", "author")
OPTIONAL_KEYS = ("short-command", )
NAME_FORMAT_REGEX = r"([A-Za_-z0-9]+)"
def main():
catalog = []
short_commands = set()
tool_names = set()
tools_brief = {}
#
# Getting README from plugin
#
for t in (TOOLS_PATH, EDGE_TOOLS_PATH):
is_edge_tool = "tools-edge" in t
for d in os.listdir(t):
if d.startswith("."):
continue
# Get README.md file
tools_path = os.path.join(t, d)
meta_path = os.path.join(tools_path, "META")
readme_path = os.path.join(tools_path, "README.md")
try:
with open(readme_path, "r") as readme_handler:
readme_text = readme_handler.readlines()
except OSError:
print(f"[!] Tool \"{d}\" doesnt has README.md file")
continue
try:
with open(meta_path, "r") as meta_handler:
cf = configparser.ConfigParser()
cf.read_string(f"[DEFAULT]\n {meta_handler.read()}")
meta = dict(cf["DEFAULT"])
except OSError:
print(f"[!] Tool \"{d}\" doesnt has README.md file")
continue
# Check that META contains all needed keys
if not all(x in meta.keys() for x in META_KEYS):
print(f"[!] Missing keys in META \"{d}\". "
f"Needed keys: \"{', '.join(META_KEYS)}\"")
exit(1)
#
# Check that 'name' and 'short-command' are unique
#
tool_name = meta["name"]
if not re.match(NAME_FORMAT_REGEX, tool_name):
print(f"Invalid name format for: '{tool_name}'. "
f"Only allowed : A-Za_-z0-9")
exit(1)
home = meta["home"]
author = meta["author"]
description = meta["description"]
display_name = meta.get("display-name", "") or tool_name
short_command = meta.get("short-command", None)
tool_type = 'edge' if is_edge_tool else 'apicheck'
if short_command:
if short_command in short_commands:
print(f"[!] Short-command \"{short_command}\" at tool "
f"'{tool_name}' already exits in another tool")
exit(1)
else:
short_commands.add(short_command)
if tool_name in tool_names:
print(f"[!] Tool name '{tool_name}' already exits used "
f"for other tool")
exit(1)
else:
tool_names.add(tool_name)
meta["type"] = tool_type
catalog.append(meta)
tools_brief[tool_name] = (
description,
author,
home,
display_name,
tool_type
)
#
# Build tools documentation
#
doc_tool_path = os.path.join(DOC_PATH,
"docs",
"tools")
readme_title = readme_text[0].replace("#", "").strip()
if not os.path.exists(doc_tool_path):
os.makedirs(doc_tool_path, exist_ok=True)
with open(os.path.join(doc_tool_path,
f"{d.replace('_', '-')}.md"), "w") as f:
f.write("\n".join([
"---",
"layout: doc",
f"title: {readme_title}",
f"type: {tool_type}",
f"permalink: /tools/{tool_type}/{tool_name}",
"---",
"\n",
]))
f.writelines(readme_text)
f.flush()
#
# Build tools index
#
tool_menu_item = []
for t_name, (
t_brief, t_author, t_home_page, display_name, tool_type
) in tools_brief.items():
tool_menu_item.append(f" - title: {display_name}")
tool_menu_item.append(f" author: {t_author}")
tool_menu_item.append(f" home: {t_home_page}")
tool_menu_item.append(f" brief: {t_brief}")
tool_menu_item.append(f" type: {tool_type}")
tool_menu_item.append(f" url: /tools/{tool_type}/{t_name}")
tool_menu_item.append("")
#
# Build Menu
#
tools_menu_path_data = os.path.join(DOC_PATH, "_data", "tools.yaml")
tools_yaml_data = """
menu:
"""
with open(os.path.join(tools_menu_path_data), "w") as f:
f.write(tools_yaml_data)
f.write("\n".join(tool_menu_item))
#
# Build catalog
#
catalog_path = os.path.join(STATIC_PATH, "catalog.json")
catalog_path_checksum = os.path.join(STATIC_PATH, "catalog.json.checksum")
with open(catalog_path, "w") as f, open(catalog_path_checksum, "w") as c:
cat_content = json.dumps(catalog)
h = hashlib.sha512()
h.update(cat_content.encode("UTF-8"))
f.write(cat_content)
c.write(h.hexdigest())
if __name__ == '__main__':
main()