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