def setUp(self):
        self.test_file_name = "fp_basic"
        self.test_src_name = self.test_file_name + ".pas"
        self.test_exe_name = self.test_file_name + ".exe"

        self.test_src_file_path = os.path.join(self.src_code_dir,
                                               self.test_src_name)

        self.output_dir = os.path.join(self.build_dir, "cfg_output_dir")
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)
        self.test_exe_path = self.output_dir

        self.language = FPLanguage(self.config_parser)
        self.build_bot = BuildBot()
        self.build_bot.set_language(self.language)
    def setUp(self):
        self.test_file_name = "cpp_basic"
        self.test_src_name = self.test_file_name + ".cpp"
        self.test_exe_name = self.test_file_name + ".exe"

        self.test_src_file_path = os.path.join(self.src_code_dir, self.test_src_name)

        self.output_dir = os.path.join(self.build_dir, "cfg_output_dir")
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)
        self.test_exe_path = os.path.join(self.output_dir, self.test_exe_name)

        self.language = CPPLanguage(self.config_parser)
        self.build_bot = BuildBot()
        self.build_bot.set_language(self.language)
class FPBuildBotTests(unittest.TestCase):
    def setUp(self):
        self.test_file_name = "fp_basic"
        self.test_src_name = self.test_file_name + ".pas"
        self.test_exe_name = self.test_file_name + ".exe"

        self.test_src_file_path = os.path.join(self.src_code_dir,
                                               self.test_src_name)

        self.output_dir = os.path.join(self.build_dir, "cfg_output_dir")
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)
        self.test_exe_path = self.output_dir

        self.language = FPLanguage(self.config_parser)
        self.build_bot = BuildBot()
        self.build_bot.set_language(self.language)

    def test_are_all_fixture_attributes_set(self):
        assert hasattr(self, "build_dir")
        assert hasattr(self, "build_dir_bin")
        assert hasattr(self, "src_code_dir")
        assert hasattr(self, "config_parser")
        assert hasattr(self, "is_config_read_ok")

    def test_can_successfully_run_build_bot(self):
        (ret_code, out, err) = self.build_bot.build(self.test_src_file_path,
                                                    self.test_exe_path)
        self.assertEquals(ret_code, 0)

    def test_does_exist_exe_file_created_by_build_bot(self):
        self.build_bot.build(self.test_src_file_path, self.test_exe_path)
        self.assertTrue(os.path.exists(self.test_exe_path))

    def test_can_successfully_run_created_exe_file(self):
        self.build_bot.build(self.test_src_file_path, self.test_exe_path)
        test_exe = os.path.join(self.output_dir, self.test_exe_name)
        (ret_code, out, err) = shell(test_exe)
        self.assertEquals(split_lines(out).pop(0), "Hello, world.")
        self.assertEquals(ret_code, 0)
class FPBuildBotTests(unittest.TestCase):
    def setUp(self):
        self.test_file_name = "fp_basic"
        self.test_src_name = self.test_file_name + ".pas"
        self.test_exe_name = self.test_file_name + ".exe"

        self.test_src_file_path = os.path.join(self.src_code_dir, self.test_src_name)

        self.output_dir = os.path.join(self.build_dir, "cfg_output_dir")
        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)
        self.test_exe_path = self.output_dir

        self.language = FPLanguage(self.config_parser)
        self.build_bot = BuildBot()
        self.build_bot.set_language(self.language)

    def test_are_all_fixture_attributes_set(self):
        assert hasattr(self, "build_dir")
        assert hasattr(self, "build_dir_bin")
        assert hasattr(self, "src_code_dir")
        assert hasattr(self, "config_parser")
        assert hasattr(self, "is_config_read_ok")

    def test_can_successfully_run_build_bot(self):
        (ret_code, out, err) = self.build_bot.build(self.test_src_file_path, self.test_exe_path)
        self.assertEquals(ret_code, 0)

    def test_does_exist_exe_file_created_by_build_bot(self):
        self.build_bot.build(self.test_src_file_path, self.test_exe_path)
        self.assertTrue(os.path.exists(self.test_exe_path))

    def test_can_successfully_run_created_exe_file(self):
        self.build_bot.build(self.test_src_file_path, self.test_exe_path)
        test_exe = os.path.join(self.output_dir, self.test_exe_name)
        (ret_code, out, err) = shell(test_exe)
        self.assertEquals(split_lines(out).pop(0), "Hello, world.")
        self.assertEquals(ret_code, 0)
 def __init__(self, config_parser):
     IAlgorithmController.__init__(self)
     self.build_bot = BuildBot()
     self.test_bot = TestBot()
     self.config_parser = config_parser
     self.work_dir = self.config_parser.get("general", "work_dir")
