-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_templates.py
47 lines (37 loc) · 1.31 KB
/
apply_templates.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
import os, re, sys
from jinja2 import Environment, FileSystemLoader, select_autoescape
"""
Apply Default Values to Jinja Templates
"""
TEMPLATE_FILES= ['service/milton.service.j2']
REAL_FILES = [t[:-3] for t in TEMPLATE_FILES]
# Should existing files be overwritten
OVERWRITE = False
REQ_ENV_VARS = [
'MILTON_PATH', # path to the milton repo
'MILTON_USER', # the user to run the service as
]
def usage():
print(sys.argv[0], "usage:")
print("")
print("You must define the following environment variables")
print("to render the Jinja templates:")
for env_var in REQ_ENV_VARS:
print(" "*4,env_var)
for env_var in REQ_ENV_VARS:
if env_var not in os.environ:
usage()
raise Exception(f"Error: required env var not defined: {env_var}")
for template_file, real_file in zip(TEMPLATE_FILES, REAL_FILES):
template_dir = os.path.basepath(template_file)
env = Environment(loader=FileSystemLoader(template_dir))
render_kwargs = {}
for env_var in REQ_ENV_VARS:
render_kwargs[env_var.lower()] = os.environ[env_var]
content = env.get_template(template_file).render(**render_kwargs)
# Write to file
if os.path.exists(rfile) and not OVERWRITE:
raise Exception("Error: file %s already exists!"%(rfile))
else:
with open(rfile,'w') as f:
f.write(content)