-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
199 lines (159 loc) · 7.03 KB
/
app.py
File metadata and controls
199 lines (159 loc) · 7.03 KB
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
import subprocess
import os
from flask import Flask, jsonify
import random
from flask_cors import CORS
from flask_restful import Resource, Api, reqparse
import string
app = Flask(__name__)
api = Api(app)
# cors = CORS(app)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
app.config['CORS_HEADERS'] = 'Content-Type'
parser = reqparse.RequestParser()
# Checks if Return Code of Process is 0 or 1
# 0 means successful, 1 means error
def r_code(arg):
return True if arg.returncode == 0 else False
class ExecuteCPP:
"""This class is used for Execution of C++ Codes."""
def __init__(self, filename, custom_input, timeout):
self.filename = filename # File Name
self.custom_input = custom_input # Input to script
self.timeout = timeout # Timeout, quits execution if timeout exceeds
self.outfile = self.filename[:-3] + 'out' # .out file name
def execute(self):
error_check = subprocess.run(["g++", self.filename, '-o', self.outfile],
capture_output=True,
timeout=self.timeout
) # Compiles .cpp file and generates the .out file if no compilation error found
if r_code(error_check): # No compilation error found
try:
process = subprocess.run(
['./' + self.outfile],
input=self.custom_input,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
timeout=self.timeout
) # Execute .out file and check for run time error
if r_code(process): # No run time error
os.remove('./' + self.outfile) # Delete the .out file
return process.stdout, process.returncode, False # Return stdout and Return Code of code
else: # If run time error
return process.stderr, process.returncode, False
except subprocess.TimeoutExpired: # If code gets terminated by timeout error
os.remove('./' + self.outfile) # Removes the out file even if it's timeout error
return '', 1, True
else: # Compilation error found
return error_check.stderr.decode(), error_check.returncode, False
# As the object gets deleted it will delete the .cpp file
def __del__(self):
os.remove('./' + self.filename)
class ExecuteC:
"""This class is used for Execution of C Codes."""
def __init__(self, filename, custom_input, timeout):
self.filename = filename
self.timeout = timeout
self.custom_input = custom_input
self.outfile = self.filename[:-1] + 'out'
def execute(self):
error_check = subprocess.run(["gcc", self.filename, "-o", self.outfile],
capture_output=True,
timeout=self.timeout)
if r_code(error_check):
try:
p = subprocess.run(['./' + self.outfile], input=self.custom_input, stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True, timeout=self.timeout)
if r_code(p):
os.remove('./' + self.outfile)
return p.stdout, p.returncode, False
else:
return p.stderr, p.returncode, False
except subprocess.TimeoutExpired:
os.remove('./' + self.outfile)
return '', 1, True
else:
return error_check.stderr.decode(), error_check.returncode, False
def __del__(self):
os.remove('./' + self.filename)
class ExecuteJava:
pass
class ExecutePython:
""" This class is used for execution of Python3 codes. """
def __init__(self, filename, custom_input, timeout):
self.filename = filename
self.timeout = timeout
self.custom_input = custom_input
def execute(self):
try:
process = subprocess.run(['python', self.filename], input=self.custom_input, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, text=True, timeout=self.timeout)
if r_code(process):
return process.stdout, process.returncode, False
else:
return process.stderr, process.returncode, False
except subprocess.TimeoutExpired:
return '', 1, True
def __del__(self):
os.remove('./' + self.filename)
class Home(Resource):
@staticmethod
def get_file_name(file_type, extension, source):
# This static function generates random filename and writes code to it then returns filename
name = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=5))
filename = file_type + name + extension
with open(filename, 'w+') as file:
file.write(source)
return filename
def post(self):
parser.add_argument('source', type=str)
parser.add_argument('language', type=str)
parser.add_argument('testcases', type=str)
parser.add_argument('timeout', type=int)
args = parser.parse_args()
source = args['source']
language = args['language']
timeout = args['timeout']
testcases = args['testcases']
# If language is python
if language == 'PYTHON':
filename = self.get_file_name(file_type='PYTHON', extension='.py', source=source)
python_object = ExecutePython(filename, testcases, timeout)
output, return_code, timeout_error = python_object.execute()
# if it's cpp
elif language == 'CPP':
filename = self.get_file_name(file_type='C++', extension='.cpp', source=source)
cpp_object = ExecuteCPP(filename=filename, custom_input=testcases, timeout=timeout)
output, return_code, timeout_error = cpp_object.execute()
# if it's C
elif language == 'C':
filename = self.get_file_name(file_type='C', extension='.c', source=source)
c_object = ExecuteC(filename, testcases, timeout)
output, return_code, timeout_error = c_object.execute()
# if language is not among above or we don't support it
else:
return jsonify({
"Request Status Code": 204,
"Output": "Support for {} language will be added soon!!!".format(language)
})
return_json = {
"Request Status Code": 200,
"Input": {
"source": source,
"language": language,
"testcases": testcases,
"timeout": timeout
},
"Output": {
"stdout": output,
"ReturnCode": return_code,
"timeoutError": timeout_error
}
}
return jsonify(return_json)
# Add home class to resource
api.add_resource(Home, '/execute/v2/')
if __name__ == '__main__':
app.run(debug=True)