-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_to_arff.py
352 lines (301 loc) · 11.8 KB
/
sql_to_arff.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import sys
import os
import getopt
from datetime import date
import _mysql
import subprocess
from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, FileTransferSpeed, FormatLabel, Percentage, \
ProgressBar, ReverseBar, RotatingMarker, \
SimpleProgress, Timer
def usage():
'''prints the acceptable list of command line options to the user'''
print '''-------------------------------------------------------
Usage
-h or --help print list of commands
-uY or --db-user Y set database user to Y
-pX or --db-passwd X set password to X
-dZ or --db-database Z set db name to Z
-Ha or --db-host a set database host to a
--start-date YYYY/MM/DD ex: 1776/1/14
--end-date YYYY/MM/DD
-o or --out-file C set arff filename
-t or --td-numeric store time/date attribs as numerics instead of enum list
--extensions comma separated list of extensions
LinesAdded
LinesRemoved
TimeHour
TimeMin
Day
Month
Year
DayOfWeek
User
Comment
CommentLength
NumFiles
Diction
Style
Queequeg
def user : root
def password : ""
def db name : cvsanaly
def db host : localhost
def csv name : out.arff
-------------------------------------------------------'''
def main():
pass
try:
options, remainder = getopt.getopt(sys.argv[1:],
'hu:p:d:H:o:t',["help","db-user=","db-passwd=",
"db-database=","db-host=","start-date=","end-date=",
"out-file=","extensions=",'td-numeric'])
#fix later
except getopt.GetoptError, err:
print str(err)
usage()
sys.exit(2)
User='root'
Passwd=''
db_name='cvsanaly'
Host='localhost'
csv_name = 'out.arff'
start_date = None
end_date = None
extensions = []
tdnum = False
diction=False
for opt, arg in options:
if opt in ('-u','--db-user'):
User = arg
elif opt in ('-p','--db-passwd'):
Passwd = arg
elif opt in ('-h','--help'):
usage()
sys.exit(2)
elif opt in ('-d','--db-database'):
db_name = arg
elif opt in ('-H','--db-host'):
Host = arg
elif opt in ('--start-date'):
sd = arg.split('/')
start_date = date(int(sd[0]),int(sd[1]),int(sd[2]))
elif opt in ('--end-date'):
ed = arg.split('/')
end_date = date(int(ed[0]),int(ed[1]),int(ed[2]))
elif opt in ('-o','--out-file'):
csv_name = arg
elif opt in ('--extensions'):
extensions = arg.split(',')
elif opt in ('-t','--td-numeric'):
tdnum = True
else:
usage()
assert False, "unhandled option "+opt
csv = open(csv_name,'w')
db = _mysql.connect(host=Host,user=User,passwd=Passwd,db=db_name)
#db.query("""SELECT spam, eggs, sausage FROM breakfast
# WHERE price < 5""")
#we really aught to consider a streaming option if this gets too big
time_query=""
if start_date is not None and end_date is not None:
time_query = "where commit_date BETWEEN '"+start_date.strftime("%y/%m/%d") +"' and '"+end_date.strftime("%y/%m/%d")+"'"
elif start_date is not None:
time_query = "where commit_date >= '"+start_date.strftime("%y/%m/%d")+"'"
elif end_date is not None:
time_query = "where commit_date <= '"+end_date.strftime("%y/%m/%d")+"'"
query_str=' select a.id,rev,commit_date,message,email,added,removed,file_count from scmlog a INNER JOIN people b ON a.author_id=b.id INNER JOIN commits_lines c on a.id=c.commit_id INNER JOIN (select commit_id,count(commit_id) as file_count from actions group by commit_id) as d on a.id=d.commit_id'
#get basic data
db.query(query_str+" "+time_query)
commits = db.use_result()
#get all the rows as a tuple of dictionaries, where each key is the column name
commit_data = commits.fetch_row(maxrows=0,how=1)
#get list of buggy commits
db.query("select DISTINCT bug_commit_id from Hunk_Blames;")
buggy = db.use_result()
buggy_data = buggy.fetch_row(maxrows=0,how=1)
buggys=[]
for b in buggy_data:
buggys.append(b['bug_commit_id'])
buggy=None
buggy_data=None
db.query('select DISTINCT email from people')
users = db.use_result()
user_data = users.fetch_row(maxrows=0)
header = '% 1. Title : '+csv_name.rstrip('.arff')+'\n'
header = header + '''%
%
% 2. Sources:
% (a) Creator : Zach Welch
% (b) Date '''
header = header + date.today().isoformat() +'\n@RELATION BUGGY\n'
csv.write(header)
for ext in extensions:
if ext != 'Style':
csv.write('@ATTRIBUTE '+ext+" ")
if ext == 'LinesAdded':
csv.write('numeric\n')
if ext == 'LinesRemoved':
csv.write('numeric\n')
if ext == 'TimeHour':
if tdnum:
csv.write('numeric\n')
else:
csv.write('{')
for ndx in range(23):
csv.write(str(ndx).zfill(2)+',')
csv.write('23}\n')
if ext == 'TimeMin':
if tdnum:
csv.write('numeric\n')
else:
csv.write('{')
for ndx in range(59):
csv.write(str(ndx).zfill(2)+',')
csv.write('59}\n')
if ext == 'Day':
if tdnum:
csv.write('numeric\n')
else:
csv.write('{')
for ndx in range(1,31):
csv.write(str(ndx).zfill(2)+',')
csv.write('31}\n')
if ext == 'Month':
if tdnum:
csv.write('numeric\n')
else:
csv.write('{')
for ndx in range(1,12):
csv.write(str(ndx).zfill(2)+',')
csv.write('12}\n')
if ext == 'Year':
csv.write('numeric\n')
if ext == 'DayOfWeek':
d=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
csv.write('{')
for day in d[:-1]:
csv.write(day+',')
csv.write(str(d[-1])+'}\n')
if ext == 'User':
csv.write('{')
for user in user_data[:-1]:
csv.write(user[0].replace(' ','')+',')
csv.write(user_data[-1][0]+'}\n')
if ext == 'Comment':
csv.write('STRING\n')
if ext == 'CommentLength':
csv.write('numeric\n')
if ext == 'NumFiles':
csv.write('numeric\n')
if ext in ('Diction','Queequeg'):
csv.write('numeric\n')
if ext == 'Style':
csv.write('@ATTRIBUTE Kincaid numeric\n')
#kincaid is a navy measurment of readibility
# csv.write('@ATTRIBUTE Flesch numeric\n')
#Flesch 0-100 difficult to easy readability
# csv.write('@ATTRIBUTE Lix numeric\n')
#Lix is a measure of grade school reading level
# csv.write('@ATTRIBUTE AvgWordLength numeric\n')
#average word length
# csv.write('@ATTRIBUTE ShortSentencePerc numeric\n')
#percentage of sentences at most 11 words
# csv.write('@ATTRIBUTE LongSentencePerc numeric\n')
#percentage of sentences at least 26 words
csv.write('@ATTRIBUTE classification {clean,buggy}\n\n@DATA\n')
num_buggy = 0
widgets = ['Progress: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
' ', ETA()]
pbar = ProgressBar(widgets=widgets, maxval=len(commit_data)).start()
ndx = 0
for commit in commit_data:
pbar.update(ndx)
ndx = ndx + 1
#print out the data
date_time = commit['commit_date']
cdate=date_time.split()[0]
ctime=date_time.split()[1]
cdates = cdate.split('-')
ctimes = ctime.split(':')
if commit['email'] == '':
continue
for ext in extensions:
if ext == 'LinesAdded':
csv.write(commit['added']+',')
if ext == 'LinesRemoved':
csv.write(commit['removed']+',')
if ext == 'TimeHour':
csv.write(ctimes[0]+',')
if ext == 'TimeMin':
csv.write(ctimes[1]+',')
if ext == 'Day':
csv.write(cdates[2]+',')
if ext == 'Month':
csv.write(cdates[1]+',')
if ext == 'Year':
csv.write(cdates[0]+',')
if ext == 'DayOfWeek':
dow = date(int(cdates[0]),int(cdates[1]),int(cdates[2])).weekday()
d=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
csv.write(d[dow]+',')
if ext == 'User':
csv.write(commit['email'].replace(' ','')+',')
if ext == 'Comment':
lines = commit['message'].splitlines()
messg = ''
for line in lines:
messg = messg+' '+line.lstrip()
messg = messg.replace(',','')
messg = messg.replace('"','')
csv.write('"'+messg.strip('"')+'",')
if ext == 'CommentLength':
csv.write(str(len(commit['message']))+',')
if ext == 'NumFiles':
csv.write(commit['file_count']+',')
if ext == 'Diction':
dfile = open('dict.txt','w')
dfile.write(commit['message'])
dfile.close()
diction = subprocess.Popen('diction dict.txt|grep dict.txt|wc -l',shell=True,stdout=subprocess.PIPE)
out,err=diction.communicate()
csv.write(out.strip()+',')
if ext == 'Style':
sfile = open('style.txt','w')
sfile.write(commit['message'].upper()+'.')
if any(c.isalpha() for c in commit['message']) is False:
sfile.write(' A.\n')
sfile.close()
style = subprocess.Popen('style style.txt',shell=True,stdout=subprocess.PIPE)
out,err=style.communicate()
outlines = out.split('\n')
#kincaid is a navy measurment of readibility
csv.write(outlines[1].split(':')[1].strip()+',')
#Flesch 0-100 difficult to easy readability
#csv.write(outlines[4].split(':')[1].split('/')[0].strip()+',')
#Lix is a measure of grade school reading level
#csv.write(outlines[6].split(':')[1].split('=')[0].strip()+',')
#average word length
#csv.write(outlines[10].split()[4].strip()+',')
#percentage of sentences at most 11 words
# csv.write(outlines[12].split('%')[0].strip()+',')
#percentage of sentences at least 26 words
# csv.write(outlines[13].split('%')[0].strip()+',')
if ext == 'Queequeg':
qfile = open('queequeg.txt','w')
qfile.write(commit['message'])
qfile.close()
quee = subprocess.Popen('qq queequeg.txt|wc -l',shell=True,stdout=subprocess.PIPE)
out,err=quee.communicate()
csv.write(out.strip()+',')
if commit['id'] in buggys:
csv.write('buggy')
num_buggy = num_buggy+1
else:
csv.write('clean')
csv.write('\n')
csv.close()
print '\nNum results ',len(commit_data)
print 'Num buggy ',num_buggy
print '% Buggy ',float(num_buggy)/float(len(commit_data))
if __name__ == "__main__":
main()