Patrick Williams | 0968237 | 2020-04-03 15:10:31 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 2 | |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 3 | import argparse |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 4 | import os |
| 5 | import sys |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 6 | |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 7 | import yaml |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 8 | from mako.template import Template |
| 9 | |
| 10 | |
Ratan Gupta | 6eed403 | 2017-02-10 15:59:31 +0530 | [diff] [blame] | 11 | def generate_cpp(inventory_yaml, output_dir): |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 12 | with open(os.path.join(script_dir, inventory_yaml), "r") as f: |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 13 | ifile = yaml.safe_load(f) |
Brad Bishop | 3d0ce84 | 2017-02-07 08:33:56 -0500 | [diff] [blame] | 14 | if not isinstance(ifile, dict): |
| 15 | ifile = {} |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 16 | |
| 17 | # Render the mako template |
| 18 | |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 19 | t = Template(filename=os.path.join(script_dir, "writefru.mako.cpp")) |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 20 | |
Ratan Gupta | 6eed403 | 2017-02-10 15:59:31 +0530 | [diff] [blame] | 21 | output_hpp = os.path.join(output_dir, "fru-gen.cpp") |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 22 | with open(output_hpp, "w") as fd: |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 23 | fd.write(t.render(fruDict=ifile)) |
| 24 | |
| 25 | |
| 26 | def main(): |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 27 | valid_commands = {"generate-cpp": generate_cpp} |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 28 | parser = argparse.ArgumentParser( |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 29 | description="IPMI FRU parser and code generator" |
| 30 | ) |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 31 | |
| 32 | parser.add_argument( |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 33 | "-i", |
| 34 | "--inventory_yaml", |
| 35 | dest="inventory_yaml", |
| 36 | default="example.yaml", |
| 37 | help="input inventory yaml file to parse", |
| 38 | ) |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 39 | |
| 40 | parser.add_argument( |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 41 | "-o", |
| 42 | "--output-dir", |
| 43 | dest="outputdir", |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 44 | default=".", |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 45 | help="output directory", |
| 46 | ) |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 47 | |
| 48 | parser.add_argument( |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 49 | "command", |
| 50 | metavar="COMMAND", |
| 51 | type=str, |
Patrick Williams | 0968237 | 2020-04-03 15:10:31 -0500 | [diff] [blame] | 52 | choices=list(valid_commands.keys()), |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 53 | help="Command to run.", |
| 54 | ) |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 55 | |
| 56 | args = parser.parse_args() |
| 57 | |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 58 | if not (os.path.isfile(os.path.join(script_dir, args.inventory_yaml))): |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 59 | sys.exit("Can not find input yaml file " + args.inventory_yaml) |
| 60 | |
| 61 | function = valid_commands[args.command] |
| 62 | function(args.inventory_yaml, args.outputdir) |
| 63 | |
Patrick Williams | c5c1737 | 2022-12-08 06:18:06 -0600 | [diff] [blame^] | 64 | |
| 65 | if __name__ == "__main__": |
Ratan Gupta | 0f2e7fe | 2016-12-22 19:00:41 +0530 | [diff] [blame] | 66 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 67 | main() |