upgrade to python3;add some validator examples

This commit is contained in:
2025-10-11 12:33:24 +08:00
parent 65632f0e60
commit 487c041148
25 changed files with 1492 additions and 317 deletions

View File

@@ -1,10 +1,22 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging, os, sys, socket, time, xmlrpclib, bz2, Cookie
import logging
import os
import sys
import socket
import time
import xmlrpc.client as xmlrpclib
import bz2
import http.cookies as Cookie
import unittest
from engineconfig import getConfig
from entity import Submit, Problem, TestCase, PresetCode, DataFile
class DataSourceError(Exception): pass
class DataSourceError(Exception):
pass
class DataSource:
@@ -17,12 +29,14 @@ class DataSource:
def _get_config_retry(self):
ret = self.config.retry
if ret < 1: ret = 1
if ret < 1:
ret = 1
return ret
def _get_config_retry_wait(self):
ret = self.config.retry_wait
if ret < 0.1: ret = 0.1
if ret < 0.1:
ret = 0.1
return ret
def _do_action(self, func, args):
@@ -32,7 +46,7 @@ class DataSource:
while retry > 0:
try:
ret = func(*args)
except DataSourceError, e:
except DataSourceError:
self.logger.exception("DataSourceError")
if retry > 0:
time.sleep(retry_wait)
@@ -74,7 +88,7 @@ class DataSource:
ret.append(TestCase(self, row))
return ret
def get_test(self, tid, raw = True):
def get_test(self, tid, raw=True):
func = self.driver.get_test
args = (tid, )
row = self._do_action(func, args)
@@ -87,13 +101,13 @@ class DataSource:
func = self.driver.get_presetcodes
args = (pid, lang)
rows = self._do_action(func, args)
return map(lambda row: PresetCode(self, row), rows)
return list(map(lambda row: PresetCode(self, row), rows))
def get_datafiles(self, pid):
func = self.driver.get_datafiles
args = (pid, )
rows = self._do_action(func, args)
return map(lambda row: DataFile(self, row), rows)
return list(map(lambda row: DataFile(self, row), rows))
def get_datafile_data(self, datafileid):
func = self.driver.get_datafile_data
@@ -139,7 +153,7 @@ class DataSource:
class JspAuthTransport(xmlrpclib.Transport):
def __init__(self):
xmlrpclib.Transport.__init__(self)
self.__cookies = Cookie.SmartCookie()
self.__cookies = Cookie.SimpleCookie()
def request(self, host, handler, request_body, verbose=0):
# issue XML-RPC request
@@ -168,17 +182,17 @@ class JspAuthTransport(xmlrpclib.Transport):
def get_jsession_id(self):
if self.__cookies.has_key('MoodleSession'):
if 'MoodleSession' in self.__cookies:
return self.__cookies['MoodleSession'].value
return None
def __sendJsessionCookie(self, connection):
if self.__cookies.has_key('MoodleSession'):
if 'MoodleSession' in self.__cookies:
connection.putheader(
'Cookie',
'MoodleSession=%s' % self.__cookies['MoodleSession'].value)
if self.__cookies.has_key('MoodleSessionTest'):
if 'MoodleSessionTest' in self.__cookies:
connection.putheader(
'Cookie',
'MoodleSessionTest=%s' % self.__cookies['MoodleSessionTest'].value)
@@ -209,9 +223,9 @@ class XmlRpcDataSource:
while True:
try:
return self.server.oj.get_judge_id()
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_judge_id')
time.sleep(self.config.retry_wait)
@@ -219,9 +233,9 @@ class XmlRpcDataSource:
while True:
try:
return self.server.oj.reset_submits(judgeid)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to reset_submits')
time.sleep(self.config.retry_wait)
@@ -230,12 +244,12 @@ class XmlRpcDataSource:
try:
submits = self.server.oj.get_submits(judgeid, limit)
for submit in submits:
if not isinstance(submit['code'], (str, unicode)):
if not isinstance(submit['code'], str):
submit['code'] = submit['code'].__str__()
return submits
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
time.sleep(self.config.retry_wait)
self.logger.exception('Failed to get_submits')
@@ -243,9 +257,9 @@ class XmlRpcDataSource:
while True:
try:
return self.server.oj.get_problem(problemid)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_problem')
time.sleep(self.config.retry_wait)
@@ -254,15 +268,15 @@ class XmlRpcDataSource:
try:
tests = self.server.oj.get_tests(problemid, full)
for test in tests:
if not isinstance(test['input'], (str, unicode)):
if not isinstance(test['input'], str):
test['input'] = test['input'].__str__()
if not isinstance(test['output'], (str, unicode)):
if not isinstance(test['output'], str):
test['output'] = test['output'].__str__()
self.logger.debug('Got %d test case(s)', len(tests))
return tests
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_tests')
time.sleep(self.config.retry_wait)
@@ -270,16 +284,16 @@ class XmlRpcDataSource:
while True:
try:
test = self.server.oj.get_gztest(testid)
if not isinstance(test['input'], (str, unicode)):
if not isinstance(test['input'], str):
test['input'] = test['input'].__str__()
if not isinstance(test['output'], (str, unicode)):
if not isinstance(test['output'], str):
test['output'] = test['output'].__str__()
test['input'] = bz2.decompress(test['input'])
test['output'] = bz2.decompress(test['output'])
return test
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_tests')
time.sleep(self.config.retry_wait)
@@ -288,13 +302,13 @@ class XmlRpcDataSource:
try:
codes = self.server.oj.get_presetcodes(problemid, lang)
for code in codes:
if not isinstance(code['code'], (str, unicode)):
if not isinstance(code['code'], str):
code['code'] = code['code'].__str__()
self.logger.debug('Got %d presetcodes', len(codes))
return codes
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_presetcodes')
time.sleep(self.config.retry_wait)
@@ -304,9 +318,9 @@ class XmlRpcDataSource:
files = self.server.oj.get_datafiles(problemid)
self.logger.debug('Got %d datafiles', len(files))
return files
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_datafiles')
time.sleep(self.config.retry_wait)
@@ -315,9 +329,9 @@ class XmlRpcDataSource:
try:
data = self.server.oj.get_datafile_data(datafileid)
return str(data)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_datafiles')
time.sleep(self.config.retry_wait)
@@ -327,24 +341,24 @@ class XmlRpcDataSource:
try:
return self.server.oj.update_submit_compilemessage(
id, compilemsg)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to update_submit_compilemessage')
time.sleep(self.config.retry_wait)
def update_submit_test_results(self, id, results):
for r in results:
if not isinstance(r['stdout'], (str, unicode)): r['stdout'] = ''
if not isinstance(r['stderr'], (str, unicode)): r['stderr'] = ''
if not isinstance(r['stdout'], str): r['stdout'] = ''
if not isinstance(r['stderr'], str): r['stderr'] = ''
r['stdout'] = xmlrpclib.Binary(r['stdout'])
r['stderr'] = xmlrpclib.Binary(r['stderr'])
while True:
try:
return self.server.oj.update_submit_test_results(id, results)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to update_submit_compilemessage')
time.sleep(self.config.retry_wait)
@@ -355,9 +369,9 @@ class XmlRpcDataSource:
mc.oj.update_submit_status(id, newstatus)
try:
return mc()
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
raise DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to update_submits_status')
time.sleep(self.config.retry_wait)
@@ -365,9 +379,9 @@ class XmlRpcDataSource:
while True:
try:
return self.server.oj.get_submit_status(sid)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
return DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_submit_status')
time.sleep(self.config.retry_wait)
@@ -375,13 +389,13 @@ class XmlRpcDataSource:
while True:
try:
msg = self.server.oj.get_submit_compilemessage(sid)
if isinstance(msg, (str, unicode)):
if isinstance(msg, str):
return msg
else:
return msg.__str__()
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
return DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_submit_status')
time.sleep(self.config.retry_wait)
@@ -389,16 +403,16 @@ class XmlRpcDataSource:
while True:
try:
return self.server.oj.get_submit(sid)
except xmlrpclib.Error, e:
except xmlrpclib.Error as e:
return DataSourceError(e)
except socket.error, e:
except socket.error as e:
self.logger.exception('Failed to get_submit')
time.sleep(self.config.retry_wait)
class DataSourceTest(unittest.TestCase):
def setUp(self):
execfile(os.path.join('..', 'testdata', 'test_config.py'))
exec(open(os.path.join('..', 'testdata', 'test_config.py')).read())
self.config = getConfig()
self.datasource = self.config.datasources[0]
self.dbname = os.path.join('..', 'testdata', self.config.testdb)

