[master 1/10] Add a much much simpler path normalizer and joiner.

vathpela installerbot-noreply at redhat.com
Wed Oct 28 17:42:40 UTC 2015


From: Peter Jones <pjones at redhat.com>

I'm just so, so tired of futzing around with os.path.join() and
os.path.normalize() not doing their jobs usefully.

This version of normalize doesn't try to resolve .. or symlinks; it only
de-dupes slashes.  I can't find anywhere we depend on resolving ..  or
symlinks, so it's just not as pretty as it could be.

So basically if you need to normalize slashes, use
util.normalize_path_slashes() .  If you need to resolve symlinks, use
os.path.normalize()

Signed-off-by: Peter Jones <pjones at redhat.com>
---
 blivet/util.py | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/blivet/util.py b/blivet/util.py
index 0f43b2c..344e582 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -200,6 +200,26 @@ def notify_kernel(path, action="change"):
     f.write("%s\n" % action)
     f.close()
 
+def normalize_path_slashes(path):
+    """ Normalize the slashes in a filesystem path.
+        Does not actually examine the filesystme in any way.
+    """
+    while True:
+        idx = path.find("//")
+        if idx < 0:
+            break
+        result = path[0:idx]
+        path = result + path[idx+1:]
+    return path
+
+def join_paths(*paths):
+    """ Joins filesystem paths without any consiration of slashes or
+        whatnot and then normalizes repeated slashes.
+    """
+    if len(paths) == 1 and hasattr(paths[0], "__iter__"):
+        return join_paths(*paths[0])
+    return normalize_path_slashes('/'.join(paths))
+
 def get_sysfs_attr(path, attr):
     if not attr:
         log.debug("get_sysfs_attr() called with attr=None")


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


More information about the anaconda-patches mailing list