device.py 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. try:
  2. from .error import RenderError
  3. from .validations import valid_fs_path_or_raise, allowed_device_or_raise, valid_cgroup_perm_or_raise
  4. except ImportError:
  5. from error import RenderError
  6. from validations import valid_fs_path_or_raise, allowed_device_or_raise, valid_cgroup_perm_or_raise
  7. class Device:
  8. def __init__(self, host_device: str, container_device: str, cgroup_perm: str = "", allow_disallowed=False):
  9. hd = valid_fs_path_or_raise(host_device.rstrip("/"))
  10. cd = valid_fs_path_or_raise(container_device.rstrip("/"))
  11. if not hd or not cd:
  12. raise RenderError(
  13. "Expected [host_device] and [container_device] to be set. "
  14. f"Got host_device [{host_device}] and container_device [{container_device}]"
  15. )
  16. cgroup_perm = valid_cgroup_perm_or_raise(cgroup_perm)
  17. if not allow_disallowed:
  18. hd = allowed_device_or_raise(hd)
  19. self.cgroup_perm: str = cgroup_perm
  20. self.host_device: str = hd
  21. self.container_device: str = cd
  22. def render(self):
  23. result = f"{self.host_device}:{self.container_device}"
  24. if self.cgroup_perm:
  25. result += f":{self.cgroup_perm}"
  26. return result