[master 4/13] Make get_sysfs_attr() use our path joiners and add sysfs_readlink()

vathpela installerbot-noreply at redhat.com
Fri Nov 6 20:00:33 UTC 2015


From: Peter Jones <pjones at redhat.com>

Because seriously, code shouldn't have to care about if we do
get_sysfs_attr("/sys/foo/","bar") or get_sysfs_attr("/sys/foo","/bar")

This also makes these functions able to deal with an alternate root
directory separately from the sysfs path.

Signed-off-by: Peter Jones <pjones at redhat.com>
---
 blivet/devicelibs/edd.py | 12 ++++++------
 blivet/util.py           | 32 +++++++++++++++++++++++++++-----
 2 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/blivet/devicelibs/edd.py b/blivet/devicelibs/edd.py
index de11500..1112771 100644
--- a/blivet/devicelibs/edd.py
+++ b/blivet/devicelibs/edd.py
@@ -80,7 +80,7 @@ def __init__(self, sysfspath):
             that this is a particular device.  Used for logging later.
         """
 
-        self.version = util.get_sysfs_attr(self.sysfspath, "version")
+        self.version = util.get_sysfs_attr(self.sysfspath, "version", root=fsroot)
         """ The edd version this entry claims conformance with, from
             /sys/firmware/edd/int13_devXX/version """
 
@@ -239,7 +239,7 @@ def __repr__(self):
         return "<EddEntry%s>" % (self._fmt(' ', ''),)
 
     def load(self):
-        interface = util.get_sysfs_attr(self.sysfspath, "interface")
+        interface = util.get_sysfs_attr(self.sysfspath, "interface", root=fsroot)
         # save this so we can log it from the matcher.
         self.interface = interface
         if interface:
@@ -302,11 +302,11 @@ def load(self):
                 else:
                     raise e
 
-        self.mbr_sig = util.get_sysfs_attr(self.sysfspath, "mbr_signature")
-        sectors = util.get_sysfs_attr(self.sysfspath, "sectors")
+        self.mbr_sig = util.get_sysfs_attr(self.sysfspath, "mbr_signature", root=fsroot)
+        sectors = util.get_sysfs_attr(self.sysfspath, "sectors", root=fsroot)
         if sectors:
             self.sectors = int(sectors)
-        hbus = util.get_sysfs_attr(self.sysfspath, "host_bus")
+        hbus = util.get_sysfs_attr(self.sysfspath, "host_bus", root=fsroot)
         if hbus:
             match = re_host_bus_pci.match(hbus)
             if match:
@@ -372,7 +372,7 @@ def devname_from_ata_pci_dev(self):
             ata_port_idx = int(components[5][3:])
 
             fn = components[0:6] + ['ata_port', ata_port]
-            port_no = int(util.get_sysfs_attr(os.path.join(*fn), 'port_no'))
+            port_no = int(util.get_sysfs_attr(os.path.join(*fn), 'port_no', root=fsroot))
 
             if self.edd.type == "ATA":
                 # On ATA, port_no is kernel's ata_port->local_port_no, which
diff --git a/blivet/util.py b/blivet/util.py
index 0f52845..d1d81c7 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -339,25 +339,47 @@ def join_paths(*paths):
         return join_paths(*paths[0])
     return normalize_path_slashes('/'.join(paths))
 
-def get_sysfs_attr(path, attr):
+def get_sysfs_attr(path, attr, root=None):
     if not attr:
         log.debug("get_sysfs_attr() called with attr=None")
         return None
+    if not isinstance(path, Path):
+        path = Path(path=path, root=root)
+    elif root != None:
+        path.newroot(root)
 
-    attribute = "%s/%s" % (path, attr)
-    attribute = os.path.realpath(attribute)
+    attribute = path + attr
+    fullattr = os.path.realpath(attribute.ondisk)
 
-    if not os.path.isfile(attribute) and not os.path.islink(attribute):
+    if not os.path.isfile(fullattr) and not os.path.islink(fullattr):
         log.warning("%s is not a valid attribute", attr)
         return None
 
-    f = open(attribute, "r")
+    f = open(fullattr, "r")
     data = f.read()
     f.close()
     sdata = "".join(["%02x" % (ord(x),) for x in data])
     testdata_log.debug("sysfs attr %s: %s", attribute, sdata)
     return data.strip()
 
+def sysfs_readlink(path, link, root=None):
+    if not link:
+        log.debug("sysfs_readlink() called with link=None")
+    if isinstance(path, Path):
+        linkpath = path + link
+    else:
+        linkpath = Path(path, root=root) + link
+
+    linkpath = Path(os.path.normpath(linkpath), root=linkpath.root)
+    fullpath = os.path.normpath(linkpath.ondisk)
+
+    if not os.path.islink(fullpath):
+        log.warning("%s is not a valid symlink", linkpath)
+        return None
+
+    output = os.readlink(fullpath)
+    testdata_log.debug("new sysfs link: \"%s\" -> \"%s\"", linkpath, output)
+    return output
 
 def get_sysfs_path_by_name(dev_node, class_name="block"):
     """ Return sysfs path for a given device.


-- 
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/65bd2713eaf077abc975fef39f04ac0f865de844


More information about the anaconda-patches mailing list