devices.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import re
  24. class DeviceCollection(object):
  25. '''Bag for devices.
  26. Used as default value for :py:meth:`DeviceManager.__missing__` factory.
  27. :param vm: VM for which we manage devices
  28. :param class_: device class
  29. '''
  30. def __init__(self, vm, class_):
  31. self._vm = vm
  32. self._class = class_
  33. self._set = set()
  34. def attach(self, device):
  35. '''Attach (add) device to domain.
  36. :param str device: device identifier (format is class-dependent)
  37. '''
  38. if device in self:
  39. raise KeyError(
  40. 'device {!r} of class {} already attached to {!r}'.format(
  41. device, self._class, self._vm))
  42. self._vm.fire_event_pre('device-pre-attach:{}'.format(self._class),
  43. device)
  44. self._set.add(device)
  45. self._vm.fire_event('device-attach:{}'.format(self._class), device)
  46. def detach(self, device):
  47. '''Detach (remove) device from domain.
  48. :param str device: device identifier (format is class-dependent)
  49. '''
  50. if device not in self:
  51. raise KeyError(
  52. 'device {!r} of class {} not attached to {!r}'.format(
  53. device, self._class, self._vm))
  54. self._vm.fire_event_pre('device-pre-detach:{}'.format(self._class),
  55. device)
  56. self._set.remove(device)
  57. self._vm.fire_event('device-detach:{}'.format(self._class), device)
  58. def __iter__(self):
  59. return iter(self._set)
  60. def __contains__(self, item):
  61. return item in self._set
  62. def __len__(self):
  63. return len(self._set)
  64. class DeviceManager(dict):
  65. '''Device manager that hold all devices by their classess.
  66. :param vm: VM for which we manage devices
  67. '''
  68. def __init__(self, vm):
  69. super(DeviceManager, self).__init__()
  70. self._vm = vm
  71. def __missing__(self, key):
  72. self[key] = DeviceCollection(self._vm, key)
  73. return self[key]
  74. class RegexDevice(str):
  75. def __init__(self, *args, **kwargs):
  76. super(RegexDevice, self).__init__(*args, **kwargs)
  77. dev_match = self.regex.match(self)
  78. if not dev_match:
  79. raise ValueError('Invalid device identifier: {!r}'.format(self))
  80. for group in self.regex.groupindex:
  81. setattr(self, group, dev_match.group(group))
  82. class PCIDevice(RegexDevice):
  83. regex = re.compile(
  84. r'^(?P<bus>[0-9a-f]+):(?P<device>[0-9a-f]+)\.(?P<function>[0-9a-f]+)$')