1# Lint as: python2, python3 2from __future__ import absolute_import 3from __future__ import division 4from __future__ import print_function 5import itertools 6import common 7import six 8 9 10def _get_unpassable_types(arg): 11 """ Given an argument, returns a set of types contained in arg that are 12 unpassable. If arg is an atomic type (e.g. int) it either returns an 13 empty set (if the type is passable) or a singleton of the type (if the 14 type is not passable). """ 15 if isinstance(arg, (six.string_types, int, int)): 16 return set() 17 elif isinstance(arg, (list, tuple, set, frozenset, dict)): 18 if isinstance(arg, dict): 19 # keys and values must both be passable 20 parts = itertools.chain(six.iterkeys(arg), six.itervalues(arg)) 21 else: 22 # for all other containers we just iterate 23 parts = iter(arg) 24 types = set() 25 for part in parts: 26 types |= _get_unpassable_types(part) 27 return types 28 else: 29 return set([type(arg)]) 30 31 32def _validate_args(args): 33 """ Validates arguments. Lists and dictionaries are valid argument types, 34 so you can pass *args and **dargs in directly, rather than having to 35 iterate over them yourself. """ 36 unpassable_types = _get_unpassable_types(args) 37 if unpassable_types: 38 msg = "arguments of type '%s' cannot be passed to remote profilers" 39 msg %= ", ".join(t.__name__ for t in unpassable_types) 40 raise TypeError(msg) 41 42 43class profiler_proxy(object): 44 """ This is a server-side class that acts as a proxy to a real client-side 45 profiler class.""" 46 47 def __init__(self, profiler_name): 48 self.name = profiler_name 49 50 # does the profiler support rebooting? 51 profiler_module = common.setup_modules.import_module( 52 profiler_name, "autotest_lib.client.profilers.%s" % profiler_name) 53 profiler_class = getattr(profiler_module, profiler_name) 54 self.supports_reboot = profiler_class.supports_reboot 55 56 57 def initialize(self, *args, **dargs): 58 _validate_args(args) 59 _validate_args(dargs) 60 self.args, self.dargs = args, dargs 61 62 63 def setup(self, *args, **dargs): 64 assert self.args == args and self.dargs == dargs 65 # the actual setup happens lazily at start() 66 67 68 def start(self, test, host=None): 69 raise NotImplementedError('start not implemented') 70 71 72 def stop(self, test, host=None): 73 raise NotImplementedError('stop not implemented') 74 75 76 def report(self, test, host=None, wait_on_client=True): 77 raise NotImplementedError('report not implemented') 78