def __init__(self, work_dir, allowed_languages=None): self.work_dir = work_dir if allowed_languages is None: self.languages = Language.languages() else: self.languages = [ Language.from_name(name) for name in allowed_languages ]
def run(self): contest = Contest.from_name(self.args.contest) if contest is None: print(f"No contest named {self.args.contest}!", file=sys.stderr) exit(1) try: Language.from_name(self.args.language) except ValueError: print( f"Language {self.args.language} is not currently supported by TuringArena!", file=sys.stderr) print( f"Currently available languages: {', '.join(lang.name for lang in Language.languages())}" ) exit(1) contest.add_language(self.args.language)
def run_server(driver_connection, source_path, interface_path, downward_tee, upward_tee): program = Program(source_path=source_path, interface_path=interface_path) language = Language.from_source_path(program.source_path) interface = load_interface(program.interface_path) with ExitStack() as stack: temp_dir = stack.enter_context(TemporaryDirectory()) runner = language.ProgramRunner( program=program, language=language, interface=interface, temp_dir=temp_dir, ) connection = stack.enter_context(runner.run_in_process()) sandbox_tee = SandboxTee( downward_tee=stack.enter_context(open(downward_tee, "w")), upward_tee=stack.enter_context(open(upward_tee, "w")), ) stack.callback(lambda: connection.manager.get_status( kill_reason="still running after communication end", )) context = Executor( bindings={}, phase=None, process=connection.manager, request_lookahead=None, driver_connection=driver_connection, sandbox_connection=connection, sandbox_tee=sandbox_tee, ) try: try: context.execute(interface) except InterfaceExitReached: pass context.report_ready() request = context.next_request() assert False, f"driver was not explicitly stopped, got {request}" except CommunicationError as e: logging.debug(f"communication error", exc_info=True) context.send_driver_state(DriverState.ERROR) # error info = connection.manager.get_status( kill_reason="communication error") message, = e.args context.send_driver_upward(f"{message} (process {info.error})") except DriverStop: context.send_driver_state(DriverState.READY) # ok, no errors
def run(self): contest = Contest.from_name(self.args.contest) if contest is None: print(f"No contest named {self.args.contest}!", file=sys.stderr) exit(1) languages = [ Language.from_name(lang) for lang in contest.allowed_languages ] print( tabulate(((lang.name, lang.extension) for lang in languages), headers=("Name", "Extension")))
def define_algorithm(interface_text: str, source_text: str, language_name: str = "C++") -> Program: language = Language.from_name(language_name) with ExitStack() as stack: tmp_dir = stack.enter_context(TemporaryDirectory()) source_path = os.path.join(tmp_dir, f"source{language.extension}") interface_path = os.path.join(tmp_dir, "interface.txt") with open(interface_path, "w") as f: print(interface_text, file=f) with open(source_path, "w") as f: print(source_text, file=f) yield Program( source_path=source_path, interface_path=interface_path, )
def evaluate(current_user, problem, contest): submitted_file = request.files["source"] ext = os.path.splitext(submitted_file.filename)[1] allowed_extensions = [ Language.from_name(lang).extension for lang in contest.allowed_languages ] if ext not in allowed_extensions: raise RuntimeError( f"Unsupported file extension {ext}: please select another file!") submission = Submission.new(current_user, problem, contest, submitted_file.filename) os.makedirs(os.path.split(submission.path)[0], exist_ok=True) submitted_file.save(submission.path) threading.Thread(target=evaluate_thread, args=(problem, submission)).start() return redirect( url_for("submission.submission_view", submission_id=submission.id))
from turingarena.driver.language import Language disabled_language = Language( name="javascript", extension=".js", ProgramRunner=None, Generator=None, supported_for_evaluator=False, )
from turingarena.driver.language import Language from .generator import JavaCodeGen from .runner import JavaProgramRunner language = Language( name="Java", extension=".java", ProgramRunner=JavaProgramRunner, Generator=JavaCodeGen, supported_for_evaluator=False, )
from turingarena.driver.language import Language from .generator import RustCodeGen from .runner import RustProgramRunner language = Language( name="Rust", extension=".rs", ProgramRunner=RustProgramRunner, Generator=RustCodeGen, supported_for_evaluator=False, )
from turingarena.driver.language import Language from .generator import GoCodeGen from .runner import GoProgramRunner disabled_language = Language( name="Go", extension=".go", ProgramRunner=GoProgramRunner, Generator=GoCodeGen, supported_for_evaluator=False, )
def language(self): try: return Language.from_extension(self.extension) except UnknownFileExtension: return None
from turingarena.driver.language import Language from .generator import CppCodeGen from .runner import CppProgramRunner language = Language( name="C++", extension=".cpp", ProgramRunner=CppProgramRunner, Generator=CppCodeGen, supported_for_evaluator=True, )
from turingarena.driver.language import Language from .generator import BashCodeGen from .runner import BashProgramRunner disabled_language = Language( name="Bash", extension=".sh", ProgramRunner=BashProgramRunner, Generator=BashCodeGen, supported_for_evaluator=False, )
from turingarena.driver.language import Language from .generator import CCodeGen from .runner import CProgramRunner language = Language( name="C", extension=".c", ProgramRunner=CProgramRunner, Generator=CCodeGen, supported_for_evaluator=False, )
from turingarena.driver.language import Language from turingarena.driver.languages.python.generator import PythonCodeGen from .generator import PythonCodeGen from .runner import PythonProgramRunner language = Language( name="Python", extension=".py", ProgramRunner=PythonProgramRunner, Generator=PythonCodeGen, supported_for_evaluator=True, )
from turingarena.driver.language import Language from .generator import RubyCodeGen from .runner import RubyProgramRunner language = Language( name="Ruby", extension=".rb", ProgramRunner=RubyProgramRunner, Generator=RubyCodeGen, supported_for_evaluator=True, )