Esempio n. 1
0
    def _judge_single(self, test_case):
        # test case input and output path
        in_file_path = os.path.join(self._test_cases_dir, test_case['in'])
        out_file_path = os.path.join(self._test_cases_dir, test_case['out'])
        # run test case output path & run test case error path
        run_out_file_path = os.path.join(self._runner_path, test_case['out'])
        run_out_err_path = os.path.join(
            self._runner_path, test_case['in'] + '.err')

        command = self._run_config["command"].format(exe_path=self._exe_path, exe_dir=self._runner_path,
                                                     max_memory=int(self._memory_limit * 1024)).split(" ")
        runner_env = ["PATH=" + os.environ.get("PATH", "")] + self._run_config.get("env", [])
        seccomp_rule = self._run_config.get("seccomp_rule")

        run_result = runner.run(max_cpu_time=self._time_limit * 3,
                                max_real_time=self._time_limit * 3,
                                max_memory=self._memory_limit * 1024 * 1024,
                                max_stack=128 * 1024 * 1024,
                                max_output_size=1024 * 1024 * 16,
                                max_process_number=UNLIMITED,
                                exe_path=command[0],
                                args=command[1::],
                                env=runner_env,
                                input_path=in_file_path,
                                output_path=run_out_file_path,
                                error_path=run_out_err_path,
                                log_path=self._run_log,
                                seccomp_rule_name=seccomp_rule,
                                uid=RUN_USER_UID,
                                gid=RUN_GROUP_GID,
                                memory_limit_check_only=self._run_config.get("memory_limit_check_only", 0))
        run_result['memory'] = run_result['memory'] // 1024 // 1024
        logger.debug(run_result)
        if run_result["result"] != Result.RESULT_SUCCESS:
            return run_result
        if self._spj:
            spj_run_result = self._judge_single_spj(
                in_file_path, out_file_path, run_out_file_path, test_case)
            if spj_run_result['exit_code'] == 0:
                run_result['result'] = spj_run_result['result']
            elif spj_run_result['exit_code'] == 1:
                run_result['result'] = Result.RESULT_WRONG_ANSWER
            else:
                run_result['result'] = Result.RESULT_SYSTEM_ERROR
        else:
            if not os.path.exists(run_out_file_path):
                run_result["result"] = Result.RESULT_WRONG_ANSWER
            else:
                run_result["result"] = Runner.diff(
                    standard_path=out_file_path, output_path=run_out_file_path)
        return run_result
def make_engine(connection_str=None, host_name=None, db_name=None):

    logger.debug(settings['db_connection']['db'])
    DEFAULT_DB_NAME = settings['db_connection']['db']
    DEFAULT_DB_SERVER = settings['db_connection']['host']
    connection=None

    if db_name is None:
        database_name = DEFAULT_DB_NAME
    else:
        database_name = db_name

    if connection_str is None:
        conn_str = "DRIVER={SQL Server}; SERVER=" + DEFAULT_DB_SERVER + "; DATABASE=" + str(database_name)
    else:
        conn_str = connection_str + str(database_name)

    params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=VIDEADDQASPRC03;DATABASE=VideaSuperSnapshot")
    engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
    return engine
Esempio n. 3
0
 def compile(self):
     if self._spj:
         self._compile_spj()
     compile_config = self._language_config['compile']
     command = compile_config["compile_command"]
     command = command.format(
         src_path=self._src_path, exe_dir=self._runner_path, exe_path=self._exe_path)
     _command = command.split(" ")
     os.chdir(self._runner_path)
     runner_env = ["PATH=" + os.environ.get("PATH", "")] + self._compile_config.get("env", [])
     if os.getenv("JAVA_OPTS"):
         runner_env.append(f'JAVA_OPTS={os.getenv("JAVA_OPTS")}')
     result = runner.run(max_cpu_time=compile_config["max_cpu_time"],
                         max_real_time=compile_config["max_real_time"],
                         max_memory=compile_config["max_memory"],
                         max_stack=128 * 1024 * 1024,
                         max_output_size=20 * 1024 * 1024,
                         max_process_number=UNLIMITED,
                         exe_path=_command[0],
                         input_path=self._src_path,
                         output_path=self._compiler_out,
                         error_path=self._compiler_out,
                         args=_command[1::],
                         env=runner_env,
                         log_path=self._compiler_log,
                         seccomp_rule_name=None,
                         uid=RUN_USER_UID,
                         gid=RUN_GROUP_GID)
     logger.debug('compile result:' + str(result))
     os.chdir(self._original_dir)
     if result["result"] != Result.RESULT_SUCCESS:
         if os.path.exists(self._compiler_out):
             with open(self._compiler_out, encoding="utf-8") as f:
                 error = f.read().strip()
                 logger.debug(error)
                 if error:
                     raise CompileError(error)
         raise CompileError(
             "Compiler runtime error, info: \n%s\n" % json.dumps(result))
     else:
         return result
