-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
327 lines (289 loc) · 9.12 KB
/
app.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
import sqlite3
import subprocess
from flask import Flask, render_template,request,flash
app=Flask(__name__)
#This is used for flash messages, but they don't work yet, so can actually be deleted from the code
app.config['SECRET_KEY'] = 'aZfe285BnpXvcz'
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def checkDB():
try:
conn = get_db_connection()
isinit = conn.execute('SELECT * from is_init').fetchall()
conn.close()
except:
with open('schema.sql') as f:
conn.executescript(f.read())
conn.close()
isinit = 0
return isinit
def runScript(sql):
scriptRan = False
try:
conn = get_db_connection()
conn.executescript(sql)
conn.close()
scriptRan = True
except:
scriptRan = False
return scriptRan
def getTests():
conn = get_db_connection()
tests = conn.execute('SELECT * FROM tests').fetchall()
conn.close()
return tests
def getAllRuns():
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_runs').fetchall()
conn.close()
return tests
def getRun(id):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_runs where id=' + str(id)).fetchone()
conn.close()
return tests
def getTest(id):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM tests where id=' + str(id)).fetchone()
conn.close()
return tests
def getTestData(idd):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_data where test_id=' + str(idd)).fetchall()
conn.close()
return tests
def getaTestData(idd):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_data where test_id=' + str(idd)).fetchone()
conn.close()
return tests
def addTest(testname,npath,cpath,rpath):
retVal = True
try:
conn = get_db_connection()
cursor=conn.cursor()
tests = cursor.execute('INSERT INTO tests (testname,nodepath,circompath,rustpath) VALUES (?,?,?,?)',(testname,npath,cpath,rpath))
## crete test data record
tests = conn.execute('INSERT INTO test_data (test_id,input_data,expected_output) VALUES (?,?,?)',(cursor.lastrowid,"",""))
conn.commit()
conn.close()
retVal = True
except Exception as ex:
print(ex)
retVal = False
return retVal
def createRun(id,rundescription):
retVal = 0
try:
conn = get_db_connection()
cursor=conn.cursor()
tests = cursor.execute('INSERT INTO test_runs (test_id,rundescription) VALUES (?,?)',(id,rundescription))
retVal = cursor.lastrowid
conn.commit()
conn.close()
except Exception as ex:
print(ex)
retVal = 0
return retVal
def insertTestData(id,circomoutput,circomMatch,rustoutput,rustMatch,wereEqual):
retVal = False
try:
conn = get_db_connection()
cursor=conn.cursor()
tests = cursor.execute('INSERT INTO test_outcomes (run_id,circomoutput,circommatch,rustoutput,rustmatch,wereEqual) VALUES (?,?,?,?,?,?)',(id,circomoutput,circomMatch,rustoutput,rustMatch,wereEqual))
conn.commit()
conn.close()
retVal = True
except Exception as ex:
print(ex)
retVal = False
return retVal
def getOutcomes(idd):
conn = get_db_connection()
tests = conn.execute('SELECT * FROM test_outcomes where run_id=' + str(idd)).fetchall()
conn.close()
return tests
def updateTest(testname,npath,cpath,rpath,id):
retVal = True
try:
conn = get_db_connection()
tests = conn.execute('UPDATE tests set testname=?,nodepath=?,circompath=?,rustpath=? where id=?',(testname,npath,cpath,rpath,id))
conn.commit()
conn.close()
retVal = True
except:
retVal = False
return retVal
def updateTestData(f1data,f2data,expectOutput,id):
retVal = True
try:
conn = get_db_connection()
tests = conn.execute('UPDATE test_data set field1_data=?,field2_data=?,expected_output=? where tesT_id=?',(f1data,f2data,expectOutput,id))
conn.commit()
conn.close()
retVal = True
except:
retVal = False
return retVal
def delTest(id):
retVal = True
try:
conn = get_db_connection()
tests = conn.execute('DELETE from tests where id=?',(id,))
conn.commit()
tests = conn.execute('DELETE from test_data where test_id=?',(id,))
conn.commit()
conn.close()
print("Test " +str(id) + " deleted")
retVal = True
except Exception as ex:
print("Test " +str(id) + " NOT deleted")
print(ex)
retVal = False
return retVal
@app.route('/')
def fuzzy():
isDBinit = checkDB()
tmptests = getTests()
return render_template('index.html',tests=tmptests)
@app.route('/addtest', methods=('GET', 'POST'))
def createTest():
respMsg = ''
if request.method == 'POST':
testname = request.form['tname']
nodepath = request.form['npath']
circompath = request.form['cpath']
rustpath = request.form['rpath']
wasinserted = addTest(testname,nodepath,circompath,rustpath)
if wasinserted:
flash('Test created successfully')
else:
flash('Failed to create Test')
return render_template('createTest.html')
if request.method == 'GET':
return render_template('createTest.html')
@app.route('/<int:idd>/edittest', methods=('GET', 'POST'))
def editTest(idd):
respMsg = ''
if request.method == 'POST':
id = idd
testname = request.form['tname']
nodepath = request.form['npath']
circompath = request.form['cpath']
rustpath = request.form['rpath']
wasinserted = updateTest(testname,nodepath,circompath,rustpath,id)
if wasinserted:
flash('Test updated successfully')
else:
flash('Failed to update Test')
test = getTest(idd)
return render_template('editTest.html',test=test)
if request.method == 'GET':
test = getTest(idd)
return render_template('editTest.html',test=test)
@app.route('/<int:idd>/deletetest', methods=('GET', 'POST'))
def deleteTest(idd):
respMsg = ''
if request.method == 'POST':
id = idd
wasinserted = delTest(idd)
if wasinserted:
respMsg = 'Test updated successfully'
else:
respMsg = 'Failed to update Test'
return respMsg
if request.method == 'GET':
tmptests = getTests()
return render_template('index.html',tests=tmptests)
@app.route('/<int:idd>/edittestdata', methods=('GET', 'POST'))
def editTestData(idd):
respMsg = ''
if request.method == 'POST':
id = idd
f1data = request.form['f1data']
f2data = request.form['f2data']
expectedOutcome = request.form['expectedoutcome']
wasinserted = updateTestData(f1data,f2data,expectedOutcome,idd)
if wasinserted:
respMsg = 'Test updated successfully'
else:
respMsg = 'Failed to update Test'
tmptestData = getaTestData(idd)
return render_template('editTestData.html',testData=tmptestData)
if request.method == 'GET':
tmptestData = getaTestData(idd)
return render_template('editTestData.html',testData=tmptestData)
@app.route('/<int:idd>/runtest', methods=('GET', 'POST'))
def runTest(idd):
respMsg = ''
if request.method == 'GET':
id = idd
testtorun = getTest(id)
testname = testtorun['testname']
nodepath = testtorun['nodepath']
circompath = testtorun['circompath']
rustpath = testtorun['rustpath']
tmptestData = getTestData(idd)
runId = createRun(id,testname + " test run")
for row in tmptestData:
nodeCommand = "node " + nodepath + " '" + row['field1_data'] + "' '" + row['field2_data'] + "' " + circompath
rustCommand = "cd " + rustpath + " && cargo run -q -- '" + row['field1_data'] + "' '" + row['field2_data'] + "'"
circomResp = subprocess.check_output(nodeCommand,shell=True)
rustResp = subprocess.check_output(rustCommand,shell=True)
castCommand = "cast --to-base " + str(rustResp.decode()[1:67]) + " 10"
castResp = subprocess.check_output(castCommand,shell=True)
circomMatch = 0
rustMatch = 0
wereEqual = 0
cout = circomResp.decode()[0:len(circomResp.decode())-1]
rout = castResp.decode()[0:len(castResp.decode())-1]
if cout == rout:
wereEqual = 1
print("Expected outcome: ")
print(row['expected_output'])
if cout == row['expected_output']:
circomMatch = 1
if rout == row['expected_output']:
rustMatch = 1
wasinserted = insertTestData(runId,cout,circomMatch,rout,rustMatch,wereEqual)
testOutcomes = getOutcomes(runId)
runVals = getRun(runId)
createdDate = runVals['created']
descr = runVals['rundescription']
tmpruns = getAllRuns()
return render_template('runTest.html',testOutcomes=testOutcomes,runs=tmpruns,createdDate=createdDate,descr=descr)
@app.route('/<int:idd>/viewruns', methods=('GET', 'POST'))
def viewRun(idd):
respMsg = ''
if request.method == 'GET':
try:
id = idd
testOutcomes = getOutcomes(id)
runVals = getRun(int(testOutcomes[0]['run_id']))
createdDate = runVals['created']
descr = runVals['rundescription']
tmpruns = getAllRuns()
except:
print("Failed to retrieve any tests")
tmpruns = getAllRuns()
return render_template('runTest.html',runs=tmpruns)
return render_template('runTest.html',testOutcomes=testOutcomes,runs=tmpruns,createdDate=createdDate,descr=descr)
@app.route('/runsql', methods=('GET', 'POST'))
def runSQL():
respMsg = ''
wasinserted = False
if request.method == 'POST':
SQLscript = request.form['sql']
if SQLscript != "":
wasinserted = runScript(SQLscript)
if wasinserted:
flash('Script ran successfully')
else:
flash('Script Failed to run')
return render_template('runscript.html',respMsg=respMsg)
if request.method == 'GET':
return render_template('runscript.html',respMsg=respMsg)
if __name__ == "__main__":
app.run()