2018-07-04 15:39:44 +01:00
|
|
|
# Copyright 2018 ARM Limited
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2017-03-20 16:24:22 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from wa.framework import pluginloader
|
|
|
|
from wa.framework.exception import ConfigError
|
2018-02-16 14:09:02 +00:00
|
|
|
from wa.framework.instrument import is_installed
|
2017-03-20 16:24:22 +00:00
|
|
|
from wa.framework.plugin import Plugin
|
2018-05-11 14:39:33 +01:00
|
|
|
from wa.utils.log import log_error, indentcontext
|
2018-01-29 09:47:52 +00:00
|
|
|
from wa.utils.misc import isiterable
|
2018-02-05 17:25:20 +00:00
|
|
|
from wa.utils.types import identifier
|
2017-03-20 16:24:22 +00:00
|
|
|
|
|
|
|
|
2018-01-12 15:22:11 +00:00
|
|
|
class OutputProcessor(Plugin):
|
2017-03-20 16:24:22 +00:00
|
|
|
|
2018-01-12 15:22:11 +00:00
|
|
|
kind = 'output_processor'
|
2017-03-20 16:24:22 +00:00
|
|
|
requires = []
|
|
|
|
|
2018-01-29 09:47:52 +00:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super(OutputProcessor, self).__init__(**kwargs)
|
|
|
|
self.is_enabled = True
|
|
|
|
|
2017-03-20 16:24:22 +00:00
|
|
|
def validate(self):
|
2018-01-12 15:22:11 +00:00
|
|
|
super(OutputProcessor, self).validate()
|
2017-03-20 16:24:22 +00:00
|
|
|
for instrument in self.requires:
|
|
|
|
if not is_installed(instrument):
|
|
|
|
msg = 'Instrument "{}" is required by {}, but is not installed.'
|
|
|
|
raise ConfigError(msg.format(instrument, self.name))
|
|
|
|
|
2018-09-04 17:18:25 +01:00
|
|
|
def initialize(self, context):
|
2017-03-20 16:24:22 +00:00
|
|
|
pass
|
|
|
|
|
2018-09-04 17:18:25 +01:00
|
|
|
def finalize(self, context):
|
2017-03-20 16:24:22 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ProcessorManager(object):
|
|
|
|
|
|
|
|
def __init__(self, loader=pluginloader):
|
|
|
|
self.loader = loader
|
|
|
|
self.logger = logging.getLogger('processor')
|
|
|
|
self.processors = []
|
|
|
|
|
2017-10-23 12:25:10 +01:00
|
|
|
def install(self, processor, context):
|
2018-01-12 15:22:11 +00:00
|
|
|
if not isinstance(processor, OutputProcessor):
|
|
|
|
processor = self.loader.get_output_processor(processor)
|
2017-03-20 16:24:22 +00:00
|
|
|
self.logger.debug('Installing {}'.format(processor.name))
|
2017-10-23 12:25:10 +01:00
|
|
|
processor.logger.context = context
|
2017-03-20 16:24:22 +00:00
|
|
|
self.processors.append(processor)
|
2018-07-11 11:03:47 +01:00
|
|
|
context.add_augmentation(processor)
|
2017-03-20 16:24:22 +00:00
|
|
|
|
2018-01-29 09:47:52 +00:00
|
|
|
def disable_all(self):
|
|
|
|
for output_processor in self.processors:
|
|
|
|
self._disable_output_processor(output_processor)
|
|
|
|
|
|
|
|
def enable_all(self):
|
|
|
|
for output_processor in self.processors:
|
|
|
|
self._enable_output_processor(output_processor)
|
|
|
|
|
|
|
|
def enable(self, to_enable):
|
|
|
|
if isiterable(to_enable):
|
|
|
|
for inst in to_enable:
|
|
|
|
self._enable_output_processor(inst)
|
|
|
|
else:
|
|
|
|
self._enable_output_processor(to_enable)
|
|
|
|
|
|
|
|
def disable(self, to_disable):
|
|
|
|
if isiterable(to_disable):
|
|
|
|
for inst in to_disable:
|
|
|
|
self._disable_output_processor(inst)
|
|
|
|
else:
|
|
|
|
self._disable_output_processor(to_disable)
|
|
|
|
|
|
|
|
def get_output_processor(self, processor):
|
|
|
|
if isinstance(processor, OutputProcessor):
|
|
|
|
return processor
|
|
|
|
|
2018-02-05 17:25:20 +00:00
|
|
|
processor = identifier(processor)
|
2018-01-29 09:47:52 +00:00
|
|
|
for p in self.processors:
|
|
|
|
if processor == p.name:
|
|
|
|
return p
|
|
|
|
raise ValueError('Output processor {} is not installed'.format(processor))
|
|
|
|
|
|
|
|
def get_enabled(self):
|
|
|
|
return [p for p in self.processors if p.is_enabled]
|
|
|
|
|
|
|
|
def get_disabled(self):
|
|
|
|
return [p for p in self.processors if not p.is_enabled]
|
|
|
|
|
2017-03-20 16:24:22 +00:00
|
|
|
def validate(self):
|
|
|
|
for proc in self.processors:
|
|
|
|
proc.validate()
|
|
|
|
|
2018-09-04 17:18:25 +01:00
|
|
|
def initialize(self, context):
|
2017-03-20 16:24:22 +00:00
|
|
|
for proc in self.processors:
|
2018-09-04 17:18:25 +01:00
|
|
|
proc.initialize(context)
|
2017-03-20 16:24:22 +00:00
|
|
|
|
2018-09-04 17:18:25 +01:00
|
|
|
def finalize(self, context):
|
2017-03-20 16:24:22 +00:00
|
|
|
for proc in self.processors:
|
2018-09-04 17:18:25 +01:00
|
|
|
proc.finalize(context)
|
2017-03-20 16:24:22 +00:00
|
|
|
|
|
|
|
def process_job_output(self, context):
|
2018-05-11 14:38:13 +01:00
|
|
|
self.do_for_each_proc('process_job_output', 'Processing using "{}"',
|
2017-03-20 16:24:22 +00:00
|
|
|
context.job_output, context.target_info,
|
|
|
|
context.run_output)
|
|
|
|
|
|
|
|
def export_job_output(self, context):
|
|
|
|
self.do_for_each_proc('export_job_output', 'Exporting using "{}"',
|
|
|
|
context.job_output, context.target_info,
|
|
|
|
context.run_output)
|
|
|
|
|
|
|
|
def process_run_output(self, context):
|
|
|
|
self.do_for_each_proc('process_run_output', 'Processing using "{}"',
|
|
|
|
context.run_output, context.target_info)
|
|
|
|
|
|
|
|
def export_run_output(self, context):
|
|
|
|
self.do_for_each_proc('export_run_output', 'Exporting using "{}"',
|
|
|
|
context.run_output, context.target_info)
|
|
|
|
|
|
|
|
def do_for_each_proc(self, method_name, message, *args):
|
2018-05-11 14:39:33 +01:00
|
|
|
with indentcontext():
|
2017-03-20 16:24:22 +00:00
|
|
|
for proc in self.processors:
|
2018-01-29 09:47:52 +00:00
|
|
|
if proc.is_enabled:
|
|
|
|
proc_func = getattr(proc, method_name, None)
|
|
|
|
if proc_func is None:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
self.logger.info(message.format(proc.name))
|
|
|
|
proc_func(*args)
|
2018-07-05 14:02:05 +01:00
|
|
|
except Exception as e: # pylint: disable=broad-except
|
2018-01-29 09:47:52 +00:00
|
|
|
if isinstance(e, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
log_error(e, self.logger)
|
|
|
|
|
|
|
|
def _enable_output_processor(self, inst):
|
|
|
|
inst = self.get_output_processor(inst)
|
|
|
|
self.logger.debug('Enabling output processor {}'.format(inst.name))
|
|
|
|
if not inst.is_enabled:
|
|
|
|
inst.is_enabled = True
|
|
|
|
|
|
|
|
def _disable_output_processor(self, inst):
|
|
|
|
inst = self.get_output_processor(inst)
|
|
|
|
self.logger.debug('Disabling output processor {}'.format(inst.name))
|
|
|
|
if inst.is_enabled:
|
|
|
|
inst.is_enabled = False
|