|
|
|
@ -6,8 +6,12 @@ import re
|
|
|
|
|
import serial.tools.list_ports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_USB_devices():
|
|
|
|
|
def list_USB_devices(full=True):
|
|
|
|
|
"""
|
|
|
|
|
Return a list of USB devices as a dict.
|
|
|
|
|
|
|
|
|
|
if full is True, the name is Manufacturer_SerialNumber
|
|
|
|
|
otherwise, just Manufacturer
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
full_pattern = re.compile('[^a-zA-Z0-9\\\/]|_')
|
|
|
|
@ -16,45 +20,45 @@ def list_USB_devices():
|
|
|
|
|
for p in ports:
|
|
|
|
|
if p.manufacturer:
|
|
|
|
|
key = p.manufacturer
|
|
|
|
|
if p.serial_number:
|
|
|
|
|
if p.serial_number and full:
|
|
|
|
|
key += '_' + p.serial_number
|
|
|
|
|
key = re.sub(full_pattern, '_', key)
|
|
|
|
|
result[key] = p.device
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_USB_devices_old():
|
|
|
|
|
result = {}
|
|
|
|
|
path = Path('/sys/bus/usb/devices/')
|
|
|
|
|
|
|
|
|
|
for syspath in path.glob('usb*'):
|
|
|
|
|
for devfile in syspath.glob('**/dev'):
|
|
|
|
|
if devfile.is_file():
|
|
|
|
|
devpath = devfile.parent
|
|
|
|
|
# Get devnames
|
|
|
|
|
proc = subprocess.Popen(['udevadm', 'info', '-q', 'name', '-p', devpath],
|
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
|
output = proc.stdout.read()
|
|
|
|
|
devname = output.decode('utf-8')
|
|
|
|
|
if not devname.startswith('bus') and not devname.startswith('input'):
|
|
|
|
|
# get properties
|
|
|
|
|
proc = subprocess.Popen(['udevadm', 'info', '-q', 'property', '--export', '-p', devpath],
|
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
|
output = proc.stdout.read()
|
|
|
|
|
output = output.decode('utf-8')
|
|
|
|
|
# Not nice, but convert the output into dict
|
|
|
|
|
output = output.replace("='", "\':\'")
|
|
|
|
|
output = re.sub('^', "\'", output, flags=re.MULTILINE)
|
|
|
|
|
output = '{' + output[:-1].replace('\n', ', ') + '}'
|
|
|
|
|
properties = ast.literal_eval(output)
|
|
|
|
|
if 'ID_SERIAL' in properties:
|
|
|
|
|
p = str(path.joinpath('/dev', devname))
|
|
|
|
|
p = p.replace('\n', '')
|
|
|
|
|
result[properties['ID_SERIAL']] = p
|
|
|
|
|
return result
|
|
|
|
|
#def list_USB_devices_old():
|
|
|
|
|
# result = {}
|
|
|
|
|
# path = Path('/sys/bus/usb/devices/')
|
|
|
|
|
#
|
|
|
|
|
# for syspath in path.glob('usb*'):
|
|
|
|
|
# for devfile in syspath.glob('**/dev'):
|
|
|
|
|
# if devfile.is_file():
|
|
|
|
|
# devpath = devfile.parent
|
|
|
|
|
# # Get devnames
|
|
|
|
|
# proc = subprocess.Popen(['udevadm', 'info', '-q', 'name', '-p', devpath],
|
|
|
|
|
# stdout=subprocess.PIPE)
|
|
|
|
|
# output = proc.stdout.read()
|
|
|
|
|
# devname = output.decode('utf-8')
|
|
|
|
|
# if not devname.startswith('bus') and not devname.startswith('input'):
|
|
|
|
|
# # get properties
|
|
|
|
|
# proc = subprocess.Popen(['udevadm', 'info', '-q', 'property', '--export', '-p', devpath],
|
|
|
|
|
# stdout=subprocess.PIPE)
|
|
|
|
|
# output = proc.stdout.read()
|
|
|
|
|
# output = output.decode('utf-8')
|
|
|
|
|
# # Not nice, but convert the output into dict
|
|
|
|
|
# output = output.replace("='", "\':\'")
|
|
|
|
|
# output = re.sub('^', "\'", output, flags=re.MULTILINE)
|
|
|
|
|
# output = '{' + output[:-1].replace('\n', ', ') + '}'
|
|
|
|
|
# properties = ast.literal_eval(output)
|
|
|
|
|
# if 'ID_SERIAL' in properties:
|
|
|
|
|
# p = str(path.joinpath('/dev', devname))
|
|
|
|
|
# p = p.replace('\n', '')
|
|
|
|
|
# result[properties['ID_SERIAL']] = p
|
|
|
|
|
# return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_USB_devices(devices):
|
|
|
|
|
def update_USB_devices(devices, fullname=True):
|
|
|
|
|
"""
|
|
|
|
|
Find paths for devices.
|
|
|
|
|
|
|
|
|
@ -62,8 +66,9 @@ def update_USB_devices(devices):
|
|
|
|
|
detection, and bind the path.
|
|
|
|
|
|
|
|
|
|
devices: dictionnary
|
|
|
|
|
fullname: if True, Manufacturer_SN, else Manufacturer.
|
|
|
|
|
"""
|
|
|
|
|
res = list_USB_devices()
|
|
|
|
|
res = list_USB_devices(full=fullname)
|
|
|
|
|
for device in devices:
|
|
|
|
|
name = devices[device]['name']
|
|
|
|
|
if name in res:
|
|
|
|
|