<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import os . path <EOL> import resource <EOL> import shutil <EOL> import sys <EOL> from . proxy import run_subprocess_through_proxy <EOL> from . subproc import run_subprocess <EOL> from . util import temp_directory <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> COMMANDS = { } <EOL> def configure ( command , bin_path , user = None ) : <EOL> """<STR_LIT>""" <EOL> cmd_argv = [ bin_path ] <EOL> if command == "<STR_LIT>" : <EOL> cmd_argv . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> COMMANDS [ command ] = { <EOL> '<STR_LIT>' : cmd_argv , <EOL> '<STR_LIT:user>' : user , <EOL> } <EOL> def is_configured ( command ) : <EOL> """<STR_LIT>""" <EOL> return command in COMMANDS <EOL> if hasattr ( sys , '<STR_LIT>' ) : <EOL> if os . path . isdir ( sys . prefix + "<STR_LIT>" ) : <EOL> configure ( "<STR_LIT>" , sys . prefix + "<STR_LIT>" , "<STR_LIT>" ) <EOL> LIMITS = { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : None , <EOL> } <EOL> def set_limit ( limit_name , value ) : <EOL> """<STR_LIT>""" <EOL> LIMITS [ limit_name ] = value <EOL> class JailResult ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . stdout = self . stderr = self . status = None <EOL> def jail_code ( command , code = None , files = None , extra_files = None , argv = None , <EOL> stdin = None , slug = None ) : <EOL> """<STR_LIT>""" <EOL> if not is_configured ( command ) : <EOL> raise Exception ( "<STR_LIT>" % command ) <EOL> with temp_directory ( ) as homedir : <EOL> os . chmod ( homedir , <NUM_LIT:0> <NUM_LIT> ) <EOL> tmptmp = os . path . join ( homedir , "<STR_LIT>" ) <EOL> os . mkdir ( tmptmp ) <EOL> os . chmod ( tmptmp , <NUM_LIT:0> <NUM_LIT> ) <EOL> argv = argv or [ ] <EOL> for filename in files or ( ) : <EOL> dest = os . path . join ( homedir , os . path . basename ( filename ) ) <EOL> if os . path . islink ( filename ) : <EOL> os . symlink ( os . readlink ( filename ) , dest ) <EOL> elif os . path . isfile ( filename ) : <EOL> shutil . copy ( filename , homedir ) <EOL> else : <EOL> shutil . copytree ( filename , dest , symlinks = True ) <EOL> if code : <EOL> with open ( os . path . join ( homedir , "<STR_LIT>" ) , "<STR_LIT:wb>" ) as jailed : <EOL> jailed . write ( code ) <EOL> argv = [ "<STR_LIT>" ] + argv <EOL> for name , content in extra_files or ( ) : <EOL> with open ( os . path . join ( homedir , name ) , "<STR_LIT:wb>" ) as extra : <EOL> extra . write ( content ) <EOL> cmd = [ ] <EOL> rm_cmd = [ ] <EOL> user = COMMANDS [ command ] [ '<STR_LIT:user>' ] <EOL> if user : <EOL> cmd . extend ( [ '<STR_LIT>' , '<STR_LIT>' , user ] ) <EOL> rm_cmd . extend ( [ '<STR_LIT>' , '<STR_LIT>' , user ] ) <EOL> cmd . extend ( [ '<STR_LIT>' ] ) <EOL> cmd . extend ( COMMANDS [ command ] [ '<STR_LIT>' ] ) <EOL> cmd . extend ( argv ) <EOL> use_proxy = LIMITS [ "<STR_LIT>" ] <EOL> if use_proxy is None : <EOL> use_proxy = int ( os . environ . get ( "<STR_LIT>" , "<STR_LIT:0>" ) ) <EOL> if use_proxy : <EOL> run_subprocess_fn = run_subprocess_through_proxy <EOL> else : <EOL> run_subprocess_fn = run_subprocess <EOL> status , stdout , stderr = run_subprocess_fn ( <EOL> cmd = cmd , cwd = homedir , env = { } , slug = slug , <EOL> stdin = stdin , <EOL> realtime = LIMITS [ "<STR_LIT>" ] , rlimits = create_rlimits ( ) , <EOL> ) <EOL> result = JailResult ( ) <EOL> result . status = status <EOL> result . stdout = stdout <EOL> result . stderr = stderr <EOL> rm_cmd . extend ( [ <EOL> '<STR_LIT>' , tmptmp , <EOL> '<STR_LIT>' , '<STR_LIT:1>' , '<STR_LIT>' , '<STR_LIT:1>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:{}>' , '<STR_LIT:;>' <EOL> ] ) <EOL> run_subprocess_fn ( rm_cmd , cwd = homedir ) <EOL> return result <EOL> def create_rlimits ( ) : <EOL> """<STR_LIT>""" <EOL> rlimits = [ ] <EOL> rlimits . append ( ( resource . RLIMIT_NPROC , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) ) <EOL> cpu = LIMITS [ "<STR_LIT>" ] <EOL> if cpu : <EOL> rlimits . append ( ( resource . RLIMIT_CPU , ( cpu , cpu + <NUM_LIT:1> ) ) ) <EOL> vmem = LIMITS [ "<STR_LIT>" ] <EOL> if vmem : <EOL> rlimits . append ( ( resource . RLIMIT_AS , ( vmem , vmem ) ) ) <EOL> fsize = LIMITS [ "<STR_LIT>" ] <EOL> rlimits . append ( ( resource . RLIMIT_FSIZE , ( fsize , fsize ) ) ) <EOL> return rlimits </s>
