]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
cmd/bloom: fix a message pluralization.
[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)
22         if not b.valid():
23             debug1("bloom: Existing invalid bloom found, regenerating.\n")
24             b = None
25
26     add = []
27     rest = []
28     add_count = 0
29     rest_count = 0
30     for name in glob.glob('%s/*.idx' % path):
31         ix = git.open_idx(name)
32         ixbase = os.path.basename(name)
33         if b and (ixbase in b.idxnames):
34             rest.append(name)
35             rest_count += len(ix)
36         else:
37             add.append(name)
38             add_count += len(ix)
39     total = add_count + rest_count
40
41     if not add:
42         log("bloom: Nothing to do\n")
43         return
44
45     if b:
46         if len(b) != rest_count:
47             log("bloom: size %d != idx total %d, regenerating\n"
48                     % (len(b), rest_count))
49             b = None
50         elif (b.bits < git.MAX_BLOOM_BITS and
51               b.pfalse_positive(add_count) > git.MAX_PFALSE_POSITIVE):
52             log("bloom: %d more entries => %.2f false positive, regenerating\n"
53                     % (add_count, b.pfalse_positive(add_count)))
54             b = None
55         else:
56             b = git.ShaBloom(outfilename, readwrite=True, expected=add_count)
57     if not b: # Need all idxs to build from scratch
58         add += rest
59         add_count += rest_count
60     del rest
61     del rest_count
62
63     msg = b is None and 'creating from' or 'adding'
64     log('bloom: %s %d file%s (%d object%s).\n' % (msg, len(add),
65                                                   len(add)!=1 and 's' or '',
66                                                   add_count,
67                                                   add_count!=1 and 's' or ''))
68
69     tfname = None
70     if b is None:
71         tfname = os.path.join(path, 'bup.tmp.bloom')
72         tf = open(tfname, 'w+')
73         b = git.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
74     count = 0
75     for name in add:
76         ix = git.open_idx(name)
77         progress('Writing bloom: %d/%d\r' % (count, len(add)))
78         b.add_idx(ix)
79         count += 1
80     log('Writing bloom: %d/%d, done.\n' % (count, len(add)))
81
82     if tfname:
83         os.rename(tfname, outfilename)
84
85
86 handle_ctrl_c()
87
88 o = options.Options(optspec)
89 (opt, flags, extra) = o.parse(sys.argv[1:])
90
91 if extra:
92     o.fatal('no positional parameters expected')
93
94 if opt.k and opt.k not in (4,5):
95     o.fatal('only k values of 4 and 5 are supported')
96
97 git.check_repo_or_die()
98
99 do_bloom(opt.dir or git.repo('objects/pack'), opt.output)