]> arthur.barton.de Git - bup.git/blob - cmd-split.py
Use mkstemp() when creating temporary packfiles.
[bup.git] / cmd-split.py
1 #!/usr/bin/env python
2 import sys, time, struct
3 import hashsplit, git, options, client
4 from 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 v,verbose  increase log output (can be used more than once)
17 bench      print benchmark timings to stderr
18 max-pack-size=  maximum bytes in a single pack
19 max-pack-objects=  maximum number of objects in a single pack
20 fanout=  maximum number of blobs in a single tree
21 """
22 o = options.Options('bup split', optspec)
23 (opt, flags, extra) = o.parse(sys.argv[1:])
24
25 git.check_repo_or_die()
26 if not (opt.blobs or opt.tree or opt.commit or opt.name):
27     log("bup split: use one or more of -b, -t, -c, -n\n")
28     o.usage()
29
30 hashsplit.split_verbosely = opt.verbose
31 if opt.verbose >= 2:
32     git.verbose = opt.verbose - 1
33     opt.bench = 1
34 if opt.max_pack_size:
35     hashsplit.max_pack_size = int(opt.max_pack_size)
36 if opt.max_pack_objects:
37     hashsplit.max_pack_objects = int(opt.max_pack_objects)
38 if opt.fanout:
39     hashsplit.fanout = int(opt.fanout)
40 if opt.blobs:
41     hashsplit.fanout = 0
42
43 start_time = time.time()
44
45 refname = opt.name and 'refs/heads/%s' % opt.name or None
46 if opt.remote:
47     cli = client.Client(opt.remote)
48     oldref = refname and cli.read_ref(refname) or None
49     w = cli.new_packwriter()
50 else:
51     cli = None
52     oldref = refname and git.read_ref(refname) or None
53     w = git.PackWriter()
54     
55 shalist = hashsplit.split_to_shalist(w, hashsplit.autofiles(extra))
56 tree = w.new_tree(shalist)
57
58 if opt.verbose:
59     log('\n')
60 if opt.blobs:
61     for (mode,name,bin) in shalist:
62         print bin.encode('hex')
63 if opt.tree:
64     print tree.encode('hex')
65 if opt.commit or opt.name:
66     msg = 'bup split\n\nGenerated by command:\n%r' % sys.argv
67     ref = opt.name and ('refs/heads/%s' % opt.name) or None
68     commit = w.new_commit(oldref, tree, msg)
69     if opt.commit:
70         print commit.encode('hex')
71
72 w.close()  # must close before we can update the ref
73         
74 if opt.name:
75     if cli:
76         cli.update_ref(refname, commit, oldref)
77     else:
78         git.update_ref(refname, commit, oldref)
79
80 if cli:
81     cli.close()
82
83 secs = time.time() - start_time
84 size = hashsplit.total_split
85 if opt.bench:
86     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
87         % (size/1024., secs, size/1024./secs))