]> arthur.barton.de Git - bup.git/blob - cmd/split-cmd.py
--remote parameter requires a colon
[bup.git] / cmd / split-cmd.py
1 #!/usr/bin/env python
2 import sys, time
3 from bup import hashsplit, git, options, client
4 from bup.helpers import *
5
6
7 optspec = """
8 bup split [-tcb] [-n name] [--bench] [filenames...]
9 --
10 r,remote=  remote repository path
11 b,blobs    output a series of blob ids
12 t,tree     output a tree id
13 c,commit   output a commit id
14 n,name=    name of backup set to update (if any)
15 q,quiet    don't print progress messages
16 v,verbose  increase log output (can be used more than once)
17 noop       don't actually save the data anywhere
18 copy       just copy input to output, hashsplitting along the way
19 bench      print benchmark timings to stderr
20 max-pack-size=  maximum bytes in a single pack
21 max-pack-objects=  maximum number of objects in a single pack
22 fanout=    maximum number of blobs in a single tree
23 bwlimit=   maximum bytes/sec to transmit to server
24 """
25 o = options.Options('bup split', optspec)
26 (opt, flags, extra) = o.parse(sys.argv[1:])
27
28 handle_ctrl_c()
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 and --copy are 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     if opt.remote and opt.remote.find(":") == -1:
61         o.fatal("--remote argument must contain a colon")
62     try:
63         cli = client.Client(opt.remote)
64     except client.ClientError:
65         o.fatal("server exited unexpectedly; see errors above")
66     oldref = refname and cli.read_ref(refname) or None
67     pack_writer = cli.new_packwriter()
68 else:
69     cli = None
70     oldref = refname and git.read_ref(refname) or None
71     pack_writer = git.PackWriter()
72
73 files = extra and (open(fn) for fn in extra) or [sys.stdin]
74 if pack_writer:
75     shalist = hashsplit.split_to_shalist(pack_writer, files)
76     tree = pack_writer.new_tree(shalist)
77 else:
78     last = 0
79     for (blob, bits) in hashsplit.hashsplit_iter(files):
80         hashsplit.total_split += len(blob)
81         if opt.copy:
82             sys.stdout.write(str(blob))
83         megs = hashsplit.total_split/1024/1024
84         if not opt.quiet and last != megs:
85             progress('%d Mbytes read\r' % megs)
86             last = megs
87     progress('%d Mbytes read, done.\n' % megs)
88
89 if opt.verbose:
90     log('\n')
91 if opt.blobs:
92     for (mode,name,bin) in shalist:
93         print bin.encode('hex')
94 if opt.tree:
95     print tree.encode('hex')
96 if opt.commit or opt.name:
97     msg = 'bup split\n\nGenerated by command:\n%r' % sys.argv
98     ref = opt.name and ('refs/heads/%s' % opt.name) or None
99     commit = pack_writer.new_commit(oldref, tree, msg)
100     if opt.commit:
101         print commit.encode('hex')
102
103 if pack_writer:
104     pack_writer.close()  # must close before we can update the ref
105
106 if opt.name:
107     if cli:
108         cli.update_ref(refname, commit, oldref)
109     else:
110         git.update_ref(refname, commit, oldref)
111
112 if cli:
113     cli.close()
114
115 secs = time.time() - start_time
116 size = hashsplit.total_split
117 if opt.bench:
118     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
119         % (size/1024., secs, size/1024./secs))