def test_get_device_type_no_device_file(tmpdir): filename = tmpdir.join('test') filename.ensure(file=True) with pytest.raises(ValueError) as excinfo: _util.get_device_type(str(filename)) message = 'not a device file: {0!r}'.format(str(filename)) assert str(excinfo.value) == message
def test_get_device_type_not_existing(tmpdir): """ Test that an OSError is raised when checking device type using a file that does not actually exist. """ filename = tmpdir.join('test_get_device_type_not_existing') assert not tmpdir.check(file=True) with pytest.raises(OSError): _util.get_device_type(str(filename))
def from_device_file(cls, context, filename): """ Create a new device from the given device file: >>> context = Context() >>> device = Device.from_device_file(context, '/dev/sda') >>> device Device(u'/sys/devices/pci0000:00/0000:00:0d.0/host2/target2:0:0/2:0:0:0/block/sda') >>> device.device_node u'/dev/sda' .. warning:: Though the example seems to suggest that ``device.device_node == filename`` holds with ``device = Device.from_device_file(context, filename)``, this is only true in a majority of cases. There *can* be devices, for which this relation is actually false! Thus, do *not* expect :attr:`~Device.device_node` to be equal to the given ``filename`` for the returned :class:`Device`. Especially, use :attr:`~Device.device_node` if you need the device file of a :class:`Device` created with this method afterwards. ``context`` is the :class:`Context` in which to search the device. ``filename`` is a string containing the path of a device file. Return a :class:`Device` representing the given device file. Raise :exc:`~exceptions.ValueError` if ``filename`` is no device file at all. Raise :exc:`~exceptions.EnvironmentError` if ``filename`` does not exist or if its metadata was inaccessible. .. versionadded:: 0.15 """ device_type = get_device_type(filename) device_number = os.stat(filename).st_rdev return cls.from_device_number(context, device_type, device_number)
def from_device_file(cls, context, filename): """ Create a new device from the given device file: >>> from pyudev import Context, Device >>> context = Context() >>> device = Devices.from_device_file(context, '/dev/sda') >>> device Device(u'/sys/devices/pci0000:00/0000:00:0d.0/host2/target2:0:0/2:0:0:0/block/sda') >>> device.device_node u'/dev/sda' .. warning:: Though the example seems to suggest that ``device.device_node == filename`` holds with ``device = Devices.from_device_file(context, filename)``, this is only true in a majority of cases. There *can* be devices, for which this relation is actually false! Thus, do *not* expect :attr:`~Device.device_node` to be equal to the given ``filename`` for the returned :class:`Device`. Especially, use :attr:`~Device.device_node` if you need the device file of a :class:`Device` created with this method afterwards. ``context`` is the :class:`Context` in which to search the device. ``filename`` is a string containing the path of a device file. Return a :class:`Device` representing the given device file. Raise :exc:`DeviceNotFoundByFileError` if ``filename`` is no device file at all or if ``filename`` does not exist or if its metadata was inaccessible. .. versionadded:: 0.18 """ try: device_type = get_device_type(filename) device_number = os.stat(filename).st_rdev except (EnvironmentError, ValueError) as err: raise DeviceNotFoundByFileError(err) return cls.from_device_number(context, device_type, device_number)
def test_get_device_type_block_device(a_device): """ Check that the device type of a block device is actually block. """ assert _util.get_device_type(a_device.device_node) == 'block'
def test_get_device_type_character_device(a_device): """ Check that the device type of a character device is actually char. """ assert _util.get_device_type(a_device.device_node) == 'char'
def test_get_device_type_not_existing(tmpdir): filename = tmpdir.join('test') assert not tmpdir.check(file=True) with pytest.raises(EnvironmentError) as excinfo: _util.get_device_type(str(filename)) pytest.assert_env_error(excinfo.value, errno.ENOENT, str(filename))
def test_get_device_type_block_device(): assert _util.get_device_type('/dev/sda') == 'block'
def test_get_device_type_character_device(): assert _util.get_device_type('/dev/console') == 'char'
def test_get_device_type_block_device(): try: assert _util.get_device_type("/dev/sda") == "block" except EnvironmentError: pytest.skip("device node not found")
def test_get_device_type_character_device(): assert _util.get_device_type("/dev/console") == "char"
def test_get_device_type_block_device(): try: assert _util.get_device_type('/dev/sda') == 'block' except EnvironmentError: pytest.skip('device node not found')