|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import os |
| 5 | +import glob |
| 6 | +import yaml |
| 7 | +from jinja2 import Template |
| 8 | + |
| 9 | + |
| 10 | +def compile_kernels(fns): |
| 11 | + template_dir = "templates" |
| 12 | + output_dir = os.path.join("src", "george") |
| 13 | + |
| 14 | + with open(os.path.join(template_dir, "parser.h")) as f: |
| 15 | + PARSER_TEMPLATE = Template(f.read()) |
| 16 | + with open(os.path.join(template_dir, "kernels.h")) as f: |
| 17 | + CPP_TEMPLATE = Template(f.read()) |
| 18 | + with open(os.path.join(template_dir, "kernels.py")) as f: |
| 19 | + PYTHON_TEMPLATE = Template(f.read()) |
| 20 | + |
| 21 | + specs = [] |
| 22 | + for i, fn in enumerate(fns): |
| 23 | + with open(fn, "r") as f: |
| 24 | + spec = yaml.load(f.read(), Loader=yaml.FullLoader) |
| 25 | + print("Found kernel '{0}'".format(spec["name"])) |
| 26 | + spec["index"] = i |
| 27 | + spec["reparams"] = spec.get("reparams", {}) |
| 28 | + specs.append(spec) |
| 29 | + print("Found {0} kernel specifications".format(len(specs))) |
| 30 | + |
| 31 | + fn = os.path.join(output_dir, "include", "george", "parser.h") |
| 32 | + with open(fn, "w") as f: |
| 33 | + print("Saving parser to '{0}'".format(fn)) |
| 34 | + f.write(PARSER_TEMPLATE.render(specs=specs)) |
| 35 | + fn = os.path.join(output_dir, "include", "george", "kernels.h") |
| 36 | + with open(fn, "w") as f: |
| 37 | + print("Saving C++ kernels to '{0}'".format(fn)) |
| 38 | + f.write(CPP_TEMPLATE.render(specs=specs)) |
| 39 | + fn = os.path.join(output_dir, "kernels.py") |
| 40 | + with open(fn, "w") as f: |
| 41 | + print("Saving Python kernels to '{0}'".format(fn)) |
| 42 | + f.write(PYTHON_TEMPLATE.render(specs=specs)) |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + # If the kernel specifications are included (development mode) re-compile |
| 47 | + # them first. |
| 48 | + kernel_specs = glob.glob(os.path.join("kernels", "*.yml")) |
| 49 | + compile_kernels(kernel_specs) |
0 commit comments