-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcccc.py
293 lines (249 loc) · 9.92 KB
/
cccc.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import json
import os
import re
import subprocess
from typing import Any, Dict, List
from xml.dom import minidom
from exit_codes import ExitCode, log_debug, log_err
CCCC_EXTENSIONS = ["c", "cc", "cpp", "c++", "h", "hpp", "hh"]
class Cccc:
def __init__(self, path):
self.cccc_path = os.path.join(path, "CCCC", "cccc")
def run_n_parse_cccc(self, files_list: list, output_dir: os.path):
self.run_tool_cccc(files_list, output_dir)
return cccc_output_reader(os.path.join(output_dir, "outputs"))
def run_tool_cccc(self, files_list: list, output_dir: str):
try:
output_subdir = self._output_subdir(output_dir)
args = [self.cccc_path, "--outdir=" + output_subdir]
args.extend(files_list)
return subprocess.run(args, capture_output=True, check=True)
except subprocess.CalledProcessError as ex:
log_err(
"\tCCCC exited with an error.\n{}\n{}\n",
ExitCode.CCCC_TOOL_ERR,
ex.stdout,
ex.stderr,
)
def _output_subdir(self, output_dir):
output_subdir = os.path.join(output_dir, "outputs")
if not os.path.exists(output_subdir):
os.mkdir(output_subdir)
return output_subdir
def cccc_output_reader(cccc_xml_directory_path: str):
base_dir = os.path.realpath(cccc_xml_directory_path)
per_function_res = []
with open(
os.path.join(cccc_xml_directory_path, "cccc.xml"), "r"
) as cccc_file:
cccc_xml = minidom.parse(cccc_file)
project = cccc_xml.getElementsByTagName("CCCC_Project")
modules = (
project[0]
.getElementsByTagName("oo_design")[0]
.getElementsByTagName("module")
)
for module in modules:
module_name = module.getElementsByTagName("name")[
0
].firstChild.nodeValue
WMC = module.getElementsByTagName("weighted_methods_per_class_unity")[
0
].getAttribute("value")
DIT = module.getElementsByTagName("depth_of_inheritance_tree")[
0
].getAttribute("value")
NOC = module.getElementsByTagName("number_of_children")[
0
].getAttribute("value")
CBO = module.getElementsByTagName("coupling_between_objects")[
0
].getAttribute("value")
log_debug(
"\tCCCC output reader. Reading path: {}",
os.path.join(base_dir, module_name + ".xml"),
)
with open(
os.path.join(base_dir, module_name + ".xml"), "r"
) as moduleFile:
module_xml = minidom.parse(moduleFile)
CC_module = (
module_xml.getElementsByTagName("module_summary")[0]
.getElementsByTagName("McCabes_cyclomatic_complexity")[0]
.getAttribute("value")
)
member_functions = module_xml.getElementsByTagName(
"procedural_detail"
)[0].getElementsByTagName("member_function")
list_of_member_functions: List[Dict[str, Any]] = []
for member_function in member_functions:
member_function_name = member_function.getElementsByTagName(
"name"
)[0].firstChild.nodeValue
file_in = None
line_number = None
definition_only = True
for extent in member_function.getElementsByTagName("extent"):
if (
extent.getElementsByTagName("description")[
0
].firstChild.nodeValue
== "definition"
):
definition_only = False
file_in = extent.getElementsByTagName("source_reference")[
0
].getAttribute("file")
line_number = extent.getElementsByTagName(
"source_reference"
)[0].getAttribute("line")
if definition_only:
# If it is not the implementation of the function, we skip it
continue
member_function_cc = member_function.getElementsByTagName(
"McCabes_cyclomatic_complexity"
)[0].getAttribute("value")
lines_of_code = member_function.getElementsByTagName(
"lines_of_code"
)[0].getAttribute("value")
lines_of_comment = member_function.getElementsByTagName(
"lines_of_comment"
)[0].getAttribute("value")
per_function_values = {
"file": file_in,
"line_number": line_number,
"func_name": member_function_name,
"functionCC": member_function_cc,
"loc": lines_of_code,
"cloc": lines_of_comment,
}
list_of_member_functions.append(per_function_values)
per_module_metrics = {
"CC": CC_module,
"WMC": WMC,
"DIT": DIT,
"NOC": NOC,
"CBO": CBO,
}
# {"filename": file_in, "func_name": func_name,
# "line_number": line_number, "values": per_function_values}
per_function_res.append(
{
"module_name": module_name,
"per_module_metrics": per_module_metrics,
"functions": list_of_member_functions,
}
)
return per_function_res
def standardizer_cccc(data):
# Support structures
tmp_dict_files = {}
tmp_dict_modules = {}
# for d in data:
# for module in d:
for module in data:
# If there are no functions, the module represents a class which is not
# defined in the files we analyzed.
# Hence, all its stats are 0, and the other tools will not have those
# entries, so we can omit it.
# We could still put these in the 'global' section
if len(module["functions"]) == 0:
continue
if module["module_name"] not in tmp_dict_modules:
tmp_dict_modules[
module["module_name"]
] = { # It's going to be added in the 'global' section
"class name": module["module_name"],
"CC": module["per_module_metrics"]["CC"],
"C&K": {
"WMC": module["per_module_metrics"]["WMC"],
"DIT": module["per_module_metrics"]["DIT"],
"NOC": module["per_module_metrics"]["NOC"],
"CBO": module["per_module_metrics"]["CBO"],
},
}
for func in module["functions"]:
if (
func["file"] not in tmp_dict_files
): # Create new per_file struct
per_file = {"filename": func["file"], "functions": []}
tmp_dict_files[func["file"]] = per_file
else:
per_file = tmp_dict_files[func["file"]]
per_func = None
for i in per_file["functions"]:
if i["line number"] == func["line_number"]:
per_func = i
break
if per_func is None: # New function
per_func = {
"function name": re.sub(
r"\([^)]*\)", "", func["func_name"]
),
"line number": func["line_number"],
"LOC": int(func["loc"]), # LOC
"CLOC": int(func["cloc"]),
"CC": float(func["functionCC"]),
"class name": module[
"module_name"
], # The function is part of this module
}
per_file["functions"].append(per_func)
else:
log_debug(
"\t_standardizer_cccc() warning: same function found twice."
"\n\tanalyzed function:"
"\n\t{}\n"
"\talready present function:\n\t{}",
func,
per_func,
)
formatted_output = {"classes": [], "files": []}
for module in tmp_dict_modules:
if (
module != "anonymous"
): # Do not add the per_module stats if in "anonymous"
formatted_output["classes"].append(tmp_dict_modules[module])
for file in tmp_dict_files.values():
formatted_output["files"].append(file)
return formatted_output
def helper_cccc(standardized_output: dict):
"""Calculate McCabe's Weighted Method Count metric.
This version uses the McCabe's CC for calculating the weight of
each method."""
for module in standardized_output["classes"]:
WMC = 0
n_func = 0
module_name = module["class name"]
for file in standardized_output["files"]:
for func in file["functions"]:
if "class name" in func and func["class name"] == module_name:
WMC += func["CC"]
n_func += 1
module["WMC"] = WMC
module["no. functions"] = n_func
def helper_test_cccc(standardized_output: dict, output: dict):
"""Calculate McCabe's Weighted Method Count metric.
This version uses the McCabe's CC for calculating the weight of
each method."""
tot_loc = 0
tot_cloc = 0
for file in standardized_output["files"]:
for function in file["functions"]:
tot_loc += function["LOC"]
tot_cloc += function["CLOC"]
output["LOC"] = tot_loc
output["CLOC"] = tot_cloc
output["classes"] = standardized_output["classes"]
output["files"] = standardized_output["files"]
for module in output["classes"]:
WMC = 0
n_func = 0
module_name = module["class name"]
for file in output["files"]:
for func in file["functions"]:
if "class name" in func and func["class name"] == module_name:
WMC += func["CC"]
n_func += 1
module["WMC"] = WMC
module["no. functions"] = n_func