File: make_dot.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (239 lines) | stat: -rw-r--r-- 8,520 bytes parent folder | download | duplicates (3)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os
import inspect, linecache
from rpython.flowspace.model import *
from rpython.tool.udir import udir
from py.process import cmdexec
from rpython.tool.error import offset2lineno

class DotGen:

    def __init__(self, graphname, rankdir=None):
        self.graphname = safename(graphname)
        self.lines = []
        self.source = None
        self.emit("digraph %s {" % self.graphname)
        if rankdir:
            self.emit('rankdir="%s"' % rankdir)

    def generate(self, storedir=None, target='ps'):
        source = self.get_source()
        if target is None:
            return source    # unprocessed
        if storedir is None:
            storedir = udir
        pdot = storedir.join('%s.dot' % self.graphname)
        pdot.write(source)
        ptarget = pdot.new(ext=target)
        cmdexec('dot -T%s %s>%s' % (target, str(pdot),str(ptarget)))
        return ptarget

    def get_source(self):
        if self.source is None:
            self.emit("}")
            self.source = '\n'.join(self.lines)
            del self.lines
        return self.source

    def emit(self, line):
        self.lines.append(line)

    def enter_subgraph(self, name):
        self.emit("subgraph %s {" % (safename(name),))

    def leave_subgraph(self):
        self.emit("}")

    def emit_edge(self, name1, name2, label="",
                  style="dashed",
                  color="black",
                  dir="forward",
                  weight="5",
                  ports=None,
                  ):
        d = locals()
        attrs = [('%s="%s"' % (x, d[x].replace('"', '\\"').replace('\n', '\\n')))
                 for x in ['label', 'style', 'color', 'dir', 'weight']]
        self.emit('edge [%s];' % ", ".join(attrs))
        if ports:
            self.emit('%s:%s -> %s:%s' % (safename(name1), ports[0],
                                          safename(name2), ports[1]))
        else:
            self.emit('%s -> %s' % (safename(name1), safename(name2)))

    def emit_node(self, name,
                  shape="diamond",
                  label="",
                  color="black",
                  fillcolor="white",
                  style="filled",
                  width="0.75",
                  ):
        d = locals()
        attrs = [('%s="%s"' % (x, d[x].replace('"', '\\"').replace('\n', '\\n')))
                 for x in ['shape', 'label', 'color', 'fillcolor', 'style', 'width']]
        self.emit('%s [%s];' % (safename(name), ", ".join(attrs)))


TAG_TO_COLORS = {
    "timeshifted":  "#cfa5f0",
    "portal":       "#cfa5f0",
    "PortalEntry": "#84abf0",
    "PortalReentry": "#f084c2",
}
DEFAULT_TAG_COLOR = "#a5e6f0"
RETURN_COLOR = "green"
EXCEPT_COLOR = "#ffa000"