Binary file not shown.

View File

@@ -1,11 +1,17 @@
import logging, os, thread, threading, time, Queue
import logging
import _thread
import threading
import time
import queue
from engineconfig import getConfig
class JudgeEngine:
def __init__(self):
self.quit_event = threading.Event()
self.test_queue = Queue.Queue()
self.test_queue = queue.Queue()
self.config = getConfig()
self.quit_event.clear()
@@ -14,11 +20,11 @@ class JudgeEngine:
def run(self):
# one thread mode is good for debugging
if self.config.test_threads == 1:
thread.start_new_thread(self.transport, ())
_thread.start_new_thread(self.transport, ())
self.test()
else:
for i in range(self.config.test_threads):
thread.start_new_thread(self.test, ())
_thread.start_new_thread(self.test, ())
self.transport()
for ds in self.config.datasources:
@@ -30,7 +36,7 @@ class JudgeEngine:
total = 0
while not self.quit_event.isSet():
self.logger.info(
"%d submit in test queue, %d processed" % \
"%d submit in test queue, %d processed" %
(self.test_queue.qsize(), total))
c = 16 - self.test_queue.qsize()
if c > 0:
@@ -45,10 +51,10 @@ class JudgeEngine:
def test(self):
while not self.quit_event.isSet():
#pdb.set_trace()
# pdb.set_trace()
try:
submit = self.test_queue.get(True, self.config.fetch_interval)
except Queue.Empty:
except queue.Empty:
continue
tester = self.config.get_tester(submit.language)
if tester:
@@ -64,6 +70,8 @@ class JudgeEngine:
self.quit_event.set()
return
class JudgeError(Exception): pass
class JudgeError(Exception):
pass
# vim: set expandtab tabstop=4 shiftwidth=4:

