-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric-diff.py
executable file
·317 lines (250 loc) · 8.65 KB
/
metric-diff.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""metric-diff
Find differences in metrics produced by different tools
"""
import argparse
import enum
import json
import logging
import math
import os
import sys
import typing as T
from exit_codes import ExitCode, log_conf, log_debug, log_err
# Metric value type
M = T.TypeVar("M", int, float)
def check_missing_field(condition: T.Any, field: str, filename: str) -> None:
"""Check missing field"""
if not condition:
log_err(
"\n\n{} does not have the '" + field + "' field",
ExitCode.PROGRAMMING_ERROR,
filename,
)
class Metrics(enum.Enum):
"""List of metrics."""
def __str__(self) -> str:
return self.value
CC = "CC"
SLOC = "SLOC"
PLOC = "PLOC"
LLOC = "LLOC"
CLOC = "CLOC"
BLANK = "BLANK"
HALSTEAD = "HALSTEAD"
class CompareMetrics:
"""Compare metrics."""
def __init__(
self,
first_json_filename: str,
second_json_filename: str,
first_json_data: T.Dict[str, T.Any],
second_json_data: T.Dict[str, T.Any],
) -> None:
self.first_json_filename = os.path.basename(first_json_filename)
self.second_json_filename = os.path.basename(second_json_filename)
self.first_json_data = first_json_data
self.second_json_data = second_json_data
self.metrics_json = {
Metrics.CC: "CC",
Metrics.SLOC: "SLOC",
Metrics.PLOC: "PLOC",
Metrics.LLOC: "LLOC",
Metrics.CLOC: "CLOC",
Metrics.BLANK: "BLANK",
Metrics.HALSTEAD: "Halstead",
}
def compare_global_metrics(self, metrics: T.List[Metrics]) -> None:
print("\nComparing global metrics...\n")
for m in metrics:
self._compare_metrics(self.first_json_data, self.second_json_data, m)
def compare_file_metrics(self, metrics: T.List[Metrics]) -> None:
print("\nComparing file metrics...\n")
for m in metrics:
check_missing_field(
self.first_json_data["files"], "files", self.first_json_filename,
)
check_missing_field(
self.second_json_data["files"], "files", self.second_json_filename,
)
for file_one, file_two in zip(
self.first_json_data["files"], self.second_json_data["files"]
):
self._compare_metrics(file_one, file_two, m)
def compare_function_metrics(self, metrics: T.List[Metrics]) -> None:
print("\nComparing function metrics...\n")
for m in metrics:
for file_one, file_two in zip(
self.first_json_data["files"], self.second_json_data["files"]
):
check_missing_field(
file_one["functions"], "functions", self.first_json_filename,
)
check_missing_field(
file_two["functions"], "functions", self.second_json_filename,
)
for function_one, function_two in zip(
file_one["functions"], file_two["functions"]
):
self._compare_metrics(function_one, function_two, m)
def _compare_metrics(
self,
dict_one: T.Dict[str, T.Any],
dict_two: T.Dict[str, T.Any],
metric: Metrics,
) -> None:
# Get metric
json_metric = self.metrics_json.get(metric)
metric_first_file = dict_one.get(json_metric, None)
metric_second_file = dict_two.get(json_metric, None)
# Compare metrics
self._compare_two_metric(metric_first_file, metric_second_file, str(metric))
def _compare_two_metric(
self,
metric_first_file: T.Optional[int],
metric_second_file: T.Optional[int],
metric_as_string: str,
) -> None:
if metric_first_file is None:
log_err(
"\n\n{} metric not found in {}",
ExitCode.METRIC_NOT_FOUND,
metric_as_string,
self.first_json_filename,
)
if metric_second_file is None:
log_err(
"\n\n{} metric not found in {}",
ExitCode.METRIC_NOT_FOUND,
metric_as_string,
self.second_json_filename,
)
if self._check_metrics_types(metric_first_file, metric_second_file):
log_err(
"\n\n{} metric is different\n\n" "{}: {}\n" "{}: {}",
ExitCode.DIFFERENT_METRIC_VALUE,
metric_as_string,
self.first_json_filename,
metric_first_file,
self.second_json_filename,
metric_second_file,
)
def _check_metrics_types(self, metric_one: T.Any, metric_two: T.Any) -> bool:
if type(metric_one) is int:
return metric_one != metric_two
elif type(metric_one) is float:
return not math.isclose(metric_one, metric_two, rel_tol=1e-6)
else:
for halstead_one, halstead_two in zip(
metric_one.values(), metric_two.values()
):
if type(halstead_one) is int:
return halstead_one != halstead_two
else:
return not math.isclose(halstead_one, halstead_two, rel_tol=1e-6)
return True
def check_metrics(metrics: T.Optional[T.List[str]]) -> T.List[Metrics]:
"""Check metrics inserted by the user."""
if not metrics:
return [m for m in Metrics]
metrics_as_str = [str(m) for m in Metrics]
check = [Metrics(metric) for metric in metrics if metric in metrics_as_str]
if check:
return check
return [m for m in Metrics]
def run_comparison(
first_json_file: str,
second_json_file: str,
metrics: T.List[Metrics],
enable_global: bool,
enable_files: bool,
enable_functions: bool,
) -> None:
"""Run metrics comparison."""
with open(first_json_file, "r") as first_json:
first_json_data = json.load(first_json)
with open(second_json_file, "r") as second_json:
second_json_data = json.load(second_json)
diff = CompareMetrics(
first_json_file, second_json_file, first_json_data, second_json_data
)
print("Check", *metrics, "metric" if len(metrics) == 1 else "metrics")
if enable_global:
diff.compare_global_metrics(metrics)
if enable_files:
diff.compare_file_metrics(metrics)
if enable_functions:
diff.compare_function_metrics(metrics)
if not (enable_global or enable_files or enable_functions):
diff.compare_global_metrics(metrics)
print("Done. No differences between analyzed metrics.")
def main() -> None:
parser = argparse.ArgumentParser(
description="This tool compares metrics produced by others software "
"and saved as json files",
epilog="The manual and the source code of this program can be found on"
" GitHub at https://github.com/SoftengPoliTo/SoftwareMetrics",
)
# Optional args
parser.add_argument(
"-v",
"--verbosity",
action="store_true",
help="Increase output verbosity, useful for debugging purposes",
)
parser.add_argument(
"-g",
"--globals",
action="store_true",
help="Compare json files global metrics",
)
parser.add_argument(
"-f", "--files", action="store_true", help="Compare json files file metrics",
)
parser.add_argument(
"-fu",
"--functions",
action="store_true",
help="Compare json files function metrics",
)
# Args
parser.add_argument(
"-m",
"--metrics",
type=str,
nargs="*",
required=True,
help="List of metrics to be compared",
)
parser.add_argument(
"-i",
"--inputs",
metavar="FILE.json",
type=str,
nargs=2,
required=True,
help="Json files to compare",
)
args = parser.parse_args()
# If the -v option is enabled, the program is in debug mode
log_conf(args.verbosity)
log_debug("\targs={}", vars(args))
# Check json files inserted by the user
def check_json_name(name):
if not os.path.isfile(name):
log_err("\t{} is not a valid file.", ExitCode.WRONG_FILES, name)
check_json_name(args.inputs[0])
check_json_name(args.inputs[1])
# Check metrics inserted by the user
metrics = check_metrics(args.metrics)
# Run comparison
run_comparison(
args.inputs[0],
args.inputs[1],
metrics,
args.globals,
args.files,
args.functions,
)
if __name__ == "__main__":
main()