Home | History | Annotate | Download | only in scripts
      1 # Copyright (C) 2014-2016 Intel Corporation.   All Rights Reserved.
      2 #
      3 # Permission is hereby granted, free of charge, to any person obtaining a
      4 # copy of this software and associated documentation files (the "Software"),
      5 # to deal in the Software without restriction, including without limitation
      6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
      7 # and/or sell copies of the Software, and to permit persons to whom the
      8 # Software is furnished to do so, subject to the following conditions:
      9 #
     10 # The above copyright notice and this permission notice (including the next
     11 # paragraph) shall be included in all copies or substantial portions of the
     12 # Software.
     13 #
     14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     20 # IN THE SOFTWARE.
     21 
     22 # Python source
     23 from __future__ import print_function
     24 import os
     25 import sys
     26 import argparse
     27 import knob_defs
     28 from mako.template import Template
     29 from mako.exceptions import RichTraceback
     30 
     31 def write_template_to_string(template_filename, **kwargs):
     32     try:
     33         template = Template(filename=os.path.abspath(template_filename))
     34         # Split + Join fixes line-endings for whatever platform you are using
     35         return '\n'.join(template.render(**kwargs).splitlines())
     36     except:
     37         traceback = RichTraceback()
     38         for (filename, lineno, function, line) in traceback.traceback:
     39             print("File %s, line %s, in %s" % (filename, lineno, function))
     40             print(line, "\n")
     41         print("%s: %s" % (str(traceback.error.__class__.__name__), traceback.error))
     42 
     43 def write_template_to_file(template_filename, output_filename, **kwargs):
     44     output_dirname = os.path.dirname(output_filename)
     45     if not os.path.exists(output_dirname):
     46         os.makedirs(output_dirname)
     47     with open(output_filename, "w") as outfile:
     48         print(write_template_to_string(template_filename, **kwargs), file=outfile)
     49 
     50 def main(args=sys.argv[1:]):
     51 
     52     # parse args
     53     parser = argparse.ArgumentParser()
     54     parser.add_argument("--input", "-i", help="Path to knobs.template", required=True)
     55     parser.add_argument("--output", "-o", help="Path to output file", required=True)
     56     parser.add_argument("--gen_h", "-gen_h", help="Generate gen_knobs.h", action="store_true", default=False)
     57     parser.add_argument("--gen_cpp", "-gen_cpp", help="Generate gen_knobs.cpp", action="store_true", required=False)
     58 
     59     args = parser.parse_args()
     60 
     61     if args.input:
     62         if args.gen_h:
     63             write_template_to_file(args.input,
     64                                    args.output,
     65                                    filename='gen_knobs',
     66                                    knobs=knob_defs.KNOBS,
     67                                    includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'],
     68                                    gen_header=True)
     69 
     70         if args.gen_cpp:
     71             write_template_to_file(args.input,
     72                                    args.output,
     73                                    filename='gen_knobs',
     74                                    knobs=knob_defs.KNOBS,
     75                                    includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'],
     76                                    gen_header=False)
     77 
     78     return 0
     79 
     80 if __name__ == '__main__':
     81     sys.exit(main())
     82 
     83