blob: 624d0526946ba413faf1d794960229a00e5cedaf [file] [log] [blame]
Patrick Williams09682372020-04-03 15:10:31 -05001#!/usr/bin/env python3
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +05302
Patrick Williamsc5c17372022-12-08 06:18:06 -06003import argparse
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +05304import os
5import sys
Patrick Williamsc5c17372022-12-08 06:18:06 -06006
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +05307import yaml
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +05308from mako.template import Template
9
10
Ratan Gupta6eed4032017-02-10 15:59:31 +053011def generate_cpp(inventory_yaml, output_dir):
Patrick Williamsc5c17372022-12-08 06:18:06 -060012 with open(os.path.join(script_dir, inventory_yaml), "r") as f:
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053013 ifile = yaml.safe_load(f)
Brad Bishop3d0ce842017-02-07 08:33:56 -050014 if not isinstance(ifile, dict):
15 ifile = {}
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053016
17 # Render the mako template
18
Patrick Williamsc5c17372022-12-08 06:18:06 -060019 t = Template(filename=os.path.join(script_dir, "writefru.mako.cpp"))
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053020
Ratan Gupta6eed4032017-02-10 15:59:31 +053021 output_hpp = os.path.join(output_dir, "fru-gen.cpp")
Patrick Williamsc5c17372022-12-08 06:18:06 -060022 with open(output_hpp, "w") as fd:
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053023 fd.write(t.render(fruDict=ifile))
24
25
26def main():
Patrick Williamsc5c17372022-12-08 06:18:06 -060027 valid_commands = {"generate-cpp": generate_cpp}
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053028 parser = argparse.ArgumentParser(
Patrick Williamsc5c17372022-12-08 06:18:06 -060029 description="IPMI FRU parser and code generator"
30 )
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053031
32 parser.add_argument(
Patrick Williamsc5c17372022-12-08 06:18:06 -060033 "-i",
34 "--inventory_yaml",
35 dest="inventory_yaml",
36 default="example.yaml",
37 help="input inventory yaml file to parse",
38 )
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053039
40 parser.add_argument(
Patrick Williamsc5c17372022-12-08 06:18:06 -060041 "-o",
42 "--output-dir",
43 dest="outputdir",
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053044 default=".",
Patrick Williamsc5c17372022-12-08 06:18:06 -060045 help="output directory",
46 )
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053047
48 parser.add_argument(
Patrick Williamsc5c17372022-12-08 06:18:06 -060049 "command",
50 metavar="COMMAND",
51 type=str,
Patrick Williams09682372020-04-03 15:10:31 -050052 choices=list(valid_commands.keys()),
Patrick Williamsc5c17372022-12-08 06:18:06 -060053 help="Command to run.",
54 )
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053055
56 args = parser.parse_args()
57
Patrick Williamsc5c17372022-12-08 06:18:06 -060058 if not (os.path.isfile(os.path.join(script_dir, args.inventory_yaml))):
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053059 sys.exit("Can not find input yaml file " + args.inventory_yaml)
60
61 function = valid_commands[args.command]
62 function(args.inventory_yaml, args.outputdir)
63
Patrick Williamsc5c17372022-12-08 06:18:06 -060064
65if __name__ == "__main__":
Ratan Gupta0f2e7fe2016-12-22 19:00:41 +053066 script_dir = os.path.dirname(os.path.realpath(__file__))
67 main()