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
14 from __future__ import absolute_import
19 from pkg_resources import get_distribution
20 from argparse import RawDescriptionHelpFormatter
21 from oslo_config import cfg
23 from yardstick import _init_logging, LOG
24 from yardstick.cmd.commands import task
25 from yardstick.cmd.commands import runner
26 from yardstick.cmd.commands import scenario
27 from yardstick.cmd.commands import testcase
28 from yardstick.cmd.commands import plugin
29 from yardstick.cmd.commands import env
36 help='increase output verbosity to debug'),
37 cfg.BoolOpt('verbose',
40 help='increase output verbosity to info')
42 CONF.register_cli_opts(cli_opts)
44 CONFIG_SEARCH_PATHS = [sys.prefix + "/etc/yardstick",
49 def find_config_files(path_list):
50 for path in path_list:
51 abspath = os.path.abspath(os.path.expanduser(path))
52 confname = abspath + "/yardstick.conf"
53 if os.path.isfile(confname):
60 '''Command-line interface to yardstick'''
64 'task': task.TaskCommands,
65 'runner': runner.RunnerCommands,
66 'scenario': scenario.ScenarioCommands,
67 'testcase': testcase.TestcaseCommands,
68 'plugin': plugin.PluginCommands,
74 self._version = 'yardstick version %s ' % \
75 get_distribution('yardstick').version
77 def _find_actions(self, subparsers, actions_module):
78 '''find action methods'''
79 # Find action methods inside actions_module and
80 # add them to the command parser.
81 # The 'actions_module' argument may be a class
82 # or module. Action methods start with 'do_'
83 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
84 command = attr[3:].replace('_', '-')
85 callback = getattr(actions_module, attr)
86 desc = callback.__doc__ or ''
87 arguments = getattr(callback, 'arguments', [])
88 subparser = subparsers.add_parser(
92 for (args, kwargs) in arguments:
93 subparser.add_argument(*args, **kwargs)
94 subparser.set_defaults(func=callback)
96 def _add_command_parsers(self, categories, subparsers):
97 '''add commands to command-line parser'''
98 for category in categories:
99 command_object = categories[category]()
100 desc = command_object.__doc__ or ''
101 subparser = subparsers.add_parser(
102 category, description=desc,
103 formatter_class=RawDescriptionHelpFormatter
105 subparser.set_defaults(command_object=command_object)
106 cmd_subparsers = subparser.add_subparsers(title='subcommands')
107 self._find_actions(cmd_subparsers, command_object)
109 def _register_cli_opt(self):
111 # register subcommands to parse additional command line arguments
112 def parser(subparsers):
113 self._add_command_parsers(YardstickCLI.categories, subparsers)
115 category_opt = cfg.SubCommandOpt("category",
116 title="Command categories",
117 help="Available categories",
119 self._register_opt(category_opt)
121 def _register_opt(self, opt):
123 CONF.register_cli_opt(opt)
124 self.opts.append(opt)
126 def _load_cli_config(self, argv):
128 # load CLI args and config files
129 CONF(argv, project="yardstick", version=self._version,
130 default_config_files=find_config_files(CONFIG_SEARCH_PATHS))
132 def _handle_global_opts(self):
136 LOG.setLevel(logging.INFO)
139 LOG.setLevel(logging.DEBUG)
141 def _dispath_func_notask(self):
143 # dispatch to category parser
144 func = CONF.category.func
147 def _dispath_func_task(self, task_id):
149 # dispatch to category parser
150 func = CONF.category.func
151 func(CONF.category, task_id=task_id)
153 def _clear_config_opts(self):
156 CONF.unregister_opts(self.opts)
158 def main(self, argv): # pragma: no cover
159 '''run the command line interface'''
161 self._register_cli_opt()
163 self._load_cli_config(argv)
165 self._handle_global_opts()
167 self._dispath_func_notask()
169 self._clear_config_opts()
171 def api(self, argv, task_id): # pragma: no cover
172 '''run the api interface'''
174 self._register_cli_opt()
176 self._load_cli_config(argv)
178 self._handle_global_opts()
180 self._dispath_func_task(task_id)
182 self._clear_config_opts()