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
17 from pkg_resources import get_distribution
18 from argparse import RawDescriptionHelpFormatter
20 from yardstick.cmd.commands import task
21 from yardstick.cmd.commands import runner
22 from yardstick.cmd.commands import scenario
26 '''Command-line interface to yardstick'''
30 'task': task.TaskCommands,
31 'runner': runner.RunnerCommands,
32 'scenario': scenario.ScenarioCommands
36 self._version = 'yardstick version %s ' % \
37 get_distribution('yardstick').version
39 def _get_base_parser(self):
40 '''get base (top level) parser'''
42 parser = argparse.ArgumentParser(
44 formatter_class=RawDescriptionHelpFormatter,
45 description=YardstickCLI.__doc__ or ''
52 help="display version",
53 version=self._version,
59 help="increase output verbosity to debug",
65 help="increase output verbosity to info",
71 def _find_actions(self, subparsers, actions_module):
72 '''find action methods'''
73 # Find action methods inside actions_module and
74 # add them to the command parser.
75 # The 'actions_module' argument may be a class
76 # or module. Action methods start with 'do_'
77 for attr in (a for a in dir(actions_module) if a.startswith('do_')):
78 command = attr[3:].replace('_', '-')
79 callback = getattr(actions_module, attr)
80 desc = callback.__doc__ or ''
81 arguments = getattr(callback, 'arguments', [])
82 subparser = subparsers.add_parser(
86 for (args, kwargs) in arguments:
87 subparser.add_argument(*args, **kwargs)
88 subparser.set_defaults(func=callback)
90 def _get_parser(self):
91 '''get a command-line parser'''
92 parser = self._get_base_parser()
94 subparsers = parser.add_subparsers(
99 for category in YardstickCLI.categories:
100 command_object = YardstickCLI.categories[category]()
101 desc = command_object.__doc__ or ''
102 subparser = subparsers.add_parser(
103 category, description=desc,
104 formatter_class=RawDescriptionHelpFormatter
106 subparser.set_defaults(command_object=command_object)
107 cmd_subparsers = subparser.add_subparsers(title='subcommands')
108 self._find_actions(cmd_subparsers, command_object)
112 def main(self, argv):
113 '''run the command line interface'''
115 # get argument parser
116 parser = self._get_parser()
119 args = parser.parse_args(argv)
122 logger = logging.getLogger('yardstick')
123 logger.setLevel(logging.WARNING)
126 logger.setLevel(logging.INFO)
129 logger.setLevel(logging.DEBUG)
131 # dispatch to category parser