-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_names.py
119 lines (83 loc) · 2.99 KB
/
cross_names.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
import sys, os
'''
Goes though all go files in the tree and looks for copy-paste dirt :
command line args:
-s:<path> - path to recursively scan *.go files
Working example:
in the zabo-api-provider-gemini/
(and assuming there is a folder zabo-api-provider-bittrex/
func (c *Controller) GetTransactionsPage(context *zaboutils.ExtendedContext, account *zaboresources.Account, cursor *string) ([]*zaboresources.AccountTransactionRes, *string, bool, *echo.HTTPError) {
...
c.logger.LogWithContextf(zaboutils.Severity.INFO, context, zaboutils.FuncName(c.GetBalance), "Next cursor is %v", "Bittrex")
...
}
-- this should issue a warning as the word bittrex is out of context
'''
prefix = "zabo-api-provider-"
providers = []
## scans go files in the path tree and gets existing descriptions, so we don't need to enter it manually again.
def scan_one_file(fname, provider_name):
warns = 0
line_number = 0
# print("Scanning file %s for %s" % (fname, provider_name))
with open(fname) as f:
for line in f:
line_number += 1
pos = 0
line = line.lower()
for p in providers:
if p == provider_name:
continue
pos = line.find(p)
if pos >= 0:
warns += 1
print("%s:%d:%d Warning: provider name: '%s' alien name: '%s'" % (
fname, line_number, pos, provider_name, p))
return warns
def scan_for_functions(rootDir, provider):
if rootDir is None: return 0, 0
n = 0
w = 0
for lists in os.listdir(rootDir):
if lists.startswith(prefix):
provider = lists[len(prefix):]
path = os.path.join(rootDir, lists)
if path.endswith(".go") and provider is not None:
w += scan_one_file(path, provider)
n += 1
if os.path.isdir(path):
n1, w1 = scan_for_functions(path, provider)
n += n1
w += w1
return n, w
###################################################
def get_providers(rootDir):
if rootDir is None: return []
res = []
for lists in os.listdir(rootDir):
path = os.path.join(rootDir, lists)
if os.path.isdir(path):
if lists.startswith(prefix):
res.append(lists[len(prefix):].lower())
return res
###################################################
def find_matching(f, lt='(', rt=')'):
cnt = 0
for i in range(len(f)):
if f[i] == lt:
cnt += 1
elif f[i] == rt:
cnt -= 1
if cnt == 0:
return i
return -1
if __name__ == "__main__":
path = None
for arg in sys.argv[1:]:
if arg[0:1] == "-" or arg[0:1] == "/":
if arg[1:].lower().startswith("s:"):
path = arg[3:]
providers = get_providers(path)
print("Providers:", providers)
n, w = scan_for_functions(path, None)
print("Scanned %d files, %d warnings issued" % (n, w))