-
Notifications
You must be signed in to change notification settings - Fork 44
/
build_chart.py
49 lines (48 loc) · 1.84 KB
/
build_chart.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
import os
import re
import json
import glob
import yaml
from io import StringIO
from jinja2 import Environment, FileSystemLoader
root_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
version = open(os.path.join(root_dir, 'VERSION'), 'r').read().strip()
appyter_path = os.path.join(root_dir, 'appyters')
appyters = [
dict(
path=os.path.dirname(path),
**json.load(open(path, 'r')),
)
for path in glob.glob(os.path.join(appyter_path, '*', 'appyter.json'))
]
env = Environment(
extensions=['jinja2.ext.with_'],
loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')),
)
template = env.get_template('Chart.yaml.j2')
chart_files_spec = template.render(
appyters=appyters,
version=version,
)
chart_files = re.compile(r'\n+---', re.MULTILINE).split(chart_files_spec)
# load first two files (Chart.yaml & questions.yaml)
chart_desc, chart_questions = yaml.load(StringIO(chart_files[0])), yaml.load(StringIO(chart_files[1]))
chart_root = os.path.join(root_dir, 'charts', chart_desc['name'], chart_desc['version'])
# create values.yaml from questions.yaml
os.makedirs(chart_root, exist_ok=True)
with open(os.path.join(chart_root, 'values.yaml'), 'w') as fw:
for question in chart_questions['questions']:
print(
f"# {question['description']} ({question['group']})",
f"{'' if question['required'] else '#'}{question['variable']}: \"{question['default']}\"",
sep='\n',
file=fw,
)
# create templates
for chart_file in chart_files:
m = re.compile(r'\n*# Source: ([^\n]+)\n(.+)', re.DOTALL | re.MULTILINE).match(chart_file)
chart_file_path, chart_file_content = m.group(1), m.group(2)
chart_file_full_path = os.path.join(chart_root, chart_file_path)
os.makedirs(os.path.dirname(chart_file_full_path), exist_ok=True)
with open(chart_file_full_path, 'w') as fw:
fw.write(chart_file_content)