forked from Joshua2000B/Advanced-Databases-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
284 lines (219 loc) · 10.1 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
DEBUG = False
from flask import Flask, render_template, request, redirect, send_file
from flask_socketio import SocketIO
import threading
import time
import io
if DEBUG:
import os
try:
os.remove("./data/srcml.db3")
except:
pass
import srcml_database
from srcml_analysis import *
app = Flask(__name__)
socket_io = SocketIO(app)
srcml_database._create_database()
@app.route('/', methods=['GET', 'POST'])
def repos():
if request.method == 'POST':
repo_id = request.form['repo_id']
success = srcml_database.remove_repo(repo_id)
if success:
return redirect('/') # Redirect to refresh the page
# For GET requests, retrieve and display repositories
repos = srcml_database.retrieve_repos()
return render_template('repos.html', repos=repos)
@app.route('/add', methods=['GET', 'POST'])
def add_repo():
if request.method == 'GET':
return render_template('add_repo.html')
if request.method == 'POST':
# filePath = request.form['filePath']
github_link = request.form['githubLink']
# You can replace the following line with actual srcML processing logic
result = f"Executing srcML for the GitHub repository: {github_link}"
thread = threading.Thread(target=process_github_link,args=(github_link,))
thread.start()
return render_template('add_repo.html', result=result)
return render_template('add_repo.html')
def process_github_link(github_link):
socket_io.emit("update",{'message':'Downloading...'})
status = download_github_repo(github_link)
if status:
socket_io.emit("update",{'message':'Downloaded the file!'})
else:
socket_io.emit("update",{'message':'Error downloading the file.'})
return
socket_io.emit("update",{'message':'Converting to srcML...'})
repo_name = "/".join(github_link.split("/")[-2:])
status = convert_to_srcml(repo_name) and convert_to_srcml(repo_name,True) and add_srcml_to_database(repo_name)
if status:
socket_io.emit("update",{'message':'Converted!'})
else:
socket_io.emit("update",{'message':'Error!'})
return
socket_io.emit("update",{'message':'Running stereocode...'})
status = run_stereocode(repo_name)
if status:
socket_io.emit("update",{'message':'Stereotyped!'})
else:
socket_io.emit("update",{'message':'Error!'})
return
socket_io.emit("update",{'message':'Collecting and evaluating names. This could take a while...'})
status = run_namecollector(repo_name) and add_names_to_database(repo_name)
if status:
socket_io.emit("update",{'message':'Collected!'})
else:
socket_io.emit("update",{'message':'Error!'})
return
socket_io.emit("update",{'message':'Counting tags...'})
status = count_tags(repo_name)
if status:
socket_io.emit("update",{'message':'Counted!'})
else:
socket_io.emit("update",{'message':'Error!'})
return
socket_io.emit("update",{'message':'Done! Redirecting...'})
socket_io.emit("finish",{'redirect':f'/'})
@app.route('/files/<repo_id>')
def list_files(repo_id):
files = srcml_database.retrieve_files(repo_id)
return render_template('files.html', files=files,repo=srcml_database.get_repo_name_from_id(repo_id))
@app.route('/identifiers/file/<file_id>')
def list_identifiers(file_id):
identifiers = srcml_database.retrieve_identifiers(file_id)
return render_template('identifiers.html', identifiers=identifiers,display=srcml_database.get_file_name_from_id(file_id))
@app.route('/identifiers/repo/<repo_id>')
def list_identifiers_from_repo(repo_id):
identifiers = srcml_database.retrieve_identifiers_from_repo(repo_id)
return render_template('identifiers.html', identifiers=identifiers,display=srcml_database.get_repo_name_from_id(repo_id))
@app.route('/tags/file/<file_id>')
def list_tags(file_id):
tags = srcml_database.retrieve_tags(file_id)
return render_template('tags.html', tags=tags,display=srcml_database.get_file_name_from_id(file_id))
@app.route('/tags/repo/<repo_id>')
def list_tags_from_repo(repo_id):
tags = srcml_database.retrieve_tags_from_repo(repo_id)
return render_template('tags.html', tags=tags,display=srcml_database.get_repo_name_from_id(repo_id))
@app.route('/xpath_run/all',methods=['GET','POST'])
def xpath_on_all():
if request.method == 'GET':
return render_template('run_xpath.html')
if request.method == 'POST':
xpath = request.form['xpath']
thread = threading.Thread(target=execute_xpath_on_all,args=(xpath,))
thread.start()
result="Running your XPath!"
return render_template('run_xpath.html', result=result)
def execute_xpath_on_all(xpath):
run_xpath_on_all(xpath)
time.sleep(1)
socket_io.emit("finish",{'redirect':'/'})
@app.route('/xpath_run/repo/<repo_id>',methods=['GET', 'POST'])
def xpath_on_repo(repo_id):
if request.method == 'GET':
return render_template('run_xpath.html')
if request.method == 'POST':
xpath = request.form['xpath']
thread = threading.Thread(target=execute_xpath_on_repo,args=(repo_id,xpath))
thread.start()
result="Running your XPath!"
return render_template('run_xpath.html', result=result)
def execute_xpath_on_repo(repo_id,xpath):
run_xpath_on_repo(repo_id,xpath)
time.sleep(1)
socket_io.emit("finish",{'redirect':'/'})
@app.route('/xpath_run/file/<file_id>',methods=['GET', 'POST'])
def xpath_on_file(file_id):
if request.method == 'GET':
return render_template('run_xpath.html')
if request.method == 'POST':
xpath = request.form['xpath']
thread = threading.Thread(target=execute_xpath_on_file,args=(file_id,xpath))
thread.start()
result="Running your XPath!"
return render_template('run_xpath.html', result=result)
def execute_xpath_on_file(file_id,xpath):
run_xpath_on_file(srcml_database.get_repo_id_from_file_id(file_id),file_id,xpath)
time.sleep(1)
socket_io.emit("finish",{'redirect':'/files/'+srcml_database.get_repo_id_from_file_id(file_id)})
@app.route('/srcql_run/all',methods=['GET','POST'])
def srcql_on_all():
if request.method == 'GET':
return render_template('run_srcql.html')
if request.method == 'POST':
srcql = request.form['srcql']
thread = threading.Thread(target=execute_srcql_on_all,args=(srcql,))
thread.start()
result="Running your srcql!"
return render_template('run_srcql.html', result=result)
def execute_srcql_on_all(srcql):
run_srcql_on_all(srcql)
time.sleep(1)
socket_io.emit("finish",{'redirect':'/'})
@app.route('/srcql_run/repo/<repo_id>',methods=['GET', 'POST'])
def srcql_on_repo(repo_id):
if request.method == 'GET':
return render_template('run_srcql.html')
if request.method == 'POST':
srcql = request.form['srcql']
thread = threading.Thread(target=execute_srcql_on_repo,args=(repo_id,srcql))
thread.start()
result="Running your srcql!"
return render_template('run_srcql.html', result=result)
def execute_srcql_on_repo(repo_id,srcql):
run_srcql_on_repo(repo_id,srcql)
time.sleep(1)
socket_io.emit("finish",{'redirect':'/'})
@app.route('/srcql_run/file/<file_id>',methods=['GET', 'POST'])
def srcql_on_file(file_id):
if request.method == 'GET':
return render_template('run_srcql.html')
if request.method == 'POST':
srcql = request.form['srcql']
thread = threading.Thread(target=execute_srcql_on_file,args=(file_id,srcql))
thread.start()
result="Running your srcql!"
return render_template('run_srcql.html', result=result)
def execute_srcql_on_file(file_id,srcql):
run_srcql_on_file(srcml_database.get_repo_id_from_file_id(file_id),file_id,srcql)
time.sleep(1)
socket_io.emit("finish",{'redirect':'/files/'+srcml_database.get_repo_id_from_file_id(file_id)})
@app.route('/download/file/<file_id>', methods=['GET'])
def download_file(file_id):
repo_name = srcml_database.get_repo_name_from_file_id(file_id)
file_name = srcml_database.get_file_name_from_id(file_id)
file_buffer = io.BytesIO()
file_buffer.write(get_unit_code(repo_name,file_name))
file_buffer.seek(0)
return send_file(file_buffer,as_attachment=True,download_name=file_name.split("/")[-1],mimetype="text/plain")
@app.route('/download_srcml/file/<file_id>', methods=['GET'])
def download_srcml_file(file_id):
repo_name = srcml_database.get_repo_name_from_file_id(file_id)
file_name = srcml_database.get_file_name_from_id(file_id)
file_buffer = io.BytesIO()
file_buffer.write(get_unit_text(repo_name,file_name))
file_buffer.seek(0)
return send_file(file_buffer,as_attachment=True,download_name=file_name.split("/")[-1]+".xml",mimetype="text/plain")
@app.route('/download/repo/<repo_id>', methods=['GET'])
def download_repo(repo_id):
return send_file("data/"+srcml_database.get_repo_name_from_id(repo_id)+"/code.zip",as_attachment=True,download_name=srcml_database.get_repo_name_from_id(repo_id).split("/")[-1]+".zip")
@app.route('/download_srcml/repo/<repo_id>', methods=['GET'])
def download_srcml_repo(repo_id):
return send_file("data/"+srcml_database.get_repo_name_from_id(repo_id)+"/code.xml",as_attachment=True,download_name=srcml_database.get_repo_name_from_id(repo_id).split("/")[-1]+".xml")
@app.route('/queries/file/<file_id>')
def list_queries(file_id):
queries = srcml_database.retrieve_queries(file_id)
return render_template('queries.html', queries=queries,display=srcml_database.get_file_name_from_id(file_id))
@app.route('/queries/repo/<repo_id>')
def list_queries_from_repo(repo_id):
queries = srcml_database.retrieve_queries_from_repo(repo_id)
return render_template('queries.html', queries=queries,display=srcml_database.get_repo_name_from_id(repo_id))
@app.route('/download/query_result/<query_id>/<file_id>', methods=['GET'])
def download_query_result(query_id, file_id):
repo_name = srcml_database.get_repo_name_from_file_id(file_id)
return send_file(f"data/{repo_name}/query_{query_id}_{file_id}.xml",as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)