~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
#!/usr/bin/env python2.4
from subprocess import Popen, PIPE
from urllib import urlencode
from xml.sax.saxutils import escape
import os.path

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

    def get_label(self):
        label = None
        if self.committer is not None:
            label="%s\\n%s" % (self.name, self.committer)
        elif self.rev_id is not None:
            try:
                first_segment = '-'.join(self.rev_id.split('-')[:-2])
            except ValueError:
                first_segment = []
            if '@' in first_segment:
                label="%s\\n%s" % (self.name, first_segment)
        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)
        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"
    for (start, end) in relations:
        if start.name not in defined:
            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:
            defined.add(end.name)
            my_def = end.define()
            if my_def is not None:
                yield "    %s;\n" % my_def

        yield "    %s->%s;\n" % (start.name, end.name)
    yield "}\n"

def invoke_dot(input, out_file=None, file_type='svg'):
    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 == 2:
            print "You need 'dot' to create ancestry graphs"
        return
    for line in input:
        dot_proc.stdin.write(line)
    dot_proc.stdin.close()
    return dot_proc.wait()