]> arthur.barton.de Git - bup.git/blob - cmd/split-cmd.py
Move cmd-*.py to cmd/*-cmd.py.
[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 """
25 o = options.Options('bup split', optspec)
26 (opt, flags, extra) = o.parse(sys.argv[1:])
27
28 git.check_repo_or_die()
29 if not (opt.blobs or opt.tree or opt.commit or opt.name or
30         opt.noop or opt.copy):
31     o.fatal("use one or more of -b, -t, -c, -n, -N, --copy")
32 if (opt.noop or opt.copy) and (opt.blobs or opt.tree or 
33                                opt.commit or opt.name):
34     o.fatal('-N is incompatible with -b, -t, -c, -n')
35
36 if opt.verbose >= 2:
37     git.verbose = opt.verbose - 1
38     opt.bench = 1
39 if opt.max_pack_size:
40     hashsplit.max_pack_size = parse_num(opt.max_pack_size)
41 if opt.max_pack_objects:
42     hashsplit.max_pack_objects = parse_num(opt.max_pack_objects)
43 if opt.fanout:
44     hashsplit.fanout = parse_num(opt.fanout)
45 if opt.blobs:
46     hashsplit.fanout = 0
47
48 start_time = time.time()
49
50 refname = opt.name and 'refs/heads/%s' % opt.name or None
51 if opt.noop or opt.copy:
52     cli = w = oldref = None
53 elif opt.remote:
54     cli = client.Client(opt.remote)
55     oldref = refname and cli.read_ref(refname) or None
56     w = cli.new_packwriter()
57 else:
58     cli = None
59     oldref = refname and git.read_ref(refname) or None
60     w = git.PackWriter()
61
62 files = extra and (open(fn) for fn in extra) or [sys.stdin]
63 if w:
64     shalist = hashsplit.split_to_shalist(w, files)
65     tree = w.new_tree(shalist)
66 else:
67     last = 0
68     for (blob, bits) in hashsplit.hashsplit_iter(files):
69         hashsplit.total_split += len(blob)
70         if opt.copy:
71             sys.stdout.write(str(blob))
72         megs = hashsplit.total_split/1024/1024
73         if not opt.quiet and last != megs:
74             progress('%d Mbytes read\r' % megs)
75             last = megs
76     progress('%d Mbytes read, done.\n' % megs)
77
78 if opt.verbose:
79     log('\n')
80 if opt.blobs:
81     for (mode,name,bin) in shalist:
82         print bin.encode('hex')
83 if opt.tree:
84     print tree.encode('hex')
85 if opt.commit or opt.name:
86     msg = 'bup split\n\nGenerated by command:\n%r' % sys.argv
87     ref = opt.name and ('refs/heads/%s' % opt.name) or None
88     commit = w.new_commit(oldref, tree, msg)
89     if opt.commit:
90         print commit.encode('hex')
91
92 if w:
93     w.close()  # must close before we can update the ref
94         
95 if opt.name:
96     if cli:
97         cli.update_ref(refname, commit, oldref)
98     else:
99         git.update_ref(refname, commit, oldref)
100
101 if cli:
102     cli.close()
103
104 secs = time.time() - start_time
105 size = hashsplit.total_split
106 if opt.bench:
107     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
108         % (size/1024., secs, size/1024./secs))