Update links in user guide
[yardstick.git] / yardstick / cmd / cli.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
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 ##############################################################################
9
10 '''
11 Command-line interface to yardstick
12 '''
13
14 import logging
15 import os
16 import sys
17
18 from pkg_resources import get_distribution
19 from argparse import RawDescriptionHelpFormatter
20 from oslo_config import cfg
21
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
26
27 CONF = cfg.CONF
28 cli_opts = [
29     cfg.BoolOpt('debug',
30                 short='d',
31                 default=False,
32                 help='increase output verbosity to debug'),
33     cfg.BoolOpt('verbose',
34                 short='v',
35                 default=False,
36                 help='increase output verbosity to info')
37 ]
38 CONF.register_cli_opts(cli_opts)
39
40 CONFIG_SEARCH_PATHS = [sys.prefix + "/etc/yardstick",
41                        "~/.yardstick",
42                        "/etc/yardstick"]
43
44
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):
50             return [confname]
51
52     return None
53
54
55 class YardstickCLI():
56     '''Command-line interface to yardstick'''
57
58     # Command categories
59     categories = {
60         'task': task.TaskCommands,
61         'runner': runner.RunnerCommands,
62         'scenario': scenario.ScenarioCommands,
63         'testcase': testcase.TestcaseCommands
64     }
65
66     def __init__(self):
67         self._version = 'yardstick version %s ' % \
68             get_distribution('yardstick').version
69
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(
82                 command,
83                 description=desc
84             )
85             for (args, kwargs) in arguments:
86                 subparser.add_argument(*args, **kwargs)
87             subparser.set_defaults(func=callback)
88
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
97             )
98             subparser.set_defaults(command_object=command_object)
99             cmd_subparsers = subparser.add_subparsers(title='subcommands')
100             self._find_actions(cmd_subparsers, command_object)
101
102     def main(self, argv):
103         '''run the command line interface'''
104
105         # register subcommands to parse additional command line arguments
106         def parser(subparsers):
107             self._add_command_parsers(YardstickCLI.categories, subparsers)
108
109         category_opt = cfg.SubCommandOpt("category",
110                                          title="Command categories",
111                                          help="Available categories",
112                                          handler=parser)
113         CONF.register_cli_opt(category_opt)
114
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))
118
119         # handle global opts
120         logger = logging.getLogger('yardstick')
121         logger.setLevel(logging.WARNING)
122
123         if CONF.verbose:
124             logger.setLevel(logging.INFO)
125
126         if CONF.debug:
127             logger.setLevel(logging.DEBUG)
128
129         # dispatch to category parser
130         func = CONF.category.func
131         func(CONF.category)