Esempio n. 4
0
    def _judge_single_spj(self, input_path, output_path, user_output_path, test_case):
        # 将test case拷贝到运行目录下面,并且分配权限
        spj_in_file_path = os.path.join(self._runner_path, test_case['in'])
        spj_out_file_path = os.path.join(self._runner_path, test_case['out'])
        shutil.copyfile(input_path, spj_in_file_path)
        shutil.copyfile(output_path, spj_out_file_path)
        os.chown(spj_in_file_path, RUN_USER_UID, RUN_GROUP_GID)
        os.chown(spj_out_file_path, RUN_USER_UID, RUN_GROUP_GID)

        command = self._spj_config['run']['command'].format(exe_path=self._spj_exe_path,
                                                            in_file_path=spj_in_file_path,
                                                            out_file_path=spj_out_file_path,
                                                            user_out_file_path=user_output_path).split(" ")
        runner_env = ["PATH=" + os.environ.get("PATH", "")]
        if os.getenv("JAVA_OPTS"):
            runner_env.append(f'JAVA_OPTS={os.getenv("JAVA_OPTS")}')
        seccomp_rule = self._spj_config['run']["seccomp_rule"]
        in_file_path = os.path.join(self._test_cases_dir, test_case['in'])
        # run test case output path & run test case error path
        run_out_file_path = os.path.join(self._runner_path, test_case['out'] + '.spj.out')
        run_out_err_path = os.path.join(self._runner_path, test_case['out'] + '.spj.err')
        run_result = runner.run(max_cpu_time=self._time_limit * 3,
                                max_real_time=self._time_limit * 3,
                                max_memory=self._memory_limit * 1024 * 1024,
                                max_stack=128 * 1024 * 1024,
                                max_output_size=1024 * 1024 * 16,
                                max_process_number=UNLIMITED,
                                exe_path=command[0],
                                args=command[1::],
                                env=runner_env,
                                input_path=in_file_path,
                                output_path=run_out_file_path,
                                error_path=run_out_err_path,
                                log_path=self._run_log,
                                seccomp_rule_name=seccomp_rule,
                                uid=RUN_USER_UID,
                                gid=RUN_GROUP_GID,
                                memory_limit_check_only=self._run_config.get("memory_limit_check_only", 0))
        logger.debug(run_result)
        return run_result
Esempio n. 5
0
 def _compile_spj(self):
     compile_config = self._spj_config['compile']
     command = compile_config['compile_command']
     command = command.format(
         src_path=self._spj_src_path, exe_path=self._spj_exe_path)
     _command = command.split(' ')
     os.chdir(self._runner_path)
     runner_env = compile_config.get("env", [])
     runner_env.append("PATH=" + os.getenv("PATH"))
     if os.getenv("JAVA_OPTS"):
         runner_env.append("JAVA_OPTS=" + os.getenv("JAVA_OPTS"))
     spj_compile_result = runner.run(max_cpu_time=compile_config['max_cpu_time'],
                                     max_real_time=compile_config['max_real_time'],
                                     max_memory=compile_config['max_memory'],
                                     max_stack=128 * 1024 * 1024,
                                     max_output_size=20 * 1024 * 1024,
                                     max_process_number=UNLIMITED,
                                     exe_path=_command[0],
                                     input_path=self._spj_src_path,
                                     output_path=self._compiler_spj_out,
                                     error_path=self._compiler_spj_out,
                                     args=_command[1::],
                                     env=runner_env,
                                     log_path=self._compiler_log,
                                     seccomp_rule_name=None,
                                     uid=RUN_USER_UID,
                                     gid=RUN_GROUP_GID)
     logger.debug(spj_compile_result)
     if spj_compile_result["result"] != Result.RESULT_SUCCESS:
         if os.path.exists(self._compiler_out):
             with open(self._compiler_out, encoding="utf-8") as f:
                 error = f.read().strip()
                 if error:
                     raise CompileError('Compile spj error.\n' + error)
         raise CompileError("Compile spj runtime error, info:\n%s" %
                            json.dumps(spj_compile_result))