class AlgorithmController(IAlgorithmController):
    def __init__(self, config_parser):
        IAlgorithmController.__init__(self)
        self.build_bot = BuildBot()
        self.test_bot = TestBot()
        self.config_parser = config_parser
        self.work_dir = self.config_parser.get("general", "work_dir")

    def remove_algorithm(self, name):
        current_algorithm = Algorithm.objects.filter(name=name).get()
        # TODO: add full delete: remove status and test data
        # current_algorithm.status_id.delete()
        # current_algorithm.test_data_id.delete()
        current_algorithm.delete()

    def get_algorithm(self, name):
        # Algorithm.objects.filter(name=name).first()
        return Algorithm.objects.filter(name=name).get()

    def add_algorithm(self, algorithm):
        try:
            (ret_code, out, err) = self._build_algorithm(algorithm)
        except WindowsError:
            self.remove_algorithm(algorithm.name)
            raise

        if ret_code != STATUS_SUCCESS:
            self.remove_algorithm(algorithm.name)

        return ret_code, out, err

    def run_algorithm(self, name):
        algorithm = self.get_algorithm(name)
        exe_path = self._get_exe_path(algorithm)
        self._create_test_data(algorithm)
        return self.test_bot.run(file=exe_path,
                                 run_string=algorithm.testdata_id.run_options)

    def search_algorithm(self, name):
        return Algorithm.objects.get(name__iregex=r'\y{0}\y'.format(name)).get()

    def get_algorithms_list(self):
        return Algorithm.objects.all()

    def get_tagged_algorithms_list(self, tag):
        tag_db = Tag.objects.filter(tag_name=tag).all()
        # tagged_algorithm_ids = TagList.objects.filter(tag_id=tag_db).all()
        tagged_algorithms = Algorithm.objects.filter(
            algorithm_id__in=TagList.objects.filter(tag_id=tag_db).values_list('algorithm_id'))

        return tagged_algorithms

    def get_algorithm_names_list(self):
        alg_obj_list = self.get_algorithms_list()
        result = []
        for item in alg_obj_list:
            result.append(item.name)
        return result
        
    def get_tagged_algorithm_names_list(self, tag):
        alg_obj_list = self.get_tagged_algorithms_list(tag)
        result = []
        for item in alg_obj_list:
            result.append(item.name)
        return result

    def update_algorithm(self,
                         original_name,
                         name,
                         description,
                         source_code,
                         build_options,
                         testdata_id,
                         price,
                         language,
                         tags):
        old_algorithm = self.get_algorithm(original_name)
        algorithm = self.get_algorithm(original_name)

        algorithm.user_id = old_algorithm.user_id
        algorithm.status_id = old_algorithm.status_id

        algorithm.name = name
        algorithm.description = description
        algorithm.source_code = source_code
        algorithm.build_options = build_options
        algorithm.testdata_id = testdata_id
        algorithm.price = price
        algorithm.language = language

        old_tags_list = TagList.objects.filter(algorithm_id=algorithm).all()
        old_tags_list.delete()

        all_tags = Tag.objects.all()
        for tag in all_tags:
            try:
                TagList.objects.filter(tag_id=tag).all()
            except TagList.DoesNotExist:
                tag.delete()

        for tag in tags:
            db_tag = ""

            try:
                db_tag = Tag.objects.filter(tag_name=tag.strip()).get()
            except Tag.DoesNotExist:
                db_tag = Tag.objects.create(tag_name=tag.strip())
                db_tag.save()

            algorithm_tag = TagList.objects.create(tag_id=db_tag,
                                                   algorithm_id=algorithm)
            algorithm_tag.save()

        (ret_code, out, err) = self._build_algorithm(algorithm)
        if ret_code == STATUS_SUCCESS:
            algorithm.save()
        return ret_code, out, err

    @staticmethod
    def get_languages_list():
        return [CPPLanguage.get_name(), CSLanguage.get_name(), FPLanguage.get_name()]

    @staticmethod
    def create_algorithm(name,
                         description,
                         source_code,
                         build_options,
                         testdata_id,
                         price,
                         user_id,
                         status_id,
                         language,
                         tags):

        algorithm = Algorithm.objects.create(name=name,
                                             description=description,
                                             source_code=source_code,
                                             build_options=build_options,
                                             testdata_id=testdata_id,
                                             price=price, user_id=user_id,
                                             status_id=status_id,
                                             language=language)

        algorithm.save()

        for tag in tags:
            db_tag = ""

            try:
                db_tag = Tag.objects.filter(tag_name=tag.strip()).get()
            except Tag.DoesNotExist:
                db_tag = Tag.objects.create(tag_name=tag.strip())
                db_tag.save()

            algorithm_tag = TagList.objects.create(tag_id=db_tag,
                                                   algorithm_id=algorithm)
            algorithm_tag.save()

        return algorithm

    def _create_test_data(self, algorithm):
        test_data = self._get_algorithm_dir(algorithm) + os.sep + "test_data.txt"
        with open(test_data, "wb") as test_data_file:
            test_data_file.write(algorithm.testdata_id.input_data)
        return test_data

    def _get_algorithm_dir(self, algorithm):
        return self.work_dir + os.sep + str(algorithm.user_id.user_id) + os.sep + str(algorithm.algorithm_id)

    def _get_exe_path(self, algorithm):
        return self._get_algorithm_dir(algorithm) + os.sep + algorithm.name + ".exe"

    def _build_algorithm(self, algorithm):
        if algorithm.language == CPPLanguage.get_name():
            language = CPPLanguage(self.config_parser)
        elif algorithm.language == CSLanguage.get_name():
            language = CSLanguage(self.config_parser)
        elif algorithm.language == FPLanguage.get_name():
            language = FPLanguage(self.config_parser)
        else:
            return [STATUS_ERROR_LANGUAGE_NOT_FOUND, "", ""]

        self.build_bot.set_language(language)

        algorithm_dir = self._get_algorithm_dir(algorithm)
        if not os.path.exists(algorithm_dir):
            os.makedirs(algorithm_dir)

        source_file = algorithm_dir + os.sep + algorithm.name + "." + language.get_extension()

        with open(source_file, "wb") as src_file:
            src_file.write(algorithm.source_code)

        exe_path = self._get_exe_path(algorithm)

        return self.build_bot.build(source_file, str(exe_path))
 def __init__(self, config_parser):
     IAlgorithmController.__init__(self)
     self.build_bot = BuildBot()
     self.test_bot = TestBot()
     self.config_parser = config_parser
     self.work_dir = self.config_parser.get("general", "work_dir")
