Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import os | |
| 6 import threading | 7 import threading |
| 7 | 8 |
| 8 from devil.android import device_blacklist | 9 from devil.android import device_blacklist |
| 9 from devil.android import device_errors | 10 from devil.android import device_errors |
| 10 from devil.android import device_utils | 11 from devil.android import device_utils |
| 11 from devil.utils import parallelizer | 12 from devil.utils import parallelizer |
| 12 from pylib.base import environment | 13 from pylib.base import environment |
| 14 from pylib import constants | |
|
jbudorick
2015/10/08 14:34:49
nit: this should precede pylib.base.environment
agrieve
2015/10/08 16:03:28
Done.
| |
| 15 | |
| 16 | |
| 17 def _DeviceCachePath(device): | |
| 18 file_name = 'device_cache_%s.json' % device.adb.GetDeviceSerial() | |
| 19 return os.path.join(constants.GetOutDirectory(), file_name) | |
| 13 | 20 |
| 14 | 21 |
| 15 class LocalDeviceEnvironment(environment.Environment): | 22 class LocalDeviceEnvironment(environment.Environment): |
| 16 | 23 |
| 17 def __init__(self, args, _error_func): | 24 def __init__(self, args, _error_func): |
| 18 super(LocalDeviceEnvironment, self).__init__() | 25 super(LocalDeviceEnvironment, self).__init__() |
| 19 self._blacklist = (device_blacklist.Blacklist(args.blacklist_file) | 26 self._blacklist = (device_blacklist.Blacklist(args.blacklist_file) |
| 20 if args.blacklist_file | 27 if args.blacklist_file |
| 21 else None) | 28 else None) |
| 22 self._device_serial = args.test_device | 29 self._device_serial = args.test_device |
| 23 self._devices_lock = threading.Lock() | 30 self._devices_lock = threading.Lock() |
| 24 self._devices = [] | 31 self._devices = [] |
| 25 self._max_tries = 1 + args.num_retries | 32 self._max_tries = 1 + args.num_retries |
| 26 self._tool_name = args.tool | 33 self._tool_name = args.tool |
| 34 self._enable_device_cache = args.enable_device_cache | |
| 27 | 35 |
| 28 #override | 36 #override |
| 29 def SetUp(self): | 37 def SetUp(self): |
| 30 available_devices = device_utils.DeviceUtils.HealthyDevices( | 38 available_devices = device_utils.DeviceUtils.HealthyDevices( |
| 31 self._blacklist) | 39 self._blacklist, enable_device_files_cache=self._enable_device_cache) |
| 32 if not available_devices: | 40 if not available_devices: |
| 33 raise device_errors.NoDevicesError | 41 raise device_errors.NoDevicesError |
| 34 if self._device_serial: | 42 if self._device_serial: |
| 35 self._devices = [d for d in available_devices | 43 self._devices = [d for d in available_devices |
| 36 if d.adb.GetDeviceSerial() == self._device_serial] | 44 if d.adb.GetDeviceSerial() == self._device_serial] |
| 37 if not self._devices: | 45 if not self._devices: |
| 38 raise device_errors.DeviceUnreachableError( | 46 raise device_errors.DeviceUnreachableError( |
| 39 'Could not find device %r' % self._device_serial) | 47 'Could not find device %r' % self._device_serial) |
| 40 else: | 48 else: |
| 41 self._devices = available_devices | 49 self._devices = available_devices |
| 42 | 50 |
| 51 if self._enable_device_cache: | |
| 52 for d in self._devices: | |
| 53 cache_path = _DeviceCachePath(d) | |
| 54 if os.path.exists(cache_path): | |
| 55 logging.info('Using device cache: %s', cache_path) | |
| 56 with open(cache_path) as f: | |
| 57 d.LoadCacheData(f.read()) | |
| 58 os.unlink(cache_path) | |
| 59 | |
| 43 @property | 60 @property |
| 44 def devices(self): | 61 def devices(self): |
| 45 if not self._devices: | 62 if not self._devices: |
| 46 raise device_errors.NoDevicesError() | 63 raise device_errors.NoDevicesError() |
| 47 return self._devices | 64 return self._devices |
| 48 | 65 |
| 49 @property | 66 @property |
| 50 def parallel_devices(self): | 67 def parallel_devices(self): |
| 51 return parallelizer.SyncParallelizer(self.devices) | 68 return parallelizer.SyncParallelizer(self.devices) |
| 52 | 69 |
| 53 @property | 70 @property |
| 54 def max_tries(self): | 71 def max_tries(self): |
| 55 return self._max_tries | 72 return self._max_tries |
| 56 | 73 |
| 57 @property | 74 @property |
| 58 def tool(self): | 75 def tool(self): |
| 59 return self._tool_name | 76 return self._tool_name |
| 60 | 77 |
| 61 #override | 78 #override |
| 62 def TearDown(self): | 79 def TearDown(self): |
| 63 pass | 80 # Write the cache even when not using it so that a bad cache will be fixed |
|
jbudorick
2015/10/08 14:34:49
I lost you on the reasoning here as written. I can
agrieve
2015/10/08 16:03:28
Reworded
agrieve
2015/10/09 00:28:31
Should also note that the extra time is negligible
jbudorick
2015/10/09 00:30:31
Fair enough.
| |
| 81 # by running once without it enabled. | |
| 82 for d in self._devices: | |
| 83 cache_path = _DeviceCachePath(d) | |
| 84 with open(cache_path, 'w') as f: | |
| 85 f.write(d.DumpCacheData()) | |
| 86 logging.info('Wrote device cache: %s', cache_path) | |
| 64 | 87 |
| 65 def BlacklistDevice(self, device): | 88 def BlacklistDevice(self, device): |
| 66 if not self._blacklist: | 89 if not self._blacklist: |
| 67 logging.warning( | 90 logging.warning( |
| 68 'Attempted to blacklist %s, but no blacklist was provided.', | 91 'Attempted to blacklist %s, but no blacklist was provided.', |
| 69 str(device)) | 92 str(device)) |
| 70 return | 93 return |
| 71 | 94 |
| 72 device_serial = device.adb.GetDeviceSerial() | 95 device_serial = device.adb.GetDeviceSerial() |
| 73 self._blacklist.Extend([device_serial]) | 96 self._blacklist.Extend([device_serial]) |
| 74 with self._devices_lock: | 97 with self._devices_lock: |
| 75 self._devices = [d for d in self._devices if str(d) != device_serial] | 98 self._devices = [d for d in self._devices if str(d) != device_serial] |
| 76 | 99 |
| OLD | NEW |