1 # Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
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:
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
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
23 from __future__
import print_function
28 from mako
.template
import Template
29 from mako
.exceptions
import RichTraceback
31 def write_template_to_string(template_filename
, **kwargs
):
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())
37 traceback
= RichTraceback()
38 for (filename
, lineno
, function
, line
) in traceback
.traceback
:
39 print("File %s, line %s, in %s" % (filename
, lineno
, function
))
41 print("%s: %s" % (str(traceback
.error
.__class
__.__name
__), traceback
.error
))
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
)
50 def main(args
=sys
.argv
[1:]):
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)
59 args
= parser
.parse_args()
63 write_template_to_file(args
.input,
66 knobs
=knob_defs
.KNOBS
,
67 includes
=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'],
71 write_template_to_file(args
.input,
74 knobs
=knob_defs
.KNOBS
,
75 includes
=['core/knobs_init.h', 'common/os.h', 'sstream', 'iomanip'],
80 if __name__
== '__main__':