forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanGoSymbols.py
159 lines (124 loc) · 3.76 KB
/
scanGoSymbols.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
import optparse
from modules.GoSymbols import PackageToXml, ProjectToXml
import json
from modules.GoSymbolsExtractor import GoSymbolsExtractor
def displaySymbols(symbols, all = False, stats = False):
# types, funcs, vars
if all:
names = []
for item in symbols["types"]:
names.append(item["name"])
for item in symbols["funcs"]:
names.append(item["name"])
for item in symbols["vars"]:
names.append(item)
if stats:
print " Total symbols: %s" % len(names)
else:
for item in sorted(names):
print " %s" % item
else:
names = []
for item in symbols["types"]:
names.append(item["name"])
if stats:
print " Total types: %s" % len(names)
else:
for item in sorted(names):
print " type: %s" % item
names = []
for item in symbols["funcs"]:
names.append(item["name"])
if stats:
print " Total funcs: %s" % len(names)
else:
for item in sorted(names):
print " func: %s" % item
names = []
for item in symbols["vars"]:
names.append(item)
if stats:
print " Total vars: %s" % len(names)
else:
for item in sorted(names):
print " var: %s" % item
def setOptionParser():
parser = optparse.OptionParser("%prog -l|-u|-p [-a] [-s] [-x] dir")
parser.add_option_group( optparse.OptionGroup(parser, "dir", "Directory to scan at.") )
parser.add_option(
"", "-l", "--list", dest="list", action = "store_true", default = False,
help = "List all symbols for all import paths of a golang devel package."
)
parser.add_option(
"", "", "--prefix", dest="prefix", default = "",
help = "Prefix prepended to all listed import paths followed by slash /."
)
parser.add_option(
"", "-a", "--all", dest="all", action = "store_true", default = False,
help = "Normally symbols are prefixed by its type. This flag ignores the type and list all symbols as equal."
)
parser.add_option(
"", "-s", "--stats", dest="stats", action = "store_true", default = False,
help = "Don't list symbols, show their count for each import path instead."
)
parser.add_option(
"", "-x", "--xml", dest="xml", action = "store_true", default = False,
help = "List symbols in xml format."
)
parser.add_option(
"", "-u", "--usedip", dest="usedip", action = "store_true", default = False,
help = "List all imported paths/packages."
)
parser.add_option(
"", "-p", "--provides", dest="provides", action = "store_true", default = False,
help = "List all provided import paths."
)
return parser
if __name__ == "__main__":
parser = setOptionParser()
options, args = parser.parse_args()
if len(args) < 1:
print "Synopsis: prog -l|-u|-p [-a] [-s] [-x] dir"
exit(1)
go_dir = args[0]
prefix = ""
if options.prefix != "":
prefix = options.prefix + "/"
#obj = ProjectToXml(options.prefix, go_dir)
#print obj
#print obj.getError()
#exit(0)
gse_obj = GoSymbolsExtractor(go_dir)
if not gse_obj.extract():
print gse_obj.getError()
exit(1)
if options.provides:
ip = gse_obj.getSymbolsPosition()
ips = []
for pkg in ip:
ips.append(ip[pkg])
for ip in sorted(ips):
if ip == "." and options.prefix != "":
print options.prefix
else:
print "%s%s" % (prefix, ip)
elif options.list:
ip = gse_obj.getSymbolsPosition()
symbols = gse_obj.getSymbols()
ip_used = gse_obj.getImportedPackages()
for pkg in ip:
print "Import path: %s%s" % (prefix, ip[pkg])
#print json.dumps(symbols[pkg])
if options.xml:
obj = PackageToXml(symbols[pkg], "%s%s" % (prefix, ip[pkg]), imports=False)
if obj.getStatus():
print obj#.getError()
else:
print obj.getError()
exit(0)
else:
displaySymbols(symbols[pkg], options.all, options.stats)
elif options.usedip:
ip_used = gse_obj.getImportedPackages()
for uip in sorted(ip_used):
print uip