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
3 # found in the LICENSE file.
8 from pylib
import cmd_helper
10 _INDENTATION_RE
= re
.compile(r
'^( *)')
11 _LSUSB_BUS_DEVICE_RE
= re
.compile(r
'^Bus (\d{3}) Device (\d{3}):')
12 _LSUSB_ENTRY_RE
= re
.compile(r
'^ *([^ ]+) +([^ ]+) *([^ ].*)?$')
13 _LSUSB_GROUP_RE
= re
.compile(r
'^ *([^ ]+.*):$')
15 _USBDEVFS_RESET
= ord('U') << 8 |
20
19 """Call lsusb and return the parsed output."""
20 lsusb_raw_output
= cmd_helper
.GetCmdOutput(['lsusb', '-v'])
24 for line
in lsusb_raw_output
.splitlines():
27 devices
.append(device
)
32 m
= _LSUSB_BUS_DEVICE_RE
.match(line
)
38 depth_stack
= [device
]
41 indent_match
= _INDENTATION_RE
.match(line
)
45 depth
= 1 + len(indent_match
.group(1)) / 2
46 if depth
> len(depth_stack
):
47 logging
.error('lsusb parsing error: unexpected indentation: "%s"', line
)
50 while depth
< len(depth_stack
):
55 m
= _LSUSB_GROUP_RE
.match(line
)
58 cur
[m
.group(1)] = new_group
59 depth_stack
.append(new_group
)
62 m
= _LSUSB_ENTRY_RE
.match(line
)
68 cur
[m
.group(1)] = new_entry
69 depth_stack
.append(new_entry
)
72 logging
.error('lsusb parsing error: unrecognized line: "%s"', line
)
75 devices
.append(device
)
80 def get_lsusb_serial(device
):
81 return device
.get('Device Descriptor', {}).get('iSerial', {}).get('_desc')
84 def get_android_devices():
85 return [serial
for serial
in (get_lsusb_serial(d
) for d
in lsusb())