def GetBusNumberToDeviceTreeMap(fast=True): """Gets devices currently attached. Args: fast [bool]: whether to do it fast (only get description, not the whole dictionary, from lsusb) Returns: map of {bus number: bus object} where the bus object has all the devices attached to it in a tree. """ if fast: info_map = {} for line in lsusb.raw_lsusb().splitlines(): match = _LSUSB_BUS_DEVICE_RE.match(line) if match: info_map[(int(match.group(1)), int(match.group(2)))] = ({ 'desc': match.group(3) }) else: info_map = {((int(line['bus']), int(line['device']))): line for line in _GetParsedLSUSBOutput()} tree = {} bus_num = -1 for line in _GetUSBDevicesOutput().splitlines(): match = _T_LINE_REGEX.match(line) if match: bus_num = int(match.group('bus')) parent_num = int(match.group('prnt')) # usb-devices starts counting ports from 0, so add 1 port_num = int(match.group('port')) + 1 device_num = int(match.group('dev')) # create new bus if necessary if bus_num not in tree: tree[bus_num] = USBBusNode(bus_num=bus_num) # create the new device new_device = USBDeviceNode(bus_num=bus_num, device_num=device_num, info=info_map[(bus_num, device_num)]) # add device to bus if parent_num != 0: tree[bus_num].FindDeviceNumber(parent_num).AddChild( port_num, new_device) else: tree[bus_num].AddChild(port_num, new_device) match = _S_LINE_REGEX.match(line) if match: if bus_num == -1: raise ValueError('S line appears before T line in input file') # put the serial number in the device tree[bus_num].FindDeviceNumber(device_num).serial = match.group( 'serial') return tree
def GetBusNumberToDeviceTreeMap(fast=True): """Gets devices currently attached. Args: fast [bool]: whether to do it fast (only get description, not the whole dictionary, from lsusb) Returns: map of {bus number: bus object} where the bus object has all the devices attached to it in a tree. """ if fast: info_map = {} for line in lsusb.raw_lsusb().splitlines(): match = _LSUSB_BUS_DEVICE_RE.match(line) if match: info_map[(int(match.group(1)), int(match.group(2)))] = ( {'desc':match.group(3)}) else: info_map = {((int(line['bus']), int(line['device']))): line for line in _GetParsedLSUSBOutput()} tree = {} bus_num = -1 for line in _GetUSBDevicesOutput().splitlines(): match = _T_LINE_REGEX.match(line) if match: bus_num = int(match.group('bus')) parent_num = int(match.group('prnt')) # usb-devices starts counting ports from 0, so add 1 port_num = int(match.group('port')) + 1 device_num = int(match.group('dev')) # create new bus if necessary if bus_num not in tree: tree[bus_num] = USBBusNode(bus_num=bus_num) # create the new device new_device = USBDeviceNode(bus_num=bus_num, device_num=device_num, info=info_map.get((bus_num, device_num), {'desc': 'NOT AVAILABLE'})) # add device to bus if parent_num != 0: tree[bus_num].FindDeviceNumber(parent_num).AddChild( port_num, new_device) else: tree[bus_num].AddChild(port_num, new_device) match = _S_LINE_REGEX.match(line) if match: if bus_num == -1: raise ValueError('S line appears before T line in input file') # put the serial number in the device tree[bus_num].FindDeviceNumber(device_num).serial = match.group('serial') return tree