-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-metrics.py
84 lines (62 loc) · 1.83 KB
/
code-metrics.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
#!/usr/bin/python3
import os, sys, json, commands, lizard
program = sys.argv[1]
output = sys.argv[2] if len(sys.argv) == 3 else None
filename, extension = os.path.splitext(program)
bindir = os.path.dirname(os.path.realpath(__file__))
# count total number of lines
lines = len(open(program, "r").readlines())
# call lizard
ana = lizard.analyze_file(program)
# call cloc
cloc = commands.getstatusoutput("cloc --csv %s 2> /dev/null | tail -1" % program)[1].split(",")
# call commentedCodeDetector.py
halstead = {}
if extension in [".cc", ".c", ".java", ".py"]:
out = commands.getstatusoutput("%s/commentedCodeDetector.py -fm %s 2> /dev/null" % (bindir, program))[1]
for line in out.split("\n"):
key, val = map(str.strip, line.split())
halstead[key] = float(val)
# call argon
argon = None
if extension == ".hs":
out = commands.getstatusoutput("argon --json %s 2> /dev/null" % program)[1]
argon = json.loads(out)[0]['blocks']
# build the output
inf = {}
ccn_max = 0.0
inf = {}
if argon is not None:
for func in argon:
f = {}
f['name'] = func['name']
f['long_name'] = str(func['name'])+":"+str(func['col'])
f['ccn'] = func['complexity']
inf[f['name']] = f
ccn_max = max(f['ccn'], ccn_max)
for func in ana.function_list:
f = {}
f['name'] = func.name
f['long_name'] = func.long_name
f['tokens'] = func.token_count
f['nloc'] = func.nloc
f['lines'] = func.length
f['ccn'] = func.cyclomatic_complexity * 1.0
f['parameters'] = func.parameters
inf[func.name] = f
ccn_max = max(func.cyclomatic_complexity, ccn_max)
f = {}
f['lines'] = lines
f['nloc'] = ana.nloc
f['tokens'] = ana.token_count
f['name'] = program
f['ccn'] = ccn_max
f['comments'] = int(cloc[3])
f['language'] = cloc[1]
f['halstead'] = halstead
inf["*"] = f
# write output
if output:
json.dump(inf, open(output, "w"), indent=2)
else:
json.dump(inf, sys.stdout, indent=2)