~abentley/bzrtools/bzrtools.dev

257.1.2 by Aaron Bentley
Updated GPL notices
1
# Copyright (C) 2004, 2005 Aaron Bentley
612 by Aaron Bentley
Update email address
2
# <aaron@aaronbentley.com>
257.1.2 by Aaron Bentley
Updated GPL notices
3
#
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.
8
#
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.
13
#
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
17
125 by Aaron Bentley
Added pastegraph
18
from subprocess import Popen, PIPE
19
import os.path
139 by Aaron Bentley
Tweaked missing-dot handling
20
import errno
143 by Aaron Bentley
Used rsvga for nice antialiasing
21
import tempfile
22
import shutil
139 by Aaron Bentley
Tweaked missing-dot handling
23
167 by Aaron Bentley
Moved extention lists to dotgraph
24
RSVG_OUTPUT_TYPES = ('png', 'jpg')
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
25
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
189 by Aaron Bentley
Enabled client-side imagemaps
26
                    'cmapx')
140 by Aaron Bentley
Mapped some email addresses to names
27
139 by Aaron Bentley
Tweaked missing-dot handling
28
class NoDot(Exception):
29
    def __init__(self):
30
        Exception.__init__(self, "Can't find dot!")
125 by Aaron Bentley
Added pastegraph
31
143 by Aaron Bentley
Used rsvga for nice antialiasing
32
class NoRsvg(Exception):
33
    def __init__(self):
34
        Exception.__init__(self, "Can't find rsvg!")
35
125 by Aaron Bentley
Added pastegraph
36
class Node(object):
142 by Aaron Bentley
Clustered the branch revision history
37
    def __init__(self, name, color=None, label=None, rev_id=None,
189.1.1 by John Arbash Meinel
Adding an html target.
38
                 cluster=None, node_style=None, date=None, message=None):
125 by Aaron Bentley
Added pastegraph
39
        self.name = name
40
        self.color = color
128 by Aaron Bentley
Got initial graphing functionality working
41
        self.label = label
130 by Aaron Bentley
Added committer to revisions
42
        self.committer = None
137 by Aaron Bentley
Put dotted outlines on missing revisions
43
        self.rev_id = rev_id
172 by Aaron Bentley
Marked missing nodes
44
        if node_style is None:
45
            self.node_style = []
142 by Aaron Bentley
Clustered the branch revision history
46
        self.cluster = cluster
178 by Aaron Bentley
Switched from clusters to forced ranking
47
        self.rank = None
189.1.1 by John Arbash Meinel
Adding an html target.
48
        self.date = date
49
        self.message = message
189 by Aaron Bentley
Enabled client-side imagemaps
50
        self.href = None
135 by Aaron Bentley
Enhanced revision-crediting
51
628 by Aaron Bentley
Add escaping to HTML output
52
    @staticmethod
53
    def get_attribute(name, value):
54
        if value is None:
55
            return ''
56
        value = value.replace("\\", "\\\\")
57
        value = value.replace('"', '\\"')
58
        value = value.replace('\n', '\\n')
59
        return '%s="%s"' % (name, value)
60
125 by Aaron Bentley
Added pastegraph
61
    def define(self):
137 by Aaron Bentley
Put dotted outlines on missing revisions
62
        attributes = []
130 by Aaron Bentley
Added committer to revisions
63
        style = []
125 by Aaron Bentley
Added pastegraph
64
        if self.color is not None:
137 by Aaron Bentley
Put dotted outlines on missing revisions
65
            attributes.append('fillcolor="%s"' % self.color)
66
            style.append('filled')
67
        style.extend(self.node_style)
68
        if len(style) > 0:
69
            attributes.append('style="%s"' % ",".join(style))
157 by Aaron Bentley
Got graph showing merge selection decently
70
        label = self.label
135 by Aaron Bentley
Enhanced revision-crediting
71
        if label is not None:
137 by Aaron Bentley
Put dotted outlines on missing revisions
72
            attributes.append('label="%s"' % label)
141 by Aaron Bentley
Switched to use boxes
73
        attributes.append('shape="box"')
628 by Aaron Bentley
Add escaping to HTML output
74
        tooltip = None
189.1.1 by John Arbash Meinel
Adding an html target.
75
        if self.message is not None:
628 by Aaron Bentley
Add escaping to HTML output
76
            tooltip = self.message
