Skip to content

Commit

Permalink
chore: update find-cycles to work with Python 2 or 3 (aws#10428)
Browse files Browse the repository at this point in the history
I ran into a cycle issue and tried to use find-cycles to diagnose it; unfortunately, it wasn't Python 3-compatible. A quick update makes it work for either Python 2 or 3.

Apologies for the extra noise -- I've got my editor to automatically delete trailing whitespace.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
njlynch authored Oct 2, 2020
1 parent 1fc1ab1 commit af2e8ff
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions scripts/find-cycles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
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:
with open(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)
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, [])

0 comments on commit af2e8ff

Please sign in to comment.