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