forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-cycles.py
46 lines (35 loc) · 1 KB
/
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
#!/usr/bin/env python
import json
import sys
import collections
import pprint
def find(xs, x):
for i, value in enumerate(xs):
if x == value:
return i
return None
filenames = sys.argv[1:]
graph = collections.defaultdict(set)
for filename in filenames:
with file(filename) as f:
package_json = json.load(f)
for key in ['devDependencies', 'dependencies']:
if key in package_json:
graph[package_json['name']].update(package_json[key].keys())
checked = set()
# Do a check for cycles for each package. This is slow but it works,
# and it has the advantage that it can give good diagnostics.
def check_for_cycles(package, path):
i = find(path, package)
if i is not None:
cycle = path[i:] + [package]
print 'Cycle: %s' % ' => '.join(cycle)
return
if package in checked:
return
checked.add(package)
deps = graph.get(package, [])
for dep in deps:
check_for_cycles(dep, path + [package])
for package in graph.keys():
check_for_cycles(package, [])