forked from lest/prometheus-rpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
139 lines (116 loc) · 4.43 KB
/
generate.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
#!/usr/bin/python3
"""
This script generates default, spec, unit and init files for CentOS build_files.
"""
import argparse
import logging
import os
import sys
from pprint import pprint
import jinja2
import yaml
def renderTemplateFromFile(templates_dir, template_file, context):
return (
jinja2.Environment(loader=jinja2.FileSystemLoader(templates_dir or "./"))
.get_template(template_file)
.render(context)
)
def renderTemplateFromString(templates_dir, template_string, context):
return (
jinja2.Environment(loader=jinja2.FileSystemLoader(templates_dir or "./"))
.from_string(template_string)
.render(context)
)
if __name__ == "__main__":
env_template_config = os.environ.get("TEMPLATE_CONFIG_FILE", "./templating.yaml")
env_templates_dir = os.environ.get("TEMPLATES_DIRECTORY", "./templates/")
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument(
"--templates",
metavar="N",
type=str,
nargs="+",
default="all",
help="A list of templates to generate",
)
parser.add_argument(
"--template-config",
metavar="file",
type=str,
default=env_template_config,
help="The configuration file to generate templates with",
)
parser.add_argument(
"--templates-dir",
metavar="directory",
type=str,
default=env_templates_dir,
help="The directory that templates are stored in",
)
args = parser.parse_args()
templates = args.templates
template_config = args.template_config
templates_dir = args.templates_dir
logging.basicConfig(level=logging.INFO)
with open(template_config, "r") as tc:
config = yaml.safe_load(tc)
# Work out which templates we are calculating
if templates == "all":
work = config["packages"]
else:
work = {}
for t in templates:
work[t] = config["packages"][t]
for exporter_name, exporter_config in work.items():
logging.info("Building {}".format(exporter_name.upper()))
try:
os.makedirs(exporter_name, exist_ok=True)
except OSError:
logging.error("Failed to create directory %s" % exporter_name)
sys.exit(1)
# First we need to work out the context for this build
context = exporter_config["context"]["static"]
# Add in the exporter name as a context variable
context["name"] = exporter_name
# Use the compiled contexts to build the dynamic contexts.
# Dynamic contexts can use other dynamic contexts as long
# as they are compiled before.
for context_name in ["tarball", "sources"]:
to_template = exporter_config["context"]["dynamic"][context_name]
print("context name: %s" % context_name)
if type(to_template) is str:
context[context_name] = renderTemplateFromString(
templates_dir=templates_dir,
template_string=to_template,
context=context,
)
elif type(to_template) is list:
context_element = []
for item in to_template:
context_element.append(
renderTemplateFromString(
templates_dir=templates_dir,
template_string=item,
context=context,
)
)
context[context_name] = context_element
else:
raise TypeError(
"Invalid type {} for key {}".format(type(to_template), context_name)
)
pprint(context)
for build_step, template in exporter_config["build_steps"].items():
output = "{name}/autogen_{name}.{build_step}".format(
**{"name": exporter_name, "build_step": build_step}
)
logging.info("Rendering step {} for {}".format(build_step, exporter_name))
rendered = renderTemplateFromString(
templates_dir=templates_dir, template_string=template, context=context
)
logging.info(
"Writing {} step {} to {}".format(exporter_name, build_step, output)
)
# print(rendered)
with open(output, "w") as output_file:
output_file.write(rendered)