Binary file not shown.

View File

@@ -1,9 +1,8 @@
#!/usr/bin/env python2.6
#!/usr/bin/env python3
import string
from string import split
from os import path
from Queue import Queue
from queue import Queue
from subprocess import Popen, PIPE
class EngineConfig:
@@ -39,9 +38,10 @@ class EngineConfig:
stdin=pa.stdout, stdout=PIPE)
output = pb.communicate()[0]
if output:
for user in string.split(output, '\n'):
user = string.strip(user)
if user: self.runas.put(user)
for user in output.decode().split('\n'):
user = user.strip()
if user:
self.runas.put(user)
pa.wait()
pb.wait()
@@ -49,7 +49,7 @@ class EngineConfig:
self.languages[profile] = tester
def get_tester(self, profile):
if self.languages.has_key(profile):
if profile in self.languages:
return self.languages[profile]
def add_datasource(self, ds):
@@ -65,41 +65,37 @@ class EngineConfig:
default_compileguard = (
'<judgehome>/scripts/compile-guard', ' <datadir>'
)
default_runguard = split(
'/usr/bin/sudo -u <user> <judgehome>/scripts/binary-guard ' +
default_runguard = (
'/usr/bin/sudo -u <user> <judgehome>/scripts/binary-guard ' +
'-e <extraproc> ' +
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x',
' '
)
maxmem_runguard = split(
'/usr/bin/sudo -u <user> <judgehome>/scripts/binary-guard ' +
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x'
).split(' ')
maxmem_runguard = (
'/usr/bin/sudo -u <user> <judgehome>/scripts/binary-guard ' +
'-e <extraproc> ' +
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x',
' '
)
java_runguard = split(
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x'
).split(' ')
java_runguard = (
'/usr/bin/sudo -u <user> ' +
'<judgehome>/scripts/java-guard -t <timelimit> -T 5 -m 262144 ' +
'-e <extraproc> ' +
'-d <rundir> -o <statfile> -p -x', ' '
)
python_runguard = split(
'-d <rundir> -o <statfile> -p -x'
).split(' ')
python_runguard = (
'<judgehome>/scripts/python-guardd ' +
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x',
' '
)
mono_runguard = split(
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x'
).split(' ')
mono_runguard = (
'/usr/bin/sudo -u <user> ' +
'<judgehome>/scripts/mono-guard -t <timelimit> -T 5 -m <maxmem> ' +
'-e <extraproc> ' +
'-d <rundir> -o <statfile> -p -x', ' '
)
bash_runguard = split(
'-d <rundir> -o <statfile> -p -x'
).split(' ')
bash_runguard = (
'/usr/bin/sudo -u <user> <judgehome>/scripts/bash-guard ' +
'-e <extraproc> ' +
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x',
' '
)
'-t <timelimit> -T 5 -m <maxmem> -d <rundir> -o <statfile> -p -x'
).split(' ')
default_compare = (
'<judgehome>/scripts/compare-guard', '<language>', '<codefile>',
'<stdinfile>', '<stdoutfile>', '<resultfile>'
@@ -151,7 +147,7 @@ class EngineConfig:
j2se15 = SimpleTester(
source = 'Main.java', target = 'Main.class',
compile = ('<judgehome>/scripts/javac-1.5',),
run = split('/usr/bin/java -cp <datadir> -Xms8M -Xmx64M Main'),
run = ('/usr/bin/java -cp <datadir> -Xms8M -Xmx64M Main').split(),
runenv = {},
basemem = {'RSS' : 7560 },
baseproc = 8,
@@ -163,7 +159,7 @@ class EngineConfig:
j2se16 = SimpleTester(
source = 'Main.java', target = 'Main.class',
compile = ('<judgehome>/scripts/javac-1.6',),
run = split('/usr/bin/java -cp <datadir> -Xms8M -Xmx64M Main'),
run = ('/usr/bin/java -cp <datadir> -Xms8M -Xmx64M Main').split(),
runenv = {},
basemem = {'RSS' : 7560 },
baseproc = 8,

Binary file not shown.

View File

@@ -16,21 +16,21 @@ class Problem:
self.id = row['id']
self.timemodified = row['timemodified']
self.vcode = row['validator_code']
if not isinstance(self.vcode, (str, unicode)):
if not isinstance(self.vcode, str):
self.vcode = self.vcode.__str__()
self.vtype = row['validator_type']
self.vlang = row['validator_lang']
self.gcode = row['generator_code']
if not isinstance(self.gcode, (str, unicode)):
if not isinstance(self.gcode, str):
self.gcode = self.vcode.__str__()
self.gtype = row['generator_type']
self.standard_code = row['standard_code']
if row.has_key('input_filename') and row['input_filename']:
if 'input_filename' in row and row['input_filename']:
self.input_filename = row['input_filename']
else:
self.input_filename = None
if row.has_key('output_filename') and row['output_filename']:
if 'output_filename' in row and row['output_filename']:
self.output_filename = row['output_filename']
else:
self.output_filename = None
@@ -60,7 +60,7 @@ class Problem:
return self.output_filename
def get_presetcodes(self, lang):
if not self.presetcodes.has_key(lang):
if not self.presetcodes or lang not in self.presetcodes:
codes = self.datasource.get_presetcodes(self.id, lang)
for code in codes: code.problem = self
self.presetcodes[lang] = codes
@@ -119,13 +119,11 @@ class Submit:
newresults = []
for r in results:
# stdout
f = file(r[4], 'r')
r[4] = f.read(config.output_sendback_size_limit)
f.close()
with open(r[4], 'r') as f:
r[4] = f.read(config.output_sendback_size_limit)
# stderr
f = file(r[5], 'r')
r[5] = f.read(config.output_sendback_size_limit)
f.close()
with open(r[5], 'r') as f:
r[5] = f.read(config.output_sendback_size_limit)
# strip stdout and stderr send back to datasource
# preventing post data too big
@@ -163,8 +161,8 @@ class TestCase:
self.memlimit = row['memlimit']
if not self.memlimit:
self.memlimit = config.maxmem
if row.has_key('nproc') and row['nproc']:
self.nproc = string.atoi(row['nproc'])
if 'nproc' in row and row['nproc']:
self.nproc = int(row['nproc'])
else:
self.nproc = 0
@@ -192,15 +190,15 @@ class TestCase:
input = string.replace(row['input'], '\r\n', '\n')
output = string.replace(row['output'], '\r\n', '\n')
f = file(self.infile, 'w')
f.write(input)
if len(input) > 0 and input[-1] != '\n': f.write('\n')
f.close()
with open(self.infile, 'w') as f:
f.write(input)
if len(input) > 0 and input[-1] != '\n':
f.write('\n')
f = file(self.outfile, 'w')
f.write(output)
if len(output) > 0 and output[-1] != '\n': f.write('\n')
f.close()
with open(self.outfile, 'w') as f:
f.write(output)
if len(output) > 0 and output[-1] != '\n':
f.write('\n')
logger.debug('Finished')
else:
logger.debug('Skip input/output file creation')
@@ -255,13 +253,11 @@ class DataFile:
data = datasource.get_datafile_data(self.id)
data = bz2.decompress(data)
if self.type == 'text':
f = open(self.absolute_path, 'w')
f.write(string.replace(data, '\r\n', '\n'))
f.close()
with open(self.absolute_path, 'w') as f:
f.write(string.replace(data, '\r\n', '\n'))
else:
f = open(self.absolute_path, 'wb')
f.write(data)
f.close()
with open(self.absolute_path, 'wb') as f:
f.write(data)
DataFile.write_lock.release()

Binary file not shown.

View File

@@ -23,20 +23,23 @@ class InternalJudge:
return self.compare_file(tin, tout, result, self.allowpe)
def compare_file(self, input, output, result, allowpe):
fo = file(output, 'rb'); fr = file(result, 'rb')
with open(output, 'rb') as fo, open(result, 'rb') as fr:
if not allowpe:
r = 'AC'
while r == 'AC':
so = fo.read(8192); sr = fr.read(8192)
if so == '' and sr == '': break
if so != sr: r = 'WA'
else:
so = fo.read(); sr = fr.read()
r = self.compare_string(so, sr)
fo.close(); fr.close()
return r
if not allowpe:
r = 'AC'
while r == 'AC':
so = fo.read(8192)
sr = fr.read(8192)
if so == b'' and sr == b'':
break
if so != sr:
r = 'WA'
else:
so = fo.read()
sr = fr.read()
r = self.compare_string(so.decode('utf-8', errors='ignore'),
sr.decode('utf-8', errors='ignore'))
return r
def compare_string(self, output, result):
if output == result: return 'AC'
@@ -76,10 +79,10 @@ class ExternalJudge:
os.makedirs(datadir)
self.comparecmd = tester.comparecmd
self.codefile = os.path.abspath(os.path.join(datadir, tester.source))
f = file(self.codefile, 'w')
f.write(string.replace(vcode, '\r\n', '\n'))
if len(vcode) > 0 and vcode[-1] != '\n': f.write('\n')
f.close()
with open(self.codefile, 'w') as f:
f.write(string.replace(vcode, '\r\n', '\n'))
if len(vcode) > 0 and vcode[-1] != '\n':
f.write('\n')
self.logger.debug("Save validator code as %s" % self.codefile)
@@ -119,7 +122,7 @@ class ExternalJudge:
if rundir: os.chdir(rundir)
os.execv(cmd[0], cmd)
print 'WA'
print('WA')
remaintime = self.config.judgescript_wait
pid1 = 0
@@ -133,13 +136,12 @@ class ExternalJudge:
while pid1 == 0:
try:
os.kill(pid, 9)
except os.OSError, e:
except OSError:
pass
pid1, status = os.waitpid(pid, os.WNOHANG)
f = file(rfile, 'r')
ret = string.strip(f.readline())
f.close()
with open(rfile, 'r') as f:
ret = f.readline().strip()
if ret != 'AC' and ret != 'PE': ret = 'WA'
return ret
@@ -268,7 +270,7 @@ public class Main {
def testPython(self):
code = """#!/usr/bin/env python
print 'AC'
print('AC')
"""
self.j = ExternalJudge('p1', 'python-2.5', code)
x = self.j.judge('s1', 't1', self.tin.name, self.tout.name, self.rst.name, self.err.name)

Binary file not shown.

View File

@@ -13,7 +13,7 @@ class OJTestCase(unittest.TestCase):
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
execfile(os.path.join('..', 'testdata', 'test_config.py'))
exec(open(os.path.join('..', 'testdata', 'test_config.py')).read())
self.config = getConfig()
self.ds = self.config.datasources[0]
self.dbname = os.path.join('..', 'testdata', self.config.testdb)

Binary file not shown.

View File

@@ -1,6 +1,6 @@
import math, os, resource, signal, string, sys, threading, logging, time, pickle
import shutil, Queue
import shutil, queue
import unittest
import pdb
from engineconfig import getConfig
@@ -103,12 +103,12 @@ class SimpleTester(TesterBase):
if os.path.exists(pcname):
try:
os.unlink(pcname)
except OSError, e:
except OSError:
self.logger.exception(
"Failed to delete presetcode file %s" % pcname)
return False
f = open(pcname, 'w')
f.write(string.replace(presetcode.code, '\r\n', '\n'))
f.write(presetcode.code.replace('\r\n', '\n'))
f.write('\n');
f.close()
@@ -118,24 +118,24 @@ class SimpleTester(TesterBase):
if os.path.exists(datadirsource):
try:
os.unlink(datadirsource)
except OSError, e:
except OSError:
self.logger.exception("Failed to delete source")
return False
if os.path.exists(datadirtarget):
try:
os.unlink(datadirtarget)
except OSError, e:
except OSError:
self.logger.exception("Failed to delete target")
return False
# preprocess source code
code = string.replace(submit.code, '\r\n', '\n')
code = string.replace(code, chr(0x1a), '') # char generated by tc
code = string.replace(code, 'getch()', '')
code = string.replace(code, 'getch ()', '')
code = string.replace(code, 'getch ( )', '')
code = submit.code.replace('\r\n', '\n')
code = code.replace(chr(0x1a), '') # char generated by tc
code = code.replace('getch()', '')
code = code.replace('getch ()', '')
code = code.replace('getch ( )', '')
code = string.replace(code, '\r\n', '\n')
code = code.replace('\r\n', '\n')
# write source to disk
f = open(datadirsource, 'w')
f.write(code)
@@ -146,7 +146,7 @@ class SimpleTester(TesterBase):
config = getConfig()
try:
submit.user = config.runas.get_nowait()
except Queue.Empty:
except queue.Empty:
self.logger.exception("No runas user left, please create more!")
return False
rundir = self.get_rundir(submit)
@@ -157,12 +157,12 @@ class SimpleTester(TesterBase):
if os.path.exists(rundir):
try:
self._remove(rundir)
except OSError, e:
except OSError:
self.logger.exception("Failed to delete rundir")
config.runas.put(submit.user)
return False
os.mkdir(rundir)
os.chmod(rundir, 0775)
os.chmod(rundir, 0o775)
return True
@@ -199,7 +199,7 @@ class SimpleTester(TesterBase):
for code in submit.get_presetcodes():
if not code.isheader:
cmd.append(code.name)
self.logger.debug(string.join(cmd, '_'))
self.logger.debug('_'.join(cmd))
errfile = os.path.join(datadir, 'compile.err')
@@ -210,9 +210,8 @@ class SimpleTester(TesterBase):
compilemsg = None
if os.path.exists(errfile):
f = file(errfile, 'r')
compilemsg = string.join(f.readlines(), '')
f.close()
with open(errfile, 'r') as f:
compilemsg = ''.join(f.readlines())
if compilemsg:
submit.set_compilemessage(compilemsg)
@@ -283,7 +282,7 @@ class SimpleTester(TesterBase):
s = s.replace('<datadir>', datadir)
s = s.replace('<user>', submit.user)
self.runenv[k] = s
self.logger.debug(string.join(cmd, ' ') + ' ' + str(self.runenv))
self.logger.debug(' '.join(cmd) + ' ' + str(self.runenv))
(exitcode, sig, timeused, memused) = \
self._execute(submit, cmd, timelimit = testcase.timelimit * 10,
@@ -365,22 +364,22 @@ class SimpleTester(TesterBase):
try:
os.close(0)
os.open(infile, os.O_RDONLY)
except Exception, e:
print e
except Exception as e:
print(e)
sys.exit(125)
if outfile:
try:
os.close(1)
os.open(outfile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666)
except Exception, e:
print e
os.open(outfile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0o666)
except Exception as e:
print(e)
sys.exit(125)
if errfile:
try:
os.close(2)
os.open(errfile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0666)
except Exception, e:
print e
os.open(errfile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0o666)
except Exception as e:
print(e)
sys.exit(125)
#os.chdir(self.get_datadir(submit))
@@ -398,22 +397,23 @@ class SimpleTester(TesterBase):
# read information form statfile
if statfile:
try:
stat = pickle.load(file(statfile, 'r'))
with open(statfile, 'rb') as f:
stat = pickle.load(f)
exitcode = stat['exitcode']
sig = stat['sig']
timeused = stat['timeused']
memused = 0
if self.basemem.has_key('RSS'):
if 'RSS' in self.basemem:
memused += stat['memrss'] - self.basemem['RSS']
if self.basemem.has_key('Data'):
if 'Data' in self.basemem:
memused += stat['memdata'] - self.basemem['Data']
if self.basemem.has_key('Stack'):
if 'Stack' in self.basemem:
memused += stat['memstack'] - self.basemem['Stack']
memused = max(0, memused)
except Exception, e:
except Exception as e:
self.logger.exception(e)
self.logger.error("Failed to read statfile: %s" % statfile)
exitcode = 127 # judge script error
exitcode = 127 # judge script error
return (exitcode, sig, timeused, memused)

Binary file not shown.