-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathgenerate-hooks-config.py
More file actions
151 lines (108 loc) · 4.34 KB
/
generate-hooks-config.py
File metadata and controls
151 lines (108 loc) · 4.34 KB
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
#!/usr/bin/env python3
from __future__ import annotations
import importlib.metadata
import typing as t
from check_jsonschema.catalog import SCHEMA_CATALOG
version = importlib.metadata.version("check_jsonschema")
def iter_catalog_hooks():
for name in SCHEMA_CATALOG:
# copy config (new dict)
config = dict(SCHEMA_CATALOG[name]["hook_config"])
# set computed attributes
config["schema_name"] = name
config["id"] = f"check-{name}"
config["description"] = (
config.get("description")
or f"{config['name']} against the schema provided by SchemaStore"
)
if "types" in config and isinstance(config["types"], str):
config["types"] = [config["types"]]
yield config
def update_hook_config(new_config: str) -> None:
print("updating .pre-commit-hooks.yaml")
with open(".pre-commit-hooks.yaml") as fp:
content = fp.read()
autogen_marker = "# --AUTOGEN_HOOKS_START-- #"
# get the first part of the content (and assert that it's OK)
content = content.split(autogen_marker)[0]
assert "check-jsonschema" in content
content = content + autogen_marker + "\n\n" + new_config
with open(".pre-commit-hooks.yaml", "w") as fp:
fp.write(content)
def generate_hook_lines(config) -> t.Iterator[str]:
yield "# this hook is autogenerated from a script"
yield "# to modify this hook, update `src/check_jsonschema/catalog.py`"
yield "# and run `just generate-hooks` or `tox run -e generate-hooks-config`"
yield f"- id: {config['id']}"
yield f" name: {config['name']}"
yield f" description: '{config['description']}'"
add_args = " ".join(config.get("add_args", []))
if add_args:
add_args = " " + add_args
yield (
" entry: check-jsonschema --builtin-schema "
f"vendor.{config['schema_name']}{add_args}"
)
yield " language: python"
if isinstance(config["files"], list):
config["files"] = r""">
(?x)^(
{}
)$""".format("|\n ".join(config["files"]))
yield f" files: {config['files']}"
if "types" in config:
yield f" types: [{','.join(config['types'])}]"
if "types_or" in config:
yield f" types_or: [{','.join(config['types_or'])}]"
yield ""
def generate_all_hook_config_lines() -> t.Iterator[str]:
for hook_config in iter_catalog_hooks():
yield from generate_hook_lines(hook_config)
def format_all_hook_config() -> str:
return "\n".join(generate_all_hook_config_lines())
def update_usage_list_schemas() -> None:
print("updating docs/usage.rst -- list schemas")
with open("docs/usage.rst") as fp:
content = fp.read()
vendored_list_start = ".. vendored-schema-list-start\n"
vendored_list_end = "\n.. vendored-schema-list-end"
content_head = content.split(vendored_list_start)[0]
content_tail = content.split(vendored_list_end)[-1]
generated_list = "\n".join(
[vendored_list_start]
+ [f"- ``vendor.{n}``" for n in SCHEMA_CATALOG]
+ [vendored_list_end]
)
content = content_head + generated_list + content_tail
with open("docs/usage.rst", "w") as fp:
fp.write(content)
def update_precommit_usage_supported_hooks() -> None:
print("updating docs/precommit_usage.rst -- generated hooks")
with open("docs/precommit_usage.rst") as fp:
content = fp.read()
generated_list_start = ".. generated-hook-list-start\n"
generated_list_end = ".. generated-hook-list-end"
content_head = content.split(generated_list_start)[0]
content_tail = content.split(generated_list_end)[-1]
generated_list = "\n\n".join([generated_list_start] + [f"""\
``{config["id"]}``
{"~" * (len(config["id"]) + 4)}
{config["description"]}
.. code-block:: yaml
:caption: example config
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: {version}
hooks:
- id: {config["id"]}
""" for config in iter_catalog_hooks()] + [generated_list_end])
content = content_head + generated_list + content_tail
with open("docs/precommit_usage.rst", "w") as fp:
fp.write(content)
def update_docs() -> None:
update_usage_list_schemas()
update_precommit_usage_supported_hooks()
def main() -> None:
update_hook_config(format_all_hook_config())
update_docs()
if __name__ == "__main__":
main()