1#!/usr/bin/env python3
2
3import argparse
4import os
5import sys
6
7import yaml
8from mako.template import Template
9
10
11def generate_cpp(inventory_yaml, output_dir):
12    with open(inventory_yaml, "r") as f:
13        ifile = yaml.safe_load(f)
14        if not isinstance(ifile, dict):
15            ifile = {}
16
17        # Render the mako template
18
19        t = Template(filename=os.path.join(script_dir, "readfru.mako.cpp"))
20
21        output_hpp = os.path.join(output_dir, "fru-read-gen.cpp")
22        with open(output_hpp, "w") as fd:
23            fd.write(t.render(fruDict=ifile))
24
25
26def main():
27    valid_commands = {"generate-cpp": generate_cpp}
28    parser = argparse.ArgumentParser(description="IPMI FRU map code generator")
29
30    parser.add_argument(
31        "-i",
32        "--inventory_yaml",
33        dest="inventory_yaml",
34        default="example.yaml",
35        help="input inventory yaml file to parse",
36    )
37
38    parser.add_argument(
39        "-o",
40        "--output-dir",
41        dest="outputdir",
42        default=".",
43        help="output directory",
44    )
45
46    parser.add_argument(
47        "command",
48        metavar="COMMAND",
49        type=str,
50        choices=valid_commands.keys(),
51        help="Command to run.",
52    )
53
54    args = parser.parse_args()
55
56    if not (os.path.isfile(args.inventory_yaml)):
57        sys.exit("Can not find input yaml file " + args.inventory_yaml)
58
59    function = valid_commands[args.command]
60    function(args.inventory_yaml, args.outputdir)
61
62
63if __name__ == "__main__":
64    script_dir = os.path.dirname(os.path.realpath(__file__))
65    main()
66