Skip to content

Commit

Permalink
Issue #54, TemporaryDirectory for Sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
minhoryang committed Apr 29, 2016
1 parent 7244138 commit 860a957
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
50 changes: 27 additions & 23 deletions OnlineJudgeServer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from tempfile import TemporaryDirectory
import uuid
from json import load

Expand Down Expand Up @@ -84,23 +85,27 @@ def task_judge(problemset, problem, filename, data):


# WHENEVER CHANGES HAPPENED FOR CELERY, NEED TO RESTART CELERY!
@celery.task(bind=True, track_started=True, ignore_result=False)
def subtask_judge(self, previous_return=None, **kwargs):
@celery.task(track_started=True, ignore_result=False)
def subtask_judge(previous_return=None, **kwargs):
filename = kwargs['filename']
idx = kwargs['idx']
json = kwargs['json']

if not previous_return: # first run,
found = Feedback.query.get(filename)
found.status = Status['STARTED']
found.cur_idx = 0
db.session.commit()

if not os.path.exists('./UPLOADED/'):
os.makedirs('./UPLOADED/')
filepath = os.path.join('./UPLOADED/', found.filename)
with TemporaryDirectory() as dir:
filepath = os.path.join(dir, filename)
with open(filepath, 'wb') as f:
f.write(found.filedata)
return subtask_judge_run(dir, **kwargs)


def subtask_judge_run(dir, **kwargs):
filename = kwargs['filename']
idx = kwargs['idx']
json = kwargs['json']

class JudgeFailed(Exception):
pass
Expand Down Expand Up @@ -128,10 +133,10 @@ def _(*args, **kwargs):

try:
Validate(
this_program='./UPLOADED/%s' % (filename,),
this_program='./%s' % (filename,),
from_json=json,
report=report,
python=app.config['VIRTUAL_ENV'],
cwd=dir,
)
except JudgeFailed as Failed:
found = Feedback.query.get(filename)
Expand Down Expand Up @@ -263,17 +268,16 @@ def status(filename):
def submit():
f = request.files['upfile']
filename = str(uuid.uuid4())
if not os.path.exists('./UPLOADED/'):
os.makedirs('./UPLOADED/')
filepath = os.path.join('./UPLOADED/', filename)
f.save(filepath + '.origin')
data = None
with open(filepath + '.origin', 'rb') as f_origin:
data = f_origin.read()
det = Chardet(data)
data2 = None
if 'encoding' in det:
data2 = data.decode(det['encoding']).encode('utf-8')
with open(filepath, 'wb') as f_real:
f_real.write(data2)
return filename, data2
with TemporaryDirectory() as dir:
filepath = os.path.join(dir, filename)
f.save(filepath + '.origin')
data = None
with open(filepath + '.origin', 'rb') as f_origin:
data = f_origin.read()
det = Chardet(data)
data2 = None
if 'encoding' in det:
data2 = data.decode(det['encoding']).encode('utf-8')
with open(filepath, 'wb') as f_real:
f_real.write(data2)
return filename, data2
8 changes: 5 additions & 3 deletions OnlineJudgeServer/process_capsule.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class ProcessCapsule(object):
_CONDITIONS = [_SEGFAULT, EOF, TIMEOUT]
_TIMEOUT = .05

def __init__(self, program, logfile=None):
def __init__(self, program, logfile=None, cwd=None):
self.program = program
self.logfile = logfile
self.cwd = cwd
self._readpos = 0
self._runtime = None
self._initialized_pid = None
Expand Down Expand Up @@ -64,6 +65,7 @@ def run(self, with_check=True, flush_by_read=True, timeout=None):

self._runtime = spawn(
self.__cmd__(),
cwd=self.cwd,
logfile=self.logfile,
ignore_sighup=False)
self._initialized_pid = self._runtime.pid
Expand Down Expand Up @@ -155,8 +157,8 @@ class ALREADYLAUNCHED(Exceptions):


class PythonCapsule(ProcessCapsule):
def __init__(self, program, logfile=None, python=DEFAULT_PYTHON):
super().__init__(program, logfile=logfile)
def __init__(self, program, python=DEFAULT_PYTHON, **kwargs):
super().__init__(program, **kwargs)
self.python = python

def __cmd__(self):
Expand Down
18 changes: 7 additions & 11 deletions OnlineJudgeServer/terminal_capsule.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ def stream(capsule, captured_stdin, timeout=None):


@_TerminalCapsuleUtils.register(_TerminalCapsuleUtils.pprintify)
def Capture(this_program, to_json=None,
logfile=None, timeout=None, python=DEFAULT_PYTHON):
def Capture(this_program, to_json=None, timeout=None, **kwargs):
"""Run `this_program` by ProcessCapsule and Capture I/O `to_json`."""
captured = []
with Capsule(this_program, logfile=logfile, python=python) as capsule:
with Capsule(this_program, **kwargs) as capsule:
capsule.run(with_check=False)
while not capsule.is_dead():
_TerminalCapsuleUtils.endpoints(
Expand All @@ -152,15 +151,14 @@ def Capture(this_program, to_json=None,


@_TerminalCapsuleUtils.register()
def Playback(this_program, from_json,
logfile=None, timeout=None, python=DEFAULT_PYTHON):
def Playback(this_program, from_json, timeout=None, **kwargs):
"""Read I/O `from_json` and Playback it to `this_program`."""
if from_json is None:
raise Exception("-j, --json needed!")

with open(from_json, 'r') as fp:
captured = json_load(fp)
with Capsule(this_program, logfile=logfile, python=python) as capsule:
with Capsule(this_program, **kwargs) as capsule:
capsule.run(with_check=False)
for captured_stdin, _ in captured:
_TerminalCapsuleUtils.endpoints(
Expand All @@ -181,10 +179,8 @@ def Playback(this_program, from_json,


@_TerminalCapsuleUtils.register()
def Validate(this_program, from_json,
logfile=None, max_retries=50, timeout=None,
report=_TerminalCapsuleUtils.report,
python=DEFAULT_PYTHON):
def Validate(this_program, from_json, max_retries=50, timeout=None,
report=_TerminalCapsuleUtils.report, **kwargs):
"""Read I/O `from_json` and Validate it to `this_program`."""
if from_json is None:
raise Exception("-j, --json needed!")
Expand Down Expand Up @@ -280,7 +276,7 @@ def _GOT_STDOUT():
print('Output %d %s' % (now.N, buf.stdout.encode('utf-8')))
return buf.stdout

with Capsule(this_program, logfile=logfile, python=python) as capsule:
with Capsule(this_program, **kwargs) as capsule:
try:
_START()
while now.N < now.MAX:
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- CELERY_BROKER_URL=amqp://rabbitmq
- VIRTUAL_ENV=/usr/local
volumes:
- .:/opt/online-judge
- .:/opt/online-judge:ro
working_dir: /opt/online-judge
ports:
- "8000:8000"
Expand All @@ -32,7 +32,7 @@ services:
build: .
command: celery worker -A app.celery --loglevel=info
volumes:
- .:/opt/online-judge
- .:/opt/online-judge:ro
working_dir: /opt/online-judge
environment:
- CELERY_BROKER_URL=amqp://rabbitmq
Expand All @@ -45,7 +45,7 @@ services:
build: .
command: flower -A app.celery --port=5555
volumes:
- .:/opt/online-judge
- .:/opt/online-judge:ro
working_dir: /opt/online-judge
environment:
- VIRTUAL_ENV=/usr/local
Expand Down

0 comments on commit 860a957

Please sign in to comment.