0

What's the easiest way to produce a visualization of Mercurial revision tree using Python (ideally, Python 3)?

I am guessing it would have to be through a combination 2 libraries: one that provides an interface to the Mercurial repository, and one that visualizes graphs.

Use case: we have written a (pure Python) continuous integration testing module. We'd like it to display the revision tree, marking each node as "passed", "failed", "in progress", "not tested" or something along these lines.

4

2 回答 2

1

在这里,有我的脚本:

#!/usr/bin/env python
from itertools import chain
import hglib
#from pygraphviz import *

repo = hglib.open('.')

log = dict((t[0], t) for t in repo.log())
def graph(): return dict((v, []) for v in log)

forward, backward, jump = (graph() for i in range(3))

for target in log:
    for parent in repo.parents(target) or []:
        source = parent[0]
        forward[source].append(target)
        backward[target].append(source)

def endpoint(v):
    if len(forward[v]) != 1: return True
    if len(backward[forward[v][0]]) != 1: return True
    if len(backward[v]) != 1: return True
    if len(forward[backward[v][0]]) != 1: return True

for v in forward:
    if endpoint(v):
        w = v
        while len(forward[w]) == 1 == len(backward[forward[w][0]]):
            w = forward[w][0]
        jump[v] = w
    else: del jump[v]

def vertex(tupl): return 'v' + tupl[1][:5]

print 'digraph {'

colors = ['red', 'green', 'blue', 'yellow', 'cyan', 'magenta',
          'orange', 'chartreuse']
authors = dict()

for v in sorted(forward, key=int):
    if not endpoint(v) or v not in jump: continue
    node = 'v%s' % v
    if jump[v] != v:
        sep = '+' if forward[v] == jump[v] else '::'
        label = '%s%s%s' % (v, sep, jump[v])
        assert int(jump[v]) > int(v)
        v = jump[v]
        del jump[v]
    else:
        label = v
    author = log[v][4]
    print '// %s' % author
    if author not in authors: authors[author] = colors[len(authors) %
                                                       len(colors)]
    attrs = dict(color=authors[author], label=label, style='bold').items()
    print '\t%s [%s]' % (node, ','.join('%s="%s"' % kv for kv in attrs))
    for w in forward[v]: print '\t%s -> v%s' % (node, w)

print '}'

如您所知,我使用 hglib 来获取 Mercurial 数据(hglib.open('.').log()是一个元组列表,每次提交一个)。我使用 graphviz 进行可视化。没有图书馆,我只是*打印*该死的东西;-)

我像这样运行脚本加上graphviz:

python version-dag.py > log.dot ; dot -Tpdf log.dot -o log.pdf

...然后看看光荣的 .pdf 文件。Graphviz 可以做 png、eps 和其他格式。该jump字典用于将单父路径压缩到单个节点。享受 :)

于 2013-07-05T18:25:01.657 回答
1

对于可视化部分,我会查看NetworkX。它是一个 Python 库,可让您进行图形/网络处理、导入/导出和可视化。

于 2012-11-01T16:17:35.397 回答