~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to dotgraph.py

  • Committer: Aaron Bentley
  • Date: 2005-09-22 17:50:29 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050922175029-10670eb4d37ded38
Switched from clusters to forced ranking

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Aaron Bentley
2
 
# <aaron@aaronbentley.com>
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
 
 
 
1
#!/usr/bin/env python2.4
18
2
from subprocess import Popen, PIPE
 
3
from urllib import urlencode
 
4
from xml.sax.saxutils import escape
19
5
import os.path
20
6
import errno
21
7
import tempfile
22
8
import shutil
23
9
 
 
10
mail_map = {}
24
11
RSVG_OUTPUT_TYPES = ('png', 'jpg')
25
 
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
26
 
                    'cmapx')
 
12
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png')
27
13
 
28
14
class NoDot(Exception):
29
15
    def __init__(self):
35
21
 
36
22
class Node(object):
37
23
    def __init__(self, name, color=None, label=None, rev_id=None,
38
 
                 cluster=None, node_style=None, date=None, message=None):
 
24
                 cluster=None, node_style=None):
39
25
        self.name = name
40
26
        self.color = color
41
27
        self.label = label
45
31
            self.node_style = []
46
32
        self.cluster = cluster
47
33
        self.rank = None
48
 
        self.date = date
49
 
        self.message = message
50
 
        self.href = None
51
 
 
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)
 
34
 
 
35
    def get_committer(self):
 
36
        if self.committer is not None:
 
37
            if '@' in self.committer:
 
38
                try:
 
39
                    return mail_map[self.committer]
 
40
                except KeyError:
 
41
                    pass
 
42
            return self.committer
 
43
        elif self.rev_id is not None:
 
44
            try:
 
45
                first_segment = '-'.join(self.rev_id.split('-')[:-2])\
 
46
                    .strip(' ')
 
47
            except ValueError:
 
48
                first_segment = []
 
49
            if '@' in first_segment:
 
50
                try:
 
51
                    return mail_map[first_segment]
 
52
                except KeyError:
 
53
                    return first_segment
 
54
 
 
55
    def get_label(self):
 
56
        label = None
 
57
        committer = self.get_committer()
 
58
        if committer is not None:
 
59
            label = "%s\\n%s" % (self.name, committer)
 
60
        return label
60
61
 
61
62
    def define(self):
62
63
        attributes = []
71
72
        if label is not None:
72
73
            attributes.append('label="%s"' % label)
73
74
        attributes.append('shape="box"')
74
 
        tooltip = None
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)
80
 
        elif tooltip:
81
 
            attributes.append('href="#"')
82
75
        if len(attributes) > 0:
83
76
            return '%s[%s]' % (self.name, " ".join(attributes))
84
77
 
97
90
        if self.label is not None:
98
91
            attributes.append(('label', self.label))
99
92
        if do_weight:
100
 
            weight = '0'
101
93
            if self.start.cluster == self.end.cluster:
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))
 
94
                attributes.append(('weight', '1'))
 
95
            else:
 
96
                attributes.append(('weight', '0'))
108
97
        if len(attributes) > 0:
109
98
            atlist = []
110
99
            for key, value in attributes:
203
192
        shutil.rmtree(tempdir)
204
193
    return status
205
194
 
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]
 
195
def invoke_dot(input, out_file=None, file_type='svg', antialias=None):
 
196
    cmdline = ['dot', '-T%s' % file_type]
211
197
    if out_file is not None:
212
198
        cmdline.extend(('-o', out_file))
213
199
    try:
218
204
        else:
219
205
            raise
220
206
    for line in input:
221
 
        dot_proc.stdin.write(line.encode('utf-8'))
 
207
        dot_proc.stdin.write(line)
222
208
    dot_proc.stdin.close()
223
209
    return dot_proc.wait()
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')
233
 
        status = invoke_dot(input, temp_dot, file_type='dot')
234
 
 
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