forked from gofed/gofed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoapidiff.py
111 lines (89 loc) · 2.89 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
# 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
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:
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"
)
options, args = parser.parse_args()
if len(args) != 2:
print "Missing DIR1 or DIR2"
exit(1)
if options.p != "" and options.p[-1] == '/':
print "Error: --prefix can not end with '/'"
exit(1)
go_dir1 = args[0]
go_dir2 = args[1]
# 1) check if all provided import paths are the same
# 2) check each package for new/removed/changed symbols
cmp_src = CompareSourceCodes(go_dir1, go_dir2)
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)