class AlgorithmController(IAlgorithmController):
    def __init__(self, config_parser):
        IAlgorithmController.__init__(self)
        self.build_bot = BuildBot()
        self.test_bot = TestBot()
        self.config_parser = config_parser
        self.work_dir = self.config_parser.get("general", "work_dir")

    def remove_algorithm(self, name):
        current_algorithm = Algorithm.objects.filter(name=name).get()
        # TODO: add full delete: remove status and test data
        # current_algorithm.status_id.delete()
        # current_algorithm.test_data_id.delete()
        current_algorithm.delete()

    def get_algorithm(self, name):
        # Algorithm.objects.filter(name=name).first()
        return Algorithm.objects.filter(name=name).get()

    def add_algorithm(self, algorithm):
        try:
            (ret_code, out, err) = self._build_algorithm(algorithm)
        except WindowsError:
            self.remove_algorithm(algorithm.name)
            raise

        if ret_code != STATUS_SUCCESS:
            self.remove_algorithm(algorithm.name)

        return ret_code, out, err

    def run_algorithm(self, name):
        algorithm = self.get_algorithm(name)
        exe_path = self._get_exe_path(algorithm)
        return self.test_bot.run(file=exe_path,
                                 run_string=algorithm.testdata_id.run_options)

    def search_algorithm(self, name):
        return Algorithm.objects.get(name__iregex=r'\y{0}\y'.format(name)).get()

    def get_algorithms_list(self):
        return Algorithm.objects.all()

    def get_tagged_algorithms_list(self, tag):
        tag_db = Tag.objects.filter(tag_name=tag).all()
        # tagged_algorithm_ids = TagList.objects.filter(tag_id=tag_db).all()
        tagged_algorithms = Algorithm.objects.filter(
            algorithm_id__in=TagList.objects.filter(tag_id=tag_db).values_list('algorithm_id'))

        return tagged_algorithms

    def get_algorithm_names_list(self):
        alg_obj_list = self.get_algorithms_list()
        result = []
        for item in alg_obj_list:
            result.append(item.name)
        return result

    def get_tagged_algorithm_names_list(self, tag):
        alg_obj_list = self.get_tagged_algorithms_list(tag)
        result = []
        for item in alg_obj_list:
            result.append(item.name)
        return result

    def update_algorithm(self,
                         name,
                         description,
                         source_code,
                         build_options,
                         testdata_id,
                         price,
                         language,
                         tags):
        old_algorithm = self.get_algorithm(name)
        algorithm = self.get_algorithm(name)

        algorithm.user_id = old_algorithm.user_id
        algorithm.status_id = old_algorithm.status_id

        algorithm.name = name
        algorithm.description = description
        algorithm.source_code = source_code
        algorithm.build_options = build_options
        algorithm.testdata_id = testdata_id
        algorithm.price = price
        algorithm.language = language

        old_tags_list = TagList.objects.filter(algorithm_id=algorithm).all()
        old_tags_list.delete()

        all_tags = Tag.objects.all()
        for tag in all_tags:
            try:
                TagList.objects.filter(tag_id=tag).all()
            except TagList.DoesNotExist:
                tag.delete()

        for tag in tags:
            db_tag = ""

            try:
                db_tag = Tag.objects.filter(tag_name=tag.strip()).get()
            except Tag.DoesNotExist:
                db_tag = Tag.objects.create(tag_name=tag.strip())
                db_tag.save()

            algorithm_tag = TagList.objects.create(tag_id=db_tag,
                                                   algorithm_id=algorithm)
            algorithm_tag.save()

        (ret_code, out, err) = self._build_algorithm(algorithm)
        if ret_code == STATUS_SUCCESS:
            algorithm.save()
        return ret_code

    @staticmethod
    def get_languages_list():
        return [CPPLanguage.get_name(), CSLanguage.get_name(), FPLanguage.get_name()]

    @staticmethod
    def create_algorithm(name,
                         description,
                         source_code,
                         build_options,
                         testdata_id,
                         price,
                         user_id,
                         status_id,
                         language,
                         tags):

        algorithm = Algorithm.objects.create(name=name,
                                             description=description,
                                             source_code=source_code,
                                             build_options=build_options,
                                             testdata_id=testdata_id,
                                             price=price, user_id=user_id,
                                             status_id=status_id,
                                             language=language)

        algorithm.save()

        for tag in tags:
            db_tag = ""

            try:
                db_tag = Tag.objects.filter(tag_name=tag.strip()).get()
            except Tag.DoesNotExist:
                db_tag = Tag.objects.create(tag_name=tag.strip())
                db_tag.save()

            algorithm_tag = TagList.objects.create(tag_id=db_tag,
                                                   algorithm_id=algorithm)
            algorithm_tag.save()

        return algorithm

    def _get_algorithm_dir(self, algorithm):
        return self.work_dir + os.sep + str(algorithm.user_id.user_id) + os.sep + str(algorithm.algorithm_id)

    def _get_exe_path(self, algorithm):
        return self._get_algorithm_dir(algorithm) + os.sep + algorithm.name + ".exe"

    def _build_algorithm(self, algorithm):
        if algorithm.language == CPPLanguage.get_name():
            language = CPPLanguage(self.config_parser)
        elif algorithm.language == CSLanguage.get_name():
            language = CSLanguage(self.config_parser)
        elif algorithm.language == FPLanguage.get_name():
            language = FPLanguage(self.config_parser)
        else:
            return [STATUS_ERROR_LANGUAGE_NOT_FOUND, "", ""]

        self.build_bot.set_language(language)

        algorithm_dir = self._get_algorithm_dir(algorithm)
        if not os.path.exists(algorithm_dir):
            os.makedirs(algorithm_dir)

        source_file = algorithm_dir + os.sep + algorithm.name + "." + language.get_extension()

        with open(source_file, "wb") as src_file:
            src_file.write(algorithm.source_code)

        exe_path = self._get_exe_path(algorithm)

        return self.build_bot.build(source_file, str(exe_path))
