]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Adds a strip and strip-path option to bup save.
[bup.git] / lib / bup / helpers.py
index cb7854338ead99ad13358ad5ec41617dfb31e121..3c78c1890945b96046a9759061564e988b51805b 100644 (file)
@@ -411,6 +411,39 @@ def parse_date_or_fatal(str, fatal):
     else:
         return date
 
+def strip_path(prefix, path):
+    """Strips a given prefix from a path.
+
+    First both paths are normalized.
+
+    Raises an Exception if no prefix is given.
+    """
+    if prefix == None:
+        raise Exception('no path given')
+
+    normalized_prefix = realpath(prefix)
+    print "normalized_prefix: " + normalized_prefix
+    normalized_path = realpath(path)
+    print "normalized_path: " + normalized_path
+    if normalized_path.startswith(normalized_prefix):
+        return normalized_path[len(normalized_prefix):]
+    else:
+        return 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
+    using strip_path().
+    Iterates over all base_paths from long to short, to prevent that
+    a too short base_path is removed.
+    """
+    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)
+    return 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