This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from wangxinhe2006/dev
v0.1
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: python | ||
services: | ||
- docker | ||
python: | ||
- "3.5" | ||
- "3.5-dev" | ||
- "3.6" | ||
- "3.6-dev" | ||
- "3.7-dev" | ||
install: | ||
- pip install -r requirements.txt | ||
- pip install pylint | ||
script: | ||
- pylint dockerjudge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# docker-judge | ||
[![Build Status](https://www.travis-ci.org/wangxinhe2006/docker-judge.svg)](https://www.travis-ci.org/wangxinhe2006/docker-judge) | ||
|
||
A Docker Based Online Judge Engine | ||
|
||
# [License](LICENSE) | ||
Licensed under [the GNU Affero General Public License v3.0](https://www.gnu.org/licenses/agpl-3.0.html) | ||
|
||
[![agplv3-155x51.png](https://www.gnu.org/graphics/agplv3-155x51.png)](https://www.gnu.org/graphics/agplv3-155x51.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'dockerjudge - A Docker Based Online Judge Engine' | ||
|
||
import shlex | ||
import sys | ||
import threading | ||
|
||
import docker | ||
import ruamel.yaml | ||
|
||
|
||
class Thread(threading.Thread): | ||
'Subclass of threading.Thread with return value' | ||
return_value = None | ||
def run(self): | ||
try: | ||
if self._target: | ||
self.return_value = self._target(*self._args, **self._kwargs) | ||
finally: | ||
del self._target, self._args, self._kwargs | ||
|
||
def _judge(container, command, stdin='', stdout='', timeout=1): | ||
'Run each test case' | ||
result = container.exec_run(['sh', '-c', 'echo ' + shlex.quote(stdin) + ' | timeout -k ' | ||
+ (str(timeout) + ' ') * 2 + command], demux=True) | ||
if result.exit_code == 124: | ||
return 'TLE' | ||
if result.exit_code: | ||
return 'RE' | ||
if result.output[0].decode().rstrip() != stdout.rstrip(): | ||
return 'WA' | ||
return 'AC' | ||
|
||
def judge(settings, source='', tests=(), timeout=1, client=docker.from_env()): | ||
'Main judge function' | ||
container = client.containers.run(settings['image'], detach=True, | ||
network_disabled=True, tty=True) | ||
try: | ||
container.exec_run(['sh', '-c', 'echo ' + shlex.quote(source) + ' > ' + settings['source']]) | ||
compiler = container.exec_run(settings['compile'], demux=True) | ||
if not compiler.exit_code: | ||
threads = [] | ||
for stdin, stdout in tests: | ||
threads.append(Thread(target=_judge, | ||
args=(container, settings['judge'], stdin, stdout, timeout))) | ||
threads[-1].start() | ||
result = [] | ||
for thread in threads: | ||
thread.join() | ||
result.append(thread.return_value) | ||
return [result, (compiler.output[1] or b'').decode()] | ||
return [['CE'] * len(tests), compiler.output[1].decode()] | ||
finally: | ||
container.remove(force=True) | ||
|
||
if __name__ == '__main__': | ||
print(judge(ruamel.yaml.YAML().load(open('settings.yaml'))[sys.argv[1]][sys.argv[2]], | ||
sys.stdin.read(), zip(sys.argv[3::2], sys.argv[4::2]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
docker[tls] | ||
ruamel.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
c: | ||
gcc-4.8: | ||
image: gcc:4.8 | ||
source: a.c | ||
compile: gcc a.c | ||
judge: ./a.out | ||
gcc-4.9: | ||
image: gcc:4.9 | ||
source: a.c | ||
compile: gcc a.c | ||
judge: ./a.out | ||
gcc-5: | ||
image: gcc:5 | ||
source: a.c | ||
compile: gcc a.c | ||
judge: ./a.out | ||
gcc: | ||
image: gcc | ||
source: a.c | ||
compile: gcc a.c | ||
judge: ./a.out | ||
cpp: | ||
g++-4.8: | ||
image: gcc:4.8 | ||
source: a.cpp | ||
compile: g++ a.cpp | ||
judge: ./a.out | ||
g++-4.9: | ||
image: gcc:4.9 | ||
source: a.cpp | ||
compile: g++ a.cpp | ||
judge: ./a.out | ||
g++-5: | ||
image: gcc:5 | ||
source: a.cpp | ||
compile: g++ a.cpp | ||
judge: ./a.out | ||
g++: | ||
image: gcc | ||
source: a.cpp | ||
compile: g++ a.cpp | ||
judge: ./a.out | ||
go: | ||
go1.11: | ||
image: golang:1.11 | ||
source: main.go | ||
compile: go build main.go | ||
judge: ./main | ||
go1.12: | ||
image: golang:1.12 | ||
source: main.go | ||
compile: go build main.go | ||
judge: ./main | ||
go1: | ||
image: golang:1 | ||
source: main.go | ||
compile: go build main.go | ||
judge: ./main | ||
go: | ||
image: golang | ||
source: main.go | ||
compile: go build main.go | ||
judge: ./main | ||
python: | ||
python2.7: | ||
image: python:2.7 | ||
source: __init__.py | ||
compile: python -m compileall -q __init__.py | ||
judge: python __init__.py | ||
python2: | ||
image: python:2 | ||
source: __init__.py | ||
compile: python -m compileall -q __init__.py | ||
judge: python __init__.py | ||
python3.5: | ||
image: python:3.5 | ||
source: __init__.py | ||
compile: python -m compileall -q __init__.py | ||
judge: python __init__.py | ||
python3.6: | ||
image: python:3.6 | ||
source: __init__.py | ||
compile: python -m compileall -q __init__.py | ||
judge: python __init__.py | ||
python3.7: | ||
image: python3.7 | ||
source: __init__.py | ||
compile: python -m compileall -q __init__.py | ||
judge: python __init__.py | ||
python3: | ||
image: python:3 | ||
source: __init__.py | ||
compile: python -m compileall -q __init__.py | ||
judge: python __init__.py |