-
Notifications
You must be signed in to change notification settings - Fork 5
/
template.py
70 lines (59 loc) · 1.93 KB
/
template.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
import argparse
from pathlib import Path
import yaml
from jinja2 import Environment, FileSystemLoader, select_autoescape
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"filename",
type=argparse.FileType("r"),
help="Configuration file, e.g. `values.yaml`",
)
parser.add_argument(
"--template-dir",
"--templates-dir",
type=Path,
default=Path("templates"),
help="Directory to find templates",
)
parser.add_argument(
"--base-path",
"--dockerfile-path",
type=Path,
default=Path("platforms"),
help="Directory to save templated Dockerfiles",
)
args = parser.parse_args()
env = Environment(
loader=FileSystemLoader(args.template_dir), autoescape=select_autoescape()
)
values = yaml.safe_load(args.filename)
for compiler in values.get("compilers", []):
if "arch" in compiler:
target_path = (
args.base_path
/ compiler["platform"]
/ compiler["id"]
/ compiler["arch"]
/ "Dockerfile"
)
else:
target_path = (
args.base_path / compiler["platform"] / compiler["id"] / "Dockerfile"
)
print(
f"{compiler['id']}: Creating {target_path} from {compiler['template']}.j2"
)
template_path = compiler["template"]
template = env.get_template(f"{template_path}.j2")
rendered = template.render(compiler)
target_path.parent.mkdir(parents=True, exist_ok=True)
with target_path.open("w") as f:
f.write(
"# NOTE: This file is generated automatically via template.py. Do not edit manually!\n\n"
)
f.write("\n")
f.write(rendered)
f.write("\n") # enforce trailing newline
if __name__ == "__main__":
main()