]> arthur.barton.de Git - bup.git/blobdiff - cmd/save-cmd.py
Adds -f option to save to use a given indexfile.
[bup.git] / cmd / save-cmd.py
index e840e557bba85e9bb87f5bc238cb3a750f37ff2c..d278e70727a1b07e7b18b8a755a8d5378854325f 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-import sys, re, errno, stat, time, math
+import sys, stat, time, math
 from bup import hashsplit, git, options, index, client
 from bup.helpers import *
 
@@ -7,13 +7,18 @@ from bup.helpers import *
 optspec = """
 bup save [-tc] [-n name] <filenames...>
 --
-r,remote=  remote repository path
+r,remote=  hostname:/path/to/repo of remote repository
 t,tree     output a tree id
 c,commit   output a commit id
 n,name=    name of backup set to update (if any)
+d,date=    date for the commit (seconds since the epoch)
 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
+f,indexfile=  the name of the index file (normally BUP_DIR/bupindex)
+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:])
@@ -26,6 +31,16 @@ if not extra:
 
 opt.progress = (istty and not opt.quiet)
 opt.smaller = parse_num(opt.smaller or 0)
+if opt.bwlimit:
+    client.bwlimit = parse_num(opt.bwlimit)
+
+if opt.date:
+    date = parse_date_or_fatal(opt.date, o.fatal)
+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:
@@ -65,14 +80,17 @@ def _pop(force_tree):
     shalist = shalists.pop()
     tree = force_tree or w.new_tree(shalist)
     if shalists:
-        shalists[-1].append(('40000', part, tree))
+        shalists[-1].append(('40000',
+                             git.mangle_name(part, 040000, 40000),
+                             tree))
     else:  # this was the toplevel, so put it back for sanity
         shalists.append(shalist)
     return tree
 
 lastremain = None
+lastprint = 0
 def progress_report(n):
-    global count, subcount, lastremain
+    global count, subcount, lastremain, lastprint
     subcount += n
     cc = count + subcount
     pct = total and (cc*100.0/total) or 0
@@ -104,12 +122,22 @@ def progress_report(n):
             remainstr = '%dm%d' % (mins, secs)
         else:
             remainstr = '%ds' % secs
-    progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
-             % (pct, cc/1024, total/1024, fcount, ftotal,
-                remainstr, kpsstr))
+    if now - lastprint > 0.1:
+        progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s\r'
+                 % (pct, cc/1024, total/1024, fcount, ftotal,
+                    remainstr, kpsstr))
+        lastprint = now
+
+
+def vlog(s):
+    global lastprint
+    lastprint = 0
+    log(s)
 
 
-r = index.Reader(git.repo('bupindex'))
+indexfile = opt.indexfile or git.repo('bupindex')
+print indexfile
+r = index.Reader(indexfile)
 
 def already_saved(ent):
     return ent.is_valid() and w.exists(ent.sha) and ent.sha
@@ -156,10 +184,10 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
         else:
             status = ' '
         if opt.verbose >= 2:
-            log('%s %-70s\n' % (status, ent.name))
+            vlog('%s %-70s\n' % (status, ent.name))
         elif not stat.S_ISDIR(ent.mode) and lastdir != dir:
             if not lastdir.startswith(dir):
-                log('%s %-70s\n' % (status, os.path.join(dir, '')))
+                vlog('%s %-70s\n' % (status, os.path.join(dir, '')))
             lastdir = dir
 
     if opt.progress:
@@ -175,7 +203,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 != '/':
@@ -216,7 +250,12 @@ for (transname,ent) in r.filter(extra, wantrecurse=wantrecurse_during):
                 add_error(e)
                 lastskip_name = ent.name
             else:
-                (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
+                try:
+                    (mode, id) = hashsplit.split_to_blob_or_tree(w, [f],
+                                            keep_boundaries=False)
+                except IOError, e:
+                    add_error('%s: %s' % (ent.name, e))
+                    lastskip_name = ent.name
         else:
             if stat.S_ISDIR(ent.mode):
                 assert(0)  # handled above
@@ -259,7 +298,7 @@ if opt.tree:
 if opt.commit or opt.name:
     msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
     ref = opt.name and ('refs/heads/%s' % opt.name) or None
-    commit = w.new_commit(oldref, tree, msg)
+    commit = w.new_commit(oldref, tree, date, msg)
     if opt.commit:
         print commit.encode('hex')