-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwhitespaceneutralizer.py
107 lines (81 loc) · 3.94 KB
/
whitespaceneutralizer.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
#!/usr/bin/python
raise Exception("Moved to https://github.com/agaricusb/Srg2Source")
# Reconcile whitespace differences between two codebases, preferring one over the other
# in order to reduce unnecessary diff lines
import sys, os, pprint, re
import srglib
rewriteFiles = True
# Get dict of all lines without whitespace, mapped to line number and original line text
def getLinesMap(filename):
seenLines = {}
for i, originalLine in enumerate(file(filename).readlines()):
key = removeNonPrefixWhitespace(originalLine)
if not seenLines.has_key(key):
seenLines[key] = []
seenLines[key].append((i, originalLine))
return seenLines
# Remove all whitespace except at the very beginning
def removeNonPrefixWhitespace(s):
prefix, rest = re.match(r"(\s*)(.*)", s).groups()
return prefix + srglib.killWhitespace(rest)
def neutralizeWhitespace(cbFilenamePath, mcpFilenamePath, className):
cbLinesMap = getLinesMap(cbFilenamePath)
mcpLinesMap = getLinesMap(mcpFilenamePath)
lineChanges = {}
# Find whitespace-only differences in unique lines
for key in cbLinesMap:
for cbLineNo, cbOriginalLine in cbLinesMap[key]:
if not mcpLinesMap.has_key(key): continue
for mcpLineNo, mcpOriginalLine in mcpLinesMap[key]: # TODO: nested loops really necessary?
if cbOriginalLine != mcpOriginalLine:
# Lines are identical except for whitespace
# For example in (casts), CB often has a byte after the closing paren, while MCP
# doesn't.. and there's a space after array initializers { ...}. CB prefers more
# space. Unfortunately, astyle doesn't fix this. CB seems to be using the jacobe
# tool for reformatting (see Bukkit-MinecraftServer GitHub project), not astyle.
#print "CB:",cbOriginalLine
#print "MC:",mcpOriginalLine
# MCP is right
lineChanges[cbLineNo] = mcpOriginalLine
# Change those lines
newLines = []
count = 0
for i, line in enumerate(file(cbFilenamePath).readlines()):
if lineChanges.has_key(i):
newLines.append(lineChanges[i])
count += 1
else:
newLines.append(line)
# Special case: remove spurious blank line after opening brace of class declaration statement
i = 0
while i < len(newLines):
if isBlank(newLines[i]) and i >= 2:
if newLines[i - 1].startswith("{") and "class" in newLines[i - 2]:
del newLines[i]
count += 1
i += 1
if rewriteFiles:
file(cbFilenamePath, "w").write("".join(newLines))
if count != 0:
print "Neutralized %s whitespace line differences in %s" % (count, className)
def isBlank(line):
return len(srglib.killWhitespace(line)) == 0
def main():
if len(sys.argv) != 3:
print "usage: %s cbRoot mcpRoot" % (sys.argv[0],)
raise SystemExit
cbRoot = sys.argv[1]#"../CraftBukkit"
mcpRoot = sys.argv[2]#"../mcp725-pkgd"
cbSrc = os.path.join(cbRoot, "src/main/java/net/minecraft/")
mcpSrc = os.path.join(mcpRoot, "src/minecraft_server/net/minecraft/")
for cbFilenamePath in srglib.getJavaSourceFiles(cbSrc):
# Get corresponding MCP filename path
commonPath = cbFilenamePath.replace(os.path.commonprefix((cbFilenamePath, cbSrc)), "")
className = os.path.splitext(commonPath)[0] # class name including package path
mcpFilenamePath = os.path.join(mcpSrc, commonPath)
assert os.path.exists(cbFilenamePath), "CB source %s not found?" % (cbFilenamePath,)
if srglib.isPollution(mcpFilenamePath): continue
assert os.path.exists(mcpFilenamePath), "CB source %s has no corresponding MCP file at %s" % (cbFilenamePath, mcpFilenamePath)
neutralizeWhitespace(cbFilenamePath, mcpFilenamePath, className)
if __name__ == "__main__":
main()