Example #9
0
 def test_can_create_build_bot_for_fp_language(self):
     language = FPLanguage()
     build_bot = BuildBot()
     build_bot.set_language(language)
     self.assertEqual(build_bot.language.compiler_path, self.fp_compiler_path)
Example #10
0
 def test_can_create_build_bot_for_cpp_language_with_config(self):
     language = CPPLanguage(self.config_parser)
     build_bot = BuildBot()
     build_bot.set_language(language)
     self.assertEqual(build_bot.language.compiler_path, self.cpp_compiler_path)
Example #11
0
 def test_can_create_build_bot_for_cpp_language_without_config(self):
     build_bot = BuildBot()
     self.assertEqual(build_bot.language.compiler_path, CPPLanguage.DEFAULT_COMPILER_PATH)
class AlgorithmController(IAlgorithmController):
    def __init__(self, config_parser):
        IAlgorithmController.__init__(self)
        self.build_bot = BuildBot()
        self.test_bot = TestBot()
        self.config_parser = config_parser
        self.work_dir = self.config_parser.get("general", "work_dir")

    def remove_algorithm(self, name):
        current_algorithm = Algorithm.objects.filter(name=name).get()
        # TODO: add full delete: remove status and test data
        # current_algorithm.status_id.delete()
        # current_algorithm.test_data_id.delete()
        current_algorithm.delete()

    def get_algorithm(self, name):
        # Algorithm.objects.filter(name=name).first()
        return Algorithm.objects.filter(name=name).get()

    def add_algorithm(self, algorithm):
        try:
            algorithm.save()
            (ret_code, out, err) = self._build_algorithm(algorithm)
        except WindowsError:
            self.remove_algorithm(algorithm.name)
            raise

        if ret_code != STATUS_SUCCESS:
            self.remove_algorithm(algorithm.name)

        return ret_code, out, err

    def run_algorithm(self, name):
        algorithm = self.get_algorithm(name)
        exe_path = self._get_exe_path(algorithm)
        return self.test_bot.run(file=exe_path,
                                 run_string=algorithm.testdata_id.run_options)

    def search_algorithm(self, name):
        return Algorithm.objects.get(name__iregex=r'\y{0}\y'.format(name)).get()

    def get_algorithms_list(self):
        return Algorithm.objects.all()

    def get_algorithm_names_list(self):
        alg_obj_list = self.get_algorithms_list()
        result = []
        for item in alg_obj_list:
            result.append(item.name)
        return result

    def update_algorithm(self,
                         name,
                         description,
                         source_code,
                         build_options,
                         testdata_id,
                         price,
                         language):
        old_algorithm = self.get_algorithm(name)
        algorithm = self.get_algorithm(name)

        algorithm.user_id = old_algorithm.user_id
        algorithm.status_id = old_algorithm.status_id

        algorithm.name = name
        algorithm.description = description
        algorithm.source_code = source_code
        algorithm.build_options = build_options
        algorithm.testdata_id = testdata_id
        algorithm.price = price
        algorithm.language = old_algorithm.language

        (ret_code, out, err) = self._build_algorithm(algorithm)
        if ret_code == STATUS_SUCCESS:
            algorithm.save()

    @staticmethod
    def create_algorithm(name,
                         description,
                         source_code,
                         build_options,
                         testdata_id,
                         price,
                         user_id,
                         status_id,
                         language):
        return Algorithm.objects.create(name=name,
                                        description=description,
                                        source_code=source_code,
                                        build_options=build_options,
                                        testdata_id=testdata_id,
                                        price=price, user_id=user_id,
                                        status_id=status_id,
                                        language=language)

    def _get_algorithm_dir(self, algorithm):
        return self.work_dir + os.sep + str(algorithm.user_id.user_id) + os.sep + str(algorithm.algorithm_id)

    def _get_exe_path(self, algorithm):
        return self._get_algorithm_dir(algorithm) + os.sep + algorithm.name + ".exe"

    def _build_algorithm(self, algorithm):
        language = None
        if algorithm.language == "cpp":
            language = CPPLanguage(self.config_parser)
        elif algorithm.language == "cs":
            language = CSLanguage(self.config_parser)
        elif algorithm.language == "fp":
            language = FPLanguage(self.config_parser)

        self.build_bot.set_language(language)

        algorithm_dir = self._get_algorithm_dir(algorithm)
        if not os.path.exists(algorithm_dir):
            os.makedirs(algorithm_dir)

        source_file = algorithm_dir + os.sep + algorithm.name + "." + algorithm.language

        with open(source_file, "wb") as src_file:
            src_file.write(algorithm.source_code)

        exe_path = self._get_exe_path(algorithm)

        return self.build_bot.build(source_file, str(exe_path))