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_STREAM_HDLR
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
30 from yardstick.cmd.commands import report
37 help='increase output verbosity to debug')
39 CONF.register_cli_opts(cli_opts)
41 CONFIG_SEARCH_PATHS = [sys.prefix + "/etc/yardstick",
46 def find_config_files(path_list):
47 for path in path_list:
48 abspath = os.path.abspath(os.path.expanduser(path))
49 confname = abspath + "/yardstick.conf"
50 if os.path.isfile(confname):
56 class YardstickCLI(): # pragma: no cover
57 """Command-line interface to yardstick"""
61 'task': task.TaskCommands,
62 'runner': runner.RunnerCommands,
63 'scenario': scenario.ScenarioCommands,
64 'testcase': testcase.TestcaseCommands,
65 'plugin': plugin.PluginCommands,
66 'env': env.EnvCommand,
67 'report': report.ReportCommands
72 self._version = 'yardstick version %s ' % \
73 get_distribution('yardstick').version
75 def _find_actions(self, subparsers, actions_module):
76 """find action methods"""
77 # Find action methods inside actions_module and
78 # add them to the command parser.
79 # The 'actions_module' argument may be a class
80 # or module. Action methods start with 'do_'
81 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
82 command = attr[3:].replace('_', '-')
83 callback = getattr(actions_module, attr)
84 desc = callback.__doc__ or ''
85 arguments = getattr(callback, 'arguments', [])
86 subparser = subparsers.add_parser(
90 for (args, kwargs) in arguments:
91 subparser.add_argument(*args, **kwargs)
92 subparser.set_defaults(func=callback)
94 def _add_command_parsers(self, categories, subparsers):
95 """add commands to command-line parser"""
96 for category in categories:
97 command_object = categories[category]()
98 desc = command_object.__doc__ or ''
99 subparser = subparsers.add_parser(
100 category, description=desc,
101 formatter_class=RawDescriptionHelpFormatter
103 subparser.set_defaults(command_object=command_object)
104 cmd_subparsers = subparser.add_subparsers(title='subcommands')
105 self._find_actions(cmd_subparsers, command_object)
107 def _register_cli_opt(self):
109 # register subcommands to parse additional command line arguments
110 def parser(subparsers):
111 self._add_command_parsers(YardstickCLI.categories, subparsers)
113 category_opt = cfg.SubCommandOpt("category",
114 title="Command categories",
115 help="Available categories",
117 self._register_opt(category_opt)
119 def _register_opt(self, opt):
121 CONF.register_cli_opt(opt)
122 self.opts.append(opt)
124 def _load_cli_config(self, argv):
126 # load CLI args and config files
127 CONF(argv, project="yardstick", version=self._version,
128 default_config_files=find_config_files(CONFIG_SEARCH_PATHS))
130 def _handle_global_opts(self):
134 _LOG_STREAM_HDLR.setLevel(logging.DEBUG)
136 def _dispatch_func_notask(self):
138 # dispatch to category parser
139 func = CONF.category.func
142 def _dispatch_func_task(self, task_id):
144 # dispatch to category parser
145 func = CONF.category.func
146 func(CONF.category, task_id=task_id)
148 def _clear_config_opts(self):
151 CONF.unregister_opts(self.opts)
153 def main(self, argv): # pragma: no cover
154 """run the command line interface"""
156 self._register_cli_opt()
158 self._load_cli_config(argv)
160 self._handle_global_opts()
162 self._dispatch_func_notask()
164 self._clear_config_opts()
166 def api(self, argv, task_id): # pragma: no cover
167 """run the api interface"""
169 self._register_cli_opt()
171 self._load_cli_config(argv)
173 self._handle_global_opts()
175 self._dispatch_func_task(task_id)
177 self._clear_config_opts()