]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
cmd/bloom: actually, always use the same temp filename.
[bup.git] / cmd / bloom-cmd.py
1 #!/usr/bin/env python
2 import sys, glob, tempfile
3 from bup import options, git
4 from bup.helpers import *
5
6 optspec = """
7 bup bloom [options...]
8 --
9 o,output=  output bloom filename (default: auto-generated)
10 d,dir=     input directory to look for idx files (default: auto-generated)
11 k,hashes=  number of hash functions to use (4 or 5) (default: auto-generated)
12 """
13
14 def do_bloom(path, outfilename):
15     if not outfilename:
16         assert(path)
17         outfilename = os.path.join(path, 'bup.bloom')
18
19     b = None
20     if os.path.exists(outfilename):
21         b = git.ShaBloom(outfilename, readwrite=True)
22         if not b.valid():
23             b = None
24
25     add = []
26     rest = []
27     add_count = 0
28     rest_count = 0
29     for name in glob.glob('%s/*.idx' % path):
30         ix = git.open_idx(name)
31         ixbase = os.path.basename(name)
32         if b is not None and ixbase in b.idxnames:
33             rest.append(ix)
34             rest_count += len(ix)
35         else:
36             add.append(ix)
37             add_count += len(ix)
38     total = add_count + rest_count
39
40     if not add:
41         log("bloom: Nothing to do\n")
42         return
43
44     if b is not None:
45         if len(b) != rest_count:
46             log("bloom: size %d != idx total %d, regenerating\n"
47                     % (len(b), rest_count))
48             b = None
49         elif b.bits < git.MAX_BLOOM_BITS and \
50              b.pfalse_positive(add_count) > git.MAX_PFALSE_POSITIVE:
51             log("bloom: %d more entries => %.2f false positive, regenerating\n"
52                     % (add_count, b.pfalse_positive(add_count)))
53             b = None
54     if b is None: # Need all idxs to build from scratch
55         add += rest
56         add_count += rest_count
57     del rest
58     del rest_count
59
60     msg = b is None and 'creating from' or 'adding'
61     log('bloom: %s %d files (%d objects).\n' % (msg, len(add), add_count))
62
63     tempname = None
64     if b is None:
65         tfname = os.path.join(path, 'bup.tmp.bloom')
66         tf = open(tfname, 'w+')
67         b = git.ShaBloom.create(
68                 tfname, f=tf, readwrite=True, expected=add_count, k=opt.k)
69     count = 0
70     for ix in add:
71         progress('Writing bloom: %d/%d\r' % (count, len(add)))
72         b.add_idx(ix)
73         count += 1
74     log('Writing bloom: %d/%d, done.\n' % (count, len(add)))
75
76     if tempname:
77         os.rename(tempname, outfilename)
78
79
80 handle_ctrl_c()
81
82 o = options.Options(optspec)
83 (opt, flags, extra) = o.parse(sys.argv[1:])
84
85 if extra:
86     o.fatal('no positional parameters expected')
87
88 if opt.k and opt.k not in (4,5):
89     o.fatal('only k values of 4 and 5 are supported')
90
91 git.check_repo_or_die()
92
93 do_bloom(opt.dir or git.repo('objects/pack'), opt.output)