2 import sys, glob, tempfile
3 from bup import options, git
4 from bup.helpers import *
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 c,check= an idx file to check against an existing bloom filter
15 def check_bloom(path, bloomfilename, idx):
16 if not os.path.exists(bloomfilename):
17 log("bloom: %s not found to check\n" % bloomfilename)
19 b = git.ShaBloom(bloomfilename)
21 log("bloom: %s could not be opened to check\n" % bloomfilename)
23 base = os.path.basename(idx)
24 if base not in b.idxnames:
25 log("bloom: filter does not contain %s, nothing to check\n" % idx)
28 idx = os.path.join(path, idx)
29 log("bloom: checking %s" % idx)
30 for objsha in git.open_idx(idx):
31 if not b.exists(objsha):
32 add_error("bloom: ERROR: %s missing from bloom"
33 % str(objsha).encode('hex'))
36 def do_bloom(path, outfilename):
38 if os.path.exists(outfilename):
39 b = git.ShaBloom(outfilename)
41 debug1("bloom: Existing invalid bloom found, regenerating.\n")
48 for name in glob.glob('%s/*.idx' % path):
49 ix = git.open_idx(name)
50 ixbase = os.path.basename(name)
51 if b and (ixbase in b.idxnames):
57 total = add_count + rest_count
60 log("bloom: Nothing to do\n")
64 if len(b) != rest_count:
65 log("bloom: size %d != idx total %d, regenerating\n"
66 % (len(b), rest_count))
68 elif (b.bits < git.MAX_BLOOM_BITS and
69 b.pfalse_positive(add_count) > git.MAX_PFALSE_POSITIVE):
70 log("bloom: %d more entries => %.2f false positive, regenerating\n"
71 % (add_count, b.pfalse_positive(add_count)))
74 b = git.ShaBloom(outfilename, readwrite=True, expected=add_count)
75 if not b: # Need all idxs to build from scratch
77 add_count += rest_count
81 msg = b is None and 'creating from' or 'adding'
82 log('bloom: %s %d file%s (%d object%s).\n' % (msg, len(add),
83 len(add)!=1 and 's' or '',
85 add_count!=1 and 's' or ''))
89 tfname = os.path.join(path, 'bup.tmp.bloom')
90 tf = open(tfname, 'w+')
91 b = git.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
94 ix = git.open_idx(name)
95 progress('Writing bloom: %d/%d\r' % (count, len(add)))
98 log('Writing bloom: %d/%d, done.\n' % (count, len(add)))
101 os.rename(tfname, outfilename)
106 o = options.Options(optspec)
107 (opt, flags, extra) = o.parse(sys.argv[1:])
110 o.fatal('no positional parameters expected')
112 git.check_repo_or_die()
113 bloompath = opt.dir or git.repo('objects/pack')
117 outfilename = opt.output or os.path.join(bloompath, 'bup.bloom')
120 check_bloom(bloompath, outfilename, opt.check)
122 if opt.k and opt.k not in (4,5):
123 o.fatal('only k values of 4 and 5 are supported')
125 do_bloom(bloompath, outfilename)
128 log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))