File: ivmextract.py

package info (click to toggle)
psyco-doc 1.6-1
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 1,832 kB
  • ctags: 3,236
  • sloc: ansic: 23,895; python: 5,646; perl: 1,309; makefile: 153
file content (73 lines) | stat: -rw-r--r-- 2,261 bytes parent folder | download | duplicates (6)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os, sys
import xam
from ivmdump import insnlist, insntable, stackpushes, chainable
from struct import unpack


IGNORE_INSNS = ('assertdepth', 'dynamicfreq')

def dump(data):
    l = len(data)
    p = 0
    results = []
    result = []
    freq = 1
    while p < l:
        mode = insnlist[ord(data[p])]
        if mode.opcode not in insntable:
            break
        p += mode.unpacksize + 1
        if p > l:
            break
        args = mode.getargs(data, p-mode.unpacksize)
        for insn in mode.insns:
            if insn[0] in IGNORE_INSNS:
                if insn[0] == 'dynamicfreq':
                    freq = args[0]
                continue
            a = len(insn)-1
            if a:
                txt = '%s(%s)' % (insn[0], ','.join(map(str,args[:a])))
                del args[:a]
            else:
                txt = insn[0]
            result.append(txt)
        if mode.opcode not in chainable:
            results.append((freq, result))
            result = []
    results.append((freq, result))
    return results


def main(DIRECTORY):
    filename = os.path.join(DIRECTORY, 'psyco.dump')
    if not os.path.isfile(filename) and os.path.isfile(DIRECTORY):
        filename = DIRECTORY
        DIRECTORY = os.path.dirname(DIRECTORY)
    outfilename = filename + '.ivm'
    if os.path.isfile(filename):
        codebufs = xam.readdump(filename)
        f = open(outfilename, 'w')
        for codebuf in codebufs:
            if codebuf.data:
                data, addr, next, key = codebuf.splitheader()
                for freq, lst in dump(data):
                    if len(lst) > 1:
                        print >> f, 'psycodump(%d, [%s]).' % (freq,
                                                              ', '.join(lst))
        f.close()
    elif not os.path.isfile(outfilename):
        print >> sys.stderr, filename, "not found."
        sys.exit(1)
    else:
        print >> sys.stderr, "reusing text dump from", outfilename
    return outfilename


if __name__ == '__main__':
    if len(sys.argv) <= 1:
        print "Usage: python ivmextract.py <directory>"
        print "  psyco.dump is loaded from the <directory>."
        sys.exit(2)
    for dir in sys.argv[1:]:
        print "'%s'." % main(dir)