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