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 import _init_logging, LOG
23 from yardstick.cmd.commands import task
24 from yardstick.cmd.commands import runner
25 from yardstick.cmd.commands import scenario
26 from yardstick.cmd.commands import testcase
27 from yardstick.cmd.commands import plugin
28 from yardstick.cmd.commands import env
35 help='increase output verbosity to debug'),
36 cfg.BoolOpt('verbose',
39 help='increase output verbosity to info')
41 CONF.register_cli_opts(cli_opts)
43 CONFIG_SEARCH_PATHS = [sys.prefix + "/etc/yardstick",
48 def find_config_files(path_list):
49 for path in path_list:
50 abspath = os.path.abspath(os.path.expanduser(path))
51 confname = abspath + "/yardstick.conf"
52 if os.path.isfile(confname):
59 '''Command-line interface to yardstick'''
63 'task': task.TaskCommands,
64 'runner': runner.RunnerCommands,
65 'scenario': scenario.ScenarioCommands,
66 'testcase': testcase.TestcaseCommands,
67 'plugin': plugin.PluginCommands,
73 self._version = 'yardstick version %s ' % \
74 get_distribution('yardstick').version
76 def _find_actions(self, subparsers, actions_module):
77 '''find action methods'''
78 # Find action methods inside actions_module and
79 # add them to the command parser.
80 # The 'actions_module' argument may be a class
81 # or module. Action methods start with 'do_'
82 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
83 command = attr[3:].replace('_', '-')
84 callback = getattr(actions_module, attr)
85 desc = callback.__doc__ or ''
86 arguments = getattr(callback, 'arguments', [])
87 subparser = subparsers.add_parser(
91 for (args, kwargs) in arguments:
92 subparser.add_argument(*args, **kwargs)
93 subparser.set_defaults(func=callback)
95 def _add_command_parsers(self, categories, subparsers):
96 '''add commands to command-line parser'''
97 for category in categories:
98 command_object = categories[category]()
99 desc = command_object.__doc__ or ''
100 subparser = subparsers.add_parser(
101 category, description=desc,
102 formatter_class=RawDescriptionHelpFormatter
104 subparser.set_defaults(command_object=command_object)
105 cmd_subparsers = subparser.add_subparsers(title='subcommands')
106 self._find_actions(cmd_subparsers, command_object)
108 def _register_cli_opt(self):
110 # register subcommands to parse additional command line arguments
111 def parser(subparsers):
112 self._add_command_parsers(YardstickCLI.categories, subparsers)
114 category_opt = cfg.SubCommandOpt("category",
115 title="Command categories",
116 help="Available categories",
118 self._register_opt(category_opt)
120 def _register_opt(self, opt):
122 CONF.register_cli_opt(opt)
123 self.opts.append(opt)
125 def _load_cli_config(self, argv):
127 # load CLI args and config files
128 CONF(argv, project="yardstick", version=self._version,
129 default_config_files=find_config_files(CONFIG_SEARCH_PATHS))
131 def _handle_global_opts(self):
135 LOG.setLevel(logging.INFO)
138 LOG.setLevel(logging.DEBUG)
140 def _dispath_func_notask(self):
142 # dispatch to category parser
143 func = CONF.category.func
146 def _dispath_func_task(self, task_id):
148 # dispatch to category parser
149 func = CONF.category.func
150 func(CONF.category, task_id=task_id)
152 def _clear_config_opts(self):
155 CONF.unregister_opts(self.opts)
157 def main(self, argv): # pragma: no cover
158 '''run the command line interface'''
159 self._register_cli_opt()
161 self._load_cli_config(argv)
163 self._handle_global_opts()
165 self._dispath_func_notask()
167 self._clear_config_opts()
169 def api(self, argv, task_id): # pragma: no cover
170 '''run the api interface'''
171 self._register_cli_opt()
173 self._load_cli_config(argv)
175 self._handle_global_opts()
177 self._dispath_func_task(task_id)
179 self._clear_config_opts()