-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_viz.py
executable file
·148 lines (116 loc) · 5.36 KB
/
cl_viz.py
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
#!/usr/bin/env python
import argparse
import collections
import itertools
import json
import os
import re
import subprocess
import sys
import pygraphviz as pgv
if "CODALAB_SESSION" not in os.environ:
os.environ["CODALAB_SESSION"] = "top"
Bundle = collections.namedtuple('Bundle', 'name uuid deps')
def cl_run(cmd):
results = None
try:
results = subprocess.check_output("cl "+cmd,
shell=True).strip()
except:
pass
return results
def parse_dep_string(d_string):
dep = d_string.split(':')[1].strip()
matches = re.match(r"(.*)\((.*)\)", dep)
return matches.groups()
def deps_to_bundles(deps):
parses = [parse_dep_string(x) for x in deps]
bundles = [Bundle(name=x,
uuid=y,
deps=get_bundle_dependencies(y))
for (x, y) in parses]
return bundles
def get_bundle_dependencies(bundle_spec):
# Note: not using `cl info -f dependencies` because that doesn't
# include the uuids of the dependencies. So, here we parse that
# from the `cl info` command.
lines = cl_run("info {}".format(bundle_spec)).split('\n')
dep_list = [x.strip() for x in
itertools.dropwhile(lambda x: x != "dependencies:", lines)]
return deps_to_bundles(dep_list[1:]) if len(dep_list) > 0 else []
def add_bundle_to_graph(bundle, graph, attrs, depth, maxdistance):
uuid = bundle.uuid
name = bundle.name
node_name = "{}\n({})".format(name, uuid[:8])
node_attrs = attrs['nodeattrs']
node_attrs['label'] = node_name
if uuid not in graph:
graph.add_node(uuid, **node_attrs)
if depth < maxdistance:
for b in bundle.deps:
add_bundle_to_graph(b, graph, attrs, depth+1, maxdistance)
graph.add_edge(uuid, b.uuid, **attrs['edgeattrs'])
def main():
#
def_graph_attrs = {'strict': True, 'directed': True, 'splines': True,
'size': '7.75,10.25', 'nodesep': 2.0}
def_rootnode_attrs = {'root': True, 'color': None, 'shape': 'plaintext',
'fontsize': 16, 'fontcolor': 'blue'}
def_node_attrs = {'color': None, 'shape': 'plaintext', 'fontsize': 11,
'fontcolor': 'blue'}
def_edge_attrs = {'weight': 0.5}
def_out_file = 'graph.eps'
parser = argparse.ArgumentParser(epilog="Info on graphviz attributes:"
" http://www.graphviz.org/doc/info/attrs.html")
parser.add_argument("bundlespec",
help="generate dependency graph for BUNDLESPEC")
parser.add_argument("-o", "--output", action='append', default=[],
help="save rendered graph to OUTPUT [{}]"
" (may be used multiple times)".format(def_out_file))
parser.add_argument("-m", "--maxdistance", type=int, default=10,
help="max distance between nodes in the graph [{}]"
" (must be >= 0)".format(10))
parser.add_argument("--graphattrs", default=json.dumps(def_graph_attrs),
help="graphviz graph attributes (json format)."
" default='{}'".format(json.dumps(def_graph_attrs)))
parser.add_argument("--nodeattrs", default=json.dumps(def_node_attrs),
help="graphviz node attributes (json format)."
" default='{}'".format(json.dumps(def_node_attrs)))
parser.add_argument("--rootnodeattrs",
default=json.dumps(def_rootnode_attrs),
help="graphviz root node attributes (json format)."
" default='{}'".format(json.dumps(def_rootnode_attrs)))
parser.add_argument("--edgeattrs", default=json.dumps(def_edge_attrs),
help="graphviz edge attributess (json format)."
" default='{}'".format(json.dumps(def_edge_attrs)))
layout_choices = ['neato', 'dot', 'twopi', 'circo', 'fdp', 'nop']
parser.add_argument("--layout", choices=layout_choices, default='circo',
help="graphviz layout algorithm [circo]")
args = parser.parse_args()
out_files = args.output if len(args.output) > 0 else [def_out_file]
if args.maxdistance and args.maxdistance < 0:
parser.error("Minimum distance is 0")
gv_attrs = {}
gv_attrs['graphattrs'] = json.loads(args.graphattrs)
gv_attrs['nodeattrs'] = json.loads(args.nodeattrs)
gv_attrs['rootnodeattrs'] = json.loads(args.rootnodeattrs)
gv_attrs['edgeattrs'] = json.loads(args.edgeattrs)
root = args.bundlespec
root_uuid = cl_run("info -f uuid {}".format(root))
if not root_uuid:
sys.exit("try running 'cl ls' to check the bundle specification.")
root_name = cl_run("info -f name {}".format(root))
print("Creating graph for bundle: {}({})".format(root_name, root_uuid))
root_deps = get_bundle_dependencies(root_uuid)
root_bundle = Bundle(name=root_name, uuid=root_uuid, deps=root_deps)
G = pgv.AGraph(**gv_attrs['graphattrs'])
add_bundle_to_graph(root_bundle, G, gv_attrs, 0, args.maxdistance)
# Modify the root node with user-specified attributes
G.add_node(root_uuid, **gv_attrs['rootnodeattrs'])
G.layout(prog=args.layout)
for f in out_files:
print("Saving graph to: {}".format(f))
G.draw(f)
return 0
if __name__ == "__main__":
sys.exit(main())