Esempio n. 6
0
def run_submission_task(submission_id, problem_id, manifest, code, language,
                        time_limit, memory_limit):
    logger.debug(submission_id, problem_id, manifest, code, language,
                 time_limit, memory_limit)
    result_submission_task.apply_async(args=[
        submission_id, Verdict.RUNNING, None, None, {
            'node': default_nodename(None)
        }
    ],
                                       queue='result')
    if language not in ACCEPT_SUBMISSION_LANGUAGES:
        logger.warning('request language not valid')
        result_submission_task.apply_async(args=[
            submission_id, Verdict.SYSTEM_ERROR, None, None, {
                'error': f'language {language} not support',
                'node': default_nodename(None)
            }
        ],
                                           queue='result')
    # initialize runner
    try:
        validate_manifest(manifest)
    except TestCaseError as test_case_error:
        logger.debug(test_case_error)
        if OJ_ENABLE_SYNC:
            # sync test cases
            result_submission_task.apply_async(args=[
                submission_id, Verdict.SYNC_TEST_CASES, None, None, {
                    'node': default_nodename(None)
                }
            ],
                                               queue='result')
            logger.debug("sync test cases")
            try:
                sync_test_cases(manifest['hash'], problem_id)
                validate_manifest(manifest)
            except ManifestError as e:
                traceback.print_exc()
                result_submission_task.apply_async(args=[
                    submission_id, Verdict.SYSTEM_ERROR, None, None, {
                        'error': str(e),
                        'node': default_nodename(None)
                    }
                ],
                                                   queue='result')
                return
        else:
            # sync test cases disabled, return system error
            result_submission_task.apply_async(args=[
                submission_id, Verdict.SYSTEM_ERROR, None, None, {
                    'error': str(test_case_error),
                    'node': default_nodename(None)
                }
            ],
                                               queue='result')
    except ManifestError as e:
        logger.debug(e)
        result_submission_task.apply_async(args=[
            submission_id, Verdict.SYSTEM_ERROR, None, None, {
                'error': str(e),
                'node': default_nodename(None)
            }
        ],
                                           queue='result')
        return
    try:
        runner = JudgeRunner(PROBLEM_TEST_CASES_DIR, manifest, time_limit,
                             memory_limit, code,
                             load_submission_config(language),
                             load_spj_config('c'))
    except Exception as e:
        result_submission_task.apply_async(args=[
            submission_id, Verdict.SYSTEM_ERROR, None, None, {
                'error': str(e),
                'node': default_nodename(None)
            }
        ],
                                           queue='result')
        return
    # compile code
    try:
        res_compile = runner.compile()
        logger.debug('compile result:' + str(res_compile))
    except Exception as e:
        logger.debug('compile error:' + str(e))
        result_submission_task.apply_async(args=[
            submission_id, Verdict.COMPILE_ERROR, None, None, {
                'error': str(e),
                'node': default_nodename(None)
            }
        ],
                                           queue='result')
        return
    # run code
    try:
        result = runner.run()
        verdict = Verdict.ACCEPTED
        # attention: time spend and memory spend indicated maximum case time spend and maximum case memory spend
        time_cost = 0
        memory_cost = 0
        for item in result:
            # calculate max time spend and memory spend
            time_cost = max(time_cost, item['cpu_time'])
            memory_cost = max(memory_cost, item['memory'])
            if item['result'] != 0:
                verdict = Verdict.VERDICT_MAPPING[item['result']]
                break
        result_submission_task.apply_async(args=[
            submission_id, verdict, time_cost, memory_cost, {
                'result': result,
                'node': default_nodename(None)
            }
        ],
                                           queue='result')
    except Exception as e:
        traceback.print_exc()
        result_submission_task.apply_async(args=[
            submission_id, Verdict.SYSTEM_ERROR, None, None, {
                'error': str(e),
                'node': default_nodename(None)
            }
        ],
                                           queue='result')
        return
    try:
        # clean running directory
        runner.clean()
    except OSError:
        pass
