File: check_circular.py

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (36 lines) | stat: -rwxr-xr-x 1,028 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

import os
import sys
import networkx as nx

def build_graph(path):
    graph = nx.DiGraph()
    for f in os.listdir(path):
        graph.add_node(f)
        cnt = 0
        with open(path+"/"+f) as fp:
            line = fp.readline()
            enter_include = False
            exit_include = False
            while line and not exit_include:
                if line.startswith('#include "'):
                    enter_include = True
                    node = line.split()[1].replace('"', '')
                    graph.add_node(node)
                    graph.add_edge(f, node)
                elif enter_include:
                    exit_include = True
                line = fp.readline()
    return graph

def main():
    graph = build_graph("../include/xtensor")
    cycle = list(nx.simple_cycles(graph))
    for x in cycle:
        print(x)
    exception_message = ' - '.join([str(y) for y in cycle])
    if len(cycle) != 0:
        raise Exception('CircularInclude', exception_message)

main()