]> arthur.barton.de Git - bup.git/blob - cmd/midx-cmd.py
Merge branch 'master' of /tmp/bup
[bup.git] / cmd / midx-cmd.py
1 #!/usr/bin/env python
2 import sys, math, struct, glob
3 from bup import options, git
4 from bup.helpers import *
5
6 PAGE_SIZE=4096
7 SHA_PER_PAGE=PAGE_SIZE/200.
8
9
10 def merge(idxlist, bits, table):
11     count = 0
12     for e in git.idxmerge(idxlist):
13         count += 1
14         prefix = git.extract_bits(e, bits)
15         table[prefix] = count
16         yield e
17
18
19 def do_midx(outdir, outfilename, infilenames):
20     if not outfilename:
21         assert(outdir)
22         sum = Sha1('\0'.join(infilenames)).hexdigest()
23         outfilename = '%s/midx-%s.midx' % (outdir, sum)
24     
25     inp = []
26     total = 0
27     for name in infilenames:
28         ix = git.PackIdx(name)
29         inp.append(ix)
30         total += len(ix)
31
32     log('Merging %d indexes (%d objects).\n' % (len(infilenames), total))
33     if (not opt.force and (total < 1024 and len(infilenames) < 3)) \
34        or (opt.force and not total):
35         log('midx: nothing to do.\n')
36         return
37
38     pages = int(total/SHA_PER_PAGE) or 1
39     bits = int(math.ceil(math.log(pages, 2)))
40     entries = 2**bits
41     log('Table size: %d (%d bits)\n' % (entries*4, bits))
42     
43     table = [0]*entries
44
45     try:
46         os.unlink(outfilename)
47     except OSError:
48         pass
49     f = open(outfilename + '.tmp', 'w+')
50     f.write('MIDX\0\0\0\2')
51     f.write(struct.pack('!I', bits))
52     assert(f.tell() == 12)
53     f.write('\0'*4*entries)
54     
55     for e in merge(inp, bits, table):
56         f.write(e)
57         
58     f.write('\0'.join(os.path.basename(p) for p in infilenames))
59
60     f.seek(12)
61     f.write(struct.pack('!%dI' % entries, *table))
62     f.close()
63     os.rename(outfilename + '.tmp', outfilename)
64
65     # this is just for testing
66     if 0:
67         p = git.PackMidx(outfilename)
68         assert(len(p.idxnames) == len(infilenames))
69         print p.idxnames
70         assert(len(p) == total)
71         pi = iter(p)
72         for i in merge(inp, total, bits, table):
73             assert(i == pi.next())
74             assert(p.exists(i))
75
76     print outfilename
77
78 optspec = """
79 bup midx [options...] <idxnames...>
80 --
81 o,output=  output midx filename (default: auto-generated)
82 a,auto     automatically create .midx from any unindexed .idx files
83 f,force    automatically create .midx from *all* .idx files
84 """
85 o = options.Options('bup midx', optspec)
86 (opt, flags, extra) = o.parse(sys.argv[1:])
87
88 if extra and (opt.auto or opt.force):
89     o.fatal("you can't use -f/-a and also provide filenames")
90
91 git.check_repo_or_die()
92
93 if extra:
94     do_midx(git.repo('objects/pack'), opt.output, extra)
95 elif opt.auto or opt.force:
96     paths = [git.repo('objects/pack')]
97     paths += glob.glob(git.repo('index-cache/*/.'))
98     for path in paths:
99         log('midx: scanning %s\n' % path)
100         if opt.force:
101             do_midx(path, opt.output, glob.glob('%s/*.idx' % path))
102         elif opt.auto:
103             m = git.PackIdxList(path)
104             needed = {}
105             for pack in m.packs:  # only .idx files without a .midx are open
106                 if pack.name.endswith('.idx'):
107                     needed[pack.name] = 1
108             del m
109             do_midx(path, opt.output, needed.keys())
110         log('\n')
111 else:
112     o.fatal("you must use -f or -a or provide input filenames")