]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Fix typo in documentation for strip_base_path
[bup.git] / lib / bup / helpers.py
index cb378a97421cd53363b853e214eca51959b4314d..36b95602e9c8f2088ec9fd55d9693f47619b1d4f 100644 (file)
@@ -1,4 +1,5 @@
 """Helper functions and classes for bup."""
+
 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re
 from bup import _version
 
@@ -421,9 +422,9 @@ def strip_path(prefix, path):
     if prefix == None:
         raise Exception('no path given')
 
-    normalized_prefix = realpath(prefix)
+    normalized_prefix = os.path.realpath(prefix)
     debug2("normalized_prefix: %s\n" % normalized_prefix)
-    normalized_path = realpath(path)
+    normalized_path = os.path.realpath(path)
     debug2("normalized_path: %s\n" % normalized_path)
     if normalized_path.startswith(normalized_prefix):
         return normalized_path[len(normalized_prefix):]
@@ -433,17 +434,27 @@ def strip_path(prefix, path):
 def strip_base_path(path, base_paths):
     """Strips the base path from a given path.
 
-    Determines the base path for the given string and the strips it
+
+    Determines the base path for the given string and then strips it
     using strip_path().
     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
 
+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