1
#!/usr/bin/env python2.4
1
# Copyright (C) 2004, 2005 Aaron Bentley
2
# <aaron@aaronbentley.com>
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
2
18
from subprocess import Popen, PIPE
3
from urllib import urlencode
4
from xml.sax.saxutils import escape
11
24
RSVG_OUTPUT_TYPES = ('png', 'jpg')
12
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png')
25
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
14
28
class NoDot(Exception):
15
29
def __init__(self):
22
36
class Node(object):
23
37
def __init__(self, name, color=None, label=None, rev_id=None,
38
cluster=None, node_style=None, date=None, message=None):
28
42
self.committer = None
29
43
self.rev_id = rev_id
44
if node_style is None:
31
46
self.cluster = cluster
33
def get_committer(self):
34
if self.committer is not None:
35
if '@' in self.committer:
37
return mail_map[self.committer]
41
elif self.rev_id is not None:
43
first_segment = '-'.join(self.rev_id.split('-')[:-2])\
47
if '@' in first_segment:
49
return mail_map[first_segment]
55
committer = self.get_committer()
56
if committer is not None:
57
label = "%s\\n%s" % (self.name, committer)
49
self.message = message
53
def get_attribute(name, value):
56
value = value.replace("\\", "\\\\")
57
value = value.replace('"', '\\"')
58
value = value.replace('\n', '\\n')
59
return '%s="%s"' % (name, value)
70
71
if label is not None:
71
72
attributes.append('label="%s"' % label)
72
73
attributes.append('shape="box"')
75
if self.message is not None:
76
tooltip = self.message
77
attributes.append(self.get_attribute('tooltip', tooltip))
78
if self.href is not None:
79
attributes.append('href="%s"' % self.href)
81
attributes.append('href="#"')
73
82
if len(attributes) > 0:
74
83
return '%s[%s]' % (self.name, " ".join(attributes))
79
def dot_output(relations):
89
def __init__(self, start, end, label=None):
95
def dot(self, do_weight=False):
97
if self.label is not None:
98
attributes.append(('label', self.label))
101
if self.start.cluster == self.end.cluster:
103
elif self.start.rank is None:
105
elif self.end.rank is None:
107
attributes.append(('weight', weight))
108
if len(attributes) > 0:
110
for key, value in attributes:
111
atlist.append("%s=\"%s\"" % (key, value))
112
pq = ' '.join(atlist)
116
return "%s->%s%s;" % (self.start.name, self.end.name, op)
118
def make_edge(relation):
119
if hasattr(relation, 'start') and hasattr(relation, 'end'):
121
return Edge(relation[0], relation[1])
123
def dot_output(relations, ranking="forced"):
81
125
yield "digraph G\n"
128
edges = [make_edge(f) for f in relations]
84
129
def rel_appropriate(start, end, cluster):
85
130
if cluster is None:
86
return start.cluster is None or end.cluster is None
131
return (start.cluster is None and end.cluster is None) or \
132
start.cluster != end.cluster
88
134
return start.cluster==cluster and end.cluster==cluster
90
for (start, end) in relations:
91
if start.cluster is not None:
92
clusters.add(start.cluster)
93
if end.cluster is not None:
94
clusters.add(end.cluster)
137
if edge.start.cluster is not None:
138
clusters.add(edge.start.cluster)
139
if edge.end.cluster is not None:
140
clusters.add(edge.end.cluster)
95
141
clusters = list(clusters)
96
142
clusters.append(None)
97
143
for index, cluster in enumerate(clusters):
98
if cluster is not None:
144
if cluster is not None and ranking == "cluster":
99
145
yield "subgraph cluster_%s\n" % index
101
147
yield ' label="%s"\n' % cluster
102
for (start, end) in relations:
103
if start.name not in defined and start.cluster == cluster:
104
defined.add(start.name)
105
my_def = start.define()
106
if my_def is not None:
107
yield " %s;\n" % my_def
108
if end.name not in defined and end.cluster == cluster:
109
defined.add(end.name)
110
my_def = end.define()
111
if my_def is not None:
112
yield " %s;\n" % my_def
113
if rel_appropriate(start, end, cluster):
114
yield " %s->%s;\n" % (start.name, end.name)
115
if cluster is not None:
149
if edge.start.name not in defined and edge.start.cluster == cluster:
150
defined[edge.start.name] = edge.start
151
my_def = edge.start.define()
152
if my_def is not None:
153
yield " %s\n" % my_def
154
if edge.end.name not in defined and edge.end.cluster == cluster:
155
defined[edge.end.name] = edge.end
156
my_def = edge.end.define()
157
if my_def is not None:
158
yield " %s;\n" % my_def
159
if rel_appropriate(edge.start, edge.end, cluster):
160
yield " %s\n" % edge.dot(do_weight=ranking=="forced")
161
if cluster is not None and ranking == "cluster":
164
if ranking == "forced":
166
for node in defined.itervalues():
167
if node.rank not in ranks:
168
ranks[node.rank] = set()
169
ranks[node.rank].add(node.name)
170
sorted_ranks = [n for n in ranks.iteritems()]
173
for rank, nodes in sorted_ranks:
176
yield 'rank%d[style="invis"];\n' % rank
177
if last_rank is not None:
178
yield 'rank%d -> rank%d[style="invis"];\n' % (last_rank, rank)
180
for rank, nodes in ranks.iteritems():
183
node_text = "; ".join('"%s"' % n for n in nodes)
184
yield ' {rank = same; "rank%d"; %s}\n' % (rank, node_text)
119
187
def invoke_dot_aa(input, out_file, file_type='png'):
135
203
shutil.rmtree(tempdir)
138
def invoke_dot(input, out_file=None, file_type='svg', antialias=None):
139
cmdline = ['dot', '-T%s' % file_type]
206
def invoke_dot(input, out_file=None, file_type='svg', antialias=None,
207
fontname="Helvetica", fontsize=11):
208
cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname,
209
'-Efontname=%s' % fontname, '-Nfontsize=%d' % fontsize,
210
'-Efontsize=%d' % fontsize]
140
211
if out_file is not None:
141
212
cmdline.extend(('-o', out_file))
149
220
for line in input:
150
dot_proc.stdin.write(line)
221
dot_proc.stdin.write(line.encode('utf-8'))
151
222
dot_proc.stdin.close()
152
223
return dot_proc.wait()
225
def invoke_dot_html(input, out_file):
227
Produce an html file, which uses a .png file, and a cmap to provide
230
tempdir = tempfile.mkdtemp()
232
temp_dot = os.path.join(tempdir, 'temp.dot')
233
status = invoke_dot(input, temp_dot, file_type='dot')
236
temp_file = os.path.join(tempdir, 'temp.cmapx')
237
status = invoke_dot(dot, temp_file, 'cmapx')
239
png_file = '.'.join(out_file.split('.')[:-1] + ['png'])
241
status = invoke_dot(dot, png_file, 'png')
243
png_relative = png_file.split('/')[-1]
244
html = open(out_file, 'wb')
246
w('<html><head><title></title></head>\n')
248
w('<img src="%s" usemap="#G" border=0/>' % png_relative)
249
w(open(temp_file).read())
250
w('</body></html>\n')
252
shutil.rmtree(tempdir)