]> arthur.barton.de Git - bup.git/commitdiff
Adds a strip and strip-path option to bup save.
authorZoran Zaric <zz@zoranzaric.de>
Mon, 6 Dec 2010 12:00:02 +0000 (13:00 +0100)
committerAvery Pennarun <apenwarr@gmail.com>
Mon, 3 Jan 2011 04:52:33 +0000 (20:52 -0800)
If the strip option is given bup uses all given filenames as base paths
and tries to stripe them from long to short.

If the strip-path option is given bup strip the given prefix from all
paths.

Signed-off-by: Zoran Zaric <zz@zoranzaric.de>
cmd/save-cmd.py
lib/bup/helpers.py
lib/bup/t/thelpers.py

index df51a6c74934c8aca99631e70122752e9986c815..d8c73cc711222fbf6195a59ed5389b949dd564fc 100755 (executable)
@@ -16,6 +16,8 @@ v,verbose  increase log output (can be used more than once)
 q,quiet    don't show progress meter
 smaller=   only back up files smaller than n bytes
 bwlimit=   maximum bytes/sec to transmit to server
+strip      strips the path to every filename given
+strip-path= path-prefix to be stripped when saving
 """
 o = options.Options('bup save', optspec)
 (opt, flags, extra) = o.parse(sys.argv[1:])
@@ -36,6 +38,9 @@ if opt.date:
 else:
     date = time.time()
 
+if opt.strip and opt.strip_path:
+    o.fatal("--strip is incompatible with --strip-path")
+
 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
 if is_reverse and opt.remote:
     o.fatal("don't use -r in reverse mode; it's automatic")
@@ -195,7 +200,13 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
         continue
 
     assert(dir.startswith('/'))
-    dirp = dir.split('/')
+    if opt.strip:
+        stripped_base_path = strip_base_path(dir, extra)
+        dirp = stripped_base_path.split('/')
+    elif opt.strip_path:
+        dirp = strip_path(opt.strip_path, dir).split('/')
+    else:
+        dirp = dir.split('/')
     while parts > dirp:
         _pop(force_tree = None)
     if dir != '/':
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
index 9f24962644fb25072c27879968f98e38ee62fba8..306d39b943118b3dc0698354fd00e518ac3b2a9b 100644 (file)
@@ -10,3 +10,21 @@ def test_parse_num():
     WVPASSEQ(pn('2 gb'), 2*1024*1024*1024)
     WVPASSEQ(pn('1e+9 k'), 1000000000 * 1024)
     WVPASSEQ(pn('-3e-3mb'), int(-0.003 * 1024 * 1024))
+
+@wvtest
+def test_strip_path():
+    prefix = "/var/backup/daily.0/localhost"
+    empty_prefix = ""
+    non_matching_prefix = "/home"
+    path = "/var/backup/daily.0/localhost/etc/"
+
+    WVPASSEQ(strip_path(prefix, path), '/etc')
+    WVPASSEQ(strip_path(empty_prefix, path), path)
+    WVPASSEQ(strip_path(non_matching_prefix, path), path)
+    WVEXCEPT(Exception, strip_path, None, path)
+
+@wvtest
+def test_strip_base_path():
+    path = "/var/backup/daily.0/localhost/etc/"
+    base_paths = ["/var", "/var/backup", "/var/backup/daily.0/localhost"]
+    WVPASSEQ(strip_base_path(path, base_paths), '/etc')