class FlowGraphDotGen(DotGen):
    VERBOSE = False

    def __init__(self, graphname, rankdir=None):
        DotGen.__init__(self, graphname.replace('.', '_'), rankdir)

    def emit_subgraph(self, name, node):
        name = name.replace('.', '_') + '_'
        self.blocks = {id(None): '(None)'}
        self.func = None
        self.prefix = name
        self.enter_subgraph(name)
        tagcolor = TAG_TO_COLORS.get(node.tag, DEFAULT_TAG_COLOR)
        self.visit_FunctionGraph(node, tagcolor)
        for block in safe_iterblocks(node):
            self.visit_Block(block, tagcolor)
        self.leave_subgraph()

    def blockname(self, block):
        i = id(block)
        try:
            return self.blocks[i]
        except KeyError:
            self.blocks[i] = name = "%s_%d" % (self.prefix, len(self.blocks))
            return name

    def visit_FunctionGraph(self, funcgraph, tagcolor):
        name = self.prefix # +'_'+funcgraph.name
        data = funcgraph.name
        if getattr(funcgraph, 'source', None) is not None:
            source = funcgraph.source
            if self.VERBOSE:
                data += "\\n"
            else:
                data = ""
            data += "\\l".join(source.split('\n'))
        if hasattr(funcgraph, 'func'):
            self.func = funcgraph.func
        self.emit_node(name, label=data, shape="box", fillcolor=tagcolor, style="filled")
        if hasattr(funcgraph, 'startblock'):
            self.emit_edge(name, self.blockname(funcgraph.startblock), 'startblock')

    def visit_Block(self, block, tagcolor):
        # do the block itself
        name = self.blockname(block)
        if not isinstance(block, Block):
            data = "BROKEN BLOCK\\n%r" % (block,)
            self.emit_node(name, label=data)
            return

        lines = []
        for op in block.operations:
            lines.extend(repr(op).split('\n'))
        lines.append("")
        numblocks = len(block.exits)
        color = "black"
        fillcolor = getattr(block, "blockcolor", "white")
        if not numblocks:
            shape = "box"
            if len(block.inputargs) == 1:
                lines[-1] += 'return %s' % tuple(block.inputargs)
                fillcolor = RETURN_COLOR
            elif len(block.inputargs) == 2:
                lines[-1] += 'raise %s, %s' % tuple(block.inputargs)
                fillcolor = EXCEPT_COLOR
        elif numblocks == 1:
            shape = "box"
        else:
            color = "red"
            shape = "octagon"

        if block.exitswitch is not None:
            lines.append("exitswitch: %s" % (block.exitswitch,))

        iargs = " ".join(map(repr, block.inputargs))
        if self.VERBOSE:
            if block.exc_handler:
                eh = ' (EH)'
            else:
                eh = ''
            data = "%s%s%s\\n" % (name, block.at(), eh)
        else:
            data = "%s\\n" % (name,)
        data += "inputargs: %s\\n\\n" % (iargs,)
        if self.VERBOSE and block.operations and self.func:
            maxoffs = max([op.offset for op in block.operations])
            if maxoffs >= 0:
                minoffs = min([op.offset for op in block.operations
                               if op.offset >= 0])
                minlineno = offset2lineno(self.func.func_code, minoffs)
                maxlineno = offset2lineno(self.func.func_code, maxoffs)
                filename = inspect.getsourcefile(self.func)
                source = "\l".join([linecache.getline(filename, line).rstrip()
                                    for line in range(minlineno, maxlineno+1)])
                if minlineno == maxlineno:
                    data = data + r"line %d:\n%s\l\n" % (minlineno, source)
                else:
                    data = data + r"lines %d-%d:\n%s\l\n" % (minlineno,
                                                             maxlineno, source)

        data = data + "\l".join(lines)

        self.emit_node(name, label=data, shape=shape, color=color, style="filled", fillcolor=fillcolor)

        # do links/exits
        for link in block.exits:
            name2 = self.blockname(link.target)
            label = " ".join(map(repr, link.args))
            if link.exitcase is not None:
                label = "%s: %s" %(repr(link.exitcase).replace('\\', '\\\\'), label)
                self.emit_edge(name, name2, label, style="dotted", color="red")
            else:
                self.emit_edge(name, name2, label, style="solid")


def make_dot(graphname, graph, storedir=None, target='ps'):
    return make_dot_graphs(graph.name, [(graphname, graph)], storedir, target)

def show_dot(graph, storedir = None, target = 'ps'):
    name = graph.name
    fn = make_dot(name, graph, storedir, target)
    os.system('gv %s' % fn)

def make_dot_graphs(basefilename, graphs, storedir=None, target='ps'):
    dotgen = FlowGraphDotGen(basefilename)
    names = {basefilename: True}
    for graphname, graph in graphs:
        if graphname in names:
            i = 2
            while graphname + str(i) in names:
                i += 1
            graphname = graphname + str(i)
        names[graphname] = True
        dotgen.emit_subgraph(graphname, graph)
    return dotgen.generate(storedir, target)

def _makecharmap():
    result = {}
    for i in range(256):
        result[chr(i)] = '_%02X' % i
    for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789':
        result[c] = c
    result['_'] = '__'
    return result
CHAR_MAP = _makecharmap()
del _makecharmap

def safename(name):
    # turn a random string into something that is a valid dot identifier,
    # avoiding invalid characters and prepending '_' to make sure it is
    # not a keyword
    name = ''.join([CHAR_MAP[c] for c in name])
    return '_' + name