devices: add device path helper functions

Add new helper functions that can translate from a managed device
to its path. One is relative and one is the absolute path on the
host, i.e. to the device node on the host.
This commit is contained in:
Christian Kellner 2021-10-04 17:42:13 +00:00 committed by Tom Gundersen
parent a5e07cf506
commit 2447172125

View file

@ -15,8 +15,9 @@ support in osbuild itself is abstract.
import abc
import hashlib
import json
import os
from typing import Dict
from typing import Dict, Optional
from osbuild import host
@ -57,12 +58,20 @@ class DeviceManager:
self.tree = tree
self.devices = {}
def device_relpath(self, dev: Optional[Device]) -> Optional[str]:
if dev is None:
return None
return self.devices[dev.name]["path"]
def device_abspath(self, dev: Optional[Device]) -> Optional[str]:
relpath = self.device_relpath(dev)
if relpath is None:
return None
return os.path.join(self.devpath, relpath)
def open(self, dev: Device) -> Dict:
if dev.parent:
parent = self.devices[dev.parent.name]["path"]
else:
parent = None
parent = self.device_relpath(dev.parent)
args = {
# global options