-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-code-usage.py
58 lines (47 loc) · 1.6 KB
/
get-code-usage.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
import json
import os
root_dir = '/Users/blmayer/poupachef/back-end'
go_paths = []
stats = []
classes = [
'User', "Store", 'Quotation', 'Cart', 'Supplier', 'Delivery', 'Product'
]
# Recursive search
def search_down(root, entry):
if os.path.isfile(os.path.join(root, entry)):
if entry.endswith('.go'):
go_paths.append(os.path.join(root, entry))
else:
for deep_entry in os.listdir(os.path.join(root, entry)):
search_down(os.path.join(root, entry), deep_entry)
# Main execution
search_down(root_dir, "")
for go_path in go_paths:
class_stats = {x: 0 for x in classes}
file_stats = {'file': os.path.relpath(go_path, root_dir)}
# Read file
with open(go_path) as go_file:
# Read first line to get package name
header = go_file.readline()
file_stats['package'] = header.split(' ')[1].strip()
# Read the rest and update stats
lines = go_file.readlines()
for line in lines:
words = line.split(' ')
for word in words:
for go_class in classes:
if word.find(go_class) > -1:
class_stats[go_class] += 1
# Save information
for stat in class_stats:
if class_stats[stat] > 0:
info = {
'class': stat,
'value': class_stats[stat],
'package': file_stats['package'],
'file': file_stats['file']
}
# print('appending', info)
stats.append(info)
# print(stats[:5])
json.dump(stats, open('code_complete.json', 'w'))