Esempio n. 7
0
    async def load_dump(self, ctx, filename=None):
        embed = discord.Embed()
        embed.set_author(name=self.__bot.user.name,
                         url=settings.URL,
                         icon_url=self.__bot.user.avatar_url)

        updated = []
        if filename is None:
            filename = "/members.dump.json"
        try:
            with open(filename, "r") as dump_file:
                data = json.load(dump_file)
        except OSError:
            embed.colour = Colors.unauthorized
            embed.title = "File not found!"
            embed.description = "Be sure that you inserted the right " \
                                "filename and you have copied the file " \
                                "into the container!"
            embed.timestamp = datetime.datetime.utcnow()
            await ctx.send(embed=embed)
            return

        for member in data:
            async with session_lock:
                with Session() as session:
                    player_discord_id = member["player"]["discord_id"]
                    server_discord_id = member["server"]["discord_id"]
                    exp = int(member["exp"])
                    player = self.__bot.get_user(player_discord_id)
                    server = self.__bot.get_guild(server_discord_id)

                    db_player = crud_player.get_by_discord(
                        session, player_discord_id)

                    if db_player is None:
                        if player is None and "name" in member["player"]:
                            name = member["player"]["name"]
                        elif player is None and "name" not in member["player"]:
                            name = "UNKNOWN"
                        else:
                            name = player.name
                        db_player = crud_player.create(
                            session,
                            obj_in=CreatePlayer(
                                **{
                                    "discord_id":
                                    player_discord_id,
                                    "name":
                                    name,
                                    "hidden":
                                    "hidden" in member["player"]
                                    and member["player"]["hidden"] == 1
                                }))
                    else:
                        hidden = "hidden" in member["player"] and \
                                 member["player"]["hidden"] == 1
                        if hidden != db_player.hidden:
                            db_player = crud_player.update(
                                session,
                                db_obj=db_player,
                                obj_in={"hidden": hidden})

                    db_server = crud_server.get_by_discord(
                        session, server_discord_id)
                    if db_server is None:
                        if server is None and "name" in member["server"]:
                            name = member["server"]["name"]
                        elif server is None and "name" not in member["server"]:
                            name = "UNKNOWN"
                        else:
                            name = server.name

                        db_server = crud_server.create(
                            session,
                            obj_in=CreateServer(
                                **{
                                    "discord_id": server_discord_id,
                                    "name": name,
                                    "server_exp": 0,
                                    "channel": member["server"].get("channel")
                                }))
                    else:
                        if db_server.channel != member["server"]. \
                                get("channel"):
                            db_server = crud_server.update(
                                session,
                                db_obj=db_server,
                                obj_in={
                                    "channel": member["server"].get("channel")
                                })

                    db_member = crud_member.get_by_ids(session, db_player.uuid,
                                                       db_server.uuid)

                    if "level_id" in member:
                        logger.debug(member["level_id"])
                    if "level_id" in member and member["level_id"] != "NULL":
                        current_level = int(member["level_id"])
                    else:
                        current_level = 0

                    current_level, exp = process_exp(current_level, exp)
                    if current_level > 0:
                        db_level = get_create(
                            session,
                            crud_level,
                            obj_in=CreateLevel(
                                **{
                                    "value": current_level,
                                    "exp": level_exp(current_level),
                                    "title": None
                                }))
                        level_uuid = db_level.uuid
                    else:
                        level_uuid = None

                    if db_member is None:
                        db_member = crud_member.create(
                            session,
                            obj_in=CreateMember(
                                **{
                                    "exp": exp,
                                    "player_uuid": db_player.uuid,
                                    "server_uuid": db_server.uuid,
                                    "level_uuid": level_uuid
                                }))
                    else:
                        db_member = crud_member.update(session,
                                                       db_obj=db_member,
                                                       obj_in={
                                                           "level_uuid":
                                                           level_uuid,
                                                           "exp": exp
                                                       })

                    updated.append(db_member)
        embed.colour = Colors.other
        embed.title = "Members loaded from dump file."
        embed.description = f"Members updated: {len(updated)}"
        embed.timestamp = datetime.datetime.utcnow()
        await ctx.send(embed=embed)
Esempio n. 8
0
def run(max_cpu_time,
        max_real_time,
        max_memory,
        max_stack,
        max_output_size,
        max_process_number,
        exe_path,
        input_path,
        output_path,
        error_path,
        args,
        env,
        log_path,
        seccomp_rule_name,
        uid,
        gid,
        memory_limit_check_only=0):
    str_list_vars = ["args", "env"]
    int_vars = [
        "max_cpu_time", "max_real_time", "max_memory", "max_stack",
        "max_output_size", "max_process_number", "uid", "gid",
        "memory_limit_check_only"
    ]
    str_vars = [
        "exe_path", "input_path", "output_path", "error_path", "log_path"
    ]

    proc_args = ["ddlc"]

    for var in str_list_vars:
        value = vars()[var]
        if not isinstance(value, list):
            raise ValueError("{} must be a list".format(var))
        for item in value:
            if not isinstance(item, str):
                raise ValueError("{} item must be a string".format(var))
            proc_args.append("--{}={}".format(var, item))

    for var in int_vars:
        value = vars()[var]
        if not isinstance(value, int):
            raise ValueError("{} must be a int".format(var))
        if value != UNLIMITED:
            proc_args.append("--{}={}".format(var, value))

    for var in str_vars:
        value = vars()[var]
        if not isinstance(value, str):
            raise ValueError("{} must be a string".format(var))
        proc_args.append("--{}={}".format(var, value))

    if not isinstance(seccomp_rule_name,
                      str) and seccomp_rule_name is not None:
        raise ValueError("seccomp_rule_name must be a string or None")
    if seccomp_rule_name:
        proc_args.append("--seccomp_rule={}".format(seccomp_rule_name))
    logger.debug(' ||| '.join(proc_args))
    proc = subprocess.Popen(proc_args,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)

    out, err = proc.communicate()
    logger.debug(out)
    if err:
        logger.error(err)
    return json.loads(out.decode("utf-8"))