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