This repository was archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcompiler.py
39 lines (38 loc) · 1.54 KB
/
compiler.py
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
import docker
import settings
from os import path, mkdir
from judge_result import Judge_result
def compile( submission ):
'''
Compile the target submission in work_dir
Return status , info
if staus eq 'Compile Error' or 'Judger Error' then info would show reasons
and in any onther cases, the info would be None
'''
client = docker.from_env()
try:
s = client.containers.run(
image = settings.docker_repo_arguments.format(
repo_lang = submission.language.value.image ),
network_disabled = True,
volumes = { submission.work_dir : {'bind': '/opt' , 'mode':'rw' } },
working_dir = '/opt',
mem_limit = settings.COMPILE_MEMORY,
auto_remove = True,
tty = True,
detach = True)
status, info = s.exec_run(
cmd = 'timeout ' + str(settings.COMPILE_TIMEOUT) + ' ' + submission.language.value.compile_command.format(
sourcefile = submission.sourcefile))
status = int( status )
info = info.decode( 'utf-8' )
finally:
if 's' in dir():
s.remove( force = True ) # No matter how this container work, we should remove this container force finally
if status == 0:
return 'Success' , None
elif status is 124:
info = 'Compile time out'
elif len( info ) == 0:
info = 'You wanna hack me? exit code is ' + str( status )
return Judge_result.CE , info[:min( len(info) , settings.max_compile_error_length )]