forked from johnliu0/ece250-testing-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (94 loc) · 3.46 KB
/
Copy pathmain.py
File metadata and controls
106 lines (94 loc) · 3.46 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
# ECE250 Testing Server
# For the Winter 2020 term.
# Author: John Liu
import os
import subprocess
import uuid
import shutil
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
app = Flask(__name__)
# directory for where temporary files will be placed
app.config['UPLOAD_DIR'] = os.path.expanduser('~')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_files():
if 'src' not in request.files:
return 'name of files should be src'
# generate a temporary unique folder for file upload
temp_dir_name = str(uuid.uuid1())
temp_dir = os.path.join(app.config['UPLOAD_DIR'], temp_dir_name)
os.mkdir(temp_dir)
# save all uploaded files to temp folder
for file in request.files.getlist('src'):
filename = secure_filename(file.filename)
file.save(os.path.join(temp_dir, filename))
# run make
make_process = subprocess.Popen(
'make',
shell=True,
cwd=temp_dir,
universal_newlines=True,
stderr=subprocess.PIPE)
make_process.wait()
# check for compilation error
if make_process.returncode != 0:
# remove temp dir and its contents
shutil.rmtree(temp_dir)
stderr = make_process.stderr
err = []
while True:
err_line = stderr.readline()
if err_line == '':
break
err.append(err_line)
return render_template(
'compilefail.html',
error=err)
# iterate through each test case and compare with program output
test_case_data = []
test_case_files = [('test1.in', 'test1.out'), ('test2.in', 'test2.out'), ('test3.in', 'test3.out')]
test_case_num = 1
all_passed = True
for test_case_in_file, test_case_out_file in test_case_files:
# pipe testcase to program
test_case_in = subprocess.Popen(
['cat', os.path.join('testcases', 'p0', test_case_in_file)],
universal_newlines=True,
stdout=subprocess.PIPE)
test_case_in.wait()
test_process = subprocess.Popen(
'./playlistdriver',
shell=True,
cwd=temp_dir,
stdin=test_case_in.stdout,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
test_process.wait()
prog_output = test_process.stdout
expected_output_lines = []
actual_output_lines = []
success = True
with open(os.path.join('testcases', 'p0', test_case_out_file)) as test_case_out:
# compare test case output and program output line by line
while True:
test_case_line = test_case_out.readline()
prog_output_line = prog_output.readline()
if test_case_line == '' and prog_output_line == '':
break
if test_case_line != prog_output_line:
success = False
all_passed = False
expected_output_lines.append(test_case_line)
actual_output_lines.append(prog_output_line)
test_case_data.append((test_case_num, expected_output_lines, actual_output_lines, success))
test_case_num += 1
# remove temp dir and its contents
shutil.rmtree(temp_dir)
return render_template(
'testcases.html',
test_cases=test_case_data,
all_passed=all_passed)