-
Notifications
You must be signed in to change notification settings - Fork 4
/
commit.py
executable file
·311 lines (254 loc) · 11 KB
/
commit.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
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/python
import time, re, os, MySQLdb, unicodedata, cPickle, zlib
from PyRSS2Gen import RSSItem, Guid
from jinja2 import Markup
from config import Config
import synonymmapping
from common import *
from database import DB
import gdiff
re_gitsvn = re.compile('git-svn-id: \w+://.+ \w{4,12}-\w{4,12}-\w{4,12}-\w{4,12}-\w{4,12}')
svn_diff_header = re.compile('Index: .+\s=+\s-{3} .+\s\+{3} .+\s@@\ [0-9\-, \+]+@@\s+')
svn_diff_newline = re.compile('\\ No newline at end of file\s')
svn_diff_property = re.compile('Property changes on: .+\s_+\sAdded: .+\s\s+[\-\+]\s.+')
svn_diff_deletions = re.compile('^-.+$', re.MULTILINE)
punctuation = re.compile('[^a-zA-Z0-9 ]')
class Commit:
repo = None
message = ''
date = 0
files = []
commitid = -1
changedTexts = None
changedTexts_data = None
diffIsReallyBig = False
initialized = False
def __init__(self):
self.changedTexts = None
self.diffIsReallyBig = False
def loadFromSource(self, repo, m, d, files, uid, diffs):
self.initialized = True
self.repo = repo
self.message = Commit.cleanUpCommitMessage(m)
self.date = d
self.files = files
self.uniqueid = uid
self.base_paths = self.getBasePath()
self.dbkeywords = self.getSynonyms(diffs)
self.keywords = set(self.dbkeywords)
self.keywords.add('project-' + repo.tagname)
self.keywords.add('maturity-' + repo.tagmaturity)
def loadFromDatabase(self, repo, row, files, keywords, data):
self.initialized = True
self.repo = repo
self.commitid = row[DB.commit.id]
self.message = row[DB.commit.message]
self.date = row[DB.commit.date]
self.uniqueid = row[DB.commit.uniqueid]
self.files = files
self.base_paths = self.getBasePath()
self.dbkeywords = keywords
self.keywords = set(self.dbkeywords)
self.keywords.add('project-' + repo.tagname)
self.keywords.add('maturity-' + repo.tagmaturity)
self.changedTexts_data = data
if self.changedTexts_data == 'TOOLARGE':
self.diffIsReallyBig = True
@staticmethod
def cleanUpCommitMessage(msg):
msg = re_gitsvn.sub('', msg)
return msg.strip()
def getBasePath(self):
if not self.initialized:
raise Exception("called getBasePath on unitialized Commit object")
if not self.files: return ""
paths = set()
for f in self.files:
paths.add(os.path.dirname(f))
l = [p for p in paths if p]
l.sort()
return l
#Implemented in Child Classes
#Returns an array of google diff structures used for google diff pretty-outputting
def getDiffsArray(self):
pass
#retrieves the metadata the child class needs for getChangedTexts from the populated object
# during creation, this metadata is passed in independently and this is not called
def getChangedTextMetadata(self):
pass
#Returns an array of changed texts used for synonym matching, not google's diff format
def getChangedTexts(self, metadata):
pass
#/Implemented in Child Classes
def _loadChangedTextFromBackingVar(self):
if self.changedTexts != None:
return self.changedTexts
elif self.changedTexts_data == 'TOOLARGE':
self.diffIsReallyBig = True
self.changedTexts = []
else:
data = zlib.decompress(self.changedTexts_data)
data = cPickle.loads(data)
self.changedTexts = data
return self.changedTexts
def testFulltext(self, fulltext):
#We don't want to fall back on an expensive operation in the subsequent function
if self.changedTexts == None and self.changedTexts_data == None:
raise Exception("called testFulltext prior to changedTexts_data being initialized")
for d in self.getChangedTexts(None):
if fulltext in d.lower(): return True
return False
def getPrettyDiffs(self, htmlize=True):
if not htmlize:
raise Exception("Do not know how to not htmlize prettyDiffs")
diffs = self.getDiffsArray()
differ = gdiff.diff_match_patch()
for d in diffs:
differ.diff_cleanupSemantic(d)
str = differ.diff_prettyHtml(d)
if not isinstance(str, unicode):
str = unicode(str, 'utf-8')
else:
str = str.encode('utf-8')
yield Markup(str)
def getSynonyms(self, diffs):
if not self.initialized:
raise Exception("called getSynonyms on unitialized Commit object")
keywords = synonymmapping.getTags(self, diffs)
return keywords
def save(self):
if not self.initialized:
raise Exception("called save on unitialized Commit object")
conn = DB.getConn()
c = conn.cursor()
sql = "INSERT INTO " + DB.commit._table + """(repoid, date, message, uniqueid)
VALUES(%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE uniqueid = VALUES(uniqueid)"""
c.execute(sql, (self.repo.id, self.date, self.message, self.uniqueid))
if self.commitid <= 0:
self.commitid = conn.insert_id()
data = self.getChangedTexts(None)
data = cPickle.dumps(data, cPickle.HIGHEST_PROTOCOL)
data = zlib.compress(data)
sql = "DELETE FROM " + DB.commitdiffs._table + " WHERE commitid = " + str(self.commitid)
c.execute(sql)
sql = "INSERT INTO " + DB.commitdiffs._table + "(commitid, data) "
sql += "VALUES(" + str(self.commitid) + ", %s)"
c.execute(sql, [data])
if self.files:
sql = "DELETE FROM " + DB.commitfile._table + " WHERE commitid = " + str(self.commitid)
c.execute(sql)
sql = "INSERT INTO " + DB.commitfile._table + "(commitid, file) "
for f in self.files:
sql += "SELECT " + str(self.commitid) + ", %s UNION "
sql = sql[:-6]
c.execute(sql, self.files)
if self.dbkeywords:
sql = "DELETE FROM " + DB.commitkeyword._table + " WHERE commitid = " + str(self.commitid)
c.execute(sql)
sql = "INSERT INTO " + DB.commitkeyword._table + "(commitid, keyword) "
for f in self.dbkeywords:
sql += "SELECT " + str(self.commitid) + ", %s UNION "
sql = sql[:-6]
c.execute(sql, [x for x in self.dbkeywords])
sql = "DELETE FROM " + DB.commitwordmap._table + " WHERE commitid = " + str(self.commitid)
c.execute(sql)
data = self.getChangedTexts(None)
data.append(self.message.lower())
data.extend([f.lower() for f in self.files])
data = [punctuation.sub(' ', d) for d in data]
allwords = set()
for d in data:
dWords = d.split()
dTotalIndex = 0
while dTotalIndex < len(dWords):
words = []
dThisIndex = 0
sql = "INSERT INTO " + DB.commitwordmap._table + "(commitid, word) "
while dThisIndex < 500 and dTotalIndex < len(dWords):
w = dWords[dThisIndex][:50]
if len(w) > 2 and w not in allwords:
sql += "SELECT " + str(self.commitid) + ", %s UNION "
words.append(w)
allwords.add(w)
dThisIndex += 1
dTotalIndex += 1
sql = sql[:-6]
if words:
DB.execute(c, sql, words)
conn.commit()
def getpprint(self, htmlize=False):
if not self.initialized:
raise Exception("called getpprint on unitialized Commit object")
if htmlize:
process = lambda x : Markup.escape(x)
else:
process = lambda x : x
eol = "\r\n"
s = ""
s += "Project:\t %s%s" % (self.repo.name, eol)
if htmlize:
s += "Project URL:\t <a href=\"%s\">%s</a>%s" % (self.repo.url, self.repo.url, eol)
else:
s += "Project URL:\t %s %s" % (self.repo.url, eol)
s += "Commit Date:\t %s (%s)%s" % (unixToGitDateFormat(self.date), self.date, eol)
s += "Log Message:\t %s%s" % (process(self.message), eol)
s += eol + eol
if self.files:
s += "Files:\t\t %s%s" % (process(self.files[0]), eol)
for p in self.files[1:14]:
s += "\t\t %s%s" % (process(p), eol)
if len(self.files) > 15:
s += "\t\t ...%s" % (eol)
if self.base_paths:
plural = len(self.base_paths) > 1
s += "Base Path%s:\t %s%s" % ("s" if plural else "", process(self.base_paths[0]), eol)
for p in self.base_paths[1:]:
s += "\t\t %s%s" % (process(p), eol)
s += "Keywords:\t %s%s" % (", ".join(self.keywords), eol)
s += "ID:\t\t %s%s" % (self.uniqueid, eol)
internallink = Config.rooturl + "/commit/" + self.repo.tagname + "/" + self.uniqueid
if htmlize:
s += "Internal:\t <a href=\"%s\">%s</a>%s" % (internallink, internallink, eol)
else:
s += "Internal:\t %s%s" % (internallink, eol)
if self.repo.viewlink:
externallink = self.repo.viewlink.replace('%ID', self.uniqueid)
if htmlize:
s += "External:\t <a href=\"%s\">%s</a>%s" % (externallink, externallink, eol)
else:
s += "External:\t %s%s" % (externallink, eol)
if htmlize:
return Markup(s)
else:
return s
def pprint(self):
print self.getpprint(False)
def toRSSItem(self):
title = self.repo.tagname
if self.message and len(self.message) > 50: title += " - " + str(Markup.escape(self.message[:50])) + "..."
elif self.message: title += " - " + str(Markup.escape(self.message))
if self.dbkeywords: title += " - " + ",".join(self.dbkeywords)
description = "<pre>"
description += str(self.getpprint(True))
description += "</pre>"
if type(title) != unicode:
title = unicode(title, 'utf-8')
if type(description) != unicode:
description = unicode(description, 'utf-8')
title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore')
description = unicodedata.normalize('NFKD', description).encode('ascii', 'ignore')
guid = Config.rooturl + "/commit/" + self.repo.tagname + "/" + self.uniqueid
link = ''
if self.repo.viewlink:
link = self.repo.viewlink.replace('%ID', self.uniqueid)
else:
link = guid
item = RSSItem(
title = title,
link = link,
description = description,
guid = Guid(guid, isPermaLink=0),
pubDate = unixToDatetime(self.date)
)
return item