def get(request): try: jsonpickle.set_preferred_backend('demjson') jsonpickle.set_encoder_options('json', cls=JSONDateTimeEncoder) r_id = request.matchdict['run_id'] c_id = request.matchdict['contest_id'] run = Run.get_by(run_id=r_id, contest_id=c_id) if (not RequestCheckUserCapability(request, 'moodle/ejudge_submits:comment')): if (int(run.user.id) != int(RequestGetUserId(request))): raise Exception("Auth Error") comments = run.comments res = CommentRes() res.comments = comments res.run_id = request.matchdict['run_id'] res.contest_id = request.matchdict['contest_id'] if (int(run.user.id) == int(RequestGetUserId(request))): with transaction.manager: DBSession.query(Comment).filter( Comment.run_id == r_id, Comment.contest_id == c_id).update({'is_read': True}) transaction.commit() return jsonpickle.encode(res, unpicklable=False, max_depth=5) # return json.dumps(res, skipkeys = True) except Exception as e: return json.dumps({ "result": "error", "message": e.__str__(), "stack": traceback.format_exc() })
def protocol_get_test(request): contest_id = int(request.matchdict['contest_id']) run_id = int(request.matchdict['run_id']) run = Run.get_by(run_id=run_id, contest_id=contest_id) prob = run.problem return prob.get_test(int(request.matchdict['test_num']), prob.get_test_size(int(request.matchdict['test_num'])))
def get_run(request): try: try: contest_id = request.params['contest_id'] run_id = request.params['run_id'] except KeyError: return { "result": "error", "error": "run_id or contest_id is missing" } run = Run.get_by(run_id, contest_id) if run is None: return {"result": "error", "error": "No such run"} moodle_pid = DBSession.query(Problem).filter( Problem.pr_id == run.problem.ejudge_prid).first().id return { "result": "ok", "contest_id": run.contest_id, "problem_id": run.prob_id, "moodle_problem_id": moodle_pid, "signature": get_test_signature(run), "code": get_run_code(run_id, contest_id) } except Exception as e: return { "result": "error", "error": e.__str__(), "stack": traceback.format_exc() }
def get_run(request): try: try: contest_id = request.params['contest_id'] run_id = request.params['run_id'] except KeyError: return {"result": "error", "error": "run_id or contest_id is missing"} run = Run.get_by(run_id, contest_id) if run is None: return {"result": "error", "error": "No such run"} moodle_pid = DBSession.query(Problem).filter(Problem.pr_id == run.problem.ejudge_prid).first().id return {"result": "ok", "contest_id": run.contest_id, "problem_id": run.prob_id, "moodle_problem_id": moodle_pid, "signature": get_test_signature(run), "code": get_run_code(run_id, contest_id)} except Exception as e: return {"result" : "error", "error" : e.__str__(), "stack" : traceback.format_exc()}
def get_submit_archive(request): contest_id = int(request.matchdict['contest_id']) run_id = int(request.matchdict['run_id']) sources = "sources" in request.params all_tests = "all_tests" in request.params tests = request.params.get("tests", "") tests_set = set() for i in tests.split(" "): try: tests_set.add(int(i)) except ValueError: pass run = Run.get_by(run_id=run_id, contest_id=contest_id) run.parsetests prob = run.problem archive = BytesIO() zf = zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) run.tested_protocol for i in range(1, run.tests_count + 1): if all_tests or i in tests_set: zf.writestr("tests/{0:02}".format(i), prob.get_test(i, prob.get_test_size(i))) zf.writestr("tests/{0:02}.a".format(i), prob.get_corr(i, prob.get_corr_size(i))) if sources: zf.writestr("{0}{1}".format(run_id, get_lang_ext_by_id(run.lang_id)), run.get_sources()) checker_src, checker_ext = prob.get_checker() zf.writestr("checker{}".format(checker_ext), checker_src) zf.close() archive.seek(0) response = Response( content_type="application/zip", content_disposition='attachment; filename="archive_{0}_{1}.zip"'. format(contest_id, run_id), body=archive.read()) return response
def get_hint(request): try: try: contest_id = request.params['contest_id'] run_id = request.params['run_id'] except KeyError: return { "result": "error", "error": "run_id or contest_id is missing" } run = Run.get_by(run_id, contest_id) if run is None: return {"result": "error", "error": "No such run"} author_id = run.user.firstname + ' ' + run.user.lastname problem_id = run.problem.ejudge_prid problem_name = DBSession.query(Problem).filter( Problem.pr_id == problem_id).first().name signature = get_test_signature(run) if signature == 'SE' * (len(signature) // 2): hint = Hint.get_by(0, 0, 0, 'SE') else: hint = Hint.get_by(contest_id, run.prob_id, run.lang_id, signature) if hint is None: return { "result": "ok", "user_name": author_id, "problem_name": problem_name } return { "result": "ok", "hint": hint.comment, "user_name": author_id, "problem_name": problem_name } except Exception as e: return { "result": "error", "error": e.__str__(), "stack": traceback.format_exc() }
def add(request): try: if (not RequestCheckUserCapability(request, 'moodle/ejudge_submits:comment')): raise Exception("Auth Error") run = Run.get_by(run_id=request.params['run_id'], contest_id=request.params['contest_id']) if not run: raise Exception("Object not found") user = DBSession.query(User).filter( User.id == RequestGetUserId(request)).first() comment = Comment(run, user, html.escape(request.params['lines']), html.escape(request.params['comment'])) with transaction.manager: DBSession.add(comment) return {"result": "ok"} except Exception as e: return { "result": "error", "message": e.__str__(), "stack": traceback.format_exc() }
def get_protocol(request): try: contest_id = int(request.matchdict['contest_id']) run_id = int(request.matchdict['run_id']) run = Run.get_by(run_id=run_id, contest_id=contest_id) try: run.tested_protocol if (run.user.statement.filter(Statement.olympiad == 1).filter( Statement.timestop > time.time()).filter( Statement.timestart < time.time()).count() == 0): res = OrderedDict() for num in range(1, len(run.tests.keys()) + 1): res[str(num)] = run.tests[str(num)] return {"tests": res, "host": run.host} else: try: return {"tests": run.tests["1"], "host": run.host} except KeyError as e: return { "result": "error", "message": e.__str__(), "stack": traceback.format_exc() } except Exception as e: return { "result": "error", "message": run.compilation_protocol, "error": e.__str__(), "stack": traceback.format_exc(), "protocol": run.protocol } except Exception as e: return { "result": "error", "message": e.__str__(), "stack": traceback.format_exc(), "protocol": run.protocol }
def get_hint(request): try: try: contest_id = request.params['contest_id'] run_id = request.params['run_id'] except KeyError: return {"result": "error", "error": "run_id or contest_id is missing"} run = Run.get_by(run_id, contest_id) if run is None: return {"result": "error", "error": "No such run"} author_id = run.user.firstname + ' ' + run.user.lastname problem_id = run.problem.ejudge_prid problem_name = DBSession.query(Problem).filter(Problem.pr_id == problem_id).first().name signature = get_test_signature(run) if signature == 'SE' * (len(signature) // 2): hint = Hint.get_by(0, 0, 0, 'SE') else: hint = Hint.get_by(contest_id, run.prob_id, run.lang_id, signature) if hint is None: return {"result": "ok", "user_name" : author_id, "problem_name" : problem_name} return {"result": "ok", "hint" : hint.comment, "user_name" : author_id, "problem_name" : problem_name} except Exception as e: return {"result" : "error", "error" : e.__str__(), "stack" : traceback.format_exc()}
def protocol_get_outp(request): contest_id = int(request.matchdict['contest_id']) run_id = int(request.matchdict['run_id']) run = Run.get_by(run_id=run_id, contest_id=contest_id) return run.get_output_file(int(request.matchdict['test_num']), tp='o')
def protocol_get_full(request): contest_id = int(request.matchdict['contest_id']) run_id = int(request.matchdict['run_id']) run = Run.get_by(run_id=run_id, contest_id=contest_id) prob = run.problem out_path = "/home/judges/{0:06d}/var/archive/output/{1}/{2}/{3}/{4:06d}.zip".format( contest_id, to32(run_id // (32**3) % 32), to32(run_id // (32**2) % 32), to32(run_id // 32 % 32), run_id) prot = get_protocol(request) if "result" in prot and prot["result"] == "error": return prot prot = prot["tests"] out_arch = None for test_num in prot: judge_info = run.judge_tests_info[test_num] if prob.get_test_size(int(test_num)) <= 255: prot[test_num]["input"] = prob.get_test(int(test_num)) prot[test_num]["big_input"] = False else: prot[test_num]["input"] = prob.get_test(int(test_num)) + "...\n" prot[test_num]["big_input"] = True if prob.get_corr_size(int(test_num)) <= 255: prot[test_num]["corr"] = prob.get_corr(int(test_num)) prot[test_num]["big_corr"] = False else: prot[test_num]["corr"] = prob.get_corr(int(test_num)) + "...\n" prot[test_num]["big_corr"] = True try: if run.get_output_file_size(int(test_num), tp='o') <= 255: prot[test_num]["output"] = run.get_output_file(int(test_num), tp='o') prot[test_num]["big_output"] = False else: prot[test_num]["output"] = run.get_output_file( int(test_num), tp='o', size=255) + "...\n" prot[test_num]["big_output"] = True except OSError as e: prot[test_num]["output"] = judge_info.get("output", "") prot[test_num]["big_output"] = False try: if run.get_output_file_size(int(test_num), tp='c') <= 255: prot[test_num]["checker_output"] = run.get_output_file( int(test_num), tp='c') else: prot[test_num]["checker_output"] = run.get_output_file( int(test_num), tp='c', size=255) + "...\n" except OSError as e: prot[test_num]["checker_output"] = judge_info.get("checker", "") try: if run.get_output_file_size(int(test_num), tp='e') <= 255: prot[test_num]["error_output"] = run.get_output_file( int(test_num), tp='e') else: prot[test_num]["error_output"] = run.get_output_file( int(test_num), tp='e', size=255) + "...\n" except OSError as e: prot[test_num]["error_output"] = judge_info.get("stderr", "") if "term-signal" in judge_info: prot[test_num]["extra"] = "Signal {0}. ".format( judge_info["term-signal"]) + signal_description[ judge_info["term-signal"]] if "exit-code" in judge_info: if "extra" not in prot[test_num]: prot[test_num]["extra"] = str() prot[test_num]["extra"] = prot[test_num][ "extra"] + "\n Exit code {0}. ".format(judge_info["exit-code"]) for type_ in [("o", "output"), ("c", "checker_output"), ("e", "error_output")]: file_name = "{0:06d}.{1}".format(int(test_num), type_[0]) if out_arch is None: try: out_arch = zipfile.ZipFile(out_path, "r") names = set(out_arch.namelist()) except: names = {} if file_name not in names or type_[1] in prot[test_num]: continue with out_arch.open(file_name, 'r') as f: prot[test_num][ type_[1]] = f.read(1024).decode("utf-8") + "...\n" if out_arch: out_arch.close() return {"tests": prot, "audit": run.get_audit()}