From 2447172125fbae34f2641edcf988b692fcf217cf Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Mon, 4 Oct 2021 17:42:13 +0000 Subject: [PATCH] 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. --- osbuild/devices.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/osbuild/devices.py b/osbuild/devices.py index 09225bbf..9cd6ecbb 100644 --- a/osbuild/devices.py +++ b/osbuild/devices.py @@ -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