]> arthur.barton.de Git - bup.git/blob - cmd-split.py
Extremely basic 'bup server' support.
[bup.git] / cmd-split.py
1 #!/usr/bin/env python
2 import sys, time, re
3 import hashsplit, git, options
4 from helpers import *
5
6 optspec = """
7 bup split [-tcb] [-n name] [--bench] [filenames...]
8 --
9 r,remote=  remote repository path
10 b,blobs    output a series of blob ids
11 t,tree     output a tree id
12 c,commit   output a commit id
13 n,name=    name of backup set to update (if any)
14 v,verbose  increase log output (can be used more than once)
15 bench      print benchmark timings to stderr
16 """
17 o = options.Options('bup split', optspec)
18 (opt, flags, extra) = o.parse(sys.argv[1:])
19
20 git.check_repo_or_die()
21 if not (opt.blobs or opt.tree or opt.commit or opt.name):
22     log("bup split: use one or more of -b, -t, -c, -n\n")
23     o.usage()
24
25 hashsplit.split_verbosely = opt.verbose
26 if opt.verbose >= 2:
27     git.verbose = opt.verbose - 1
28     opt.bench = 1
29
30 start_time = time.time()
31
32 def server_connect(remote):
33     rs = remote.split(':', 1)
34     if len(rs) == 1:
35         p = subprocess.Popen(['bup', 'server', '-d', opt.remote],
36                              stdin=subprocess.PIPE, stdout=subprocess.PIPE)
37     else:
38         (host, dir) = rs
39         p = subprocess.Popen(['ssh', host, '--', 'bup', 'server'],
40                              stdin=subprocess.PIPE, stdout=subprocess.PIPE)
41         dir = re.sub(r'[\r\n]', ' ', dir)
42         p.stdin.write('set-dir\n%s\n' % dir)
43     return p
44
45 if opt.remote:
46     p = server_connect(opt.remote)
47     p.stdin.write('receive-objects\n')
48     w = git.PackWriter_Remote(p.stdin)
49 else:
50     w = git.PackWriter()
51     
52 (shalist,tree) = hashsplit.split_to_tree(w, hashsplit.autofiles(extra))
53
54 if opt.verbose:
55     log('\n')
56 if opt.blobs:
57     for (mode,name,bin) in shalist:
58         print bin.encode('hex')
59 if opt.tree:
60     print tree.encode('hex')
61 if opt.commit or opt.name:
62     msg = 'bup split\n\nGenerated by command:\n%r' % sys.argv
63     ref = opt.name and ('refs/heads/%s' % opt.name) or None
64     commit = w.new_commit(ref, tree, msg)
65     if opt.commit:
66         print commit.encode('hex')
67
68 if opt.remote:
69     w.close()
70     p.stdin.write('quit\n')
71     p.wait()
72
73 secs = time.time() - start_time
74 size = hashsplit.total_split
75 if opt.bench:
76     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
77         % (size/1024., secs, size/1024./secs))