blob: c79cb645d4a87bac80262bfbdbf0d33a188667dc [file] [log] [blame]
Tom Joseph31eed5c2020-03-23 20:56:47 +05301#!/usr/bin/env python3
Marri Devender Raoa62bacc2017-06-04 23:40:16 -05002
Patrick Williamsbe28ff52022-12-05 10:14:46 -06003import argparse
Marri Devender Raoa62bacc2017-06-04 23:40:16 -05004import os
5import sys
Patrick Williamsbe28ff52022-12-05 10:14:46 -06006
Marri Devender Raoa62bacc2017-06-04 23:40:16 -05007import yaml
Marri Devender Raoa62bacc2017-06-04 23:40:16 -05008from mako.template import Template
9
10
11def generate_cpp(inventory_yaml, output_dir):
Patrick Williamsbe28ff52022-12-05 10:14:46 -060012 with open(inventory_yaml, "r") as f:
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050013 ifile = yaml.safe_load(f)
14 if not isinstance(ifile, dict):
15 ifile = {}
16
17 # Render the mako template
18
Patrick Williamsbe28ff52022-12-05 10:14:46 -060019 t = Template(filename=os.path.join(script_dir, "readfru.mako.cpp"))
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050020
21 output_hpp = os.path.join(output_dir, "fru-read-gen.cpp")
Patrick Williamsbe28ff52022-12-05 10:14:46 -060022 with open(output_hpp, "w") as fd:
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050023 fd.write(t.render(fruDict=ifile))
24
25
26def main():
Patrick Williamsbe28ff52022-12-05 10:14:46 -060027 valid_commands = {"generate-cpp": generate_cpp}
28 parser = argparse.ArgumentParser(description="IPMI FRU map code generator")
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050029
30 parser.add_argument(
Patrick Williamsbe28ff52022-12-05 10:14:46 -060031 "-i",
32 "--inventory_yaml",
33 dest="inventory_yaml",
34 default="example.yaml",
35 help="input inventory yaml file to parse",
36 )
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050037
38 parser.add_argument(
Patrick Williamsbe28ff52022-12-05 10:14:46 -060039 "-o",
40 "--output-dir",
41 dest="outputdir",
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050042 default=".",
Patrick Williamsbe28ff52022-12-05 10:14:46 -060043 help="output directory",
44 )
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050045
46 parser.add_argument(
Patrick Williamsbe28ff52022-12-05 10:14:46 -060047 "command",
48 metavar="COMMAND",
49 type=str,
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050050 choices=valid_commands.keys(),
Patrick Williamsbe28ff52022-12-05 10:14:46 -060051 help="Command to run.",
52 )
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050053
54 args = parser.parse_args()
55
Patrick Williamsbe28ff52022-12-05 10:14:46 -060056 if not (os.path.isfile(args.inventory_yaml)):
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050057 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
Patrick Williamsbe28ff52022-12-05 10:14:46 -060062
63if __name__ == "__main__":
Marri Devender Raoa62bacc2017-06-04 23:40:16 -050064 script_dir = os.path.dirname(os.path.realpath(__file__))
65 main()