-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.py
186 lines (135 loc) · 6.05 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
import sys
from pathlib import Path
from typing import Any
from invoke import Context, task
CURRENT_DIRECTORY = Path(__file__).resolve()
DOCUMENTATION_DIRECTORY = CURRENT_DIRECTORY.parent / "docs"
MAIN_DIRECTORY_PATH = Path(__file__).parent
def _generate(context: Context) -> None:
"""Generate documentation output from code."""
_generate_infrahubctl_documentation(context=context)
_generate_infrahub_sdk_configuration_documentation()
def _generate_infrahubctl_documentation(context: Context) -> None:
"""Generate the documentation for infrahubctl CLI using typer-cli."""
from infrahub_sdk.ctl.cli import app
output_dir = DOCUMENTATION_DIRECTORY / "docs" / "infrahubctl"
output_dir.mkdir(parents=True, exist_ok=True)
print(" - Generate infrahubctl CLI documentation")
for cmd in app.registered_commands:
exec_cmd = f'poetry run typer --func {cmd.name} infrahub_sdk.ctl.cli_commands utils docs --name "infrahubctl {cmd.name}"'
exec_cmd += f" --output docs/docs/infrahubctl/infrahubctl-{cmd.name}.mdx"
with context.cd(MAIN_DIRECTORY_PATH):
context.run(exec_cmd)
for cmd in app.registered_groups:
exec_cmd = f"poetry run typer infrahub_sdk.ctl.{cmd.name} utils docs"
exec_cmd += f' --name "infrahubctl {cmd.name}" --output docs/docs/infrahubctl/infrahubctl-{cmd.name}.mdx'
with context.cd(MAIN_DIRECTORY_PATH):
context.run(exec_cmd)
def _generate_infrahub_sdk_configuration_documentation() -> None:
"""Generate documentation for the Infrahub SDK configuration."""
import jinja2
from infrahub_sdk.config import ConfigBase
schema = ConfigBase.model_json_schema()
env_vars = _get_env_vars()
definitions = schema.get("$defs", {})
properties = []
for name, prop in schema["properties"].items():
choices: list[dict[str, Any]] = []
kind = ""
composed_type = ""
if "allOf" in prop:
choices = definitions.get(prop["allOf"][0]["$ref"].split("/")[-1], {}).get("enum", [])
kind = definitions.get(prop["allOf"][0]["$ref"].split("/")[-1], {}).get("type", "")
if "anyOf" in prop:
composed_type = ", ".join(i["type"] for i in prop.get("anyOf", []) if "type" in i and i["type"] != "null")
properties.append(
{
"name": name,
"description": prop.get("description", ""),
"type": prop.get("type", kind) or composed_type or "object",
"choices": choices,
"default": prop.get("default", ""),
"env_vars": env_vars.get(name, []),
}
)
print(" - Generate Infrahub SDK configuration documentation")
template_file = DOCUMENTATION_DIRECTORY / "_templates" / "sdk_config.j2"
output_file = DOCUMENTATION_DIRECTORY / "docs" / "python-sdk" / "reference" / "config.mdx"
if not template_file.exists():
print(f"Unable to find the template file at {template_file}")
sys.exit(-1)
template_text = template_file.read_text(encoding="utf-8")
environment = jinja2.Environment(trim_blocks=True)
template = environment.from_string(template_text)
rendered_file = template.render(properties=properties)
output_file.write_text(rendered_file, encoding="utf-8")
print(f"Docs saved to: {output_file}")
def _get_env_vars() -> dict[str, list[str]]:
"""Retrieve environment variables for Infrahub SDK configuration."""
from collections import defaultdict
from pydantic_settings import EnvSettingsSource
from infrahub_sdk.config import ConfigBase
env_vars: dict[str, list[str]] = defaultdict(list)
settings = ConfigBase()
env_settings = EnvSettingsSource(settings.__class__, env_prefix=settings.model_config.get("env_prefix", ""))
for field_name, field in settings.model_fields.items():
for field_key, field_env_name, _ in env_settings._extract_field_info(field, field_name):
env_vars[field_key].append(field_env_name.upper())
return env_vars
@task
def format(context: Context) -> None:
"""Run RUFF to format all Python files."""
exec_cmds = ["ruff format .", "ruff check . --fix"]
with context.cd(MAIN_DIRECTORY_PATH):
for cmd in exec_cmds:
context.run(cmd)
@task
def lint_yaml(context: Context) -> None:
"""Run Linter to check all Python files."""
print(" - Check code with yamllint")
exec_cmd = "yamllint ."
with context.cd(MAIN_DIRECTORY_PATH):
context.run(exec_cmd)
@task
def lint_mypy(context: Context) -> None:
"""Run Linter to check all Python files."""
print(" - Check code with mypy")
exec_cmd = "mypy --show-error-codes infrahub_sdk"
with context.cd(MAIN_DIRECTORY_PATH):
context.run(exec_cmd)
@task
def lint_ruff(context: Context) -> None:
"""Run Linter to check all Python files."""
print(" - Check code with ruff")
exec_cmd = "ruff check ."
with context.cd(MAIN_DIRECTORY_PATH):
context.run(exec_cmd)
@task(name="lint")
def lint_all(context: Context) -> None:
"""Run all linters."""
lint_yaml(context)
lint_ruff(context)
lint_mypy(context)
@task(name="docs-validate")
def docs_validate(context: Context) -> None:
"""Validate that the generated documentation is committed to Git."""
_generate(context=context)
exec_cmd = "git diff --exit-code docs"
with context.cd(MAIN_DIRECTORY_PATH):
context.run(exec_cmd)
@task(name="docs")
def docs_build(context: Context) -> None:
"""Build documentation website."""
exec_cmd = "npm run build"
with context.cd(DOCUMENTATION_DIRECTORY):
output = context.run(exec_cmd)
if output.exited != 0:
sys.exit(-1)
@task(name="generate-infrahubctl")
def generate_infrahubctl(context: Context) -> None:
"""Generate documentation for the infrahubctl cli."""
_generate_infrahubctl_documentation(context=context)
@task(name="generate-sdk")
def generate_python_sdk(context: Context) -> None: # noqa: ARG001
"""Generate documentation for the Python SDK."""
_generate_infrahub_sdk_configuration_documentation()