440b56045713ce67422942d7ce14c056148e6bb2
[samba.git] / source4 / script / depfilter.py
1 #!/usr/bin/env python
2 #
3 # Filter out arcs in a dotty graph that are at or below a certain
4 # node.  This is useful for visualising parts of the dependency graph.
5 #
6
7 # Command line stuff
8
9 import sys, sre
10
11 if len(sys.argv) != 2:
12     print 'Usage: depfilter.py NODE'
13     sys.exit(1)
14
15 top = sys.argv[1]
16
17 # Read in dot file
18
19 lines = sys.stdin.readlines()
20
21 graph = {}
22
23 for arc in lines[1:-1]:
24     match = sre.search('"(.*)" -> "(.*)"', arc)
25     n1, n2 = match.group(1), match.group(2)
26     if not graph.has_key(n1):
27         graph[n1] = []
28     graph[n1].append(n2)
29
30 # Create subset of 'graph' rooted at 'top'
31
32 subgraph = {}
33
34 def add_deps(node):
35     if graph.has_key(node) and not subgraph.has_key(node):
36         subgraph[node] = graph[node]
37         for n in graph[node]:
38             add_deps(n)
39
40 add_deps(top)
41
42 # Generate output
43
44 print lines[0],
45
46 for key, value in subgraph.items():
47     for n in value:
48         print '\t"%s" -> "%s"' % (key, n)
49
50 print lines[-1],