| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 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 re | 6 import re |
| 7 | 7 |
| 8 from devil.utils import cmd_helper | 8 from devil.utils import cmd_helper |
| 9 | 9 |
| 10 _COULDNT_OPEN_ERROR_RE = re.compile(r'Couldn\'t open device.*') | 10 _COULDNT_OPEN_ERROR_RE = re.compile(r'Couldn\'t open device.*') |
| 11 _INDENTATION_RE = re.compile(r'^( *)') | 11 _INDENTATION_RE = re.compile(r'^( *)') |
| 12 _LSUSB_BUS_DEVICE_RE = re.compile(r'^Bus (\d{3}) Device (\d{3}):') | 12 _LSUSB_BUS_DEVICE_RE = re.compile(r'^Bus (\d{3}) Device (\d{3}):') |
| 13 _LSUSB_ENTRY_RE = re.compile(r'^ *([^ ]+) +([^ ]+) *([^ ].*)?$') | 13 _LSUSB_ENTRY_RE = re.compile(r'^ *([^ ]+) +([^ ]+) *([^ ].*)?$') |
| 14 _LSUSB_GROUP_RE = re.compile(r'^ *([^ ]+.*):$') | 14 _LSUSB_GROUP_RE = re.compile(r'^ *([^ ]+.*):$') |
| 15 | 15 |
| 16 | 16 |
| 17 def _lsusbv_on_device(bus_id, dev_id): | 17 def _lsusbv_on_device(bus_id, dev_id): |
| 18 """Calls lsusb -v on device.""" | 18 """Calls lsusb -v on device.""" |
| 19 _, raw_output = cmd_helper.GetCmdStatusAndOutputWithTimeout( | 19 _, raw_output = cmd_helper.GetCmdStatusAndOutputWithTimeout( |
| 20 ['lsusb', '-v', '-s', '%s:%s' % (bus_id, dev_id)], timeout=2) | 20 ['lsusb', '-v', '-s', '%s:%s' % (bus_id, dev_id)], timeout=10) |
| 21 | 21 |
| 22 device = {'bus': bus_id, 'device': dev_id} | 22 device = {'bus': bus_id, 'device': dev_id} |
| 23 depth_stack = [device] | 23 depth_stack = [device] |
| 24 | 24 |
| 25 # TODO(jbudorick): Add documentation for parsing. | 25 # TODO(jbudorick): Add documentation for parsing. |
| 26 for line in raw_output.splitlines(): | 26 for line in raw_output.splitlines(): |
| 27 # Ignore blank lines. | 27 # Ignore blank lines. |
| 28 if not line: | 28 if not line: |
| 29 continue | 29 continue |
| 30 # Filter out error mesage about opening device. | 30 # Filter out error mesage about opening device. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 depth_stack.append(new_entry) | 73 depth_stack.append(new_entry) |
| 74 continue | 74 continue |
| 75 | 75 |
| 76 logging.error('lsusb parsing error: unrecognized line: "%s"', line) | 76 logging.error('lsusb parsing error: unrecognized line: "%s"', line) |
| 77 | 77 |
| 78 return device | 78 return device |
| 79 | 79 |
| 80 def lsusb(): | 80 def lsusb(): |
| 81 """Call lsusb and return the parsed output.""" | 81 """Call lsusb and return the parsed output.""" |
| 82 _, lsusb_list_output = cmd_helper.GetCmdStatusAndOutputWithTimeout( | 82 _, lsusb_list_output = cmd_helper.GetCmdStatusAndOutputWithTimeout( |
| 83 ['lsusb'], timeout=2) | 83 ['lsusb'], timeout=10) |
| 84 devices = [] | 84 devices = [] |
| 85 for line in lsusb_list_output.splitlines(): | 85 for line in lsusb_list_output.splitlines(): |
| 86 m = _LSUSB_BUS_DEVICE_RE.match(line) | 86 m = _LSUSB_BUS_DEVICE_RE.match(line) |
| 87 if m: | 87 if m: |
| 88 bus_num = m.group(1) | 88 bus_num = m.group(1) |
| 89 dev_num = m.group(2) | 89 dev_num = m.group(2) |
| 90 devices.append(_lsusbv_on_device(bus_num, dev_num)) | 90 devices.append(_lsusbv_on_device(bus_num, dev_num)) |
| 91 return devices | 91 return devices |
| 92 | 92 |
| 93 def get_lsusb_serial(device): | 93 def get_lsusb_serial(device): |
| 94 try: | 94 try: |
| 95 return device['Device Descriptor']['iSerial']['_desc'] | 95 return device['Device Descriptor']['iSerial']['_desc'] |
| 96 except KeyError: | 96 except KeyError: |
| 97 return None | 97 return None |
| 98 | 98 |
| 99 def get_android_devices(): | 99 def get_android_devices(): |
| 100 return [serial for serial in (get_lsusb_serial(d) for d in lsusb()) | 100 return [serial for serial in (get_lsusb_serial(d) for d in lsusb()) |
| 101 if serial] | 101 if serial] |
| OLD | NEW |