1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-01-18 20:11:20 +00:00

Merge pull request #382 from setrofim/subcommands

Add create agenda command
This commit is contained in:
marcbonnici 2017-04-25 10:08:03 +01:00 committed by GitHub
commit 1bc71ed60b
4 changed files with 138 additions and 5 deletions

View File

@ -1,5 +1,5 @@
from wa.framework import pluginloader, log, signal
from wa.framework.command import Command
from wa.framework.command import Command, ComplexCommand, SubCommand
from wa.framework.configuration import settings
from wa.framework.configuration.core import Status
from wa.framework.exception import HostError, JobError, InstrumentError, ConfigError

82
wa/commands/create.py Normal file
View File

@ -0,0 +1,82 @@
import sys
from collections import OrderedDict
from wa import ComplexCommand, SubCommand, pluginloader
from wa.framework.target.descriptor import get_target_descriptions
from wa.utils.serializer import yaml
class CreateAgendaSubcommand(SubCommand):
name = 'agenda'
description = """
Create an agenda with the specified extensions enabled. And parameters set
to their default values.
"""
def initialize(self, context):
self.parser.add_argument('plugins', nargs='+',
help='Plugins to be added to the agendas')
self.parser.add_argument('-i', '--iterations', type=int, default=1,
help='Sets the number of iterations for all workloads')
self.parser.add_argument('-o', '--output', metavar='FILE',
help='Output file. If not specfied, STDOUT will be used instead.')
def execute(self, state, args):
agenda = OrderedDict()
agenda['config'] = OrderedDict(instrumentation=[], result_processors=[])
agenda['global'] = OrderedDict(iterations=args.iterations)
agenda['workloads'] = []
target_desc = None
targets = {td.name: td for td in get_target_descriptions()}
for name in args.plugins:
if name in targets:
if target_desc is not None:
raise ConfigError('Specifying multiple devices: {} and {}'.format(target_desc.name, name))
target_desc = targets[name]
agenda['config']['device'] = name
agenda['config']['device_config'] = target_desc.get_default_config()
continue
extcls = pluginloader.get_plugin_class(name)
config = pluginloader.get_default_config(name)
if extcls.kind == 'workload':
entry = OrderedDict()
entry['name'] = extcls.name
if name != extcls.name:
entry['label'] = name
entry['params'] = config
agenda['workloads'].append(entry)
else:
if extcls.kind == 'instrument':
agenda['config']['instrumentation'].append(name)
if extcls.kind == 'result_processor':
agenda['config']['result_processors'].append(name)
agenda['config'][name] = config
if args.output:
wfh = open(args.output, 'w')
else:
wfh = sys.stdout
yaml.dump(agenda, wfh, indent=4, default_flow_style=False)
if args.output:
wfh.close()
class CreateCommand(ComplexCommand):
name = 'create'
description = '''
Used to create various WA-related objects (see positional arguments list
for what objects may be created).\n\nUse "wa create <object> -h" for
object-specific arguments.
'''
subcmd_classes = [
#CreateWorkloadSubcommand,
#CreatePackageSubcommand,
CreateAgendaSubcommand,
]

View File

@ -15,6 +15,7 @@
import textwrap
from wa.framework.exception import CommandError
from wa.framework.plugin import Plugin
from wa.framework.version import get_wa_version
from wa.utils.doc import format_body
@ -30,7 +31,7 @@ def init_argument_parser(parser):
return parser
class Command(Plugin):
class SubCommand(object):
"""
Defines a Workload Automation command. This will be executed from the
command line as ``wa <command> [args ...]``. This defines the name to be
@ -39,15 +40,14 @@ class Command(Plugin):
command line arguments.
"""
kind = "command"
help = None
usage = None
description = None
epilog = None
formatter_class = None
def __init__(self, subparsers):
super(Command, self).__init__()
def __init__(self, logger, subparsers):
self.logger = logger
self.group = subparsers
desc = format_body(textwrap.dedent(self.description), 80)
parser_params = dict(help=(self.help or self.description), usage=self.usage,
@ -79,3 +79,46 @@ class Command(Plugin):
"""
raise NotImplementedError()
class Command(Plugin, SubCommand):
"""
Defines a Workload Automation command. This will be executed from the
command line as ``wa <command> [args ...]``. This defines the name to be
used when invoking wa, the code that will actually be executed on
invocation and the argument parser to be used to parse the reset of the
command line arguments.
"""
kind = "command"
def __init__(self, subparsers):
Plugin.__init__(self)
SubCommand.__init__(self, self.logger, subparsers)
class ComplexCommand(Command):
"""
A command that defines sub-commands.
"""
subcmd_classes = []
def __init__(self, subparsers):
self.subcommands = []
super(ComplexCommand, self).__init__(subparsers)
def initialize(self, context):
subparsers = self.parser.add_subparsers(dest='what', metavar='SUBCMD')
for subcmd_cls in self.subcmd_classes:
subcmd = subcmd_cls(self.logger, subparsers)
self.subcommands.append(subcmd)
def execute(self, state, args):
for subcmd in self.subcommands:
if subcmd.name == args.what:
subcmd.execute(state, args)
break
else:
raise CommandError('Not a valid create parameter: {}'.format(args.name))

View File

@ -82,6 +82,14 @@ class TargetDescription(object):
self._set('platform_params', platform_params)
self._set('conn_params', conn_params)
def get_default_config(self):
param_attrs = ['target_params', 'platform_params', 'conn_params']
config = {}
for pattr in param_attrs:
for n, p in getattr(self, pattr).itervalues():
config[n] = p.default
return config
def _set(self, attr, vals):
if vals is None:
vals = {}