1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 Command-line interface to yardstick
18 from pkg_resources import get_distribution
19 from argparse import RawDescriptionHelpFormatter
20 from oslo_config import cfg
22 from yardstick.cmd.commands import task
23 from yardstick.cmd.commands import runner
24 from yardstick.cmd.commands import scenario
25 from yardstick.cmd.commands import testcase
32 help='increase output verbosity to debug'),
33 cfg.BoolOpt('verbose',
36 help='increase output verbosity to info')
38 CONF.register_cli_opts(cli_opts)
40 CONFIG_SEARCH_PATHS = [sys.prefix + "/etc/yardstick",
45 def find_config_files(path_list):
46 for path in path_list:
47 abspath = os.path.abspath(os.path.expanduser(path))
48 confname = abspath + "/yardstick.conf"
49 if os.path.isfile(confname):
56 '''Command-line interface to yardstick'''
60 'task': task.TaskCommands,
61 'runner': runner.RunnerCommands,
62 'scenario': scenario.ScenarioCommands,
63 'testcase': testcase.TestcaseCommands
67 self._version = 'yardstick version %s ' % \
68 get_distribution('yardstick').version
70 def _find_actions(self, subparsers, actions_module):
71 '''find action methods'''
72 # Find action methods inside actions_module and
73 # add them to the command parser.
74 # The 'actions_module' argument may be a class
75 # or module. Action methods start with 'do_'
76 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
77 command = attr[3:].replace('_', '-')
78 callback = getattr(actions_module, attr)
79 desc = callback.__doc__ or ''
80 arguments = getattr(callback, 'arguments', [])
81 subparser = subparsers.add_parser(
85 for (args, kwargs) in arguments:
86 subparser.add_argument(*args, **kwargs)
87 subparser.set_defaults(func=callback)
89 def _add_command_parsers(self, categories, subparsers):
90 '''add commands to command-line parser'''
91 for category in categories:
92 command_object = categories[category]()
93 desc = command_object.__doc__ or ''
94 subparser = subparsers.add_parser(
95 category, description=desc,
96 formatter_class=RawDescriptionHelpFormatter
98 subparser.set_defaults(command_object=command_object)
99 cmd_subparsers = subparser.add_subparsers(title='subcommands')
100 self._find_actions(cmd_subparsers, command_object)
102 def main(self, argv):
103 '''run the command line interface'''
105 # register subcommands to parse additional command line arguments
106 def parser(subparsers):
107 self._add_command_parsers(YardstickCLI.categories, subparsers)
109 category_opt = cfg.SubCommandOpt("category",
110 title="Command categories",
111 help="Available categories",
113 CONF.register_cli_opt(category_opt)
115 # load CLI args and config files
116 CONF(argv, project="yardstick", version=self._version,
117 default_config_files=find_config_files(CONFIG_SEARCH_PATHS))
120 logger = logging.getLogger('yardstick')
121 logger.setLevel(logging.WARNING)
124 logger.setLevel(logging.INFO)
127 logger.setLevel(logging.DEBUG)
129 # dispatch to category parser
130 func = CONF.category.func