Skip to content

Commit

Permalink
Add a much much simpler path normalizer and joiner.
Browse files Browse the repository at this point in the history
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@redhat.com>
  • Loading branch information
vathpela committed Nov 6, 2015
1 parent be12df0 commit fd6c33e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions blivet/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/devicelibs_test/edd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setUp(self):
edd.log.addHandler(self.log_handler)

self.td_log_handler = logging.FileHandler("%s/%s" %
(ws, "blivet-edd-testdata.log"))
(ws, "blivet-edd-testdata.log"))
self.td_log_handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
self.td_log_handler.setFormatter(formatter)
Expand Down

0 comments on commit fd6c33e

Please sign in to comment.