-
Notifications
You must be signed in to change notification settings - Fork 0
/
self_test.py
196 lines (153 loc) · 6.78 KB
/
self_test.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
import os, sys, json,time,zipstream
from genargs import genargs_selfTest
from init import app, db
from flask_login import LoginManager, login_user, login_required, logout_user, current_user
from flask import Flask, Response, make_response, render_template, send_file, send_from_directory,url_for, flash, request, redirect
from flask import Blueprint
sys.path.append("./debug")
from debug.judge import judge
from debug.runner import runcode, stdRuncode
self_test = Blueprint('self_test', __name__, template_folder='templates')
@self_test.route('/uploadSelfTestInputFile', methods=['POST'])
def uploadSelfTestInputFile():
if not current_user.is_authenticated:
return json.loads('{"code":"1","info":"%s"}'%("请先登录!"))
username=current_user.username
user_st_path=f"{app.config['WORKPLACE_FOLDER']}/users/{username}/self_test"
if(not os.path.exists(user_st_path)):
os.system(f"mkdir {user_st_path}")
f = request.files['file']
exp=f.filename.split(".")[-1]
f.save(os.path.join(user_st_path, "self_test.in"))
print(f"\033[1m\033[35m{username}\033[0m uploaded {f.filename} as self_test.in in {user_st_path}")
os.system(f":> {user_st_path}/self_test.out")
os.system(f":> {user_st_path}/self_test.log")
os.system(f":> {user_st_path}/self_test_debug_info.txt")
return json.loads('{"code":"0","info":"%s"}'%("上传成功!"))
@self_test.route('/uploadSelfTestInputText', methods=['POST'])
def uploadSelfTestInputText():
if not current_user.is_authenticated:
return json.loads('{"code":"1","info":"%s"}'%("请先登录!"))
username=current_user.username
user_st_path=f"{app.config['WORKPLACE_FOLDER']}/users/{username}/self_test"
if(not os.path.exists(user_st_path)):
os.system(f"mkdir {user_st_path}")
text = request.json['Text']
st_input_path = os.path.join(user_st_path, "self_test.in")
os.system('echo \'%s\' > %s'%(text, st_input_path))
print(f"\033[1m\033[35m{username}\033[0m uploaded self test input as self_test.in in {user_st_path}")
os.system(f":> {user_st_path}/self_test.out")
os.system(f":> {user_st_path}/self_test.log")
os.system(f":> {user_st_path}/self_test_debug_info.txt")
return json.loads('{"code":"0","info":"%s"}'%("上传成功!"))
@app.route('/uploadSelfTestArgs', methods=['POST'])
def uploadSelfTestArgs():
if not current_user.is_authenticated:
return json.loads('{"code":"1","info":"%s"}'%("请先登录!"))
username = current_user.username
user_st_path=f"{app.config['WORKPLACE_FOLDER']}/users/{username}/self_test"
hwID = request.json['hwID']
runargs_path=f'{user_st_path}/runargs.json'
os.system('echo \'%s\' > %s'%(genargs_selfTest(hwID), runargs_path))
print(f"\033[1m\033[35m{username}\033[0m update self test to hw {hwID}")
return json.loads('{"code":"0","info":"%s"}'%("上传成功!"))
@app.route('/startSelfTest', methods=['POST'])
@login_required
def startSelfTest():
username = current_user.username
user_st_path=f"{app.config['WORKPLACE_FOLDER']}/users/{username}/self_test"
user_path=f"{app.config['WORKPLACE_FOLDER']}/users/{username}"
std_path = './static/workplace/std'
user_st_in_path = os.path.join(user_st_path,"self_test.in")
user_st_out_path = os.path.join(user_st_path,"self_test.out")
user_st_log_path = os.path.join(user_st_path,"self_test.log")
user_st_debuginfo_path = os.path.join(user_st_path,"self_test_debug_info.txt")
std_out_path = os.path.join(std_path,f"output/{username}/self_test.out")
args_path = os.path.join(user_st_path,"runargs.json")
json_path = os.path.join(user_st_path, "result.json")
os.system(f":> {user_st_path}/self_test.out")
os.system(f":> {user_st_path}/self_test.log")
os.system(f":> {user_st_path}/self_test_debug_info.txt")
hwID = 0
with open(args_path, "r") as file:
args_data = json.load(file)
hwID = args_data['hwID']
print(f"\033[1m\033[35m{username}\033[0m start self test")
user_ret = runcode(user_path,
user_st_in_path,
user_st_out_path,
user_st_log_path,
hwID,
1)
std_ret = stdRuncode(std_path,
user_st_in_path,
std_out_path,
hwID)
is_wa = 0
is_tle = 0
is_re = 0
info = ""
out = ""
log = ""
in_ = ""
if int(user_ret) >> 8 == 124: # timeout
is_tle = 1
info = "Time Limit Exceeded"
elif int(user_ret) != 0:
is_re = 1
info = "RE, see log file"
else:
is_wa, info = judge(hwID, user_st_in_path, user_st_out_path, std_out_path)
os.system(f"echo \'{info}\' > {user_st_debuginfo_path}")
with open(user_st_out_path) as outfile:
out = outfile.read()
with open(user_st_log_path) as logfile:
log = logfile.read()
with open(user_st_in_path) as infile:
in_ = infile.read()
result_data = {}
result_data['in'] = in_
result_data['out'] = out
result_data['log'] = log
result_data['debuginfo'] = info
result_data['in_path'] = user_st_in_path
result_data['out_path'] = user_st_out_path
result_data['log_path'] = user_st_log_path
result_data['debuginfo_path'] = user_st_debuginfo_path
#print(result_data)
return result_data
@app.route('/updateSelfTest', methods=['POST'])
@login_required
def updateSelfTest():
username = current_user.username
user_st_path=f"{app.config['WORKPLACE_FOLDER']}/users/{username}/self_test"
user_st_out_path = os.path.join(user_st_path,"self_test.out")
user_st_log_path = os.path.join(user_st_path,"self_test.log")
user_st_debuginfo_path = os.path.join(user_st_path,"self_test_debug_info.txt")
in_ = ""
out = ""
log = ""
info = ""
user_st_in_path = os.path.join(user_st_path,"self_test.in")
if os.path.exists(user_st_out_path):
with open(user_st_out_path) as outfile:
out = outfile.read()
if os.path.exists(user_st_log_path):
with open(user_st_log_path) as logfile:
log = logfile.read()
if os.path.exists(user_st_debuginfo_path):
with open(user_st_debuginfo_path) as debuginfofile:
info = debuginfofile.read()
if os.path.exists(user_st_in_path):
with open(user_st_in_path) as infile:
in_ = infile.read()
result_data = {}
result_data['in'] = in_
result_data['out'] = out
result_data['log'] = log
result_data['debuginfo'] = info
result_data['in_path'] = user_st_in_path
result_data['out_path'] = user_st_out_path
result_data['log_path'] = user_st_log_path
result_data['debuginfo_path'] = user_st_debuginfo_path
return result_data