1
#!/usr/bin/env python2.4
2
from subprocess import Popen, PIPE
3
from urllib import urlencode
4
from xml.sax.saxutils import escape
11
RSVG_OUTPUT_TYPES = ('png', 'jpg')
12
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
15
class NoDot(Exception):
17
Exception.__init__(self, "Can't find dot!")
19
class NoRsvg(Exception):
21
Exception.__init__(self, "Can't find rsvg!")
24
def __init__(self, name, color=None, label=None, rev_id=None,
25
cluster=None, node_style=None, date=None, message=None):
31
if node_style is None:
33
self.cluster = cluster
36
self.message = message
42
if self.color is not None:
43
attributes.append('fillcolor="%s"' % self.color)
44
style.append('filled')
45
style.extend(self.node_style)
47
attributes.append('style="%s"' % ",".join(style))
50
attributes.append('label="%s"' % label)
51
attributes.append('shape="box"')
53
if self.message is not None:
54
tooltip += self.message.replace('"', '\\"')
56
attributes.append('tooltip="%s"' % tooltip)
57
if self.href is not None:
58
attributes.append('href="%s"' % self.href)
60
attributes.append('href="#"')
61
if len(attributes) > 0:
62
return '%s[%s]' % (self.name, " ".join(attributes))
68
def __init__(self, start, end, label=None):
74
def dot(self, do_weight=False):
76
if self.label is not None:
77
attributes.append(('label', self.label))
80
if self.start.cluster == self.end.cluster:
82
elif self.start.rank is None:
84
elif self.end.rank is None:
86
attributes.append(('weight', weight))
87
if len(attributes) > 0:
89
for key, value in attributes:
90
atlist.append("%s=\"%s\"" % (key, value))
95
return "%s->%s%s;" % (self.start.name, self.end.name, op)
97
def make_edge(relation):
98
if hasattr(relation, 'start') and hasattr(relation, 'end'):
100
return Edge(relation[0], relation[1])
102
def dot_output(relations, ranking="forced"):
107
edges = [make_edge(f) for f in relations]
108
def rel_appropriate(start, end, cluster):
110
return (start.cluster is None and end.cluster is None) or \
111
start.cluster != end.cluster
113
return start.cluster==cluster and end.cluster==cluster
116
if edge.start.cluster is not None:
117
clusters.add(edge.start.cluster)
118
if edge.end.cluster is not None:
119
clusters.add(edge.end.cluster)
120
clusters = list(clusters)
121
clusters.append(None)
122
for index, cluster in enumerate(clusters):
123
if cluster is not None and ranking == "cluster":
124
yield "subgraph cluster_%s\n" % index
126
yield ' label="%s"\n' % cluster
128
if edge.start.name not in defined and edge.start.cluster == cluster:
129
defined[edge.start.name] = edge.start
130
my_def = edge.start.define()
131
if my_def is not None:
132
yield " %s\n" % my_def
133
if edge.end.name not in defined and edge.end.cluster == cluster:
134
defined[edge.end.name] = edge.end
135
my_def = edge.end.define()
136
if my_def is not None:
137
yield " %s;\n" % my_def
138
if rel_appropriate(edge.start, edge.end, cluster):
139
yield " %s\n" % edge.dot(do_weight=ranking=="forced")
140
if cluster is not None and ranking == "cluster":
143
if ranking == "forced":
145
for node in defined.itervalues():
146
if node.rank not in ranks:
147
ranks[node.rank] = set()
148
ranks[node.rank].add(node.name)
149
sorted_ranks = [n for n in ranks.iteritems()]
152
for rank, nodes in sorted_ranks:
155
yield 'rank%d[style="invis"];\n' % rank
156
if last_rank is not None:
157
yield 'rank%d -> rank%d[style="invis"];\n' % (last_rank, rank)
159
for rank, nodes in ranks.iteritems():
162
node_text = "; ".join('"%s"' % n for n in nodes)
163
yield ' {rank = same; "rank%d"; %s}\n' % (rank, node_text)
166
def invoke_dot_aa(input, out_file, file_type='png'):
168
Produce antialiased Dot output, invoking rsvg on an intermediate file.
169
rsvg only supports png, jpeg and .ico files."""
170
tempdir = tempfile.mkdtemp()
172
temp_file = os.path.join(tempdir, 'temp.svg')
173
invoke_dot(input, temp_file, 'svg')
174
cmdline = ['rsvg', temp_file, out_file]
176
rsvg_proc = Popen(cmdline)
178
if e.errno == errno.ENOENT:
180
status = rsvg_proc.wait()
182
shutil.rmtree(tempdir)
185
def invoke_dot(input, out_file=None, file_type='svg', antialias=None,
186
fontname="Helvetica", fontsize=11):
187
cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname,
188
'-Efontname=%s' % fontname, '-Nfontsize=%d' % fontsize,
189
'-Efontsize=%d' % fontsize]
190
if out_file is not None:
191
cmdline.extend(('-o', out_file))
193
dot_proc = Popen(cmdline, stdin=PIPE)
195
if e.errno == errno.ENOENT:
200
dot_proc.stdin.write(line)
201
dot_proc.stdin.close()
202
return dot_proc.wait()
204
def invoke_dot_html(input, out_file):
206
Produce an html file, which uses a .png file, and a cmap to provide
209
tempdir = tempfile.mkdtemp()
211
temp_dot = os.path.join(tempdir, 'temp.dot')
212
status = invoke_dot(input, temp_dot, file_type='dot')
215
temp_file = os.path.join(tempdir, 'temp.cmapx')
216
status = invoke_dot(dot, temp_file, 'cmapx')
218
png_file = '.'.join(out_file.split('.')[:-1] + ['png'])
220
status = invoke_dot(dot, png_file, 'png')
222
png_relative = png_file.split('/')[-1]
223
html = open(out_file, 'wb')
225
w('<html><head><title></title></head>\n')
227
w('<img src="%s" usemap="#G" border=0/>' % png_relative)
228
w(open(temp_file).read())
229
w('</body></html>\n')
231
shutil.rmtree(tempdir)