1
# Copyright (C) 2004, 2005 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from subprocess import Popen, PIPE
19
from urllib import urlencode
20
from xml.sax.saxutils import escape
27
RSVG_OUTPUT_TYPES = ('png', 'jpg')
28
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
31
class NoDot(Exception):
33
Exception.__init__(self, "Can't find dot!")
35
class NoRsvg(Exception):
37
Exception.__init__(self, "Can't find rsvg!")
40
def __init__(self, name, color=None, label=None, rev_id=None,
41
cluster=None, node_style=None, date=None, message=None):
47
if node_style is None:
49
self.cluster = cluster
52
self.message = message
58
if self.color is not None:
59
attributes.append('fillcolor="%s"' % self.color)
60
style.append('filled')
61
style.extend(self.node_style)
63
attributes.append('style="%s"' % ",".join(style))
66
attributes.append('label="%s"' % label)
67
attributes.append('shape="box"')
69
if self.message is not None:
70
tooltip += self.message.replace('"', '\\"')
72
attributes.append('tooltip="%s"' % tooltip)
73
if self.href is not None:
74
attributes.append('href="%s"' % self.href)
76
attributes.append('href="#"')
77
if len(attributes) > 0:
78
return '%s[%s]' % (self.name, " ".join(attributes))
84
def __init__(self, start, end, label=None):
90
def dot(self, do_weight=False):
92
if self.label is not None:
93
attributes.append(('label', self.label))
96
if self.start.cluster == self.end.cluster:
98
elif self.start.rank is None:
100
elif self.end.rank is None:
102
attributes.append(('weight', weight))
103
if len(attributes) > 0:
105
for key, value in attributes:
106
atlist.append("%s=\"%s\"" % (key, value))
107
pq = ' '.join(atlist)
111
return "%s->%s%s;" % (self.start.name, self.end.name, op)
113
def make_edge(relation):
114
if hasattr(relation, 'start') and hasattr(relation, 'end'):
116
return Edge(relation[0], relation[1])
118
def dot_output(relations, ranking="forced"):
123
edges = [make_edge(f) for f in relations]
124
def rel_appropriate(start, end, cluster):
126
return (start.cluster is None and end.cluster is None) or \
127
start.cluster != end.cluster
129
return start.cluster==cluster and end.cluster==cluster
132
if edge.start.cluster is not None:
133
clusters.add(edge.start.cluster)
134
if edge.end.cluster is not None:
135
clusters.add(edge.end.cluster)
136
clusters = list(clusters)
137
clusters.append(None)
138
for index, cluster in enumerate(clusters):
139
if cluster is not None and ranking == "cluster":
140
yield "subgraph cluster_%s\n" % index
142
yield ' label="%s"\n' % cluster
144
if edge.start.name not in defined and edge.start.cluster == cluster:
145
defined[edge.start.name] = edge.start
146
my_def = edge.start.define()
147
if my_def is not None:
148
yield " %s\n" % my_def
149
if edge.end.name not in defined and edge.end.cluster == cluster:
150
defined[edge.end.name] = edge.end
151
my_def = edge.end.define()
152
if my_def is not None:
153
yield " %s;\n" % my_def
154
if rel_appropriate(edge.start, edge.end, cluster):
155
yield " %s\n" % edge.dot(do_weight=ranking=="forced")
156
if cluster is not None and ranking == "cluster":
159
if ranking == "forced":
161
for node in defined.itervalues():
162
if node.rank not in ranks:
163
ranks[node.rank] = set()
164
ranks[node.rank].add(node.name)
165
sorted_ranks = [n for n in ranks.iteritems()]
168
for rank, nodes in sorted_ranks:
171
yield 'rank%d[style="invis"];\n' % rank
172
if last_rank is not None:
173
yield 'rank%d -> rank%d[style="invis"];\n' % (last_rank, rank)
175
for rank, nodes in ranks.iteritems():
178
node_text = "; ".join('"%s"' % n for n in nodes)
179
yield ' {rank = same; "rank%d"; %s}\n' % (rank, node_text)
182
def invoke_dot_aa(input, out_file, file_type='png'):
184
Produce antialiased Dot output, invoking rsvg on an intermediate file.
185
rsvg only supports png, jpeg and .ico files."""
186
tempdir = tempfile.mkdtemp()
188
temp_file = os.path.join(tempdir, 'temp.svg')
189
invoke_dot(input, temp_file, 'svg')
190
cmdline = ['rsvg', temp_file, out_file]
192
rsvg_proc = Popen(cmdline)
194
if e.errno == errno.ENOENT:
196
status = rsvg_proc.wait()
198
shutil.rmtree(tempdir)
201
def invoke_dot(input, out_file=None, file_type='svg', antialias=None,
202
fontname="Helvetica", fontsize=11):
203
cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname,
204
'-Efontname=%s' % fontname, '-Nfontsize=%d' % fontsize,
205
'-Efontsize=%d' % fontsize]
206
if out_file is not None:
207
cmdline.extend(('-o', out_file))
209
dot_proc = Popen(cmdline, stdin=PIPE)
211
if e.errno == errno.ENOENT:
216
dot_proc.stdin.write(line.encode('utf-8'))
217
dot_proc.stdin.close()
218
return dot_proc.wait()
220
def invoke_dot_html(input, out_file):
222
Produce an html file, which uses a .png file, and a cmap to provide
225
tempdir = tempfile.mkdtemp()
227
temp_dot = os.path.join(tempdir, 'temp.dot')
228
status = invoke_dot(input, temp_dot, file_type='dot')
231
temp_file = os.path.join(tempdir, 'temp.cmapx')
232
status = invoke_dot(dot, temp_file, 'cmapx')
234
png_file = '.'.join(out_file.split('.')[:-1] + ['png'])
236
status = invoke_dot(dot, png_file, 'png')
238
png_relative = png_file.split('/')[-1]
239
html = open(out_file, 'wb')
241
w('<html><head><title></title></head>\n')
243
w('<img src="%s" usemap="#G" border=0/>' % png_relative)
244
w(open(temp_file).read())
245
w('</body></html>\n')
247
shutil.rmtree(tempdir)