mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-09-14 16:18:05 -04:00
Linux device detection: Parse /proc/bus/usb/devices directly instead of using libusb
This commit is contained in:
parent
fc9053e907
commit
841ccff5d2
@ -5,10 +5,9 @@ Device scanner that fetches list of devices on system ina platform dependent
|
|||||||
manner.
|
manner.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import sys
|
import sys, re, os
|
||||||
|
|
||||||
from calibre import iswindows, isosx, plugins
|
from calibre import iswindows, isosx, plugins
|
||||||
from calibre.devices import libusb
|
|
||||||
|
|
||||||
osx_scanner = win_scanner = linux_scanner = None
|
osx_scanner = win_scanner = linux_scanner = None
|
||||||
|
|
||||||
@ -22,16 +21,38 @@ elif isosx:
|
|||||||
osx_scanner = plugins['usbobserver'][0].get_usb_devices
|
osx_scanner = plugins['usbobserver'][0].get_usb_devices
|
||||||
except:
|
except:
|
||||||
raise RuntimeError('Failed to load the usbobserver plugin: %s'%plugins['usbobserver'][1])
|
raise RuntimeError('Failed to load the usbobserver plugin: %s'%plugins['usbobserver'][1])
|
||||||
else:
|
|
||||||
linux_scanner = libusb.get_devices
|
_usb_re = re.compile(r'Vendor\s*=\s*([0-9a-fA-F]+)\s+ProdID\s*=\s*([0-9a-fA-F]+)\s+Rev\s*=\s*([0-9a-fA-f.]+)')
|
||||||
|
_DEVICES = '/proc/bus/usb/devices'
|
||||||
|
|
||||||
|
|
||||||
|
def linux_scanner():
|
||||||
|
raw = open(_DEVICES).read()
|
||||||
|
devices = []
|
||||||
|
device = None
|
||||||
|
for x in raw.splitlines():
|
||||||
|
x = x.strip()
|
||||||
|
if x.startswith('T:'):
|
||||||
|
if device:
|
||||||
|
devices.append(device)
|
||||||
|
device = []
|
||||||
|
if device is not None and x.startswith('P:'):
|
||||||
|
match = _usb_re.search(x)
|
||||||
|
if match is not None:
|
||||||
|
ven, prod, bcd = match.group(1), match.group(2), match.group(3)
|
||||||
|
ven, prod, bcd = int(ven, 16), int(prod, 16), int(bcd.replace('.', ''), 16)
|
||||||
|
device = [ven, prod, bcd]
|
||||||
|
if device:
|
||||||
|
devices.append(device)
|
||||||
|
return devices
|
||||||
|
|
||||||
class DeviceScanner(object):
|
class DeviceScanner(object):
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
if isosx and osx_scanner is None:
|
if isosx and osx_scanner is None:
|
||||||
raise RuntimeError('The Python extension usbobserver must be available on OS X.')
|
raise RuntimeError('The Python extension usbobserver must be available on OS X.')
|
||||||
if not (isosx or iswindows) and not libusb.has_library():
|
if not (isosx or iswindows) and not os.access(_DEVICES, os.R_OK):
|
||||||
raise RuntimeError('DeviceScanner requires libusb to work.')
|
raise RuntimeError('DeviceScanner requires %s to work.'%_DEVICES)
|
||||||
self.scanner = win_scanner if iswindows else osx_scanner if isosx else linux_scanner
|
self.scanner = win_scanner if iswindows else osx_scanner if isosx else linux_scanner
|
||||||
self.devices = []
|
self.devices = []
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user