-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspiler.py
185 lines (156 loc) · 6.15 KB
/
transpiler.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
"""
Transpiles templates to terraform files
"""
import os
import jinja2
import yaml
from loguru import logger
from . import utils
from .architecture import ArchitectureConfig
from .common import init
from .schema import Schema, SchemaRegistry
from .tags import report_dumper
from .template import TemplateDefinition, TemplateRoot
TEMPLATE_ROOT_FILE: str = "root.yaml"
TEMPLATE_DEFINITION_FILE: str = "definition.yaml"
def read_template_dir(
template_dir: str, template_type: str, path: str, schemas: dict[str, Schema]
) -> TemplateDefinition:
"""
Reads the `TEMPLATE_DEFINITION_FILE` from the template directory and returns a `Template`
"""
# provide either a path to a template directory or a path to a template root file
file = utils.default_file_from_path(
os.path.join(template_dir, path), TEMPLATE_DEFINITION_FILE
)
return TemplateDefinition.from_schemas(template_dir, template_type, file, schemas)
def transpile(
input_file: str, template_dir: str, out_dir: str, report: bool, debug: bool
) -> None:
"""
Transpiles the files
"""
mappings: list = []
stats: dict = {"outputFiles": 0}
jinja: jinja2.Environment
schema_registry: SchemaRegistry
architecture: ArchitectureConfig
jinja, schema_registry, architecture = init(input_file)
root: TemplateRoot = TemplateRoot.with_schema_registry(
os.path.join(template_dir, TEMPLATE_ROOT_FILE), schema_registry
)
platforms: list[dict] = architecture.platforms()
platform_names: list[str] = list(map(lambda x: x["name"], platforms))
template_data: dict = {
**architecture.metadata,
}
# create & clear output directories
logger.info("Perparing output directories...")
for platform in platform_names:
folder = os.path.join(out_dir, platform)
os.makedirs(folder, exist_ok=True)
# delete all files in folders
for path in os.listdir(folder):
file = os.path.join(folder, path)
if os.path.isfile(file) and path not in [
".terraform.lock.hcl",
"terraform.tfstate",
".terraform.tfstate.lock.info",
"terraform.tfstate.backup",
]:
os.remove(file)
# read templates
logger.info("Reading templates...")
template_registry: dict[str, TemplateDefinition] = {}
for template in root["templates"]:
template_name = template.replace("/", "")
template_registry[template_name] = read_template_dir(
template_dir, template_name, template, schema_registry
)
# write out templated files
logger.info("Generating output files...")
for platform_struct in platforms:
platform_name = platform_struct["name"]
platform_properties = platform_struct["properties"]
component_data = template_data | platform_properties
folder = os.path.join(out_dir, platform_name)
os.makedirs(folder, exist_ok=True)
for special in ["main", "versions"]:
files = read_template_dir(
template_dir, special, root[special], schema_registry
).render(platform_name, jinja, component_data)
for file in files:
path = file.save(folder, special)
mappings.append(
{
"platform": platform_name,
"component": special,
"path": path,
"properties": component_data,
}
)
stats["outputFiles"] = stats["outputFiles"] + 1
for component in architecture.components():
if component["type"] not in template_registry:
logger.error(f"Unknown component type '{component['type']}'")
exit(1)
template: TemplateDefinition = template_registry[component["type"]]
component_name = component["name"]
component_properties = component.get("properties")
# validate that all required component properties are set
if not template.validate_properties(
component_properties, architecture.components()
):
logger.error(f"Invalid properties for component '{component_name}'")
exit(1)
component_data = (
template_data
| platform_properties
| component_properties
| {
"resourceId": component_name,
"resourceType": component["type"],
}
)
for platform_struct in platforms:
platform_name = platform_struct["name"]
platform_properties = platform_struct["properties"]
folder = os.path.join(out_dir, platform_name)
files = template.render(platform_name, jinja, component_data)
for file in files:
path = file.save(folder, component["name"])
mappings.append(
{
"platform": platform_name,
"component": component["name"],
"path": path,
"properties": component_data,
}
)
stats["outputFiles"] = stats["outputFiles"] + 1
if report:
if not debug:
logger.info("Prettifying report...")
platform_mappings = {}
for mapping in mappings:
del mapping["properties"]
plat = mapping["platform"]
del mapping["platform"]
if plat in platform_mappings:
platform_mappings[plat].append(mapping)
else:
platform_mappings[plat] = [mapping]
mappings = platform_mappings
logger.info("Saving report...")
with open("out/report.yaml", "w", encoding="utf8") as file:
file.write(
yaml.dump(
{
"metadata": architecture.metadata,
"stats": stats,
"platforms": platform_names,
"mappings": mappings,
},
Dumper=report_dumper(),
)
)