~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python2.4
from subprocess import Popen, PIPE
from urllib import urlencode
from xml.sax.saxutils import escape
import os.path
import errno
import tempfile
import shutil

mail_map = {}

class NoDot(Exception):
    def __init__(self):
        Exception.__init__(self, "Can't find dot!")

class NoRsvg(Exception):
    def __init__(self):
        Exception.__init__(self, "Can't find rsvg!")

class Node(object):
    def __init__(self, name, color=None, label=None, rev_id=None,
                 cluster=None):
        self.name = name
        self.color = color
        self.label = label
        self.committer = None
        self.rev_id = rev_id
        self.node_style = []
        self.cluster = cluster

    def get_committer(self):
        if self.committer is not None:
            if '@' in self.committer:
                try:
                    return mail_map[self.committer]
                except KeyError:
                    pass
            return self.committer
        elif self.rev_id is not None:
            try:
                first_segment = '-'.join(self.rev_id.split('-')[:-2])\
                    .strip(' ')
            except ValueError:
                first_segment = []
            if '@' in first_segment:
                try:
                    return mail_map[first_segment]
                except KeyError:
                    return first_segment

    def get_label(self):
        label = None
        committer = self.get_committer()
        if committer is not None:
            label = "%s\\n%s" % (self.name, committer)
        return label

    def define(self):
        attributes = []
        style = []
        if self.color is not None:
            attributes.append('fillcolor="%s"' % self.color)
            style.append('filled')
        style.extend(self.node_style)
        if len(style) > 0:
            attributes.append('style="%s"' % ",".join(style))
        label = self.get_label()
        if label is not None:
            attributes.append('label="%s"' % label)
        attributes.append('shape="box"')
        if len(attributes) > 0:
            return '%s[%s]' % (self.name, " ".join(attributes))

    def __str__(self):
        return self.name

def dot_output(relations):
    defined = set()
    yield "digraph G\n"
    yield "{\n"
    clusters = set()
    def rel_appropriate(start, end, cluster):
        if cluster is None:
            return start.cluster is None or end.cluster is None
        else:
            return start.cluster==cluster and end.cluster==cluster

    for (start, end) in relations:
        if start.cluster is not None:
            clusters.add(start.cluster)
        if end.cluster is not None:
            clusters.add(end.cluster)
    clusters = list(clusters)
    clusters.append(None)
    for index, cluster in enumerate(clusters):
        if cluster is not None:
            yield "subgraph cluster_%s\n" % index
            yield "{\n"
            yield '    label="%s"\n' % cluster
        for (start, end) in relations:
            if start.name not in defined and start.cluster == cluster:
                defined.add(start.name)
                my_def = start.define()
                if my_def is not None:
                    yield "    %s;\n" % my_def
            if end.name not in defined and end.cluster == cluster:
                defined.add(end.name)
                my_def = end.define()
                if my_def is not None:
                    yield "    %s;\n" % my_def
            if rel_appropriate(start, end, cluster):
                yield "    %s->%s;\n" % (start.name, end.name)
        if cluster is not None:
            yield "}\n"
    yield "}\n"

def invoke_dot_aa(input, out_file, file_type='png'):
    """\
    Produce antialiased Dot output, invoking rsvg on an intermediate file.
    rsvg only supports png, jpeg and .ico files."""
    tempdir = tempfile.mkdtemp()
    try:
        temp_file = os.path.join(tempdir, 'temp.svg')
        invoke_dot(input, temp_file, 'svg')
        cmdline = ['rsvg', temp_file, out_file]
        try:
            rsvg_proc = Popen(cmdline)
        except OSError, e:
            if e.errno == errno.ENOENT:
                raise NoRsvg()
        status = rsvg_proc.wait()
    finally:
        shutil.rmtree(tempdir)
    return status

def invoke_dot(input, out_file=None, file_type='svg', antialias=None):
    cmdline = ['dot', '-T%s' % file_type]
    if out_file is not None:
        cmdline.extend(('-o', out_file))
    try:
        dot_proc = Popen(cmdline, stdin=PIPE)
    except OSError, e:
        if e.errno == errno.ENOENT:
            raise NoDot()
        else:
            raise
    for line in input:
        dot_proc.stdin.write(line)
    dot_proc.stdin.close()
    return dot_proc.wait()