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)
14 def do_bloom(path, outfilename):
17 outfilename = os.path.join(path, 'bup.bloom')
20 if os.path.exists(outfilename):
21 b = git.ShaBloom(outfilename)
23 debug1("bloom: Existing invalid bloom found, regenerating.\n")
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):
39 total = add_count + rest_count
42 log("bloom: Nothing to do\n")
46 if len(b) != rest_count:
47 log("bloom: size %d != idx total %d, regenerating\n"
48 % (len(b), rest_count))
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)))
56 b = git.ShaBloom(outfilename, readwrite=True, expected=add_count)
57 if not b: # Need all idxs to build from scratch
59 add_count += rest_count
63 msg = b is None and 'creating from' or 'adding'
64 log('bloom: %s %d files (%d objects).\n' % (msg, len(add), add_count))
68 tfname = os.path.join(path, 'bup.tmp.bloom')
69 tf = open(tfname, 'w+')
70 b = git.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
73 ix = git.open_idx(name)
74 progress('Writing bloom: %d/%d\r' % (count, len(add)))
77 log('Writing bloom: %d/%d, done.\n' % (count, len(add)))
80 os.rename(tfname, outfilename)
85 o = options.Options(optspec)
86 (opt, flags, extra) = o.parse(sys.argv[1:])
89 o.fatal('no positional parameters expected')
91 if opt.k and opt.k not in (4,5):
92 o.fatal('only k values of 4 and 5 are supported')
94 git.check_repo_or_die()
96 do_bloom(opt.dir or git.repo('objects/pack'), opt.output)