2 import sys, glob, tempfile
3 from bup import options, git
4 from bup.helpers import *
9 o,output= output bloom filename (default: auto)
10 d,dir= input directory to look for idx files (default: auto)
11 k,hashes= number of hash functions to use (4 or 5) (default: auto)
15 def do_bloom(path, outfilename):
19 outfilename = os.path.join(path, 'bup.bloom')
22 if os.path.exists(outfilename):
23 b = git.ShaBloom(outfilename)
25 debug1("bloom: Existing invalid bloom found, regenerating.\n")
32 for i,name in enumerate(glob.glob('%s/*.idx' % path)):
33 progress('bloom: counting: %d\r' % i)
34 ix = git.open_idx(name)
35 ixbase = os.path.basename(name)
36 if b and (ixbase in b.idxnames):
42 total = add_count + rest_count
45 debug1("bloom: nothing to do.\n")
49 if len(b) != rest_count:
50 log("bloom: size %d != idx total %d, regenerating\n"
51 % (len(b), rest_count))
53 elif (b.bits < git.MAX_BLOOM_BITS and
54 b.pfalse_positive(add_count) > git.MAX_PFALSE_POSITIVE):
55 log("bloom: regenerating: adding %d entries gives "
56 "%.2f%% false positives.\n"
57 % (add_count, b.pfalse_positive(add_count)))
60 b = git.ShaBloom(outfilename, readwrite=True, expected=add_count)
61 if not b: # Need all idxs to build from scratch
63 add_count += rest_count
67 msg = b is None and 'creating from' or 'adding'
68 if not _first: _first = path
69 dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
70 log('bloom: %s%s %d file%s (%d object%s).\n'
72 len(add), len(add)!=1 and 's' or '',
73 add_count, add_count!=1 and 's' or ''))
77 tfname = os.path.join(path, 'bup.tmp.bloom')
78 tf = open(tfname, 'w+')
79 b = git.ShaBloom.create(tfname, f=tf, expected=add_count, k=opt.k)
83 ix = git.open_idx(name)
84 qprogress('bloom: writing %.2f%% (%d/%d objects)\r'
85 % (icount*100.0/add_count, icount, add_count))
91 os.rename(tfname, outfilename)
96 o = options.Options(optspec)
97 (opt, flags, extra) = o.parse(sys.argv[1:])
100 o.fatal('no positional parameters expected')
102 if opt.k and opt.k not in (4,5):
103 o.fatal('only k values of 4 and 5 are supported')
105 git.check_repo_or_die()
107 paths = opt.dir and [opt.dir] or git.all_packdirs()
109 debug1('bloom: scanning %s\n' % path)
110 do_bloom(path, opt.output)