~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to dotgraph.py

  • Committer: Aaron Bentley
  • Date: 2005-09-22 18:43:45 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050922184345-1369d3faed55b34a
Set edge weight to 1 for missing revisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 Aaron Bentley
2
 
# <aaron.bentley@utoronto.ca>
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
19
3
from urllib import urlencode
20
4
from xml.sax.saxutils import escape
22
6
import errno
23
7
import tempfile
24
8
import shutil
25
 
import time
26
9
 
 
10
mail_map = {}
27
11
RSVG_OUTPUT_TYPES = ('png', 'jpg')
28
 
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png', 
29
 
                    'cmapx')
 
12
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png')
30
13
 
31
14
class NoDot(Exception):
32
15
    def __init__(self):
38
21
 
39
22
class Node(object):
40
23
    def __init__(self, name, color=None, label=None, rev_id=None,
41
 
                 cluster=None, node_style=None, date=None, message=None):
 
24
                 cluster=None, node_style=None):
42
25
        self.name = name
43
26
        self.color = color
44
27
        self.label = label
48
31
            self.node_style = []
49
32
        self.cluster = cluster
50
33
        self.rank = None
51
 
        self.date = date
52
 
        self.message = message
53
 
        self.href = None
 
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
54
61
 
55
62
    def define(self):
56
63
        attributes = []
65
72
        if label is not None:
66
73
            attributes.append('label="%s"' % label)
67
74
        attributes.append('shape="box"')
68
 
        tooltip = ''
69
 
        if self.message is not None:
70
 
            tooltip += self.message.replace('"', '\\"')
71
 
        if tooltip:
72
 
            attributes.append('tooltip="%s"' % tooltip)
73
 
        if self.href is not None:
74
 
            attributes.append('href="%s"' % self.href)
75
 
        elif tooltip:
76
 
            attributes.append('href="#"')
77
75
        if len(attributes) > 0:
78
76
            return '%s[%s]' % (self.name, " ".join(attributes))
79
77
 
213
211
        else:
214
212
            raise
215
213
    for line in input:
216
 
        dot_proc.stdin.write(line.encode('utf-8'))
 
214
        dot_proc.stdin.write(line)
217
215
    dot_proc.stdin.close()
218
216
    return dot_proc.wait()
219
 
 
220
 
def invoke_dot_html(input, out_file):
221
 
    """\
222
 
    Produce an html file, which uses a .png file, and a cmap to provide
223
 
    annotated revisions.
224
 
    """
225
 
    tempdir = tempfile.mkdtemp()
226
 
    try:
227
 
        temp_dot = os.path.join(tempdir, 'temp.dot')
228
 
        status = invoke_dot(input, temp_dot, file_type='dot')
229
 
 
230
 
        dot = open(temp_dot)
231
 
        temp_file = os.path.join(tempdir, 'temp.cmapx')
232
 
        status = invoke_dot(dot, temp_file, 'cmapx')
233
 
 
234
 
        png_file = '.'.join(out_file.split('.')[:-1] + ['png'])
235
 
        dot.seek(0)
236
 
        status = invoke_dot(dot, png_file, 'png')
237
 
 
238
 
        png_relative = png_file.split('/')[-1]
239
 
        html = open(out_file, 'wb')
240
 
        w = html.write
241
 
        w('<html><head><title></title></head>\n')
242
 
        w('<body>\n')
243
 
        w('<img src="%s" usemap="#G" border=0/>' % png_relative)
244
 
        w(open(temp_file).read())
245
 
        w('</body></html>\n')
246
 
    finally:
247
 
        shutil.rmtree(tempdir)
248
 
    return status
249