about summary refs log tree commit diff homepage
path: root/utils/hacks/TreeGraphs/DumpTreeStream.py
blob: b3614c7c2173986eb86a27affef75593b43d3beb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python

from __future__ import division

import sys, os, struct

def getTreeStream(path):
    data = open(path,'rb').read()
    paths = { 0 : ''}
    pos = 0
    while pos<len(data):
        id,tag = struct.unpack('II', data[pos:pos+8])
        pos += 8
        if tag&(1<<31):
            child = tag ^ (1<<31)
            paths[child] = paths[id]
        else:
            size = tag
            paths[id] += data[pos:pos+size]
            pos += size
    if pos!=len(data):
        raise IOError,'bad position'
    return paths

def writeTreeStream(path, output):
    paths = getTreeSTream(path)
    print 'Writing %d paths'%len(paths)
    for i,data in paths.items():
        if i!=0:
            f = open('%s%04d'%(output,i), 'wb')
            f.write(data)
            f.close()
            
def main(args):
    from optparse import OptionParser
    op = OptionParser("usage: %prog input outputPrefix")
    opts,args = op.parse_args()

    input,outputPrefix = args

    f = open(input,'rb')
    data = f.read()
    f.close()
    writeTreeStream(data, outputPrefix)
    
if __name__=='__main__':
    main(sys.argv)