-
Notifications
You must be signed in to change notification settings - Fork 42
/
tasks.py
329 lines (263 loc) · 10.8 KB
/
tasks.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import json
import os
import re
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from invoke import task
from jsondiff import diff
if TYPE_CHECKING:
from invoke import Context, Result
TOP_DIR = Path(__file__).parent.resolve()
def update_file(
filename: Path | str, sub_line: tuple[str, str], strip: str | None = None
):
"""Utility function for tasks to read, update, and write files"""
with open(filename) as handle:
lines = [
re.sub(sub_line[0], sub_line[1], line.rstrip(strip)) for line in handle
]
with open(filename, "w") as handle:
handle.write("\n".join(lines))
handle.write("\n")
@task
def generate_openapi(_):
"""Update OpenAPI schema in file 'local_openapi.json'"""
from optimade.server.main import app
if not TOP_DIR.joinpath("openapi").exists():
os.mkdir(TOP_DIR.joinpath("openapi"))
with open(TOP_DIR.joinpath("openapi/local_openapi.json"), "w") as f:
json.dump(app.openapi(), f, indent=2)
print("", file=f) # Empty EOL
@task
def generate_index_openapi(_):
"""Update OpenAPI schema in file 'local_index_openapi.json'"""
from optimade.server.main_index import app as app_index
if not TOP_DIR.joinpath("openapi").exists():
os.mkdir(TOP_DIR.joinpath("openapi"))
with open(TOP_DIR.joinpath("openapi/local_index_openapi.json"), "w") as f:
json.dump(app_index.openapi(), f, indent=2)
print("", file=f) # Empty EOL
@task(pre=[generate_openapi, generate_index_openapi])
def check_openapi_diff(_):
"""Checks the Generated OpenAPI spec against what is stored in the repo"""
with open(TOP_DIR.joinpath("openapi/openapi.json")) as handle:
openapi = json.load(handle)
with open(TOP_DIR.joinpath("openapi/local_openapi.json")) as handle:
local_openapi = json.load(handle)
with open(TOP_DIR.joinpath("openapi/index_openapi.json")) as handle:
index_openapi = json.load(handle)
with open(TOP_DIR.joinpath("openapi/local_index_openapi.json")) as handle:
local_index_openapi = json.load(handle)
openapi_diff = diff(openapi, local_openapi)
if openapi_diff != {}:
print(
"Error: Generated OpenAPI spec for test server did not match committed version.\n"
"Run 'invoke update-openapijson' and re-commit.\n"
f"Diff:\n{openapi_diff}"
)
sys.exit(1)
openapi_index_diff = diff(index_openapi, local_index_openapi)
if openapi_index_diff != {}:
print(
"Error: Generated OpenAPI spec for Index meta-database did not match committed version.\n"
"Run 'invoke update-openapijson' and re-commit.\n"
f"Diff:\n{openapi_index_diff}"
)
sys.exit(1)
@task(pre=[generate_openapi, generate_index_openapi])
def update_openapijson(c):
"""Updates the stored OpenAPI spec to what the server returns"""
c.run(
f"cp {TOP_DIR.joinpath('openapi/local_openapi.json')} {TOP_DIR.joinpath('openapi/openapi.json')}"
)
c.run(
f"cp {TOP_DIR.joinpath('openapi/local_index_openapi.json')} {TOP_DIR.joinpath('openapi/index_openapi.json')}"
)
print("Updated OpenAPI JSON specifications")
@task(help={"ver": "OPTIMADE Python tools version to set"}, post=[update_openapijson])
def setver(_, ver=""):
"""Sets the OPTIMADE Python tools version"""
match = re.fullmatch(r"v?([0-9]+\.[0-9]+\.[0-9]+)", ver)
if not match or (match and len(match.groups()) != 1):
print(
"Error: Please specify version as 'Major.Minor.Patch' or 'vMajor.Minor.Patch'"
)
sys.exit(1)
ver = match.group(1)
update_file(
TOP_DIR.joinpath("optimade/__init__.py"),
(r'__version__ = ".*"', f'__version__ = "{ver}"'),
)
update_file(
TOP_DIR.joinpath("docs/static/default_config.json"),
(r'"version": ".*",', f'"version": "{ver}",'),
)
print(f"Bumped version to {ver}")
@task(help={"ver": "OPTIMADE API version to set"}, post=[update_openapijson])
def set_optimade_ver(_, ver=""):
"""Sets the OPTIMADE API Version"""
if not ver:
print("Error: Please specify --ver='Major.Minor.Patch(-rc|a|b.NUMBER)'")
sys.exit(1)
match = re.fullmatch(r"v?([0-9]+\.[0-9]+\.[0-9]+(-(rc|a|b)+\.[0-9]+)?)", ver)
if not match or (match and len(match.groups()) != 3):
print(
"Error: ver MUST be expressed as 'Major.Minor.Patch(-rc|a|b.NUMBER)'. It may be prefixed by 'v'."
)
sys.exit(1)
ver = match.group(1)
update_file(
TOP_DIR.joinpath("optimade/__init__.py"),
("__api_version__ = .+", f'__api_version__ = "{ver}"'),
)
for regex, version in (
("[0-9]+", ver.split(".")[0]),
("[0-9]+.[0-9]+", ".".join(ver.split(".")[:2])),
("[0-9]+.[0-9]+.[0-9]+", ver),
):
update_file(
TOP_DIR.joinpath("README.md"),
(f"example/v{regex}", f"example/v{version}"),
strip="\n",
)
update_file(
TOP_DIR.joinpath("optimade-version.json"),
(r'"message": "v.+",', f'"message": "v{ver}",'),
)
update_file(
TOP_DIR.joinpath("INSTALL.md"),
(r"/v[0-9]+(\.[0-9]+){2}(-(rc|a|b)+\.[0-9]+)?", f"/v{ver.split('.')[0]}"),
)
print(f"Bumped OPTIMADE API version to {ver}")
@task
def get_markdown_spec(ctx):
"""Convert the develop OPTIMADE specification from `rst` to `md`."""
print("Attempting to run pandoc...")
ctx.run(
"pandoc "
"-f rst -t markdown_strict "
"--wrap=preserve --columns=50000 "
"https://raw.githubusercontent.com/Materials-Consortia/OPTIMADE/develop/optimade.rst "
"> optimade.md"
)
@task(
help={
"pre-clean": "Remove the 'api_reference' sub directory prior to (re)creation."
}
)
def create_api_reference_docs(context, pre_clean=False, pre_commit=False):
"""Create the API Reference in the documentation"""
import shutil
def write_file(full_path: Path, content: str):
"""Write file with `content` to `full_path`"""
if full_path.exists():
with open(full_path) as handle:
cached_content = handle.read()
if content == cached_content:
del cached_content
return
del cached_content
with open(full_path, "w") as handle:
handle.write(content)
optimade_dir = TOP_DIR.joinpath("optimade")
docs_dir = TOP_DIR.joinpath("docs/api_reference")
unwanted_subdirs = ("__pycache__", "data", "grammar", "static")
pages_template = 'title: "{name}"\n'
md_template = "# {name}\n\n::: {py_path}\n"
models_template = (
md_template + f"{' ' * 4}options:\n{' ' * 6}show_if_no_docstring: true\n"
)
if docs_dir.exists() and pre_clean:
shutil.rmtree(docs_dir, ignore_errors=True)
if docs_dir.exists():
raise RuntimeError(f"{docs_dir} should have been removed!")
docs_dir.mkdir(exist_ok=True)
for dirpath, dirnames, filenames in os.walk(optimade_dir):
for unwanted_dir in unwanted_subdirs:
if unwanted_dir in dirnames:
# Avoid walking into or through unwanted directories
dirnames.remove(unwanted_dir)
relpath = Path(dirpath).relative_to(optimade_dir)
# Create `.pages`
if str(relpath) == ".":
write_file(
full_path=docs_dir.joinpath(".pages"),
content=pages_template.format(name="API Reference"),
)
docs_sub_dir = docs_dir.joinpath(relpath)
docs_sub_dir.mkdir(exist_ok=True)
if str(relpath) != ".":
write_file(
full_path=docs_sub_dir.joinpath(".pages"),
content=pages_template.format(name=str(relpath).split("/")[-1]),
)
# Create markdown files
for filename in filenames:
if re.match(r".*\.py$", filename) is None or filename == "__init__.py":
# Not a Python file: We don't care about it!
# Or filename is `__init__.py`: We don't want it!
continue
basename = filename[: -len(".py")]
py_path = f"optimade/{relpath}/{basename}".replace("/", ".")
if str(relpath) == ".":
py_path = py_path.replace("...", ".")
print(filename, basename, py_path)
md_filename = filename.replace(".py", ".md")
# For models we want to include EVERYTHING, even if it doesn't have a doc-string
# Also for `server.config`
template = (
models_template
if str(relpath) == "models"
or (str(relpath) == "server" and basename == "config")
else md_template
)
write_file(
full_path=docs_sub_dir.joinpath(md_filename),
content=template.format(name=basename, py_path=py_path),
)
if pre_commit:
# Check if there have been any changes.
# List changes if yes.
if TYPE_CHECKING:
context: "Context" = context # type: ignore[no-redef]
# NOTE: grep returns an exit code of 1 if it doesn't find anything
# (which will be good in this case).
# Concerning the weird last grep command see:
# http://manpages.ubuntu.com/manpages/precise/en/man1/git-status.1.html
result: "Result" = context.run(
"git status --porcelain docs/api_reference | grep -E '^[? MARC][?MD]' || exit 0",
hide=True,
)
if result.stdout:
sys.exit(
f"The following files have been changed/added, please stage them:\n\n{result.stdout}"
)
@task(help={"fname": "The JSON file containing the OpenAPI schema to validate"})
def swagger_validator(_, fname):
"""This task can be used in the CI to test the generated OpenAPI schemas
with the online swagger validator.
Returns:
Non-zero exit code if validation fails, otherwise returns `0`.
"""
import requests
def print_error(string):
for line in string.split("\n"):
print(f"\033[31m{line}\033[0m")
swagger_url = "https://validator.swagger.io/validator/debug"
with open(fname) as f:
schema = json.load(f)
response = requests.post(swagger_url, json=schema)
if response.status_code != 200:
print_error(f"Server returned status code {response.status_code}.")
sys.exit(1)
try:
json_response = response.json()
except json.JSONDecodeError:
print_error(f"Unable to parse validator response as JSON: {response}")
sys.exit(1)
if json_response:
if any(json_response[k] for k in json_response):
print_error(f"Schema file {fname} did not pass validation.\n")
print_error(json.dumps(response.json(), indent=2))
sys.exit(1)