blob: be5cae6fac74542d01221c9ed4b3c26e9b880c92 [file] [log] [blame]
Tom Joseph31eed5c2020-03-23 20:56:47 +05301#!/usr/bin/env python3
Tom Josephe19540e2019-02-04 14:06:58 +05302
Patrick Williams93682252022-12-08 06:18:03 -06003import argparse
Tom Josephe19540e2019-02-04 14:06:58 +05304import os
5import sys
Patrick Williams93682252022-12-08 06:18:03 -06006
Tom Josephe19540e2019-02-04 14:06:58 +05307import yaml
Tom Josephe19540e2019-02-04 14:06:58 +05308from mako.template import Template
9
10
11def generate_cpp(sensor_yaml, output_dir):
Patrick Williams93682252022-12-08 06:18:03 -060012 with open(sensor_yaml, "r") as f:
Tom Josephe19540e2019-02-04 14:06:58 +053013 ifile = yaml.safe_load(f)
14 if not isinstance(ifile, dict):
15 ifile = {}
16
17 # Render the mako template
18
Patrick Williams93682252022-12-08 06:18:03 -060019 t = Template(
20 filename=os.path.join(script_dir, "inventorysensor.mako.cpp")
21 )
Tom Josephe19540e2019-02-04 14:06:58 +053022
23 output_cpp = os.path.join(output_dir, "inventory-sensor-gen.cpp")
Patrick Williams93682252022-12-08 06:18:03 -060024 with open(output_cpp, "w") as fd:
Tom Josephe19540e2019-02-04 14:06:58 +053025 fd.write(t.render(sensorDict=ifile))
26
27
28def main():
Patrick Williams93682252022-12-08 06:18:03 -060029 valid_commands = {"generate-cpp": generate_cpp}
Tom Josephe19540e2019-02-04 14:06:58 +053030 parser = argparse.ArgumentParser(
Patrick Williams93682252022-12-08 06:18:03 -060031 description="Inventory Object to IPMI SensorID code generator"
32 )
Tom Josephe19540e2019-02-04 14:06:58 +053033
34 parser.add_argument(
Patrick Williams93682252022-12-08 06:18:03 -060035 "-i",
36 "--sensor_yaml",
37 dest="sensor_yaml",
38 default="example.yaml",
39 help="input sensor yaml file to parse",
40 )
Tom Josephe19540e2019-02-04 14:06:58 +053041
42 parser.add_argument(
Patrick Williams93682252022-12-08 06:18:03 -060043 "-o",
44 "--output-dir",
45 dest="outputdir",
Tom Josephe19540e2019-02-04 14:06:58 +053046 default=".",
Patrick Williams93682252022-12-08 06:18:03 -060047 help="output directory",
48 )
Tom Josephe19540e2019-02-04 14:06:58 +053049
50 parser.add_argument(
Patrick Williams93682252022-12-08 06:18:03 -060051 "command",
52 metavar="COMMAND",
53 type=str,
Tom Josephe19540e2019-02-04 14:06:58 +053054 choices=valid_commands.keys(),
Patrick Williams93682252022-12-08 06:18:03 -060055 help="Command to run.",
56 )
Tom Josephe19540e2019-02-04 14:06:58 +053057
58 args = parser.parse_args()
59
Patrick Williams93682252022-12-08 06:18:03 -060060 if not (os.path.isfile(args.sensor_yaml)):
Tom Josephe19540e2019-02-04 14:06:58 +053061 sys.exit("Can not find input yaml file " + args.sensor_yaml)
62
63 function = valid_commands[args.command]
64 function(args.sensor_yaml, args.outputdir)
65
66
Patrick Williams93682252022-12-08 06:18:03 -060067if __name__ == "__main__":
Tom Josephe19540e2019-02-04 14:06:58 +053068 script_dir = os.path.dirname(os.path.realpath(__file__))
69 main()