]> arthur.barton.de Git - bup.git/blob - cmd/split-cmd.py
b065892f12af0e1c3c6b6c66464bfb8f6957c30a
[bup.git] / cmd / split-cmd.py
1 #!/usr/bin/env python
2 import sys, time, struct
3 from bup import hashsplit, git, options, client
4 from bup.helpers import *
5 from subprocess import PIPE
6
7
8 optspec = """
9 bup split [-tcb] [-n name] [--bench] [filenames...]
10 --
11 r,remote=  remote repository path
12 b,blobs    output a series of blob ids
13 t,tree     output a tree id
14 c,commit   output a commit id
15 n,name=    name of backup set to update (if any)
16 N,noop     don't actually save the data anywhere
17 q,quiet    don't print progress messages
18 v,verbose  increase log output (can be used more than once)
19 copy       just copy input to output, hashsplitting along the way
20 bench      print benchmark timings to stderr
21 max-pack-size=  maximum bytes in a single pack
22 max-pack-objects=  maximum number of objects in a single pack
23 fanout=    maximum number of blobs in a single tree
24 bwlimit=   maximum bytes/sec to transmit to server
25 """
26 o = options.Options('bup split', optspec)
27 (opt, flags, extra) = o.parse(sys.argv[1:])
28
29 git.check_repo_or_die()
30 if not (opt.blobs or opt.tree or opt.commit or opt.name or
31         opt.noop or opt.copy):
32     o.fatal("use one or more of -b, -t, -c, -n, -N, --copy")
33 if (opt.noop or opt.copy) and (opt.blobs or opt.tree or 
34                                opt.commit or opt.name):
35     o.fatal('-N is incompatible with -b, -t, -c, -n')
36
37 if opt.verbose >= 2:
38     git.verbose = opt.verbose - 1
39     opt.bench = 1
40 if opt.max_pack_size:
41     hashsplit.max_pack_size = parse_num(opt.max_pack_size)
42 if opt.max_pack_objects:
43     hashsplit.max_pack_objects = parse_num(opt.max_pack_objects)
44 if opt.fanout:
45     hashsplit.fanout = parse_num(opt.fanout)
46 if opt.blobs:
47     hashsplit.fanout = 0
48 if opt.bwlimit:
49     client.bwlimit = parse_num(opt.bwlimit)
50
51 is_reverse = os.environ.get('BUP_SERVER_REVERSE')
52 if is_reverse and opt.remote:
53     o.fatal("don't use -r in reverse mode; it's automatic")
54 start_time = time.time()
55
56 refname = opt.name and 'refs/heads/%s' % opt.name or None
57 if opt.noop or opt.copy:
58     cli = pack_writer = oldref = None
59 elif opt.remote or is_reverse:
60     cli = client.Client(opt.remote)
61     oldref = refname and cli.read_ref(refname) or None
62     pack_writer = cli.new_packwriter()
63 else:
64     cli = None
65     oldref = refname and git.read_ref(refname) or None
66     pack_writer = git.PackWriter()
67
68 files = extra and (open(fn) for fn in extra) or [sys.stdin]
69 if pack_writer:
70     shalist = hashsplit.split_to_shalist(pack_writer, files)
71     tree = pack_writer.new_tree(shalist)
72 else:
73     last = 0
74     for (blob, bits) in hashsplit.hashsplit_iter(files):
75         hashsplit.total_split += len(blob)
76         if opt.copy:
77             sys.stdout.write(str(blob))
78         megs = hashsplit.total_split/1024/1024
79         if not opt.quiet and last != megs:
80             progress('%d Mbytes read\r' % megs)
81             last = megs
82     progress('%d Mbytes read, done.\n' % megs)
83
84 if opt.verbose:
85     log('\n')
86 if opt.blobs:
87     for (mode,name,bin) in shalist:
88         print bin.encode('hex')
89 if opt.tree:
90     print tree.encode('hex')
91 if opt.commit or opt.name:
92     msg = 'bup split\n\nGenerated by command:\n%r' % sys.argv
93     ref = opt.name and ('refs/heads/%s' % opt.name) or None
94     commit = pack_writer.new_commit(oldref, tree, msg)
95     if opt.commit:
96         print commit.encode('hex')
97
98 if pack_writer:
99     pack_writer.close()  # must close before we can update the ref
100
101 if opt.name:
102     if cli:
103         cli.update_ref(refname, commit, oldref)
104     else:
105         git.update_ref(refname, commit, oldref)
106
107 if cli:
108     cli.close()
109
110 secs = time.time() - start_time
111 size = hashsplit.total_split
112 if opt.bench:
113     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
114         % (size/1024., secs, size/1024./secs))