77
        attributes.append(self.get_attribute('tooltip', tooltip))
189 by Aaron Bentley
Enabled client-side imagemaps
78
        if self.href is not None:
79
            attributes.append('href="%s"' % self.href)
189.1.1 by John Arbash Meinel
Adding an html target.
80
        elif tooltip:
81
            attributes.append('href="#"')
137 by Aaron Bentley
Put dotted outlines on missing revisions
82
        if len(attributes) > 0:
83
            return '%s[%s]' % (self.name, " ".join(attributes))
125 by Aaron Bentley
Added pastegraph
84
85
    def __str__(self):
86
        return self.name
87
175 by Aaron Bentley
Added edges
88
class Edge(object):
89
    def __init__(self, start, end, label=None):
90
        object.__init__(self)
91
        self.start = start
92
        self.end = end
93
        self.label = label
94
178 by Aaron Bentley
Switched from clusters to forced ranking
95
    def dot(self, do_weight=False):
175 by Aaron Bentley
Added edges
96
        attributes = []
97
        if self.label is not None:
98
            attributes.append(('label', self.label))
178 by Aaron Bentley
Switched from clusters to forced ranking
99
        if do_weight:
182 by Aaron Bentley
Set edge weight to 1 for missing revisions
100
            weight = '0'
178 by Aaron Bentley
Switched from clusters to forced ranking
101
            if self.start.cluster == self.end.cluster:
182 by Aaron Bentley
Set edge weight to 1 for missing revisions
102
                weight = '1'
103
            elif self.start.rank is None:
104
                weight = '1'
105
            elif self.end.rank is None:
106
                weight = '1'
107
            attributes.append(('weight', weight))
175 by Aaron Bentley
Added edges
108
        if len(attributes) > 0:
109
            atlist = []
110
            for key, value in attributes:
111
                atlist.append("%s=\"%s\"" % (key, value))
112
            pq = ' '.join(atlist)
113
            op = "[%s]" % pq
114
        else:
115
            op = ""
116
        return "%s->%s%s;" % (self.start.name, self.end.name, op)
117
118
def make_edge(relation):
176 by Aaron Bentley
Added skip labels to edges
119
    if hasattr(relation, 'start') and hasattr(relation, 'end'):
120
        return relation
175 by Aaron Bentley
Added edges
121
    return Edge(relation[0], relation[1])
122
178 by Aaron Bentley
Switched from clusters to forced ranking
123
def dot_output(relations, ranking="forced"):
124
    defined = {}
125 by Aaron Bentley
Added pastegraph
125
    yield "digraph G\n"
126
    yield "{\n"
142 by Aaron Bentley
Clustered the branch revision history
127
    clusters = set()
175 by Aaron Bentley
Added edges
128
    edges = [make_edge(f) for f in relations]
142 by Aaron Bentley
Clustered the branch revision history
129
    def rel_appropriate(start, end, cluster):
130
        if cluster is None:
169 by Aaron Bentley
Got ancestry-graph showing common and revision-history nodes properly
131
            return (start.cluster is None and end.cluster is None) or \
132
                start.cluster != end.cluster
142 by Aaron Bentley
Clustered the branch revision history
133
        else:
134
            return start.cluster==cluster and end.cluster==cluster
135
175 by Aaron Bentley
Added edges
136
    for edge in edges:
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)
142 by Aaron Bentley
Clustered the branch revision history
141
    clusters = list(clusters)
142
    clusters.append(None)
143
    for index, cluster in enumerate(clusters):
178 by Aaron Bentley
Switched from clusters to forced ranking
144
        if cluster is not None and ranking == "cluster":
142 by Aaron Bentley
Clustered the branch revision history
145
            yield "subgraph cluster_%s\n" % index
146
            yield "{\n"
147
            yield '    label="%s"\n' % cluster
175 by Aaron Bentley
Added edges
148
        for edge in edges:
149
            if edge.start.name not in defined and edge.start.cluster == cluster:
178 by Aaron Bentley
Switched from clusters to forced ranking
150
                defined[edge.start.name] = edge.start
175 by Aaron Bentley
Added edges
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:
178 by Aaron Bentley
Switched from clusters to forced ranking
155
                defined[edge.end.name] = edge.end
175 by Aaron Bentley
Added edges
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):
178 by Aaron Bentley
Switched from clusters to forced ranking
160
                yield "    %s\n" % edge.dot(do_weight=ranking=="forced")
