def test_from_device_file_no_device_file(self, tmpdir, a_context): filename = tmpdir.join('test') filename.ensure(file=True) with pytest.raises(DeviceNotFoundByFileError) as excinfo: Devices.from_device_file(a_context, str(filename)) message = 'not a device file: {0!r}'.format(str(filename)) assert str(excinfo.value) == message
def test_from_device_file_no_device_file(self, tmpdir, a_context): filename = tmpdir.join('test') filename.ensure(file=True) with pytest.raises(DeviceNotFoundByFileError) as excinfo: Devices.from_device_file(a_context, str(filename)) message = 'not a device file: {0!r}'.format(str(filename)) assert str(excinfo.value) == message
def test_from_device_file_non_existing(self, tmpdir): """ Test that an OSError is raised when constructing a ``Device`` from a file that does not actually exist. """ filename = tmpdir.join('test_from_device_file_non_existing') assert not tmpdir.check(file=True) with pytest.raises(DeviceNotFoundByFileError): Devices.from_device_file(_CONTEXT, str(filename))
def test_from_device_file_no_device_file(self, tmpdir): """ Verify that a file that is not a device file will cause an exception to be raised. """ filename = tmpdir.join('test') filename.ensure(file=True) with pytest.raises(DeviceNotFoundByFileError): Devices.from_device_file(_CONTEXT, str(filename))
def test_from_device_file_non_existing(self, tmpdir, a_context): """ Test that an OSError is raised when constructing a ``Device`` from a file that does not actually exist. """ filename = tmpdir.join('test_from_device_file_non_existing') assert not tmpdir.check(file=True) with pytest.raises(DeviceNotFoundByFileError): Devices.from_device_file(a_context, str(filename))
def test_from_device_file(self, a_context, device_datum): device = Devices.from_device_file( a_context, device_datum.device_node ) assert device.device_node == device_datum.device_node assert device.device_path == device_datum.device_path
def findGoodWeUSBDevice(self, vendorId, modelId): context = Context() # directory = "/dev/bus/usb/001/" directory = "/dev/" usb_list = [d for d in os.listdir("/dev") if d.startswith("hidraw")] # usb_list = [d for d in os.listdir(directory)] self.log.debug(usb_list) for hidraw in usb_list: # device = directory + hidraw device = "/dev/" + hidraw try: udev = Devices.from_device_file(context, device) self.log.debug(udev['DEVPATH']) if udev['DEVPATH'].find(vendorId + ":" + modelId) > -1: self.log.debug("Using: %s", hidraw) return device except Exception as e: self.log.debug(e) pass return None
def test_from_device_file(self, a_context, device_datum): device = Devices.from_device_file( a_context, device_datum.device_node ) assert device.device_node == device_datum.device_node assert device.device_path == device_datum.device_path
def __isCDInserted(self) -> bool: # using another udev context - running in a different thread try: context = pyudev.Context() device = Devices.from_device_file(context, DEV_CDROM) return MEDIA_TRACK_COUNT_AUDIO_UDEV_TAG in device except Exception: return False
def findGoodWeUSBDevice(self, vendorId, modelId): context = Context() usb_list = [d for d in os.listdir("/dev") if d.startswith("hidraw")] for hidraw in usb_list: device = "/dev/" + hidraw udev = Devices.from_device_file(context, device) if udev['DEVPATH'].find(vendorId + ":" + modelId) > -1: return device return None
def _find_usb_device(self): logger.debug('Using pyudev to find GMC tty device') context = Context() for devname in iglob('/dev/ttyUSB*'): device = Devices.from_device_file(context, devname) if device.properties['ID_BUS'] != 'usb': continue k = (device.properties['ID_VENDOR_ID'], device.properties['ID_MODEL_ID'], device.properties['ID_REVISION']) if k in self._gmc_vendor_model_revision: logger.debug('Found GMC-500+ at: %s', devname) return devname return None
def mount_and_add_iso(self, iso_path: str): """ """ if (os.path.isfile(iso_path) and os.path.splitext(iso_path)[1] == '.iso'): result = run(['udisksctl', 'loop-setup', '-f', iso_path], check=True, text=True, stdout=PIPE, stderr=PIPE, universal_newlines=True).stdout.strip() result = str.split(result, ' ')[-1][:-1] device = Devices.from_device_file(self.context, result) self.add(device) self.toggle_mount(device) self.layoutChanged.emit()
def find_paired_node(receiver_path, index, timeout): """Find the node of a device paired with a receiver""" context = _Context() receiver_phys = _Devices.from_device_file(context, receiver_path).find_parent('hid').get('HID_PHYS') if not receiver_phys: return None phys = f'{receiver_phys}:{index}' timeout += _timestamp() delta = _timestamp() while delta < timeout: for dev in context.list_devices(subsystem='hidraw'): dev_phys = dev.find_parent('hid').get('HID_PHYS') if dev_phys and dev_phys == phys: return dev.device_node delta = _timestamp() return None
def find_paired_node_wpid(receiver_path, index): """Find the node of a device paired with a receiver, get wpid from udev""" context = _Context() receiver_phys = _Devices.from_device_file(context, receiver_path).find_parent('hid').get('HID_PHYS') if not receiver_phys: return None phys = f'{receiver_phys}:{index}' for dev in context.list_devices(subsystem='hidraw'): dev_phys = dev.find_parent('hid').get('HID_PHYS') if dev_phys and dev_phys == phys: # get hid id like 0003:0000046D:00000065 hid_id = dev.find_parent('hid').get('HID_ID') # get wpid - last 4 symbols udev_wpid = hid_id[-4:] return udev_wpid return None
def test_from_device_file_links(self, a_context, a_device): """ For each link in DEVLINKS, test that the constructed device's path matches the orginal devices path. This does not hold true in the case of multipath. In this case udevadm's DEVLINKS fields holds some links that do not actually point to the originating device. See: https://bugzilla.redhat.com/show_bug.cgi?id=1263441. """ assume(not 'DM_MULTIPATH_TIMESTAMP' in a_device.properties) for link in a_device.device_links: link = os.path.join(a_context.device_path, link) device = Devices.from_device_file(a_context, link) assert device == a_device assert link in device.device_links
def test_from_device_file_links(self, a_context, device_datum): """ For each link in DEVLINKS, test that the constructed device's path matches the orginal devices path. This does not hold true in the case of multipath. In this case udevadm's DEVLINKS fields holds some links that do not actually point to the originating device. See: https://bugzilla.redhat.com/show_bug.cgi?id=1263441. """ device = Devices.from_path(a_context, device_datum.device_path) assume(not 'DM_MULTIPATH_TIMESTAMP' in device) for link in device_datum.device_links: link = os.path.join(a_context.device_path, link) device = Devices.from_device_file(a_context, link) assert device.device_path == device_datum.device_path assert link in device.device_links
def test_from_device_file(self, a_context, a_device): """ Verify that from_device_file() yields correct device. """ device = Devices.from_device_file(a_context, a_device.device_node) assert a_device == device
{{ "%-16s" | format(d.device) }} {{ "%-19s" | format(d.model)}} {{d.vendor}} {%- endfor %} """ data = {'devices': []} base_directory = '/dev/serial/by-bus' if not os.path.isdir(base_directory): print("No USB to serial converter connected") exit(0) context = Context() for root, dirs, files in os.walk(base_directory): for basename in files: os.path.join(root, basename) device = Devices.from_device_file(context, os.path.join(root, basename)) tmp = { 'device': basename, 'model': device.properties.get('ID_MODEL'), 'vendor': device.properties.get('ID_VENDOR_FROM_DATABASE') } data['devices'].append(tmp) data['devices'] = sorted(data['devices'], key=lambda i: i['device']) tmpl = Template(OUT_TMPL_SRC) print(tmpl.render(data)) exit(0)