-
Notifications
You must be signed in to change notification settings - Fork 0
/
halstead.py
186 lines (149 loc) · 5.15 KB
/
halstead.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
import json
import math
import os
import subprocess
from exit_codes import ExitCode, log_err
HALSTEAD_EXCEPTIONS = ["c", "cc", "cpp", "c++"]
class Halstead:
def __init__(self, path):
self.halstead_path = os.path.join(
path, "Halstead_Metrics_Tool", "Halstead-Metrics.jar"
)
def run_n_parse_halstead(self, files_list: list, output_dir: os.path):
results = []
for file in files_list:
results.append(self._run_n_parse_halstead(file, output_dir))
return results
def _run_n_parse_halstead(self, file: os.path, output_dir: str):
hm_tool_res = self.run_tool_halstead(file)
return json.loads(hm_tool_res)
def run_tool_halstead(self, path_to_analyze: str):
try:
results = subprocess.run(
[
"/usr/bin/java",
"-Duser.country=US",
"-Duser.language=en",
"-jar",
self.halstead_path,
path_to_analyze,
],
capture_output=True,
check=True,
)
return results.stdout
except subprocess.CalledProcessError as ex:
log_err(
"\tHalstead Metric Tool exited with an error.\n{}\n{}\n",
ExitCode.HALSTEAD_TOOL_ERR,
ex.stdout,
ex.stderr,
)
def standardizer_halstead(data):
formatted_output = {"files": []}
for d in data:
h = d["Halstead"]
per_file = {
"_Operators": h["_Operators"],
"_Operands": h["_Operands"],
"n1": h["n1"],
"n2": h["n2"],
"N1": h["N1"],
"N2": h["N2"],
"Vocabulary": int(h["Vocabulary"]),
"Length": int(h["Length"]),
"Volume": h["Volume"],
"Difficulty": h["Difficulty"],
"Effort": h["Effort"],
"Programming time": h["Programming time"],
"Estimated program length": h["Estimated program length"],
"Purity ratio": h["Purity ratio"],
}
formatted_output["files"].append(
{
"filename": d["filename"],
"Halstead": per_file,
"functions": [], # No per_function data from this tool
}
)
return formatted_output
def helper_halstead(standardized_output: dict):
all_operators = {}
all_operands = {}
for file in standardized_output["files"]:
if "Halstead" not in file:
continue
h = file["Halstead"]
for i in h["_Operators"]:
if i not in all_operators:
all_operators[i] = int(h["_Operators"][i])
else:
all_operators[i] += int(h["_Operators"][i])
for i in h["_Operands"]:
if i not in all_operands:
all_operands[i] = int(h["_Operands"][i])
else:
all_operands[i] += int(h["_Operands"][i])
standardized_output["Halstead"] = _helper_halstead(
all_operators, all_operands
)
def helper_test_halstead(standardized_output: dict, output: dict):
all_operators = {}
all_operands = {}
for file in standardized_output["files"]:
if "Halstead" not in file:
continue
h = file["Halstead"]
for i in h["_Operators"]:
if i not in all_operators:
all_operators[i] = int(h["_Operators"][i])
else:
all_operators[i] += int(h["_Operators"][i])
for i in h["_Operands"]:
if i not in all_operands:
all_operands[i] = int(h["_Operands"][i])
else:
all_operands[i] += int(h["_Operands"][i])
del file["Halstead"]["_Operators"]
del file["Halstead"]["_Operands"]
output["Halstead"] = _helper_halstead(all_operators, all_operands)
output["files"] = []
for file in standardized_output["files"]:
files = {
"filename": file["filename"],
"Halstead": file["Halstead"],
"functions": [],
}
output["files"].append(files)
def _helper_halstead(operators: dict, operands: dict) -> dict:
n1 = len(operators)
n2 = len(operands)
N1 = 0
N2 = 0
for i in operators:
N1 += operators[i]
for i in operands:
N2 += operands[i]
program_length = N1 + N2
program_vocabulary = n1 + n2
estimated_length = n1 * math.log2(n1) + n2 * math.log2(n2)
purity_ratio = estimated_length / program_length
volume = program_length * math.log2(program_vocabulary)
difficulty = (n1 / 2) * (N2 / n2)
program_effort = volume * difficulty
programming_time = program_effort / 18
halstead_output = {
"n1": n1,
"n2": n2,
"N1": N1,
"N2": N2,
"Vocabulary": program_vocabulary,
"Length": program_length,
"Volume": volume,
"Difficulty": difficulty,
"Effort": program_effort,
"Programming time": programming_time,
"Estimated program length": estimated_length,
"Purity ratio": purity_ratio,
}
return halstead_output