Tom Joseph | 31eed5c | 2020-03-23 20:56:47 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 2 | |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 3 | import argparse |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 4 | import os |
| 5 | import sys |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 6 | |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 7 | import yaml |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 8 | from mako.template import Template |
| 9 | |
| 10 | |
| 11 | def generate_cpp(inventory_yaml, output_dir): |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 12 | with open(inventory_yaml, "r") as f: |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [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 | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 19 | t = Template(filename=os.path.join(script_dir, "readfru.mako.cpp")) |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 20 | |
| 21 | output_hpp = os.path.join(output_dir, "fru-read-gen.cpp") |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 22 | with open(output_hpp, "w") as fd: |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 23 | fd.write(t.render(fruDict=ifile)) |
| 24 | |
| 25 | |
| 26 | def main(): |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 27 | valid_commands = {"generate-cpp": generate_cpp} |
| 28 | parser = argparse.ArgumentParser(description="IPMI FRU map code generator") |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 29 | |
| 30 | parser.add_argument( |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 31 | "-i", |
| 32 | "--inventory_yaml", |
| 33 | dest="inventory_yaml", |
| 34 | default="example.yaml", |
| 35 | help="input inventory yaml file to parse", |
| 36 | ) |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 37 | |
| 38 | parser.add_argument( |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 39 | "-o", |
| 40 | "--output-dir", |
| 41 | dest="outputdir", |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 42 | default=".", |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 43 | help="output directory", |
| 44 | ) |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 45 | |
| 46 | parser.add_argument( |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 47 | "command", |
| 48 | metavar="COMMAND", |
| 49 | type=str, |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 50 | choices=valid_commands.keys(), |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 51 | help="Command to run.", |
| 52 | ) |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 53 | |
| 54 | args = parser.parse_args() |
| 55 | |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 56 | if not (os.path.isfile(args.inventory_yaml)): |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 57 | sys.exit("Can not find input yaml file " + args.inventory_yaml) |
| 58 | |
| 59 | function = valid_commands[args.command] |
| 60 | function(args.inventory_yaml, args.outputdir) |
| 61 | |
Patrick Williams | be28ff5 | 2022-12-05 10:14:46 -0600 | [diff] [blame] | 62 | |
| 63 | if __name__ == "__main__": |
Marri Devender Rao | a62bacc | 2017-06-04 23:40:16 -0500 | [diff] [blame] | 64 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 65 | main() |