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

vathpela installerbot-noreply at redhat.com
Fri Nov 6 19:20:11 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 | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/blivet/util.py b/blivet/util.py
index 56d0aee..6de35e2 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -213,6 +213,21 @@ 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 "//" in path:
+        path = path.replace("//", "/")
+    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:


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


More information about the anaconda-patches mailing list