<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import io
import json
import shutil
import subprocess
import os

from config import conf
from models import save_result


class JudgeTask(object):

    def __init__(self, message):
        task = json.loads(message)
        self.submit_type = task["submit_type"]
        self.status_id = str(task["status_id"])
        self.code = task["code"]
        self.language = task["language"]
        self.testdata_id = str(task["testdata_id"])
        self.time_limit = str(task["time_limit"])
        self.memory_limit = str(task["memory_limit"])

    def go(self):
        self._clean_files()

        self._prepare_temp_dir()

        self._dump_code_to_file()

        self._prepare_testdata_file()

        self._run()

        self._read_result()

        self._save_result()

        self._clean_files()

    def _prepare_temp_dir(self):
        logging.info("Prepare temp dir")
        os.mkdir(conf.tmp_path)

    def _dump_code_to_file(self):
        logging.info("Dump code to file")
        filename = "Main." + self.language
        self.code_file = os.path.join(conf.tmp_path, filename)
        code_file = io.open(self.code_file, 'w', encoding='utf8')
        code_file.write(self.code)
        code_file.close()

    def _prepare_testdata_file(self):
        logging.info("Prepare testdata")
        input_file = os.path.join(conf.testdata_path, self.testdata_id, "in.in")
        output_file = os.path.join(conf.testdata_path, self.testdata_id, "out.out")
        shutil.copy(input_file, conf.tmp_path)
        shutil.copy(output_file, conf.tmp_path)

    def _run(self):
        logging.info("GO!GO!GO!")
        commands = ["sudo", "./Core", "-c", self.code_file, "-t",
                    self.time_limit, "-m", self.memory_limit, "-d",<commit_msg>add NoTestDataError<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import io
import json
import shutil
import subprocess
import os

from config import conf
from models import save_result


class NoTestDataException(Exception):

    def __init__(self, value=None):
        self.value = value

    def __str__(self):
        return repr(self.value)


class JudgeTask(object):

    def __init__(self, message):
        task = json.loads(message)
        self.submit_type = task["submit_type"]
        self.status_id = str(task["status_id"])
        self.code = task["code"]
        self.language = task["language"]
        self.testdata_id = str(task["testdata_id"])
        self.time_limit = str(task["time_limit"])
        self.memory_limit = str(task["memory_limit"])

        self.result = ""
        self.run_time = 0
        self.run_memory = 0
        self.others = ""

    def go(self):
        self._clean_files()

        try:
            self._prepare_temp_dir()

            self._dump_code_to_file()

            self._prepare_testdata_file()
        except NoTestDataException, e:
            self.result = 'NoTestDataError'
        except Exception, e:
            raise e
        else:
            self._run()

            self._read_result()

        self._save_result()

        self._clean_files()

    def _prepare_temp_dir(self):
        logging.info("Prepare temp dir")
        os.mkdir(conf.tmp_path)

    def _dump_code_to_file(self):
        logging.info("Dump code to file")
        filename = "Main." + self.language
        self.code_file = os.path.join(conf.tmp_path, filename)
        code_file = io.open(self.code_file, 'w', encoding='utf8')
        code_file.write(self.code)
        code_file.close()

    def _prepare_testdata_file(self):
        logging.info("Prepare testdata")
        input_file = os.path.join(
            conf.testdata_path, self.testdata_id, "in.in")
        output_file = os.path.join(
            conf.testdata_path, self.testdata_id, "out.out")
        testdata_exists = (
            os.path.exists(input_file), os.path.exists(output_file))
        if not testdata_exists[0] or not testdata_exists[1]:
            raise NoTestDataException(testdata_exists)
        shutil.copy(input_file, conf.tmp_path)
        shutil.copy(output_file, conf.tmp_path)

    def _run(self):
        logging.info("GO!GO!GO!")
        commands = ["sudo", "./Core", "-c", self.code_file, "-t",
                    self.time_limit, "-m", self.memory_limit, "-d",
{'fraction_non_alphanumeric': 0.12442713107241063, 'fraction_numerical': 0.0018331805682859762, 'mean_word_length': 2.4615384615384617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '760451', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1364, 'n_words': 258}