1 # The MIT License (MIT)
3 # Copyright (c) 2014-2015 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from termcolor
import cprint
, colored
24 import barectf
.tsdl182gen
36 cprint('Error: ', 'red', end
='', file=sys
.stderr
)
37 cprint(msg
, 'red', attrs
=['bold'], file=sys
.stderr
)
41 def _pconfig_error(e
):
50 if not hasattr(e
, 'prev'):
58 cprint('Error:', 'red', file=sys
.stderr
)
60 for i
, line
in enumerate(lines
):
61 suf
= ':' if i
< len(lines
) - 1 else ''
62 cprint(' ' + line
+ suf
, 'red', attrs
=['bold'], file=sys
.stderr
)
68 cprint('{}'.format(msg
), 'green', attrs
=['bold'])
72 ap
= argparse
.ArgumentParser()
74 ap
.add_argument('-c', '--code-dir', metavar
='DIR', action
='store',
76 help='output directory of C source file')
77 ap
.add_argument('-H', '--headers-dir', metavar
='DIR', action
='store',
79 help='output directory of C header files')
80 ap
.add_argument('-m', '--metadata-dir', metavar
='DIR', action
='store',
82 help='output directory of CTF metadata')
83 ap
.add_argument('-p', '--prefix', metavar
='PREFIX', action
='store',
84 help='override configuration\'s prefix')
85 ap
.add_argument('-V', '--version', action
='version',
86 version
='%(prog)s {}'.format(barectf
.__version
__))
87 ap
.add_argument('config', metavar
='CONFIG', action
='store',
88 help='barectf YAML configuration file')
91 args
= ap
.parse_args()
93 # validate output directories
94 for d
in [args
.code_dir
, args
.headers_dir
, args
.metadata_dir
]:
95 if not os
.path
.isdir(d
):
96 _perror('"{}" is not an existing directory'.format(d
))
98 # validate that configuration file exists
99 if not os
.path
.isfile(args
.config
):
100 _perror('"{}" is not an existing file'.format(args
.config
))
105 def _write_file(d
, name
, content
):
106 with
open(os
.path
.join(d
, name
), 'w') as f
:
114 # create configuration
116 config
= barectf
.config
.from_yaml_file(args
.config
)
117 except barectf
.config
.ConfigError
as e
:
119 except Exception as e
:
120 _perror('unknown exception: {}'.format(e
))
122 # replace prefix if needed
125 config
.prefix
= args
.prefix
126 except barectf
.config
.ConfigError
as e
:
130 metadata
= barectf
.tsdl182gen
.from_metadata(config
.metadata
)
133 _write_file(args
.metadata_dir
, 'metadata', metadata
)
134 except Exception as e
:
135 _perror('cannot write metadata file: {}'.format(e
))
138 generator
= barectf
.gen
.CCodeGenerator(config
)
141 header
= generator
.generate_header()
142 bitfield_header
= generator
.generate_bitfield_header()
145 _write_file(args
.headers_dir
, generator
.get_header_filename(), header
)
146 _write_file(args
.headers_dir
, generator
.get_bitfield_header_filename(),
148 except Exception as e
:
149 _perror('cannot write header files: {}'.format(e
))
152 c_src
= generator
.generate_c_src()
155 _write_file(args
.code_dir
, '{}.c'.format(config
.prefix
.rstrip('_')),
157 except Exception as e
:
158 _perror('cannot write C source file: {}'.format(e
))
This page took 0.045479 seconds and 4 git commands to generate.