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 |
| 13 from pylib import constants |
12 from pylib.base import environment | 14 from pylib.base import environment |
13 | 15 |
14 | 16 |
| 17 def _DeviceCachePath(device): |
| 18 file_name = 'device_cache_%s.json' % device.adb.GetDeviceSerial() |
| 19 return os.path.join(constants.GetOutDirectory(), file_name) |
| 20 |
| 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 it will be ready the first |
| 81 # time that it is enabled. Writing it every time is also necessary so that |
| 82 # an invalid cache can be flushed just by disabling it for one run. |
| 83 for d in self._devices: |
| 84 cache_path = _DeviceCachePath(d) |
| 85 with open(cache_path, 'w') as f: |
| 86 f.write(d.DumpCacheData()) |
| 87 logging.info('Wrote device cache: %s', cache_path) |
64 | 88 |
65 def BlacklistDevice(self, device): | 89 def BlacklistDevice(self, device): |
66 if not self._blacklist: | 90 if not self._blacklist: |
67 logging.warning( | 91 logging.warning( |
68 'Attempted to blacklist %s, but no blacklist was provided.', | 92 'Attempted to blacklist %s, but no blacklist was provided.', |
69 str(device)) | 93 str(device)) |
70 return | 94 return |
71 | 95 |
72 device_serial = device.adb.GetDeviceSerial() | 96 device_serial = device.adb.GetDeviceSerial() |
73 self._blacklist.Extend([device_serial]) | 97 self._blacklist.Extend([device_serial]) |
74 with self._devices_lock: | 98 with self._devices_lock: |
75 self._devices = [d for d in self._devices if str(d) != device_serial] | 99 self._devices = [d for d in self._devices if str(d) != device_serial] |
76 | 100 |
OLD | NEW |