~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to dotgraph.py

  • Committer: Aaron Bentley
  • Date: 2005-09-22 23:26:32 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20050922232632-4fb8b331c9509a12
tweaked docs

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
 
27
10
RSVG_OUTPUT_TYPES = ('png', 'jpg')
28
11
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png', 
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
34
        self.href = None
54
35
 
55
36
    def define(self):
65
46
        if label is not None:
66
47
            attributes.append('label="%s"' % label)
67
48
        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
49
        if self.href is not None:
74
50
            attributes.append('href="%s"' % self.href)
75
 
        elif tooltip:
76
 
            attributes.append('href="#"')
77
51
        if len(attributes) > 0:
78
52
            return '%s[%s]' % (self.name, " ".join(attributes))
79
53
 
213
187
        else:
214
188
            raise
215
189
    for line in input:
216
 
        dot_proc.stdin.write(line.encode('utf-8'))
 
190
        dot_proc.stdin.write(line)
217
191
    dot_proc.stdin.close()
218
192
    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