cubicweb/misc/scripts/detect_cycle.py
author julien tayon <julien.tayon@logilab.fr>
Tue, 04 Jun 2019 09:35:47 +0200
changeset 12638 8466a6dd474e
parent 11057 0b59724cb3f2
permissions -rw-r--r--
[py3] changing unicode to str py2 support being dropped, passing the code in full py3 str/bytes syntax

from __future__ import print_function

try:
    rtype, = __args__
except ValueError:
    print('USAGE: cubicweb-ctl shell <instance> detect_cycle.py -- <relation type>')
    print()

graph = {}
for fromeid, toeid in rql('Any X,Y WHERE X %s Y' % rtype):
    graph.setdefault(fromeid, []).append(toeid)

from logilab.common.graph import get_cycles

for cycle in get_cycles(graph):
    print('cycle', '->'.join(str(n) for n in cycle))