blob: f121c9472021ecea6153228fadaa93548624b351 [file] [log] [blame]
Tom Joseph31eed5c2020-03-23 20:56:47 +05301#!/usr/bin/env python3
Ratan Gupta00659052017-02-23 17:29:08 +05302
Patrick Williams93682252022-12-08 06:18:03 -06003import argparse
Ratan Gupta00659052017-02-23 17:29:08 +05304import os
5import sys
Patrick Williams93682252022-12-08 06:18:03 -06006
Ratan Gupta00659052017-02-23 17:29:08 +05307import yaml
Ratan Gupta00659052017-02-23 17:29:08 +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:
Ratan Gupta00659052017-02-23 17:29:08 +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(filename=os.path.join(script_dir, "writesensor.mako.cpp"))
Ratan Gupta00659052017-02-23 17:29:08 +053020
21 output_cpp = os.path.join(output_dir, "sensor-gen.cpp")
Patrick Williams93682252022-12-08 06:18:03 -060022 with open(output_cpp, "w") as fd:
Ratan Gupta00659052017-02-23 17:29:08 +053023 fd.write(t.render(sensorDict=ifile))
24
25
26def main():
Patrick Williams93682252022-12-08 06:18:03 -060027 valid_commands = {"generate-cpp": generate_cpp}
Ratan Gupta00659052017-02-23 17:29:08 +053028 parser = argparse.ArgumentParser(
Patrick Williams93682252022-12-08 06:18:03 -060029 description="IPMI Sensor parser and code generator"
30 )
Ratan Gupta00659052017-02-23 17:29:08 +053031
32 parser.add_argument(
Patrick Williams93682252022-12-08 06:18:03 -060033 "-i",
34 "--sensor_yaml",
35 dest="sensor_yaml",
36 default="example.yaml",
37 help="input sensor yaml file to parse",
38 )
Ratan Gupta00659052017-02-23 17:29:08 +053039
40 parser.add_argument(
Patrick Williams93682252022-12-08 06:18:03 -060041 "-o",
42 "--output-dir",
43 dest="outputdir",
Ratan Gupta00659052017-02-23 17:29:08 +053044 default=".",
Patrick Williams93682252022-12-08 06:18:03 -060045 help="output directory",
46 )
Ratan Gupta00659052017-02-23 17:29:08 +053047
48 parser.add_argument(
Patrick Williams93682252022-12-08 06:18:03 -060049 "command",
50 metavar="COMMAND",
51 type=str,
Ratan Gupta00659052017-02-23 17:29:08 +053052 choices=valid_commands.keys(),
Patrick Williams93682252022-12-08 06:18:03 -060053 help="Command to run.",
54 )
Ratan Gupta00659052017-02-23 17:29:08 +053055
56 args = parser.parse_args()
57
Patrick Williams93682252022-12-08 06:18:03 -060058 if not (os.path.isfile(args.sensor_yaml)):
Ratan Gupta00659052017-02-23 17:29:08 +053059 sys.exit("Can not find input yaml file " + args.sensor_yaml)
60
61 function = valid_commands[args.command]
62 function(args.sensor_yaml, args.outputdir)
63
64
Patrick Williams93682252022-12-08 06:18:03 -060065if __name__ == "__main__":
Ratan Gupta00659052017-02-23 17:29:08 +053066 script_dir = os.path.dirname(os.path.realpath(__file__))
67 main()