161
        if cluster is not None and ranking == "cluster":
142 by Aaron Bentley
Clustered the branch revision history
162
            yield "}\n"
178 by Aaron Bentley
Switched from clusters to forced ranking
163
164
    if ranking == "forced":
165
        ranks = {}
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()]
171
        sorted_ranks.sort()
172
        last_rank = None
173
        for rank, nodes in sorted_ranks:
174
            if rank is None:
175
                continue
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)
179
            last_rank = rank
180
        for rank, nodes in ranks.iteritems():
181
            if rank is None:
182
                continue
183
            node_text = "; ".join('"%s"' % n for n in nodes)
184
            yield ' {rank = same; "rank%d"; %s}\n' % (rank, node_text)
125 by Aaron Bentley
Added pastegraph
185
    yield "}\n"
186
143 by Aaron Bentley
Used rsvga for nice antialiasing
187
def invoke_dot_aa(input, out_file, file_type='png'):
188
    """\
189
    Produce antialiased Dot output, invoking rsvg on an intermediate file.
190
    rsvg only supports png, jpeg and .ico files."""
191
    tempdir = tempfile.mkdtemp()
192
    try:
193
        temp_file = os.path.join(tempdir, 'temp.svg')
194
        invoke_dot(input, temp_file, 'svg')
195
        cmdline = ['rsvg', temp_file, out_file]
196
        try:
197
            rsvg_proc = Popen(cmdline)
198
        except OSError, e:
199
            if e.errno == errno.ENOENT:
200
                raise NoRsvg()
201
        status = rsvg_proc.wait()
202
    finally:
203
        shutil.rmtree(tempdir)
204
    return status
205
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
206
def invoke_dot(input, out_file=None, file_type='svg', antialias=None,
179 by Aaron Bentley
Enforced a font and size
207
               fontname="Helvetica", fontsize=11):
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
208
    cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname,
179 by Aaron Bentley
Enforced a font and size
209
               '-Efontname=%s' % fontname, '-Nfontsize=%d' % fontsize,
210
               '-Efontsize=%d' % fontsize]
125 by Aaron Bentley
Added pastegraph
211
    if out_file is not None:
212
        cmdline.extend(('-o', out_file))
138 by Aaron Bentley
Handle systems without dot (the horror!). From Magnus Therning.
213
    try:
214
        dot_proc = Popen(cmdline, stdin=PIPE)
215
    except OSError, e:
139 by Aaron Bentley
Tweaked missing-dot handling
216
        if e.errno == errno.ENOENT:
217
            raise NoDot()
218
        else:
219
            raise
125 by Aaron Bentley
Added pastegraph
220
    for line in input:
312 by Aaron Bentley
forced ancestry graph to use utf-8 Dot output
221
        dot_proc.stdin.write(line.encode('utf-8'))
125 by Aaron Bentley
Added pastegraph
222
    dot_proc.stdin.close()
223
    return dot_proc.wait()
189.1.1 by John Arbash Meinel
Adding an html target.
224
225
def invoke_dot_html(input, out_file):
226
    """\
227
    Produce an html file, which uses a .png file, and a cmap to provide
228
    annotated revisions.
229
    """
230
    tempdir = tempfile.mkdtemp()
231
    try:
232
        temp_dot = os.path.join(tempdir, 'temp.dot')
189.1.2 by John Arbash Meinel
Fix datestamp, reuse pre-layed out dot file.
233
        status = invoke_dot(input, temp_dot, file_type='dot')
234
189.1.1 by John Arbash Meinel
Adding an html target.
235
        dot = open(temp_dot)
236
        temp_file = os.path.join(tempdir, 'temp.cmapx')
237
        status = invoke_dot(dot, temp_file, 'cmapx')
238
239
        png_file = '.'.join(out_file.split('.')[:-1] + ['png'])
240
        dot.seek(0)
241
        status = invoke_dot(dot, png_file, 'png')
242
243
        png_relative = png_file.split('/')[-1]
244
        html = open(out_file, 'wb')
245
        w = html.write
246
        w('<html><head><title></title></head>\n')
247
        w('<body>\n')
248
        w('<img src="%s" usemap="#G" border=0/>' % png_relative)
249
        w(open(temp_file).read())
250
        w('</body></html>\n')
251
    finally:
252
        shutil.rmtree(tempdir)
253
    return status
254