xref: /openbmc/openpower-host-ipmi-oem/scripts/inventory-sensor.py (revision 9975ae919e4cb8968639d9f953912e6c73205c44)
1*9975ae91SPatrick Williams#!/usr/bin/env python3
2b61b107fSTom Joseph
3b61b107fSTom Josephimport os
4b61b107fSTom Josephimport sys
5b61b107fSTom Josephimport yaml
6b61b107fSTom Josephimport argparse
7b61b107fSTom Josephfrom mako.template import Template
8b61b107fSTom Joseph
9b61b107fSTom Joseph
10b61b107fSTom Josephdef generate_cpp(sensor_yaml, output_dir):
11b61b107fSTom Joseph    with open(os.path.join(script_dir, sensor_yaml), 'r') as f:
12b61b107fSTom Joseph        ifile = yaml.safe_load(f)
13b61b107fSTom Joseph        if not isinstance(ifile, dict):
14b61b107fSTom Joseph            ifile = {}
15b61b107fSTom Joseph
16b61b107fSTom Joseph        # Render the mako template
17b61b107fSTom Joseph
18b61b107fSTom Joseph        t = Template(filename=os.path.join(
19b61b107fSTom Joseph                     script_dir,
20b61b107fSTom Joseph                     "inventorysensor.mako.cpp"))
21b61b107fSTom Joseph
22b61b107fSTom Joseph        output_cpp = os.path.join(output_dir, "inventory-sensor.cpp")
23b61b107fSTom Joseph        with open(output_cpp, 'w') as fd:
24b61b107fSTom Joseph            fd.write(t.render(sensorDict=ifile))
25b61b107fSTom Joseph
26b61b107fSTom Joseph
27b61b107fSTom Josephdef main():
28b61b107fSTom Joseph
29b61b107fSTom Joseph    valid_commands = {
30b61b107fSTom Joseph        'generate-cpp': generate_cpp
31b61b107fSTom Joseph    }
32b61b107fSTom Joseph    parser = argparse.ArgumentParser(
33b61b107fSTom Joseph        description="Inventory Object to IPMI SensorID code generator")
34b61b107fSTom Joseph
35b61b107fSTom Joseph    parser.add_argument(
36b61b107fSTom Joseph        '-i', '--sensor_yaml', dest='sensor_yaml',
37b61b107fSTom Joseph        default='example.yaml', help='input sensor yaml file to parse')
38b61b107fSTom Joseph
39b61b107fSTom Joseph    parser.add_argument(
40b61b107fSTom Joseph        "-o", "--output-dir", dest="outputdir",
41b61b107fSTom Joseph        default=".",
42b61b107fSTom Joseph        help="output directory")
43b61b107fSTom Joseph
44b61b107fSTom Joseph    parser.add_argument(
45b61b107fSTom Joseph        'command', metavar='COMMAND', type=str,
46*9975ae91SPatrick Williams        choices=list(valid_commands.keys()),
47b61b107fSTom Joseph        help='Command to run.')
48b61b107fSTom Joseph
49b61b107fSTom Joseph    args = parser.parse_args()
50b61b107fSTom Joseph
51b61b107fSTom Joseph    if (not (os.path.isfile(os.path.join(script_dir, args.sensor_yaml)))):
52b61b107fSTom Joseph        sys.exit("Can not find input yaml file " + args.sensor_yaml)
53b61b107fSTom Joseph
54b61b107fSTom Joseph    function = valid_commands[args.command]
55b61b107fSTom Joseph    function(args.sensor_yaml, args.outputdir)
56b61b107fSTom Joseph
57b61b107fSTom Joseph
58b61b107fSTom Josephif __name__ == '__main__':
59b61b107fSTom Joseph    script_dir = os.path.dirname(os.path.realpath(__file__))
60b61b107fSTom Joseph    main()
61