forked from capocchi/DEVSimPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexity.py
More file actions
92 lines (75 loc) · 2.97 KB
/
Complexity.py
File metadata and controls
92 lines (75 loc) · 2.97 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
# Complexity.py ---
# --------------------------------
# Copyright (c) 2020
# L. CAPOCCHI (capocchi@univ-corse.fr)
# SPE Lab - SISU Group - University of Corsica
# --------------------------------
# Version 2.0 last modified: 03/15/20
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GENERAL NOTES AND REMARKS:
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
#
# GLOBAL VARIABLES AND FUNCTIONS
#
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
import os, sys
import inspect
import Components
import textwrap
from Utilities import getInstance
def GetMacCabeMetric(path):
"""
"""
complexity = 0.0
try:
p = os.path.dirname(path)
if p not in sys.path:
sys.path.append(p)
import maccabe as codepaths
except ImportError as info:
msg = 'ERROR: maccabe module not imported: %s\n'%info
sys.stderr.write(msg)
return complexity
else:
cls = Components.GetClass(path)
if inspect.isclass(cls):
args = Components.GetArgs(cls)
devs = getInstance(cls, args)
### Get class of model
if not path.endswith('.pyc'):
### mcCabe complexity
### beware to use tab for devs code of models
L = [getattr(cls,fct) for fct in ('extTransition','intTransition','outputFnc') if hasattr(cls, fct)]
### when model is created, the transition functions are empty...
try:
source_list = list(map(inspect.getsource, L))
except Exception as info:
source_list = []
L_args = []
complexity = 0.0
for text in source_list:
### textwrap for deleting the indentation
try:
ast = codepaths.ast.parse(textwrap.dedent(text).strip())
visitor = codepaths.PathGraphingAstVisitor()
visitor.preorder(ast, visitor)
except Exception as info:
sys.stdout.write("Error in Complexity module")
else:
for graph in visitor.graphs.values():
complexity += graph.complexity()
return complexity
### for .pyc file
else:
return 0.0
else:
return 0.0
if __name__ == "__main__":
# execute only if run as a script
path = sys.argv[1]
sys.stdout.write(str(GetMacCabeMetric(path)))