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 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
6 | 6 |
7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
8 """ | 8 """ |
9 # pylint: disable=W0613 | 9 # pylint: disable=W0613 |
10 | 10 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 class DeviceUtils(object): | 59 class DeviceUtils(object): |
60 | 60 |
61 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') | 61 _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
62 | 62 |
63 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, | 63 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
64 default_retries=_DEFAULT_RETRIES): | 64 default_retries=_DEFAULT_RETRIES): |
65 """DeviceUtils constructor. | 65 """DeviceUtils constructor. |
66 | 66 |
67 Args: | 67 Args: |
68 device: Either a device serial, an existing AdbWrapper instance, an | 68 device: Either a device serial, an existing AdbWrapper instance, or an |
69 an existing AndroidCommands instance, or nothing. | 69 an existing AndroidCommands instance. |
70 default_timeout: An integer containing the default number of seconds to | 70 default_timeout: An integer containing the default number of seconds to |
71 wait for an operation to complete if no explicit value | 71 wait for an operation to complete if no explicit value |
72 is provided. | 72 is provided. |
73 default_retries: An integer containing the default number or times an | 73 default_retries: An integer containing the default number or times an |
74 operation should be retried on failure if no explicit | 74 operation should be retried on failure if no explicit |
75 value is provided. | 75 value is provided. |
76 """ | 76 """ |
77 self.adb = None | 77 self.adb = None |
78 self.old_interface = None | 78 self.old_interface = None |
79 if isinstance(device, basestring): | 79 if isinstance(device, basestring): |
80 self.adb = adb_wrapper.AdbWrapper(device) | 80 self.adb = adb_wrapper.AdbWrapper(device) |
81 self.old_interface = pylib.android_commands.AndroidCommands(device) | 81 self.old_interface = pylib.android_commands.AndroidCommands(device) |
82 elif isinstance(device, adb_wrapper.AdbWrapper): | 82 elif isinstance(device, adb_wrapper.AdbWrapper): |
83 self.adb = device | 83 self.adb = device |
84 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) | 84 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) |
85 elif isinstance(device, pylib.android_commands.AndroidCommands): | 85 elif isinstance(device, pylib.android_commands.AndroidCommands): |
86 self.adb = adb_wrapper.AdbWrapper(device.GetDevice()) | 86 self.adb = adb_wrapper.AdbWrapper(device.GetDevice()) |
87 self.old_interface = device | 87 self.old_interface = device |
88 elif not device: | |
89 self.adb = adb_wrapper.AdbWrapper('') | |
90 self.old_interface = pylib.android_commands.AndroidCommands() | |
91 else: | 88 else: |
92 raise ValueError('Unsupported type passed for argument "device"') | 89 raise ValueError('Unsupported device value: %r' % device) |
93 self._commands_installed = None | 90 self._commands_installed = None |
94 self._default_timeout = default_timeout | 91 self._default_timeout = default_timeout |
95 self._default_retries = default_retries | 92 self._default_retries = default_retries |
96 self._cache = {} | 93 self._cache = {} |
97 assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR) | 94 assert hasattr(self, decorators.DEFAULT_TIMEOUT_ATTR) |
98 assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR) | 95 assert hasattr(self, decorators.DEFAULT_RETRIES_ATTR) |
99 | 96 |
100 @decorators.WithTimeoutAndRetriesFromInstance() | 97 @decorators.WithTimeoutAndRetriesFromInstance() |
101 def IsOnline(self, timeout=None, retries=None): | 98 def IsOnline(self, timeout=None, retries=None): |
102 """Checks whether the device is online. | 99 """Checks whether the device is online. |
(...skipping 1022 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 - A dict containing the overall memory usage statistics for the PID. | 1122 - A dict containing the overall memory usage statistics for the PID. |
1126 - A dict containing memory usage statistics broken down by mapping. | 1123 - A dict containing memory usage statistics broken down by mapping. |
1127 | 1124 |
1128 Raises: | 1125 Raises: |
1129 CommandTimeoutError on timeout. | 1126 CommandTimeoutError on timeout. |
1130 """ | 1127 """ |
1131 return self.old_interface.GetMemoryUsageForPid(pid) | 1128 return self.old_interface.GetMemoryUsageForPid(pid) |
1132 | 1129 |
1133 def __str__(self): | 1130 def __str__(self): |
1134 """Returns the device serial.""" | 1131 """Returns the device serial.""" |
1135 s = self.old_interface.GetDevice() | 1132 return self.adb.GetDeviceSerial() |
1136 if not s: | |
1137 s = self.old_interface.Adb().GetSerialNumber() | |
1138 if s == 'unknown': | |
1139 raise device_errors.NoDevicesError() | |
1140 return s | |
1141 | 1133 |
1142 @staticmethod | 1134 @staticmethod |
1143 def parallel(devices=None, async=False): | 1135 def parallel(devices=None, async=False): |
1144 """Creates a Parallelizer to operate over the provided list of devices. | 1136 """Creates a Parallelizer to operate over the provided list of devices. |
1145 | 1137 |
1146 If |devices| is either |None| or an empty list, the Parallelizer will | 1138 If |devices| is either |None| or an empty list, the Parallelizer will |
1147 operate over all attached devices. | 1139 operate over all attached devices. |
1148 | 1140 |
1149 Args: | 1141 Args: |
1150 devices: A list of either DeviceUtils instances or objects from | 1142 devices: A list of either DeviceUtils instances or objects from |
1151 from which DeviceUtils instances can be constructed. If None, | 1143 from which DeviceUtils instances can be constructed. If None, |
1152 all attached devices will be used. | 1144 all attached devices will be used. |
1153 async: If true, returns a Parallelizer that runs operations | 1145 async: If true, returns a Parallelizer that runs operations |
1154 asynchronously. | 1146 asynchronously. |
1155 | 1147 |
1156 Returns: | 1148 Returns: |
1157 A Parallelizer operating over |devices|. | 1149 A Parallelizer operating over |devices|. |
1158 """ | 1150 """ |
1159 if not devices or len(devices) == 0: | 1151 if not devices or len(devices) == 0: |
1160 devices = pylib.android_commands.GetAttachedDevices() | 1152 devices = pylib.android_commands.GetAttachedDevices() |
1161 parallelizer_type = (parallelizer.Parallelizer if async | 1153 parallelizer_type = (parallelizer.Parallelizer if async |
1162 else parallelizer.SyncParallelizer) | 1154 else parallelizer.SyncParallelizer) |
1163 return parallelizer_type([ | 1155 return parallelizer_type([ |
1164 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 1156 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
1165 for d in devices]) | 1157 for d in devices]) |
OLD | NEW |