cubicweb/misc/scripts/detect_cycle.py
author Philippe Pepiot <philippe.pepiot@logilab.fr>
Wed, 18 Mar 2020 13:18:21 +0100
branch3.27
changeset 12920 018b6445aef1
parent 11057 0b59724cb3f2
permissions -rw-r--r--
Added tag 3.27.3, debian/3.27.3-1 for changeset 85284f028266
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 6198
diff changeset
     1
from __future__ import print_function
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
     2
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
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
     4
    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
     5
except ValueError:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 6198
diff changeset
     6
    print('USAGE: cubicweb-ctl shell <instance> detect_cycle.py -- <relation type>')
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 6198
diff changeset
     7
    print()
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
     8
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
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
    10
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
    11
    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
    12
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
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
    14
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
for cycle in get_cycles(graph):
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 6198
diff changeset
    16
    print('cycle', '->'.join(str(n) for n in cycle))