forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanGoSymbols.py
206 lines (163 loc) · 5.22 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
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
import optparse
from modules.GoSymbols import PackageToXml, ProjectToXml
import json
from modules.GoSymbolsExtractor import GoSymbolsExtractor
from modules.Config import Config
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."
)
parser.add_option(
"", "", "--scan-all-dirs", dest="scanalldirs", action = "store_true", default = False,
help = "Scan all dirs, including Godeps directory"
)
parser.add_option(
"", "", "--skip-dirs", dest="skipdirs", default = "",
help = "Scan all dirs except specified via SKIPDIRS. Directories are comma separated list."
)
parser.add_option(
"", "", "--pure-xml", dest="purexml", action = "store_true", default = False,
help = "Print all packages in one xml file"
)
parser.add_option(
"", "", "--skip-errors", dest="skiperrors", action = "store_true", default = False,
help = "Skip all errors during Go symbol parsing"
)
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 + "/"
if not options.scanalldirs:
noGodeps = Config().getSkippedDirectories()
else:
noGodeps = []
if options.skipdirs:
for dir in options.skipdirs.split(','):
dir = dir.strip()
if dir == "":
continue
noGodeps.append(dir)
#obj = ProjectToXml(options.prefix, go_dir)
#print obj
#print obj.getError()
#exit(0)
gse_obj = GoSymbolsExtractor(go_dir, skip_errors=options.skiperrors, noGodeps=noGodeps)
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()
if options.purexml:
print "<?xml version='1.0' encoding='ASCII'?>"
print "<project ipprefix=\"%s\" commit=\"\" nvr=\"\">" % options.prefix
print "<packages>"
package_imports = gse_obj.getPackageImports()
for pkg in ip:
if not options.purexml:
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 options.purexml:
obj.setPackageImports(package_imports[pkg])
obj.generate()
if obj.getStatus():
print obj#.getError()
else:
print obj.getError()
exit(0)
else:
displaySymbols(symbols[pkg], options.all, options.stats)
if options.purexml:
print "</packages>"
print "</project>"
elif options.usedip:
ip_used = gse_obj.getImportedPackages()
for uip in sorted(ip_used):
print uip