aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/cmd/cli.py
diff options
context:
space:
mode:
authorJo¶rgen Karlsson <jorgen.w.karlsson@ericsson.com>2015-06-12 11:19:57 +0200
committerJo¶rgen Karlsson <jorgen.w.karlsson@ericsson.com>2015-06-18 15:37:30 +0200
commit40f49a42dd8476ad2332ecf2e0a57e6bf511aa63 (patch)
tree2cc1a81ddf04efde2bc85b8dd0a0675c31516545 /yardstick/cmd/cli.py
parent407de6815f1462540ba067a16a3c69e590aa3f8e (diff)
add new command line handler
New command line handler with pluggable classes. Task subcommand added. To run a scenario: yardstick -d task start samples/ping.yaml $ yardstick -h usage: yardstick [-h] [-V] [-d] [-v] {task} ... Command-line interface to yardstick optional arguments: -h, --help show this help message and exit -V, --version display version -d, --debug increase output verbosity to debug -v, --verbose increase output verbosity to info subcommands: {task} $ yardstick task -h usage: yardstick task [-h] {start} ... Task commands. Set of commands to manage benchmark tasks. optional arguments: -h, --help show this help message and exit subcommands: {start} $ yardstick task start -h usage: yardstick task start [-h] [--keep-deploy] [--parse-only] [--output-file OUTPUT_FILE] taskfile Start a benchmark scenario. positional arguments: taskfile path to taskfile optional arguments: -h, --help show this help message and exit --keep-deploy keep context deployed in cloud --parse-only parse the benchmark config file and exit --output-file OUTPUT_FILE file where output is stored, default /tmp/yardstick.out JIRA :- Signed-off-by: Jo¶rgen Karlsson <jorgen.w.karlsson@ericsson.com> Change-Id: If0672594efa4c94c94ebb73f0bc97ecfe3e00c62
Diffstat (limited to 'yardstick/cmd/cli.py')
-rw-r--r--yardstick/cmd/cli.py128
1 files changed, 128 insertions, 0 deletions
diff --git a/yardstick/cmd/cli.py b/yardstick/cmd/cli.py
new file mode 100644
index 000000000..78e4e4c20
--- /dev/null
+++ b/yardstick/cmd/cli.py
@@ -0,0 +1,128 @@
+##############################################################################
+# Copyright (c) 2015 Ericsson AB and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+'''
+Command-line interface to yardstick
+'''
+
+import argparse
+import logging
+
+from pkg_resources import get_distribution
+from argparse import RawDescriptionHelpFormatter
+
+from yardstick.cmd.commands import task
+
+
+class YardstickCLI():
+ '''Command-line interface to yardstick'''
+
+ # Command categories
+ categories = {
+ 'task': task.TaskCommands
+ }
+
+ def __init__(self):
+ self._version = 'yardstick version %s ' % \
+ get_distribution('yardstick').version
+
+ def _get_base_parser(self):
+ '''get base (top level) parser'''
+
+ parser = argparse.ArgumentParser(
+ prog='yardstick',
+ formatter_class=RawDescriptionHelpFormatter,
+ description=YardstickCLI.__doc__ or ''
+ )
+
+ # Global options
+
+ parser.add_argument(
+ "-V", "--version",
+ help="display version",
+ version=self._version,
+ action="version"
+ )
+
+ parser.add_argument(
+ "-d", "--debug",
+ help="increase output verbosity to debug",
+ action="store_true"
+ )
+
+ parser.add_argument(
+ "-v", "--verbose",
+ help="increase output verbosity to info",
+ action="store_true"
+ )
+
+ return parser
+
+ def _find_actions(self, subparsers, actions_module):
+ '''find action methods'''
+ # Find action methods inside actions_module and
+ # add them to the command parser.
+ # The 'actions_module' argument may be a class
+ # or module. Action methods start with 'do_'
+ for attr in (a for a in dir(actions_module) if a.startswith('do_')):
+ command = attr[3:].replace('_', '-')
+ callback = getattr(actions_module, attr)
+ desc = callback.__doc__ or ''
+ arguments = getattr(callback, 'arguments', [])
+ subparser = subparsers.add_parser(
+ command,
+ description=desc
+ )
+ for (args, kwargs) in arguments:
+ subparser.add_argument(*args, **kwargs)
+ subparser.set_defaults(func=callback)
+
+ def _get_parser(self):
+ '''get a command-line parser'''
+ parser = self._get_base_parser()
+
+ subparsers = parser.add_subparsers(
+ title='subcommands',
+ )
+
+ # add subcommands
+ for category in YardstickCLI.categories:
+ command_object = YardstickCLI.categories[category]()
+ desc = command_object.__doc__ or ''
+ subparser = subparsers.add_parser(
+ category, description=desc,
+ formatter_class=RawDescriptionHelpFormatter
+ )
+ subparser.set_defaults(command_object=command_object)
+ cmd_subparsers = subparser.add_subparsers(title='subcommands')
+ self._find_actions(cmd_subparsers, command_object)
+
+ return parser
+
+ def main(self, argv):
+ '''run the command line interface'''
+
+ # get argument parser
+ parser = self._get_parser()
+
+ # parse command-line
+ args = parser.parse_args(argv)
+
+ # handle global opts
+ logger = logging.getLogger('yardstick')
+ logger.setLevel(logging.WARNING)
+
+ if args.verbose:
+ logger.getLogger('yardstick').setLevel(logging.INFO)
+
+ if args.debug:
+ logger.setLevel(logging.DEBUG)
+
+ # dispatch to category parser
+ args.func(args)