misc/scripts/detect_cycle.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 18 Nov 2016 17:08:35 +0100
brancholdstable
changeset 11843 c0a49a137217
parent 6198 f8dea560703f
child 10589 7c23b7de2b8d
permissions -rw-r--r--
Closing "oldstable" branch We now release-based branch naming.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6198
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
try:
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
    rtype, = __args__
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
except ValueError:
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
    print 'USAGE: cubicweb-ctl shell <instance> detect_cycle.py -- <relation type>'
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
    print
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
graph = {}
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
for fromeid, toeid in rql('Any X,Y WHERE X %s Y' % rtype):
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
    graph.setdefault(fromeid, []).append(toeid)
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
from logilab.common.graph import get_cycles
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
for cycle in get_cycles(graph):
f8dea560703f add a small c-c shell script to detect cycle for a given relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
    print 'cycle', '->'.join(str(n) for n in cycle)