1# SPDX-License-Identifier: Apache-2.0 2# 3# Copyright (C) 2015, ARM Limited and contributors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may 6# not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18""" Helper module for registering Analysis classes methods """ 19 20import os 21import sys 22import logging 23 24from glob import glob 25from inspect import isclass 26from importlib import import_module 27 28from analysis_module import AnalysisModule 29 30 31 32class AnalysisRegister(object): 33 """ 34 Define list of supported Analysis Classes. 35 36 :param trace: input Trace object 37 :type trace: :mod:`libs.utils.Trace` 38 """ 39 40 def __init__(self, trace): 41 42 # Setup logging 43 self._log = logging.getLogger('Analysis') 44 45 # Add workloads dir to system path 46 analysis_dir = os.path.dirname(os.path.abspath(__file__)) 47 analysis_dir = os.path.join(analysis_dir, 'analysis') 48 self._log.debug('Analysis: %s', analysis_dir) 49 50 sys.path.insert(0, analysis_dir) 51 self._log.debug('Syspath: %s', sys.path) 52 53 self._log.debug('Registering trace analysis modules:') 54 for filepath in glob(os.path.join(analysis_dir, '*.py')): 55 filename = os.path.splitext(os.path.basename(filepath))[0] 56 57 # Ignore __init__ files 58 if filename.startswith('__'): 59 continue 60 61 self._log.debug('Filename: %s', filename) 62 63 # Import the module for inspection 64 module = import_module(filename) 65 for member in dir(module): 66 # Ignore the base class 67 if member == 'AnalysisModule': 68 continue 69 handler = getattr(module, member) 70 if handler and isclass(handler) and \ 71 issubclass(handler, AnalysisModule): 72 module_name = module.__name__.replace('_analysis', '') 73 setattr(self, module_name, handler(trace)) 74 self._log.debug(' %s', module_name) 75 76# vim :set tabstop=4 shiftwidth=4 expandtab 77