-
Notifications
You must be signed in to change notification settings - Fork 1
/
ks2dot.py
executable file
·126 lines (95 loc) · 2.89 KB
/
ks2dot.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import re
import sys
assert sys.version_info[0] == 3
from subprocess import Popen, PIPE
import kshelp
filterLevel = 0
def run(cmd):
print(' '.join(cmd))
process = Popen(cmd, stdout=PIPE)
(stdout, stderr) = process.communicate()
stdout = stdout.decode("utf-8")
process.wait()
return stdout
def ksObjToNode(ksobj, id_):
global filterLevel
kshelp.exercise(ksobj)
result = ''
result += '\t"%s" [\n' % id_
print(repr(ksobj))
title = re.match(r'<.*\.(.*) object at .*', repr(ksobj)).group(1)
fieldDescriptions = ['<title> [%s]' % title]
# add non-KaitaiStruct fields
for candidate in sorted(kshelp.getFieldNamesPrint(ksobj, filterLevel)):
tmp = repr(candidate)
tmp = tmp.replace('"', '\\"')
if tmp.startswith('\'') and tmp.endswith('\''):
tmp = tmp[1:-1]
fieldDescriptions.append('<%s> .%s' % (tmp, tmp))
# add KaitaiStruct fields
for candidate in sorted(kshelp.getFieldNamesDescend(ksobj, filterLevel)):
fieldDescriptions.append('<%s> .%s' % (candidate, candidate))
result += '\t\tlabel = "%s"\n' % ('|'.join(fieldDescriptions))
result += '\t\tshape = "record"\n'
result += '\t];\n'
return result
def ksObjToDot(ksobj):
global filterLevel
dot = ''
dot += 'digraph g {\n'
dot += ' graph [\n'
dot += ' rankdir=LR, overlap=false\n'
dot += ' ];\n'
dot += ' node [\n'
dot += ' fontsize = "16"\n'
dot += ' shape = "ellipse"\n'
dot += ' ];\n'
dot += ' edge [\n'
dot += ' ];\n'
kshelp.exercise(ksobj)
ksObjs = kshelp.getLinkedKaitaiObjectsAll(ksobj)
ksObjToLabel = {}
for (i,obj) in enumerate(ksObjs):
ksObjToLabel[obj] = 'ksobj%d'%i
# declare nodes
for ksobj in ksObjs:
label = ksObjToLabel[ksobj]
dot += ksObjToNode(ksobj, label)
# declare edges
id_ = 0
for srcObj in ksObjs:
for fieldName in kshelp.getFieldNamesDescend(srcObj, filterLevel):
# fieldName can be a KaitaiStruct or a [KaitaiStruct, ...]
# normalize to list...
dstObjs = None
try:
dstObjs = getattr(srcObj, fieldName)
except AttributeError:
continue
if not isinstance(dstObjs, list):
dstObjs = [dstObjs]
# loop over list
for dstObj in dstObjs:
if not srcObj in ksObjToLabel or not dstObj in ksObjToLabel:
continue
srcName = ksObjToLabel[srcObj]
dstName = ksObjToLabel[dstObj]
dot += '\t"%s":%s -> "%s":title [\n' % (srcName, fieldName, dstName)
dot += '\t\tid = %d' % id_
id_ += 1
dot += '\t];\n'
dot += '}\n'
return dot
#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------
if __name__ == '__main__':
assert len(sys.argv) == 3
(cmd, fpath) = (sys.argv[1], sys.argv[2])
filterLevel = int(sys.argv[1])
parsed = kshelp.parseFpath(fpath)
with open('/tmp/tmp.dot', 'w') as fp:
fp.write(ksObjToDot(parsed))
run(['dot', '/tmp/tmp.dot', '-Tpng', '-o/tmp/tmp.png'])
run(['open', '/tmp/tmp.png'])