1 # The MIT License (MIT)
3 # Copyright (c) 2014-2020 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 import barectf
.config_parse_common
as barectf_config_parse_common
31 import barectf
.argpar
as barectf_argpar
36 # Colors and prints the error message `msg` and exits with status code
38 def _print_error(msg
):
39 termcolor
.cprint('Error: ', 'red', end
='', file=sys
.stderr
)
40 termcolor
.cprint(msg
, 'red', attrs
=['bold'], file=sys
.stderr
)
44 # Pretty-prints the barectf configuration error `exc` and exits with
46 def _print_config_error(exc
):
47 # reverse: most precise message comes last
48 for ctx
in reversed(exc
.context
):
51 if ctx
.message
is not None:
52 msg
= f
' {ctx.message}'
55 termcolor
.cprint(f
'{ctx.name}', color
, attrs
=['bold'], file=sys
.stderr
, end
='')
56 termcolor
.cprint(':', color
, file=sys
.stderr
, end
='')
57 termcolor
.cprint(msg
, color
, file=sys
.stderr
)
62 # Pretty-prints the unknown exception `exc`.
63 def _print_unknown_exc(exc
):
67 _print_error(f
'Unknown exception: {exc}')
70 # Finds and returns all the option items in `items` having the long name
72 def _find_opt_items(items
, long_name
):
76 if type(item
) is barectf_argpar
._OptItem
and item
.descr
.long_name
== long_name
:
77 ret_items
.append(item
)
84 # For an option item without an argument:
87 # For an option item with an argument:
90 # Uses the last option item having the long name `long_name` found in
93 # Returns `default` if there's no such option item.
94 def _opt_item_val(items
, long_name
, default
=None):
95 opt_items
= _find_opt_items(items
, long_name
)
97 if len(opt_items
) == 0:
100 opt_item
= opt_items
[-1]
102 if opt_item
.descr
.has_arg
:
103 return opt_item
.arg_text
108 class _CliError(Exception):
112 def _cfg_file_path_from_parse_res(parse_res
):
115 for item
in parse_res
.items
:
116 if type(item
) is barectf_argpar
._NonOptItem
:
117 if cfg_file_path
is not None:
118 raise _CliError('Multiple configuration file paths provided')
120 cfg_file_path
= item
.text
122 if cfg_file_path
is None:
123 raise _CliError('Missing configuration file path')
125 if not os
.path
.isfile(cfg_file_path
):
126 raise _CliError(f
'`{cfg_file_path}` is not an existing, regular file')
131 # Returns a `_CfgCmdCfg` object from the command-line parsing results
133 def _cfg_cmd_cfg_from_parse_res(parse_res
):
134 # check configuration file path
135 cfg_file_path
= _cfg_file_path_from_parse_res(parse_res
)
137 # inclusion directories
138 inclusion_dirs
= [item
.arg_text
for item
in _find_opt_items(parse_res
.items
, 'include-dir')]
140 for dir in inclusion_dirs
:
141 if not os
.path
.isdir(dir):
142 raise _CliError(f
'`{dir}` is not an existing directory')
144 inclusion_dirs
.append(os
.getcwd())
147 ignore_inclusion_file_not_found
= _opt_item_val(parse_res
.items
, 'ignore-include-not-found',
150 return _CfgCmdCfg(cfg_file_path
, inclusion_dirs
, ignore_inclusion_file_not_found
)
153 def _print_gen_cmd_usage():
154 print('''Usage: barectf generate [--code-dir=DIR] [--headers-dir=DIR]
155 [--metadata-dir=DIR] [--prefix=PREFIX]
156 [--include-dir=DIR]... [--ignore-include-not-found]
158 barectf generate --help
160 Generate the C source and CTF metadata stream files of a tracer from the
161 configuration file CONFIG-FILE-PATH.
164 -c DIR, --code-dir=DIR Write C source files to DIR instead of the CWD
165 -H DIR, --headers-dir=DIR Write C header files to DIR instead of the CWD
166 -h, --help Show this help and quit
167 --ignore-include-not-found Continue to process the configuration file when
168 included files are not found
169 -I DIR, --include-dir=DIR Add DIR to the list of directories to be
170 searched for inclusion files
171 -m DIR, --metadata-dir=DIR Write the metadata stream file to DIR instead of
173 -p PREFIX, --prefix=PREFIX Set the configuration prefix to PREFIX''')
176 # Returns a source and metadata stream file generating command object
177 # from the specific command-line arguments `orig_args`.
178 def _gen_cmd_cfg_from_args(orig_args
):
179 # parse original arguments
181 barectf_argpar
.OptDescr('h', 'help'),
182 barectf_argpar
.OptDescr('c', 'code-dir', True),
183 barectf_argpar
.OptDescr('H', 'headers-dir', True),
184 barectf_argpar
.OptDescr('I', 'include-dir', True),
185 barectf_argpar
.OptDescr('m', 'metadata-dir', True),
186 barectf_argpar
.OptDescr('p', 'prefix', True),
187 barectf_argpar
.OptDescr(long_name
='dump-config'),
188 barectf_argpar
.OptDescr(long_name
='ignore-include-not-found'),
190 res
= barectf_argpar
.parse(orig_args
, opt_descrs
)
191 assert len(res
.ingested_orig_args
) == len(orig_args
)
194 if len(_find_opt_items(res
.items
, 'help')) > 0:
195 _print_gen_cmd_usage()
198 # get common configuration file command CLI configuration
199 cfg_cmd_cfg
= _cfg_cmd_cfg_from_parse_res(res
)
202 c_source_dir
= _opt_item_val(res
.items
, 'code-dir', os
.getcwd())
203 c_header_dir
= _opt_item_val(res
.items
, 'headers-dir', os
.getcwd())
204 metadata_stream_dir
= _opt_item_val(res
.items
, 'metadata-dir', os
.getcwd())
206 for dir in [c_source_dir
, c_header_dir
, metadata_stream_dir
]:
207 if not os
.path
.isdir(dir):
208 raise _CliError(f
'`{dir}` is not an existing directory')
211 dump_config
= _opt_item_val(res
.items
, 'dump-config', False)
212 v2_prefix
= _opt_item_val(res
.items
, 'prefix')
214 return _GenCmd(_GenCmdCfg(cfg_cmd_cfg
.cfg_file_path
, c_source_dir
, c_header_dir
,
215 metadata_stream_dir
, cfg_cmd_cfg
.inclusion_dirs
,
216 cfg_cmd_cfg
.ignore_inclusion_file_not_found
, dump_config
, v2_prefix
))
219 def _show_effective_cfg_cmd_usage():
220 print('''Usage: barectf show-effective-configuration [--include-dir=DIR]...
221 [--ignore-include-not-found]
222 [--indent-spaces=COUNT] CONFIG-FILE-PATH
223 barectf show-effective-configuration --help
225 Print the effective configuration file for a the configuration file
229 -h, --help Show this help and quit
230 --ignore-include-not-found Continue to process the configuration file when
231 included files are not found
232 -I DIR, --include-dir=DIR Add DIR to the list of directories to be
233 searched for inclusion files
234 --indent-spaces=COUNT Use COUNT spaces at a time to indent YAML lines
238 # Returns an effective configuration showing command object from the
239 # specific command-line arguments `orig_args`.
240 def _show_effective_cfg_cfg_from_args(orig_args
):
241 # parse original arguments
243 barectf_argpar
.OptDescr('h', 'help'),
244 barectf_argpar
.OptDescr('I', 'include-dir', True),
245 barectf_argpar
.OptDescr(long_name
='indent-spaces', has_arg
=True),
246 barectf_argpar
.OptDescr(long_name
='ignore-include-not-found'),
248 res
= barectf_argpar
.parse(orig_args
, opt_descrs
)
249 assert len(res
.ingested_orig_args
) == len(orig_args
)
252 if len(_find_opt_items(res
.items
, 'help')) > 0:
253 _show_effective_cfg_cmd_usage()
256 # get common configuration command CLI configuration
257 cfg_cmd_cfg
= _cfg_cmd_cfg_from_parse_res(res
)
260 indent_space_count
= _opt_item_val(res
.items
, 'indent-spaces', 2)
263 indent_space_count
= int(indent_space_count
)
264 except (ValueError, TypeError):
265 raise _CliError(f
'Invalid `--indent-spaces` option argument: `{indent_space_count}`')
267 if indent_space_count
< 1 or indent_space_count
> 8:
268 raise _CliError(f
'Invalid `--indent-spaces` option argument (`{indent_space_count}`): expecting a value in [1, 8]')
270 return _ShowEffectiveCfgCmd(_ShowEffectiveCfgCmdCfg(cfg_cmd_cfg
.cfg_file_path
,
271 cfg_cmd_cfg
.inclusion_dirs
,
272 cfg_cmd_cfg
.ignore_inclusion_file_not_found
,
276 def _show_cfg_version_cmd_usage():
277 print('''Usage: barectf show-configuration-version CONFIG-FILE-PATH
278 barectf show-configuration-version --help
280 Print the major version (2 or 3) of the configuration file CONFIG-FILE-PATH.
283 -h, --help Show this help and quit''')
286 # Returns a configuration version showing command object from the
287 # specific command-line arguments `orig_args`.
288 def _show_cfg_version_cfg_from_args(orig_args
):
289 # parse original arguments
291 barectf_argpar
.OptDescr('h', 'help'),
293 res
= barectf_argpar
.parse(orig_args
, opt_descrs
)
294 assert len(res
.ingested_orig_args
) == len(orig_args
)
297 if len(_find_opt_items(res
.items
, 'help')) > 0:
298 _show_cfg_version_cmd_usage()
301 # check configuration file path
302 cfg_file_path
= _cfg_file_path_from_parse_res(res
)
304 return _ShowCfgVersionCmd(_ShowCfgVersionCmdCfg(cfg_file_path
))
307 def _print_general_usage():
308 print('''Usage: barectf COMMAND COMMAND-ARGS
313 -h, --help Show this help and quit
314 -V, --version Show version and quit
319 Generate the C source and CTF metadata stream files of a tracer from a
322 show-effective-configuration:
323 show-effective-config:
325 Print the effective configuration file for a given configuration file and
326 inclusion directories.
328 show-configuration-version:
331 Print the major version of a given configuration file.
333 Run `barectf COMMAND --help` to show the help of COMMAND.''')
336 # Returns a command object from the command-line arguments `orig_args`.
338 # All the `orig_args` elements are considered.
339 def _cmd_from_args(orig_args
):
340 # We use our `argpar` module here instead of Python's `argparse`
341 # because we need to support the two following use cases:
343 # $ barectf config.yaml
344 # $ barectf generate config.yaml
346 # In other words, the default command is `generate` (for backward
347 # compatibility reasons). The argument parser must not consider
348 # `config.yaml` as being a command name.
349 general_opt_descrs
= [
350 barectf_argpar
.OptDescr('V', 'version'),
351 barectf_argpar
.OptDescr('h', 'help'),
353 res
= barectf_argpar
.parse(orig_args
, general_opt_descrs
, False)
355 # find command name, collecting preceding (common) option items
356 cmd_from_args_funcs
= {
357 'generate': _gen_cmd_cfg_from_args
,
358 'gen': _gen_cmd_cfg_from_args
,
359 'show-effective-configuration': _show_effective_cfg_cfg_from_args
,
360 'show-effective-config': _show_effective_cfg_cfg_from_args
,
361 'show-effective-cfg': _show_effective_cfg_cfg_from_args
,
362 'show-configuration-version': _show_cfg_version_cfg_from_args
,
363 'show-config-version': _show_cfg_version_cfg_from_args
,
364 'show-cfg-version': _show_cfg_version_cfg_from_args
,
366 general_opt_items
= []
367 cmd_first_orig_arg_index
= None
368 cmd_from_args_func
= None
370 for item
in res
.items
:
371 if type(item
) is barectf_argpar
._NonOptItem
:
372 cmd_from_args_func
= cmd_from_args_funcs
.get(item
.text
)
374 if cmd_from_args_func
is None:
375 cmd_first_orig_arg_index
= item
.orig_arg_index
377 cmd_first_orig_arg_index
= item
.orig_arg_index
+ 1
381 assert type(item
) is barectf_argpar
._OptItem
382 general_opt_items
.append(item
)
385 if len(_find_opt_items(general_opt_items
, 'help')) > 0:
386 _print_general_usage()
390 if len(_find_opt_items(general_opt_items
, 'version')) > 0:
391 print(f
'barectf {barectf.__version__}')
394 # create command object
395 cmd_orig_args
= orig_args
[cmd_first_orig_arg_index
:]
397 if cmd_from_args_func
is None:
398 # default `generate` command
399 return _gen_cmd_cfg_from_args(cmd_orig_args
)
401 return cmd_from_args_func(cmd_orig_args
)
408 class _CfgCmdCfg(_CmdCfg
):
409 def __init__(self
, cfg_file_path
, inclusion_dirs
, ignore_inclusion_file_not_found
):
410 self
._cfg
_file
_path
= cfg_file_path
411 self
._inclusion
_dirs
= inclusion_dirs
412 self
._ignore
_inclusion
_file
_not
_found
= ignore_inclusion_file_not_found
415 def cfg_file_path(self
):
416 return self
._cfg
_file
_path
419 def inclusion_dirs(self
):
420 return self
._inclusion
_dirs
423 def ignore_inclusion_file_not_found(self
):
424 return self
._ignore
_inclusion
_file
_not
_found
428 def __init__(self
, cfg
):
436 raise NotImplementedError
439 class _GenCmdCfg(_CfgCmdCfg
):
440 def __init__(self
, cfg_file_path
, c_source_dir
, c_header_dir
, metadata_stream_dir
,
441 inclusion_dirs
, ignore_inclusion_file_not_found
, dump_config
, v2_prefix
):
442 super().__init
__(cfg_file_path
, inclusion_dirs
, ignore_inclusion_file_not_found
)
443 self
._c
_source
_dir
= c_source_dir
444 self
._c
_header
_dir
= c_header_dir
445 self
._metadata
_stream
_dir
= metadata_stream_dir
446 self
._dump
_config
= dump_config
447 self
._v
2_prefix
= v2_prefix
450 def c_source_dir(self
):
451 return self
._c
_source
_dir
454 def c_header_dir(self
):
455 return self
._c
_header
_dir
458 def metadata_stream_dir(self
):
459 return self
._metadata
_stream
_dir
462 def dump_config(self
):
463 return self
._dump
_config
467 return self
._v
2_prefix
470 # Source and metadata stream file generating command.
473 # create configuration
475 with
open(self
.cfg
.cfg_file_path
) as f
:
476 if self
.cfg
.dump_config
:
477 # print effective configuration file
478 print(barectf
.effective_configuration_file(f
, True, self
.cfg
.inclusion_dirs
,
479 self
.cfg
.ignore_inclusion_file_not_found
))
481 # barectf.configuration_from_file() reads the file again
485 config
= barectf
.configuration_from_file(f
, True, self
.cfg
.inclusion_dirs
,
486 self
.cfg
.ignore_inclusion_file_not_found
)
487 except barectf
._ConfigurationParseError
as exc
:
488 _print_config_error(exc
)
489 except Exception as exc
:
490 _print_unknown_exc(exc
)
492 if self
.cfg
.v2_prefix
is not None:
495 # For historical reasons, the `--prefix` option applies the
496 # barectf 2 configuration prefix rules. Therefore, get the
497 # equivalent barectf 3 prefixes first.
498 v3_prefixes
= barectf_config_parse_common
._v
3_prefixes
_from
_v
2_prefix
(self
.cfg
.v2_prefix
)
499 cg_opts
= config
.options
.code_generation_options
500 cg_opts
= barectf
.ConfigurationCodeGenerationOptions(v3_prefixes
.identifier
,
501 v3_prefixes
.file_name
,
502 cg_opts
.default_stream_type
,
503 cg_opts
.header_options
,
504 cg_opts
.clock_type_c_types
)
505 config
= barectf
.Configuration(config
.trace
, barectf
.ConfigurationOptions(cg_opts
))
507 # create a barectf code generator
508 code_gen
= barectf
.CodeGenerator(config
)
510 def write_file(dir, file):
511 with
open(os
.path
.join(dir, file.name
), 'w') as f
:
512 f
.write(file.contents
)
514 def write_files(dir, files
):
516 write_file(dir, file)
519 # generate and write metadata stream file
520 write_file(self
.cfg
.metadata_stream_dir
, code_gen
.generate_metadata_stream())
522 # generate and write C header files
523 write_files(self
.cfg
.c_header_dir
, code_gen
.generate_c_headers())
525 # generate and write C source files
526 write_files(self
.cfg
.c_source_dir
, code_gen
.generate_c_sources())
527 except Exception as exc
:
528 # We know `config` is valid, therefore the code generator cannot
529 # fail for a reason known to barectf.
530 _print_unknown_exc(exc
)
533 class _ShowEffectiveCfgCmdCfg(_CfgCmdCfg
):
534 def __init__(self
, cfg_file_path
, inclusion_dirs
, ignore_inclusion_file_not_found
,
536 super().__init
__(cfg_file_path
, inclusion_dirs
, ignore_inclusion_file_not_found
)
537 self
._indent
_space
_count
= indent_space_count
540 def indent_space_count(self
):
541 return self
._indent
_space
_count
544 # Effective configuration showing command.
545 class _ShowEffectiveCfgCmd(_Cmd
):
548 with
open(self
.cfg
.cfg_file_path
) as f
:
549 print(barectf
.effective_configuration_file(f
, True, self
.cfg
.inclusion_dirs
,
550 self
.cfg
.ignore_inclusion_file_not_found
,
551 self
.cfg
.indent_space_count
))
552 except barectf
._ConfigurationParseError
as exc
:
553 _print_config_error(exc
)
554 except Exception as exc
:
555 _print_unknown_exc(exc
)
558 class _ShowCfgVersionCmdCfg(_CmdCfg
):
559 def __init__(self
, cfg_file_path
):
560 self
._cfg
_file
_path
= cfg_file_path
563 def cfg_file_path(self
):
564 return self
._cfg
_file
_path
567 class _ShowCfgVersionCmd(_Cmd
):
570 with
open(self
.cfg
.cfg_file_path
) as f
:
571 print(barectf
.configuration_file_major_version(f
))
572 except barectf
._ConfigurationParseError
as exc
:
573 _print_config_error(exc
)
574 except Exception as exc
:
575 _print_unknown_exc(exc
)
579 # create command from arguments
581 cmd
= _cmd_from_args(sys
.argv
[1:])
582 except barectf_argpar
._Error
as exc
:
583 _print_error(f
'Command-line: For argument `{exc.orig_arg}`: {exc.msg}')
584 except _CliError
as exc
:
585 _print_error(f
'Command-line: {exc}')