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
| import subprocess import os import time import re import json import platform
dir = os.path.abspath(os.path.dirname(__file__)) os_type = '1'
if(platform.system() == 'Windows'): os_type = '2'
def match(name): res = re.search(r'^([0-9]+)\(([0-9]+)\)', name) if(res): if(name.endswith(".c") or name.endswith(".cpp")): return True return False
def untest(name): res = re.search(r'^0-([0-9]{14})-([0-9]+)\(([0-9]+)\)', name) if(res): return True return False
def edit(dir): for home, dirs, files in os.walk(dir): for filename in files: if match(filename):
os.rename(os.path.join(home, filename), os.path.join( home, time.strftime("0-%Y%m%d%H%M%S-", time.localtime()) + filename))
def generate(dir): for home, dirs, files in os.walk(dir): for filename in files: if untest(filename): newname = '1' + filename[1:]
os.rename(os.path.join(home, filename), os.path.join(home, newname)) return newname return ''
def build(cname, oname): try: res = subprocess.check_output("gcc \"%s\" -o %s" % (cname, oname), stderr=subprocess.STDOUT, shell=True) return True except subprocess.CalledProcessError as e: return False
def run(oname, idata, odata): r, w = os.pipe() os.write(w, bytes(idata, "utf-8")) os.close(w)
if(os_type == '1'): try: res = subprocess.check_output("./%s" % (oname), stdin=r, timeout=5) except subprocess.TimeoutExpired as e: return False except subprocess.CalledProcessError as e: return False if(os_type == '2'): try: res = subprocess.check_output("%s" % (oname), stdin=r, timeout=5) except subprocess.TimeoutExpired as e: return False except subprocess.CalledProcessError as e: return False try: result = res.decode("utf-8").replace('\r', '') except: return False
if result.strip().replace(" ","") == odata.strip().replace(" ",""): return True print(result) return False
def test(oname, question): question_list = { '1': [['24', '0'], ['11', '1'], ['0', '0'], ['-2', '0'], ['-21', '1']], '2': [['a', 'a'], ['A', 'a'], ['Z', 'z'], ['0', '0'], ['Q', 'q']], '3': [['12/14/16\n10/10/15', '10/10/15'], ['13/12/16\n10/10/15', '-1']], '4': [['1 2 3 4', '4 1']], '5': [['3 4 5', '1\n1'], ['1.5 1.5 1.5', '1\n0'], ['0 0 0', '0\n0'], ['-3 -4 -5', '0\n0']], '6': [['2019 1 1', '1'], ['1900 3 1', '60'], ['2000 3 1', '61']], '7': [['1 2 3 4 5 6 7 8 9 10', '1 1\n'], ['89 56 23 14 789 54 1223 78 12 65', '12 9\n'], ['3 2 1 1 5 6 7 8 9 10', '1 3\n']], '8':[['1 2 3 4 5 6 7 8 9 10', '55.00 5.50\n'],['1 1 1 1 1 1 1 1 1 1', '10.00 1.00\n'],['0.6798 0.5958 0.5754 0.4854 0.3895 0.2901 0.1942 0.1053 0.045 0.003','3.40 0.34\n']], '9':[['1 2 3 4 5\n2 3 4 5 1\n3 4 5 1 2\n4 5 1 2 3\n5 1 2 3 4','15 15 15 15 15\n3.00 3.00 3.00 3.00 3.00\n3.00 3.00 3.00 3.00 3.00\n5 5 5 5 5\n1 1 1 1 1'],['24 14 8 0 46\n36 20 20 20 96\n34 20 20 2 76\n40 20 20 18 98\n36 20 20 20 96','92 192 152 196 192\n18.40 38.40 30.40 39.20 38.40\n34.00 18.80 17.60 12.00 82.40\n40 20 20 20 98\n24 14 8 0 46']], '10':[['ASJFKLSJFLSJFLSJFIIQJKZVNIRUQ','13\n'],['ABCDEFGHIJKLMNOPQRSTUVWXY','25\n'],['ONNSISSTUPID','8\n']], '11':[['dzldzldzl\nd','3\n'],['13757769913\n3','2\n'],['2302018dzllp\nl','2\n'],['QCHZK\nq','0\n']], '12':[['ASJFKLSJFLSJFLSJFIIQJKZVNIRUQ','ASJFKLSJFLSJFLSJFIIQJKZVNIRUQ\n'],['ABCDEFGHIJKLMNOPQRSTUVWXY','ABCDEFGHIJKLMNOPQRSTUVWXY\n']] } if(not question in question_list): return False for test_data in question_list[question]: if(not run(oname, test_data[0], test_data[1])): print(test_data[0]) print(test_data[1]) return False return True
def write(data): f = open(os.path.join(dir, 'result.json'), 'r') old_data = json.loads(f.read()[9:-1]) f.close() print(data) old_data.append(data) f = open(os.path.join(dir, 'result.json'), 'w') f.write('callback('+json.dumps(old_data) + ')') f.close
if __name__ == "__main__": os.chdir(dir) while(True): homework_dir = os.path.join(dir, 'homework2') edit(homework_dir) while(True): cname = generate(homework_dir) if(cname == ''): break
res = re.search(r'^1-([0-9]{14})-([0-9]+)\(([0-9]+)\)', cname) if(res): date = res.group(1) xmuid = res.group(2) question = res.group(3) cname = os.path.join(homework_dir, cname) status = '0' if build(cname, 'raw.o'): if(test('raw.o', question)): status = '1' else: status = '3' else: status = '2' write({'date': date[:8], 'time': date[8:10] + ':' + date[10:12] + ':' + date[12:14], 'xmuid': xmuid, 'question': question, 'status': status}) time.sleep(5)
|