/usr/share/pyshared/pycallgraph/output/graphviz.py is in python-pycallgraph 1.0.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from __future__ import division
import tempfile
import os
import textwrap
from ..metadata import __version__
from ..exceptions import PyCallGraphException
from ..color import Color
from .output import Output
class GraphvizOutput(Output):
def __init__(self, **kwargs):
self.tool = 'dot'
self.output_file = 'pycallgraph.png'
self.output_type = 'png'
self.font_name = 'Verdana'
self.font_size = 7
self.group_font_size = 10
self.group_border_color = Color(0, 0, 0, 0.8)
Output.__init__(self, **kwargs)
self.prepare_graph_attributes()
@classmethod
def add_arguments(cls, subparsers, parent_parser, usage):
defaults = cls()
subparser = subparsers.add_parser(
'graphviz', help='Graphviz generation',
parents=[parent_parser], usage=usage,
)
subparser.add_argument(
'-l', '--tool', dest='tool', default=defaults.tool,
help='The tool from Graphviz to use, e.g. dot, neato, etc.',
)
cls.add_output_file(
subparser, defaults, 'The generated Graphviz file'
)
subparser.add_argument(
'-f', '--output-format', type=str, default=defaults.output_type,
help='Image format to produce, e.g. png, ps, dot, etc. '
'See http://www.graphviz.org/doc/info/output.html for more.',
)
subparser.add_argument(
'--font-name', type=str, default=defaults.font_name,
help='Name of the font to be used',
)
subparser.add_argument(
'--font-size', type=int, default=defaults.font_size,
help='Size of the font to be used',
)
def sanity_check(self):
self.ensure_binary(self.tool)
def prepare_graph_attributes(self):
generated_message = '\\n'.join([
r'Generated by Python Call Graph v%s' % __version__,
r'http://pycallgraph.slowchop.com',
])
self.graph_attributes = {
'graph': {
'overlap': 'scalexy',
'fontname': self.font_name,
'fontsize': self.font_size,
'fontcolor': Color(0, 0, 0, 0.5).rgba_web(),
'label': generated_message,
},
'node': {
'fontname': self.font_name,
'fontsize': self.font_size,
'fontcolor': Color(0, 0, 0).rgba_web(),
'style': 'filled',
'shape': 'rect',
},
'edge': {
'fontname': self.font_name,
'fontsize': self.font_size,
'fontcolor': Color(0, 0, 0).rgba_web(),
}
}
def done(self):
source = self.generate()
self.debug(source)
fd, temp_name = tempfile.mkstemp()
with os.fdopen(fd, 'w') as f:
f.write(source)
cmd = '{} -T{} -o{} {}'.format(
self.tool, self.output_type, self.output_file, temp_name
)
self.verbose('Executing: {}'.format(cmd))
try:
ret = os.system(cmd)
if ret:
raise PyCallGraphException(
'The command "%(cmd)s" failed with error '
'code %(ret)i.' % locals())
finally:
os.unlink(temp_name)
self.verbose('Generated {} with {} nodes.'.format(
self.output_file, len(self.processor.func_count),
))
def generate(self):
'''Returns a string with the contents of a DOT file for Graphviz to
parse.
'''
indent_join = '\n' + ' ' * 12
return textwrap.dedent('''\
digraph G {{
// Attributes
{}
// Groups
{}
// Nodes
{}
// Edges
{}
}}
'''.format(
indent_join.join(self.generate_attributes()),
indent_join.join(self.generate_groups()),
indent_join.join(self.generate_nodes()),
indent_join.join(self.generate_edges()),
))
def attrs_from_dict(self, d):
output = []
for attr, val in d.iteritems():
output.append('%s = "%s"' % (attr, val))
return ', '.join(output)
def node(self, key, attr):
return '"{}" [{}];'.format(
key, self.attrs_from_dict(attr),
)
def edge(self, edge, attr):
return '"{0.src_func}" -> "{0.dst_func}" [{1}];'.format(
edge, self.attrs_from_dict(attr),
)
def generate_attributes(self):
output = []
for section, attrs in self.graph_attributes.iteritems():
output.append('{} [ {} ];'.format(
section, self.attrs_from_dict(attrs),
))
return output
def generate_groups(self):
if not self.processor.config.groups:
return ''
output = []
for group, nodes in self.processor.groups():
funcs = [node.name for node in nodes]
funcs = '" "'.join(funcs)
group_color = self.group_border_color.rgba_web()
group_font_size = self.group_font_size
output.append(
'subgraph "cluster_{group}" {{ '
'"{funcs}"; '
'label = "{group}"; '
'fontsize = "{group_font_size}"; '
'fontcolor = "black"; '
'style = "bold"; '
'color="{group_color}"; }}'.format(**locals()))
return output
def generate_nodes(self):
output = []
for node in self.processor.nodes():
attr = {
'color': self.node_color_func(node).rgba_web(),
'label': self.node_label_func(node),
}
output.append(self.node(node.name, attr))
return output
def generate_edges(self):
output = []
for edge in self.processor.edges():
attr = {
'color': self.edge_color_func(edge).rgba_web(),
'label': self.edge_label_func(edge),
}
output.append(self.edge(edge, attr))
return output
|