Iterates over all base_paths from long to short, to prevent that
a too short base_path is removed.
"""
+ normalized_path = os.path.realpath(path)
sorted_base_paths = sorted(base_paths, key=len, reverse=True)
for bp in sorted_base_paths:
- if path.startswith(realpath(bp)):
- return strip_path(bp, path)
+ if normalized_path.startswith(os.path.realpath(bp)):
+ return strip_path(bp, normalized_path)
return path
- normalized_path = realpath(path)
+ def graft_path(graft_points, path):
++ normalized_path = os.path.realpath(path)
+ for graft_point in graft_points:
+ old_prefix, new_prefix = graft_point
+ if normalized_path.startswith(old_prefix):
+ return re.sub(r'^' + old_prefix, new_prefix, normalized_path)
+ return normalized_path
+
# hashlib is only available in python 2.5 or higher, but the 'sha' module
# produces a DeprecationWarning in python 2.6 or higher. We want to support
base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
WVPASSEQ(strip_base_path(path, base_paths), '/etc')
+@wvtest
+def test_strip_symlinked_base_path():
+ tmpdir = os.path.join(os.getcwd(),"test_strip_symlinked_base_path.tmp")
+ symlink_src = os.path.join(tmpdir, "private", "var")
+ symlink_dst = os.path.join(tmpdir, "var")
+ path = os.path.join(symlink_dst, "a")
+
+ os.mkdir(tmpdir)
+ os.mkdir(os.path.join(tmpdir, "private"))
+ os.mkdir(symlink_src)
+ os.symlink(symlink_src, symlink_dst)
+
+ result = strip_base_path(path, [symlink_dst])
+
+ os.remove(symlink_dst)
+ os.rmdir(symlink_src)
+ os.rmdir(os.path.join(tmpdir, "private"))
+ os.rmdir(tmpdir)
+
+ WVPASSEQ(result, "/a")
+
+ @wvtest
+ def test_graft_path():
+ middle_matching_old_path = "/user"
+ non_matching_old_path = "/usr"
+ matching_old_path = "/home"
+ matching_full_path = "/home/user"
+ new_path = "/opt"
+
+ all_graft_points = [(middle_matching_old_path, new_path),
+ (non_matching_old_path, new_path),
+ (matching_old_path, new_path)]
+
+ path = "/home/user/"
+
+ WVPASSEQ(graft_path([(middle_matching_old_path, new_path)], path),
+ "/home/user")
+ WVPASSEQ(graft_path([(non_matching_old_path, new_path)], path),
+ "/home/user")
+ WVPASSEQ(graft_path([(matching_old_path, new_path)], path), "/opt/user")
+ WVPASSEQ(graft_path(all_graft_points, path), "/opt/user")
+ WVPASSEQ(graft_path([(matching_full_path, new_path)], path),
+ "/opt")