jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame^] | 1 | import itertools |
| 2 | from autotest_lib.server import autotest |
| 3 | |
| 4 | |
| 5 | |
| 6 | def get_unpassable_types(arg): |
| 7 | """ Given an argument, returns a set of types contained in arg that are |
| 8 | unpassable. If arg is an atomic type (e.g. int) it either returns an |
| 9 | empty set (if the type is passable) or a singleton of the type (if the |
| 10 | type is not passable). """ |
| 11 | if isinstance(arg, (basestring, int, long)): |
| 12 | return set() |
| 13 | elif isinstance(arg, (list, tuple, set, frozenset, dict)): |
| 14 | if isinstance(arg, dict): |
| 15 | # keys and values must both be passable |
| 16 | parts = itertools.chain(arg.iterkeys(), arg.itervalues()) |
| 17 | else: |
| 18 | # for all other containers we just iterate |
| 19 | parts = iter(arg) |
| 20 | types = set() |
| 21 | for part in parts: |
| 22 | types |= get_unpassable_types(arg) |
| 23 | return types |
| 24 | else: |
| 25 | return set([type(arg)]) |
| 26 | |
| 27 | |
| 28 | def validate_args(args): |
| 29 | """ Validates arguments. Lists and dictionaries are valid argument types, |
| 30 | so you can pass *args and **dargs in directly, rather than having to |
| 31 | iterate over them yourself. """ |
| 32 | unpassable_types = get_unpassable_types(args) |
| 33 | if unpassable_types: |
| 34 | msg = "arguments of type '%s' cannot be passed to remote profilers" |
| 35 | msg %= ", ".join(t.__name__ for t in unpassable_types) |
| 36 | raise TypeError(msg) |
| 37 | |
| 38 | |
| 39 | class profiler_proxy(object): |
| 40 | """ This is a server-side class that acts as a proxy to a real client-side |
| 41 | profiler class.""" |
| 42 | |
| 43 | def __init__(self, job, profiler_name): |
| 44 | self.job = job |
| 45 | self.name = profiler_name |
| 46 | self.installed_hosts = {} |
| 47 | |
| 48 | |
| 49 | def _install(self): |
| 50 | """ Install autotest on any current job hosts. """ |
| 51 | current_job_hosts = self.job.hosts |
| 52 | current_profiler_hosts = set(self.installed_hosts.keys()) |
| 53 | # install autotest on any new hosts in job.hosts |
| 54 | for host in current_job_hosts - current_profiler_hosts: |
| 55 | tmp_dir = host.get_tmp_dir(parent="/tmp/profilers") |
| 56 | at = autotest.Autotest(host) |
| 57 | at.install(autodir=tmp_dir) |
| 58 | self.installed_hosts[host] = at |
| 59 | # drop any installs from hosts no longer in job.hosts |
| 60 | for host in current_profiler_hosts - current_job_hosts: |
| 61 | del self.installed_hosts[host] |
| 62 | |
| 63 | |
| 64 | def setup(self, *args, **dargs): |
| 65 | validate_args(args) |
| 66 | validate_args(dargs) |
| 67 | self._install() |
| 68 | |
| 69 | |
| 70 | def initialize(self, *args, **dargs): |
| 71 | validate_args(args) |
| 72 | validate_args(dargs) |
| 73 | |
| 74 | |
| 75 | def start(self, test): |
| 76 | pass |
| 77 | |
| 78 | |
| 79 | def stop(self, test): |
| 80 | pass |
| 81 | |
| 82 | |
| 83 | def report(self, test): |
| 84 | pass |