<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from celery import shared_task <EOL> from django . template . loader import get_template <EOL> from django . template import Context <EOL> from sandboxes . models import Sandbox <EOL> import json <EOL> import os <EOL> import time <EOL> def create_script ( commands ) : <EOL> template = get_template ( '<STR_LIT>' ) <EOL> c = Context ( { '<STR_LIT>' : commands [ : - <NUM_LIT:1> ] , '<STR_LIT>' : commands [ - <NUM_LIT:1> ] } ) <EOL> return template . render ( c ) <EOL> @ shared_task <EOL> def run_code ( sandbox , cmd , files ) : <EOL> """<STR_LIT>""" <EOL> directory = '<STR_LIT>' % sandbox . id <EOL> data_dir = directory + '<STR_LIT>' <EOL> os . makedirs ( data_dir ) <EOL> for key , val in files . iteritems ( ) : <EOL> with open ( data_dir + key , '<STR_LIT>' ) as destination : <EOL> for chunk in val . chunks ( ) : <EOL> destination . write ( chunk ) <EOL> with open ( os . path . join ( directory , '<STR_LIT:start>' ) , '<STR_LIT:w>' ) as script : <EOL> script . write ( create_script ( cmd ) ) <EOL> img = sandbox . image . docker_image_name <EOL> volumes = { <EOL> '<STR_LIT>' : { } <EOL> } <EOL> docker_cmd = '<STR_LIT>' <EOL> try : <EOL> client = sandbox . docker_server . api <EOL> container_id = client . create_container ( image = img , <EOL> command = docker_cmd , <EOL> volumes = volumes ) <EOL> except Exception as e : <EOL> raise e <EOL> sandbox . container_id = container_id [ '<STR_LIT>' ] <EOL> sandbox . save ( ) <EOL> binds = { <EOL> directory : '<STR_LIT>' <EOL> } <EOL> client . start ( container_id , binds ) <EOL> return container_id [ '<STR_LIT>' ] <EOL> @ shared_task <EOL> def kill_sandb ( c_id , timeout ) : <EOL> try : <EOL> sandbox = Sandbox . objects . get ( container_id = c_id ) <EOL> client = sandbox . docker_server . api <EOL> time . sleep ( int ( timeout ) ) <EOL> client . kill ( sandbox . container_id ) <EOL> except Exception as e : <EOL> raise e <EOL> return True </s>
