Tom Joseph | 31eed5c | 2020-03-23 20:56:47 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 2 | |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 3 | import argparse |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 4 | import os |
| 5 | import sys |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 6 | |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 7 | import yaml |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 8 | from mako.template import Template |
| 9 | |
| 10 | |
| 11 | def generate_cpp(sensor_yaml, output_dir): |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 12 | with open(sensor_yaml, "r") as f: |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 13 | ifile = yaml.safe_load(f) |
| 14 | if not isinstance(ifile, dict): |
| 15 | ifile = {} |
| 16 | |
| 17 | # Render the mako template |
| 18 | |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 19 | t = Template( |
| 20 | filename=os.path.join(script_dir, "inventorysensor.mako.cpp") |
| 21 | ) |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 22 | |
| 23 | output_cpp = os.path.join(output_dir, "inventory-sensor-gen.cpp") |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 24 | with open(output_cpp, "w") as fd: |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 25 | fd.write(t.render(sensorDict=ifile)) |
| 26 | |
| 27 | |
| 28 | def main(): |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 29 | valid_commands = {"generate-cpp": generate_cpp} |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 30 | parser = argparse.ArgumentParser( |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 31 | description="Inventory Object to IPMI SensorID code generator" |
| 32 | ) |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 33 | |
| 34 | parser.add_argument( |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 35 | "-i", |
| 36 | "--sensor_yaml", |
| 37 | dest="sensor_yaml", |
| 38 | default="example.yaml", |
| 39 | help="input sensor yaml file to parse", |
| 40 | ) |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 41 | |
| 42 | parser.add_argument( |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 43 | "-o", |
| 44 | "--output-dir", |
| 45 | dest="outputdir", |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 46 | default=".", |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 47 | help="output directory", |
| 48 | ) |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 49 | |
| 50 | parser.add_argument( |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 51 | "command", |
| 52 | metavar="COMMAND", |
| 53 | type=str, |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 54 | choices=valid_commands.keys(), |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 55 | help="Command to run.", |
| 56 | ) |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 57 | |
| 58 | args = parser.parse_args() |
| 59 | |
Patrick Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 60 | if not (os.path.isfile(args.sensor_yaml)): |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 61 | 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 Williams | 9368225 | 2022-12-08 06:18:03 -0600 | [diff] [blame] | 67 | if __name__ == "__main__": |
Tom Joseph | e19540e | 2019-02-04 14:06:58 +0530 | [diff] [blame] | 68 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 69 | main() |