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