]> arthur.barton.de Git - bup.git/blob - cmd/bloom-cmd.py
Configure python, use it, and embed during install
[bup.git] / cmd / bloom-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7 import sys, glob, tempfile
8 from bup import options, git, bloom
9 from bup.helpers import *
10
11 optspec = """
12 bup bloom [options...]
13 --
14 ruin       ruin the specified bloom file (clearing the bitfield)
15 f,force    ignore existing bloom file and regenerate it from scratch
16 o,output=  output bloom filename (default: auto)
17 d,dir=     input directory to look for idx files (default: auto)
18 k,hashes=  number of hash functions to use (4 or 5) (default: auto)
19 c,check=   check the given .idx file against the bloom filter
20 """
21
22
23 def ruin_bloom(bloomfilename):
24     rbloomfilename = git.repo_rel(bloomfilename)
25     if not os.path.exists(bloomfilename):
26         log("%s\n" % bloomfilename)
27         add_error("bloom: %s not found to ruin\n" % rbloomfilename)
28         return
29     b = bloom.ShaBloom(bloomfilename, readwrite=True, expected=1)
30     b.map[16:16+2**b.bits] = '\0' * 2**b.bits
31
32
33 def check_bloom(path, bloomfilename, idx):
34     rbloomfilename = git.repo_rel(bloomfilename)
35     ridx = git.repo_rel(idx)
36     if not os.path.exists(bloomfilename):
37         log("bloom: %s: does not exist.\n" % rbloomfilename)
38         return
39     b = bloom.ShaBloom(bloomfilename)
40     if not b.valid():
41         add_error("bloom: %r is invalid.\n" % rbloomfilename)
42         return
43     base = os.path.basename(idx)
44     if base not in b.idxnames:
45         log("bloom: %s does not contain the idx.\n" % rbloomfilename)
46         return
47     if base == idx:
48         idx = os.path.join(path, idx)
49     log("bloom: bloom file: %s\n" % rbloomfilename)
50     log("bloom:   checking %s\n" % ridx)
51     for objsha in git.open_idx(idx):
52         if not b.exists(objsha):
53             add_error("bloom: ERROR: object %s missing" 
54                       % str(objsha).encode('hex'))
55
56
57 _first = None
58 def do_bloom(path, outfilename):
59     global _first
60     b = None
61     if os.path.exists(outfilename) and not opt.force:
62         b = bloom.ShaBloom(outfilename)
63         if not b.valid():
64             debug1("bloom: Existing invalid bloom found, regenerating.\n")
65             b = None
66
67     add = []
68     rest = []
69     add_count = 0
70     rest_count = 0
71     for i,name in enumerate(glob.glob('%s/*.idx' % path)):
72         progress('bloom: counting: %d\r' % i)
73         ix = git.open_idx(name)
74         ixbase = os.path.basename(name)
75         if b and (ixbase in b.idxnames):
76             rest.append(name)
77             rest_count += len(ix)
78         else:
79             add.append(name)
80             add_count += len(ix)
81     total = add_count + rest_count
82
83     if not add:
84         debug1("bloom: nothing to do.\n")
85         return
86
87     if b:
88         if len(b) != rest_count:
89             debug1("bloom: size %d != idx total %d, regenerating\n"
90                    % (len(b), rest_count))
91             b = None
92         elif (b.bits < bloom.MAX_BLOOM_BITS and
93               b.pfalse_positive(add_count) > bloom.MAX_PFALSE_POSITIVE):
94             debug1("bloom: regenerating: adding %d entries gives "
95                    "%.2f%% false positives.\n"
96                    % (add_count, b.pfalse_positive(add_count)))
97             b = None
98         else:
99             b = bloom.ShaBloom(outfilename, readwrite=True, expected=add_count)
100     if not b: # Need all idxs to build from scratch
101         add += rest
102         add_count += rest_count
103     del rest
104     del rest_count
105
106     msg = b is None and 'creating from' or 'adding'
107     if not _first: _first = path
108     dirprefix = (_first != path) and git.repo_rel(path)+': ' or ''
109     progress('bloom: %s%s %d file%s (%d object%s).\n'
110         % (dirprefix, msg,
111            len(add), len(add)!=1 and 's' or '',
112            add_count, add_count!=1 and 's' or ''))
113
114     tfname = None
115     if b is None:
116         tfname = os.path.join(path, 'bup.tmp.bloom')
117         b = bloom.create(tfname, expected=add_count, k=opt.k)
118     count = 0
119     icount = 0
120     for name in add:
121         ix = git.open_idx(name)
122         qprogress('bloom: writing %.2f%% (%d/%d objects)\r' 
123                   % (icount*100.0/add_count, icount, add_count))
124         b.add_idx(ix)
125         count += 1
126         icount += len(ix)
127
128     # Currently, there's an open file object for tfname inside b.
129     # Make sure it's closed before rename.
130     b.close()
131
132     if tfname:
133         os.rename(tfname, outfilename)
134
135
136 handle_ctrl_c()
137
138 o = options.Options(optspec)
139 (opt, flags, extra) = o.parse(sys.argv[1:])
140
141 if extra:
142     o.fatal('no positional parameters expected')
143
144 git.check_repo_or_die()
145
146 if not opt.check and opt.k and opt.k not in (4,5):
147     o.fatal('only k values of 4 and 5 are supported')
148
149 paths = opt.dir and [opt.dir] or git.all_packdirs()
150 for path in paths:
151     debug1('bloom: scanning %s\n' % path)
152     outfilename = opt.output or os.path.join(path, 'bup.bloom')
153     if opt.check:
154         check_bloom(path, outfilename, opt.check)
155     elif opt.ruin:
156         ruin_bloom(outfilename)
157     else:
158         do_bloom(path, outfilename)
159
160 if saved_errors:
161     log('WARNING: %d errors encountered during bloom.\n' % len(saved_errors))
162     sys.exit(1)
163 elif opt.check:
164     log('All tests passed.\n')