gen_knobs.py revision e1222ade0039289993fbec261408eea5e0d7d9ae
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 23from __future__ import print_function 24import os 25import sys 26import knob_defs 27from mako.template import Template 28from mako.exceptions import RichTraceback 29 30def write_template_to_string(template_filename, **kwargs): 31 try: 32 template = Template(filename=template_filename) 33 # Split + Join fixes line-endings for whatever platform you are using 34 return '\n'.join(template.render(**kwargs).splitlines()) 35 except: 36 traceback = RichTraceback() 37 for (filename, lineno, function, line) in traceback.traceback: 38 print("File %s, line %s, in %s" % (filename, lineno, function)) 39 print(line, "\n") 40 print("%s: %s" % (str(traceback.error.__class__.__name__), traceback.error)) 41 42def write_template_to_file(template_filename, output_filename, **kwargs): 43 with open(output_filename, "w") as outfile: 44 print(write_template_to_string(template_filename, **kwargs), file=outfile) 45 46def main(args=sys.argv[1:]): 47 if len(args) != 1: 48 print('Usage:', sys.argv[0], '<output_directory>', file=sys.stderr) 49 return 1 50 51 output_dir = args[0] 52 if not os.path.isdir(output_dir): 53 if os.path.exists(output_dir): 54 print('ERROR: Invalid output directory:', output_dir, file=sys.stderr) 55 return 1 56 57 try: 58 os.makedirs(output_dir) 59 except: 60 print('ERROR: Could not create output directory:', output_dir, file=sys.stderr) 61 return 1 62 63 # Output path exists, now just run the template 64 template_file = os.sep.join([sys.path[0], 'templates', 'knobs.template']) 65 output_file = os.sep.join([output_dir, 'gen_knobs.cpp']) 66 output_header = os.sep.join([output_dir, 'gen_knobs.h']) 67 68 for f in [output_header, output_file]: 69 write_template_to_file(template_file, f, 70 filename='gen_knobs', 71 knobs=knob_defs.KNOBS, 72 includes=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'], 73 gen_header=True if f == output_header else False) 74 75 return 0 76 77if __name__ == '__main__': 78 sys.exit(main()) 79 80