c74298dd8d3d912bfa6cad6eaff7c528045309fd
[bbaumbach/samba-autobuild/.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 from __future__ import print_function
10 import sys, sre
11
12 if len(sys.argv) != 2:
13     print('Usage: depfilter.py NODE')
14     sys.exit(1)
15
16 top = sys.argv[1]
17
18 # Read in dot file
19
20 lines = sys.stdin.readlines()
21
22 graph = {}
23
24 for arc in lines[1:-1]:
25     match = sre.search('"(.*)" -> "(.*)"', arc)
26     n1, n2 = match.group(1), match.group(2)
27     if n1 not in graph:
28         graph[n1] = []
29     graph[n1].append(n2)
30
31 # Create subset of 'graph' rooted at 'top'
32
33 subgraph = {}
34
35 def add_deps(node):
36     if node in graph and node not in subgraph:
37         subgraph[node] = graph[node]
38         for n in graph[node]:
39             add_deps(n)
40
41 add_deps(top)
42
43 # Generate output
44
45 print(lines[0], end=' ')
46
47 for key, value in subgraph.items():
48     for n in value:
49         print('\t"%s" -> "%s"' % (key, n))
50
51 print(lines[-1], end=' ')