IODA Bundle
run-mccabe.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import sys
4 import ast
5 import mccabe
6 from pathlib import Path
7 
8 IODA_CONV_PATH = Path(__file__).parent/"@SCRIPT_LIB_PATH@"
9 if not IODA_CONV_PATH.is_dir():
10  IODA_CONV_PATH = Path(__file__).parent/'..'/'lib-python'
11 sys.path.append(str(IODA_CONV_PATH.resolve()))
12 
13 from utils import collect_sources
14 
15 
16 def process(py_source, max_complexity):
17  code = py_source.text()
18  tree = compile(code, py_source, "exec", ast.PyCF_ONLY_AST)
19  visitor = mccabe.PathGraphingAstVisitor()
20  visitor.preorder(tree, visitor)
21  for graph in visitor.graphs.values():
22  if graph.complexity() > max_complexity:
23  text = "{}:{}:{} {} {}"
24  return text.format(py_source,
25  graph.lineno, graph.column,
26  graph.entity, graph.complexity())
27 
28 
29 def main():
30  max_complexity = int(sys.argv[1])
31  ok = True
32  for py_source in collect_sources():
33  error = process(py_source, max_complexity)
34  if error:
35  ok = False
36  print(error)
37  if not ok:
38  sys.exit(1)
39 
40 
41 if __name__ == "__main__":
42  main()
def main()
Definition: run-mccabe.py:29
def process(py_source, max_complexity)
Definition: run-mccabe.py:16