forked from jantman/misc-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_find_cycles.py
executable file
·65 lines (48 loc) · 1.66 KB
/
dot_find_cycles.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
#!/usr/bin/env python
"""
dot_find_cycles.py - uses Pydot and NetworkX to find cycles in a dot file directed graph.
Very helpful for Puppet stuff.
By Jason Antman <[email protected]> 2012.
Free for all use, provided that you send any changes you make back to me, update the changelog, and keep this comment intact.
REQUIREMENTS:
Python
python-networkx - <http://networkx.lanl.gov/>
graphviz-python - <http://www.graphviz.org/>
pydot - <http://code.google.com/p/pydot/>
(all of these are available as native packages at least on CentOS)
USAGE:
dot_find_cycles.py /path/to/file.dot
The canonical source of this script can always be found from:
<http://blog.jasonantman.com/2012/03/python-script-to-find-dependency-cycles-in-graphviz-dot-files/>
CHANGELOG:
Wednesday 2012-03-28 Jason Antman <[email protected]>:
- initial script creation
"""
import sys
from os import path, access, R_OK
import networkx as nx
def usage():
sys.stderr.write("dot_find_cycles.py by Jason Antman <http://blog.jasonantman.com>\n")
sys.stderr.write(" finds cycles in dot file graphs, such as those from Puppet\n\n")
sys.stderr.write("USAGE: dot_find_cycles.py /path/to/file.dot\n")
def main():
path = ""
if (len(sys.argv) > 1):
path = sys.argv[1]
else:
usage()
sys.exit(1)
try:
fh = open(path)
except IOError as e:
sys.stderr.write("ERROR: could not read file " + path + "\n")
usage()
sys.exit(1)
# read in the specified file, create a networkx DiGraph
G = nx.DiGraph(nx.read_dot(path))
C = nx.simple_cycles(G)
for i in C:
print i
# Run
if __name__ == "__main__":
main()