-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
325 lines (277 loc) · 9.99 KB
/
run.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
#!flask/bin/python
from flask import Flask,request,jsonify,render_template
from werkzeug.utils import secure_filename
from flask_cors import CORS, cross_origin
import os
from google.cloud import vision
from subprocess import call, Popen, PIPE
import json
import sqlite3
import random
from faker import Faker
from cpp2python import conversiond
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime,timedelta
from db_setup import db_session
UPLOAD_FOLDER = './uploads'
app = Flask(__name__)
CORS(app, support_credentials=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///wmn19.db'
app.secret_key = "wmn19"
db = SQLAlchemy(app)
from db_setup import init_db
init_db()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/cpp')
def cpp():
return render_template('cplus.html')
@app.route('/analytics')
def analytics():
# funcrandom()
return render_template('analytics.html')
@app.route('/conversion')
def conversion():
# funcrandom()
return render_template('conversion.html')
@app.route('/python')
def python():
# funcrandom()
return render_template('python.html')
# @app.route('/psuedo')
# def psuedo():
# return render_template('psuedo.html')
@app.route('/upload',methods=['POST'])
@cross_origin(supports_credentials=True)
def upload():
codedatafile = request.files.get('file')
if codedatafile:
filename = secure_filename(codedatafile.filename)
codedatafile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
textcode = detect_document(filepath)
# import pdb; pdb.set_trace()
output = compilec(textcode)
data = datetime.today().date()
save_changes(data)
return jsonify({'status':'ok','text':textcode,'output':output})
return jsonify({'status':'No file !!'})
@app.route('/uploadcpp',methods=['POST'])
@cross_origin(supports_credentials=True)
def uploadcpp():
codedatafile = request.files.get('file')
if codedatafile:
filename = secure_filename(codedatafile.filename)
codedatafile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
textcode = detect_document(filepath)
# import pdb; pdb.set_trace()
output = compilecpp(textcode)
data = datetime.today().date()
save_changes(data)
return jsonify({'status':'ok','text':textcode,'output':output})
return jsonify({'status':'No file !!'})
@app.route('/uploadcppconversion',methods=['POST'])
@cross_origin(supports_credentials=True)
def uploadcppconversion():
codedatafile = request.files.get('file')
if codedatafile:
filename = secure_filename(codedatafile.filename)
codedatafile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
textcode = detect_document(filepath)
# import pdb; pdb.set_trace()
converse(textcode)
# data = datetime.today().date()
# save_changes(data)
return jsonify({'status':'ok','text':textcode,'output':'success'})
return jsonify({'status':'No file !!'})
# @app.route('/uploadpython',methods=['POST'])
# @cross_origin(supports_credentials=True)
# def uploadpython():
# codedatafile = request.files.get('file')
# if codedatafile:
# filename = secure_filename(codedatafile.filename)
# codedatafile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
# textcode = detect_document(filepath)
# # import pdb; pdb.set_trace()
# output = compilepy(textcode)
# data = datetime.today().date()
# save_changes(data)
# return jsonify({'status':'ok','text':textcode,'output':output})
# return jsonify({'status':'No file !!'})
@app.route('/runupload',methods=['POST'])
def runupload():
codedatafile = request.files.get('file')
if codedatafile:
filename = secure_filename(codedatafile.filename)
codedatafile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
print("detecing+++++++++++++++++++++++++++++++++++++++++++++")
textcode = detect_document(filepath)
return jsonify({'status':'ok','text':textcode})
return jsonify({'status':'No file !!'})
@app.route('/run',methods=['POST'])
def compileandrun():
print("processing+++++++++++++++++++++++++")
data = request.json.get('code')
output = compilec(data)
return jsonify({'output':output})
# @app.route('/runpy',methods=['POST'])
# def compileandrunpython():
# print("processing+++++++++++++++++++++++++")
# data = request.json.get('code')
# output = compilec(data)
# return jsonify({'output':output})
@app.route('/runcpp',methods=['POST'])
def compileandruncpp():
print("processing+++++++++++++++++++++++++")
data = request.json.get('code')
output = compilecpp(data)
return jsonify({'output':output})
# @app.route('/runpython',methods=['POST'])
# def compileandrunpython():
# print("processing+++++++++++++++++++++++++")
# data = request.json.get('code')
# output = compilepy(data)
# return jsonify({'output':output})
@app.route('/runconversion',methods=['POST'])
def runconversion():
data = request.json.get('code')
output = converse(data)
# import pdb; pdb.set_trace()
return output
def converse(textcode):
# import pdb; pdb.set_trace()
conversiond()
f = open("submission.py","r")
contents = f.read()
# json.dump(d, open("text.txt",'w'))
# file_object = open("../submission.py", "r+")
# print file_object.readlines()
return contents
def compilec(textcode):
with open('submission.c','w+') as mycode:
mycode.write(textcode)
ccompile = Popen(["gcc","submission.c"], stderr=PIPE)
ccompileerr = ccompile.communicate()[1].decode()
if ccompileerr != '':
call(["rm","submission.c"])
return ccompileerr
runoutput = Popen(["./a.out"], stdout=PIPE)
# import pdb; pdb.set_trace()
output = runoutput.communicate()[0]
call(["rm","submission.c", "a.out"])
return output.decode()
def compilepy(textcode):
with open('submission.py','w+') as mycode:
mycode.write(textcode)
ccompile = Popen(["python","submission.py"], stderr=PIPE)
#import pdb; pdb.set_trace()
import py_compile
py_compile.compile('submission.py')
runoutput = Popen(["python ./submission.pyc"], stdout=PIPE)
output = runoutput.communicate()[0]
call(["rm","submission.py", "submission.pyc"])
return output.decode()
def compilecpp(textcode):
with open('submission.cpp','w+') as mycode:
mycode.write(textcode)
ccompile = Popen(["g++","submission.cpp"], stderr=PIPE)
ccompileerr = ccompile.communicate()[1].decode()
if ccompileerr != '':
return ccompileerr
runoutput = Popen(["./a.out"], stdout=PIPE)
output = runoutput.communicate()[0]
call(["rm","submission.cpp", "a.out"])
return output.decode()
def detect_document(path):
"""Detects document features in an image."""
client = vision.ImageAnnotatorClient()
with open(path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.document_text_detection(image=image)
codedata = response.text_annotations[0].description
return codedata
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def save_changes(data):
"""
Save the changes to the database
"""
# import pdb; pdb.set_trace()
qry = db_session.query(Codebase).filter(Codebase.date==data)
qry = qry.first()
if qry:
qry.code_count += 1
db_session.commit()
return "ok"
codebaseobject = Codebase()
codebaseobject.code_count = 1
db_session.add(codebaseobject)
db_session.commit()
return "ook"
class Codebase(db.Model):
""""""
__tablename__ = "codebase"
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date,default=datetime.today().date())
code_count = db.Column(db.Integer)
# def __init__(self, date,code_count):
# """"""
# self.date = date
# self.code_count = code_count
@app.route('/getseven',methods=["GET"])
def getseven():
sevendays = (datetime.now() -timedelta(days=7)).date()
resultdata = db_session.query(Codebase).filter(Codebase.date>sevendays)
mydict = {}
for result in resultdata:
mydict[str(result.date)] = result.code_count
return jsonify({'result':mydict})
@app.route('/getdata',methods=["GET"])
def getdata():
resultdata = db_session.query(Codebase).all()
mydict = {}
# import pdb; pdb.set_trace()
for result in resultdata:
mydict[str(result.date)] = result.code_count
return jsonify({'result':mydict})
# @app.route('/bills/fetch',methods=["POST"])
# def fetch
# @app.route('/getdata',methods=["GET"])
# def getdata():
# conn = sqlite3.connect("/home/anshu/wmn19.db")
# cur = conn.cursor()
# cur.execute("SELECT * FROM codebase")
# resultdata = cur.fetchall()
# mydict = {}
# for result in resultdata:
# mydict[str(result.date)] = result.code_count
# return jsonify({'result':mydict})
def funcrandom():
fake = Faker()
for i in range(300):
datedata = fake.date_between(start_date='-1y', end_date='today')
nodata = random.randint(1,500)
codebaseobject = Codebase()
codebaseobject.date = datedata
codebaseobject.code_count = nodata
db_session.add(codebaseobject)
db_session.commit()
if __name__ == '__main__':
app.run(host='0.0.0.0')