forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goapidiff.py
195 lines (162 loc) · 5.14 KB
/
goapidiff.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
# Check difference of APIs of two commits
# Output is number of symbols added and removed.
# You can list of those symbols as well
# Projects that change exported symbols with each commit should not be used
# as a built or install time dependency until they stabilize.
import optparse
from modules.GoSymbols import CompareSourceCodes
from modules.Utils import YELLOW, RED, BLUE, ENDC
from modules.Config import Config
from os import path
MSG_NEG=1
MSG_POS=2
MSG_NEUTRAL=4
def displayApiDifference(status, color=True, msg_type=MSG_POS & MSG_NEUTRAL & MSG_NEG, prefix=""):
spkgs = sorted(status.keys())
for pkg in spkgs:
if pkg == "+" or pkg == "-":
lines = []
if pkg == "-":
if msg_type & MSG_NEG > 0:
if color:
lines.append("%s%s%s" % (RED, status[pkg], ENDC))
else:
lines.append("%s" % msg)
else:
if msg_type & MSG_POS > 0:
if color:
lines.append("%s%s%s" % (BLUE, status[pkg], ENDC))
else:
lines.append("%s" % msg)
for line in lines:
print line
continue
lines = []
for msg in status[pkg]:
if msg[0] == "-":
if msg_type & MSG_NEG > 0:
if color:
lines.append("\t%s%s%s" % (RED, msg, ENDC))
else:
lines.append("\t%s" % msg)
elif msg[0] == "+":
if msg_type & MSG_POS > 0:
if color:
lines.append("\t%s%s%s" % (BLUE, msg, ENDC))
else:
lines.append("\t%s" % msg)
else:
if msg_type & MSG_NEUTRAL > 0:
lines.append("\t%s" % msg)
if lines != []:
if prefix != "":
if pkg == ".":
pkg = prefix
else:
pkg = "%s/%s" % (prefix, pkg)
if color:
print "%sPackage: %s%s" % (YELLOW, pkg, ENDC)
else:
print "Package: %s" % pkg
for line in lines:
print line
if __name__ == "__main__":
parser = optparse.OptionParser("prog [-e] [-d] DIR1 DIR2")
parser.add_option_group( optparse.OptionGroup(parser, "DIR1", "Directory with old source codes") )
parser.add_option_group( optparse.OptionGroup(parser, "DIR2", "Directory with new source codes") )
parser.add_option(
"", "-c", "--color", dest="color", action = "store_true", default = False,
help = "Color output."
)
parser.add_option(
"", "-v", "--verbose", dest="verbose", action = "store_true", default = False,
help = "Verbose mode."
)
parser.add_option(
"", "-e", "--error", dest="error", action = "store_true", default = False,
help = "Show errors only."
)
parser.add_option(
"", "-a", "--all", dest="all", action = "store_true", default = False,
help = "Show all differences between APIs"
)
parser.add_option(
"", "", "--prefix", dest="p", default = "",
help = "Import paths prefix"
)
parser.add_option(
"", "", "--old-xml", dest="oldxml", default = "",
help = "Use old symbols from xml"
)
parser.add_option(
"", "", "--new-xml", dest="newxml", default = "",
help = "Use new symbols from xml"
)
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(
"", "", "--skip-errors", dest="skiperrors", action = "store_true", default = False,
help = "Skip all errors during Go symbol parsing"
)
options, args = parser.parse_args()
if options.p != "" and options.p[-1] == '/':
print "Error: --prefix can not end with '/'"
exit(1)
missing_args = 2
# file exists?
if options.oldxml != "":
if not path.exists(options.oldxml):
print "Error: %s does not exists" % options.oldxml
exit(1)
missing_args = missing_args - 1
# file exists?
if options.newxml != "":
if not path.exists(options.newxml):
print "Error: %s does not exists" % options.newxml
exit(1)
missing_args = missing_args - 1
if missing_args == 2:
if len(args) < 2:
print "Missing DIR1 or DIR2"
exit(1)
if missing_args == 1:
if len(args) < 1:
print "Missing DIR"
exit(1)
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)
# 1) check if all provided import paths are the same
# 2) check each package for new/removed/changed symbols
cmp_src = CompareSourceCodes(skip_errors=options.skiperrors, noGodeps=noGodeps)
if options.oldxml != "" and options.newxml != "":
cmp_src.compareXmls(options.oldxml, options.newxml)
elif options.newxml != "":
cmp_src.compareDirXml(args[0], options.newxml)
elif options.oldxml != "":
cmp_src.compareXmlDir(options.oldxml, args[0])
else:
cmp_src.compareDirs(args[0], args[1])
for e in cmp_src.getError():
print "Error: %s" % e
if not options.error:
status = cmp_src.getStatus()
msg_type = MSG_NEG
if options.all:
msg_type = MSG_POS | MSG_NEUTRAL | MSG_NEG
elif options.verbose:
msg_type = MSG_POS | MSG_NEG
displayApiDifference(status, options.color, msg_type, options.p)