-
Notifications
You must be signed in to change notification settings - Fork 2
/
prettydiff.py
299 lines (280 loc) · 8.56 KB
/
prettydiff.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- coding: utf-8 -*-
# Script to create a pretty diff for TF2 patches and submit them to the wiki
# Written by WindPower
import re
import wikitools
import subprocess
import getpass
lineMatch = re.compile(r'^@@\s*-(\d+),\d+\s*\+(\d+),(\d+)\s*@@')
lineMatch2 = re.compile(r'^@@\s*-(\d+)\s*\+(\d+),(\d+)\s*@@')
lineMatch3 = re.compile(r'^@@\s*-(\d+),\d+\s*\+(\d+)\s*@@')
binaryFileRe = re.compile(r'Binary files (.+) and (.+) differ')
textFileRe = re.compile(r'--- (.[^\n]+)\n\+\+\+ (.[^\n]+)\n(.+)', re.DOTALL)
statusRe = re.compile(r'^\s?(\w)\s+\"?(.[^\"]+)\"?')
statusReRenamed = re.compile(r'^R\s+\"?(.[^\"]+)\"?\s+->\s+\"?(.[^\"]+)\"?')
def u(s):
if isinstance(s, unicode):
return s
if isinstance(s, str):
try:
return unicode(s)
except Exception:
try:
return unicode(s.decode('utf8'))
except Exception:
try:
return unicode(s.decode('windows-1252'))
except Exception:
return unicode(s, errors='ignore')
try:
return unicode(s)
except Exception:
try:
return u(str(s))
except Exception:
return s
def pootDiff(wiki, patchName, gitRepo):
patchName = u(patchName)
files = []
p = subprocess.Popen(['git', 'status', '--short'], shell=True, stdout=subprocess.PIPE, cwd=gitRepo)
filesChanged, err = p.communicate()
filesChanged = filesChanged.strip().split('\n')
for file in filesChanged:
if re.search(statusReRenamed, file):
isRenamed = True
oldname = re.search(statusReRenamed, file).group(1)
newname = re.search(statusReRenamed, file).group(2)
filename = newname
else:
isRenamed = False
filename = re.search(statusRe, file).group(2)
status = re.search(statusRe, file).group(1)
print 'Processing:', filename
if isRenamed:
files.append({
'name': filename,
'contents': u'',
'isBinary': False,
'isNew': False,
'isDeleted': False,
'isRenamed': True,
'oldName': oldname,
'newName': newname
})
else:
p = subprocess.Popen(['git', 'diff', '--cached', filename], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=gitRepo)
diff, err = p.communicate()
diff = u(diff)
if err != '' and status == u'D':
files.append({
'name': filename,
'contents': u'',
'isBinary': True,
'isNew': False,
'isDeleted': True,
'isRenamed': False
})
continue
if re.search(binaryFileRe, diff) is not None:
isBinary = True
contents = u''
else:
isBinary = False
try:
contents = u(re.search(textFileRe, diff).group(3).encode('utf-8')).strip()
except:
contents = u' '
files.append({
'name': filename,
'contents': contents,
'isBinary': isBinary,
'isNew': status == u'A',
'isDeleted': status == u'D',
'isRenamed': False
})
def cmpFiles(left, right):
if left['isRenamed']:
if right['isRenamed']:
return cmp(left['name'], right['name'])
if right['isBinary']:
return -1
if not right['isBinary']:
return 1
if left['isBinary']:
if right['isRenamed']:
return 1
if right['isBinary']:
return cmp(left['name'], right['name'])
if not right['isBinary']:
return 1
if not left['isBinary']:
if right['isRenamed']:
return -1
if right['isBinary']:
return -1
if not right['isBinary']:
return cmp(left['name'], right['name'])
files.sort(cmp=cmpFiles)
def formatFile(f):
isDelete = f['isDeleted']
isAdd = f['isNew']
isBinary = f['isBinary']
isRenamed = f['isRenamed']
if isRenamed:
oldName = f['oldName']
newName = f['newName']
contents = []
lines = f['contents'].split(u'\n')
def getLineContent(l):
if not l.strip():
return u' '
return l.replace(u'\t', u' ')
diffOpen = False
lineOld = -1
lineNew = -1
if not isBinary and not isRenamed:
for l in lines:
l = l.replace(u'\r', '').replace(u'\n', '')
lineRes = lineMatch.search(l)
lineRes2 = lineMatch2.search(l)
lineRes3 = lineMatch3.search(l)
if lineRes is not None or lineRes2 is not None or lineRes3 is not None:
if lineRes:
res = lineRes.group(1)
res2 = lineRes.group(2)
res3 = lineRes.group(3)
elif lineRes2:
res = lineRes2.group(1)
res2 = lineRes2.group(2)
res3 = lineRes2.group(3)
elif lineRes3:
res = lineRes3.group(1)
res2 = lineRes3.group(2)
res3 = 1
if diffOpen:
contents.append((u'\u2026'))
diffOpen = True
lineOld = int(res)
lineNew = int(res2)
elif diffOpen and len(l):
if l[0] == u' ':
contents.append((u' ', lineOld, lineNew, l[1:]))
lineOld += 1
lineNew += 1
elif l[0] == u'+':
contents.append((u'+', lineNew, l[1:]))
lineNew += 1
elif l[0] == u'-':
contents.append((u'-', lineOld, l[1:]))
lineOld += 1
ret = u'<div class="diff-file'
if isDelete:
ret += u' diff-file-deleted'
ret += u'"><div class="'
if isRenamed:
ret += u'diff-file-renamed'
elif isBinary:
ret += u'diff-name-binary'
else:
ret += u'diff-name-text'
ret += u'">'
if isRenamed:
ret += u'Renamed'
elif isAdd:
ret += u'Added'
elif isDelete:
ret += u'Deleted'
else:
ret += u'Modified'
ret += u': <span class="diff-name">'
subPageName = u'Template:PatchDiff/' + patchName + u'/' + f['name'].replace("{", "(")
if not isBinary and not isRenamed:
ret += u'[[' + subPageName + u'|' + f['name'] + u']]'
elif isRenamed:
ret += '{0} -> {1}'.format(oldName, newName)
else:
ret += f['name']
ret += u'</span></div>'
if len(contents):
ret += u'<div class="diff-contents"></div>'
diffRet = []
lastBlock = None
for c in contents:
if not len(c):
continue
d = u'<div class="diff-line-entry'
if c[0] == u' ':
d += u' diff-line-nochange'
elif c[0] == u'+':
d += u' diff-line-add'
elif c[0] == u'-':
d += u' diff-line-remove'
else: # Ellipsis
diffRet.append(u'<div class="diff-line-ellipsis">\u2026</div>')
lastBlock = None
classIndex = diffRet[-1].find(u'">')
if classIndex != -1:
diffRet[-1] = diffRet[-1][:classIndex] + u' diff-line-last' + diffRet[-1][classIndex:]
continue
if c[0] != lastBlock:
d += u' diff-line-first'
if lastBlock is not None:
classIndex = diffRet[-1].find(u'">')
if classIndex != -1:
diffRet[-1] = diffRet[-1][:classIndex] + u' diff-line-last' + diffRet[-1][classIndex:]
lastBlock = c[0]
d += u'">'
if c[0] == u' ':
d += u'<span class="diff-line-old">' + u(c[1]) + u'</span><span class="diff-line-new">' + u(c[2]) + u'</span>'
elif c[0] == u'+':
d += u'<span class="diff-line-old diff-line-na">N/A</span><span class="diff-line-new">' + u(c[1]) + u'</span>'
elif c[0] == u'-':
d += u'<span class="diff-line-old">' + u(c[1]) + u'</span><span class="diff-line-new diff-line-na">N/A</span>'
d += u'<span class="diff-line">' + getLineContent(c[-1]) + u'</span></div>'
diffRet.append(d)
finalText = u(u''.join(diffRet))
n = 0
success = False
page = wikitools.page.Page(wiki, subPageName)
while n < 10 and not success:
try:
page.edit(finalText, summary=u'Diff of file "' + f['name'] + u'" for patch [[:' + patchName + u']].', minor=True, bot=True, skipmd5=True, timeout=60)
page.setPageInfo()
success = page.exists
except:
print 'Failed to edit, attempt %s of 10' % str(n)
n += 1
if not success:
page.edit(u'<div class="diff-file">File too large to diff</div>', summary=u'Diff of file "' + f['name'] + u'" for patch [[:' + patchName + u']].', minor=True, bot=True, skipmd5=True)
ret += u'</div>'
return ret
patchDiff = u''
for f in files:
print 'Processing file:', f['name'], 'in patch:', patchName
d = formatFile(f)
patchDiff += d
print 'Editing patch diff page:', u'Template:PatchDiff/' + patchName
success = False
page = wikitools.page.Page(wiki, u'Template:PatchDiff/' + patchName)
while not success:
try:
page.edit(patchDiff, summary=u'Diff of patch [[:' + patchName + u']].', minor=True, bot=True)
page.setPageInfo()
success = page.exists
except:
print 'Failed to edit, retrying'
def poot(wikiApi, wikiUsername, wikiPassword, patchName, gitRepo):
wiki = wikitools.wiki.Wiki(wikiApi)
if wiki.login(wikiUsername, wikiPassword):
return pootDiff(wiki, patchName, gitRepo)
else:
print 'Invalid wiki username/password.'
if __name__ == '__main__':
wikiApi = raw_input('Poot Wiki API URL: ')
wikiUsername = raw_input('Poot Wiki username: ')
wikiPassword = getpass.getpass('Poot Wiki password: ')
patchName = raw_input('Poot Wiki patch page title: ')
gitRepo = raw_input('Poot path of git repo: ')
print 'Pooting...'
poot(wikiApi, wikiUsername, wikiPassword, patchName, gitRepo)
print 'Is gud.'