From 0845f5f67aabd311a4b0bf70d099be07d688bb80 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Mon, 26 Sep 2016 15:58:08 +0530 Subject: removed python_stdout_evaluator as python_stdio_evaluator takes care of 'stdout only' cases --- yaksh/python_stdout_evaluator.py | 65 ---------------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 yaksh/python_stdout_evaluator.py diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py deleted file mode 100644 index 8f69b24..0000000 --- a/yaksh/python_stdout_evaluator.py +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env python -import sys -import traceback -import os -from os.path import join -import importlib -from contextlib import contextmanager - -# local imports -from code_evaluator import CodeEvaluator -from file_utils import copy_files, delete_files - - -@contextmanager -def redirect_stdout(): - from StringIO import StringIO - new_target = StringIO() - - old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout - try: - yield new_target # run some code with the replaced stdout - finally: - sys.stdout = old_target # restore to the previous value - - -class PythonStdoutEvaluator(CodeEvaluator): - """Tests the Python code obtained from Code Server""" - - def teardown(self): - super(PythonStdoutEvaluator, self).teardown() - # Delete the created file. - if self.files: - delete_files(self.files) - - def compile_code(self, user_answer, file_paths, expected_output): - self.files = [] - if file_paths: - self.files = copy_files(file_paths) - if hasattr(self, 'output_value'): - return None - else: - submitted = compile(user_answer, '', mode='exec') - with redirect_stdout() as output_buffer: - exec_scope = {} - exec submitted in exec_scope - self.output_value = output_buffer.getvalue() - return self.output_value - - def check_code(self, user_answer, file_paths, expected_output): - success = False - tb = None - if expected_output in user_answer: - success = False - err = ("Incorrect Answer: Please avoid " - "printing the expected output directly" - ) - elif self.output_value == expected_output: - success = True - err = "Correct answer" - - else: - success = False - err = "Incorrect Answer" - del tb - return success, err -- cgit From ed46c4a2cf6bbf41f50faafc785f001e3d10d195 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Mon, 26 Sep 2016 18:15:04 +0530 Subject: refactored stdio_evaluator --- yaksh/bash_stdio_evaluator.py | 17 ++++++++--------- yaksh/cpp_stdio_evaluator.py | 15 +++++++-------- yaksh/java_stdio_evaluator.py | 14 ++++++-------- yaksh/python_stdio_evaluator.py | 4 ++-- yaksh/stdio_evaluator.py | 20 ++++++++++++++++---- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/yaksh/bash_stdio_evaluator.py b/yaksh/bash_stdio_evaluator.py index 56f2e35..8ff0743 100644 --- a/yaksh/bash_stdio_evaluator.py +++ b/yaksh/bash_stdio_evaluator.py @@ -3,12 +3,12 @@ import subprocess import os from os.path import isfile -#local imports -from code_evaluator import CodeEvaluator -from stdio_evaluator import Evaluator +# local imports +from stdio_evaluator import StdIOEvaluator from file_utils import copy_files, delete_files -class BashStdioEvaluator(CodeEvaluator): + +class BashStdioEvaluator(StdIOEvaluator): """Evaluates Bash StdIO based code""" def setup(self): @@ -41,9 +41,8 @@ class BashStdioEvaluator(CodeEvaluator): stdout=subprocess.PIPE, stderr=subprocess.PIPE ) - evaluator = Evaluator() - success, err = evaluator.evaluate(user_answer, proc, - expected_input, - expected_output - ) + success, err = self.evaluate_stdio(user_answer, proc, + expected_input, + expected_output + ) return success, err diff --git a/yaksh/cpp_stdio_evaluator.py b/yaksh/cpp_stdio_evaluator.py index 4ea1bbf..720ed0f 100644 --- a/yaksh/cpp_stdio_evaluator.py +++ b/yaksh/cpp_stdio_evaluator.py @@ -4,12 +4,12 @@ import os from os.path import isfile #local imports -from code_evaluator import CodeEvaluator -from stdio_evaluator import Evaluator + +from stdio_evaluator import StdIOEvaluator from file_utils import copy_files, delete_files -class CppStdioEvaluator(CodeEvaluator): +class CppStdioEvaluator(StdIOEvaluator): """Evaluates C StdIO based code""" def setup(self): @@ -76,11 +76,10 @@ class CppStdioEvaluator(CodeEvaluator): stdout=subprocess.PIPE, stderr=subprocess.PIPE ) - evaluator = Evaluator() - success, err = evaluator.evaluate(user_answer, proc, - expected_input, - expected_output - ) + success, err = self.evaluate_stdio(user_answer, proc, + expected_input, + expected_output + ) os.remove(self.ref_output_path) else: err = "Error:" diff --git a/yaksh/java_stdio_evaluator.py b/yaksh/java_stdio_evaluator.py index 27dd4a9..f4b8773 100644 --- a/yaksh/java_stdio_evaluator.py +++ b/yaksh/java_stdio_evaluator.py @@ -4,12 +4,11 @@ import os from os.path import isfile #local imports -from code_evaluator import CodeEvaluator -from stdio_evaluator import Evaluator +from stdio_evaluator import StdIOEvaluator from file_utils import copy_files, delete_files -class JavaStdioEvaluator(CodeEvaluator): +class JavaStdioEvaluator(StdIOEvaluator): """Evaluates Java StdIO based code""" def setup(self): @@ -61,11 +60,10 @@ class JavaStdioEvaluator(CodeEvaluator): stdout=subprocess.PIPE, stderr=subprocess.PIPE ) - evaluator = Evaluator() - success, err = evaluator.evaluate(user_answer, proc, - expected_input, - expected_output - ) + success, err = self.evaluate_stdio(user_answer, proc, + expected_input, + expected_output + ) os.remove(self.user_output_path) else: err = "Compilation Error:" diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index 4a02267..2cfd9c8 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -53,11 +53,11 @@ class PythonStdioEvaluator(CodeEvaluator): tb = None if self.output_value == expected_output: success = True - err = "Correct Answer" + err = "Correct answer" else: success = False err = dedent(""" - Incorrect Answer: + Incorrect answer: Given input - {0} Expected output - {1} Your output - {2} diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py index 4f5cfaf..efb2ae5 100644 --- a/yaksh/stdio_evaluator.py +++ b/yaksh/stdio_evaluator.py @@ -1,6 +1,18 @@ -class Evaluator(object): +# Local imports +from code_evaluator import CodeEvaluator - def evaluate(self, user_answer, proc, expected_input, expected_output): + +class StdIOEvaluator(CodeEvaluator): + + def setup(self): + super(StdIOEvaluator, self).setup() + pass + + def teardown(self): + super(StdIOEvaluator, self).teardown() + pass + + def evaluate_stdio(self, user_answer, proc, expected_input, expected_output): success = False ip = expected_input.replace(",", " ") user_output, output_err = proc.communicate(input='{0}\n'.format(ip)) @@ -13,9 +25,9 @@ class Evaluator(object): format(expected_input, repr(expected_output)) if output_err == '': if user_output == expected_output: - success, err = True, "Correct Answer" + success, err = True, "Correct answer" else: - err = " Incorrect Answer\n" + error_msg +\ + err = " Incorrect answer\n" + error_msg +\ "\n Your output is {0}".format(repr(user_output)) else: err = "Error:"+"\n"+output_err -- cgit From 6c0f1a9182f53d4340ef2324ae93c50872a83a10 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Tue, 27 Sep 2016 14:27:18 +0530 Subject: modified test cases wrt the refactor changes --- yaksh/evaluator_tests/test_bash_evaluation.py | 6 +++--- yaksh/evaluator_tests/test_c_cpp_evaluation.py | 17 ++++++++--------- yaksh/evaluator_tests/test_java_evaluation.py | 8 ++++---- yaksh/evaluator_tests/test_python_evaluation.py | 14 +++++++------- 4 files changed, 22 insertions(+), 23 deletions(-) mode change 100644 => 100755 yaksh/evaluator_tests/test_bash_evaluation.py mode change 100644 => 100755 yaksh/evaluator_tests/test_c_cpp_evaluation.py mode change 100644 => 100755 yaksh/evaluator_tests/test_java_evaluation.py mode change 100644 => 100755 yaksh/evaluator_tests/test_python_evaluation.py diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py old mode 100644 new mode 100755 index addc5e6..084e5e4 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -88,7 +88,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -108,7 +108,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -142,7 +142,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py old mode 100644 new mode 100755 index 0042d0f..c6994f6 --- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py +++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py @@ -84,7 +84,6 @@ class CAssertionEvaluationTestCases(unittest.TestCase): self.assertTrue(result.get('success')) self.assertEquals(result.get('error'), "Correct answer") - class CppStdioEvaluationTestCases(unittest.TestCase): def setUp(self): @@ -107,7 +106,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -127,7 +126,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_string_input(self): @@ -145,7 +144,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -208,7 +207,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_correct_answer(self): @@ -225,7 +224,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_array_input(self): @@ -246,7 +245,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_string_input(self): @@ -265,7 +264,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_incorrect_answer(self): @@ -332,7 +331,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py old mode 100644 new mode 100755 index 74ac677..60afb3b --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -127,7 +127,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -149,7 +149,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -219,7 +219,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_string_input(self): @@ -239,7 +239,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct Answer") + self.assertEquals(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py old mode 100644 new mode 100755 index 2a109d5..b72d26b --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -344,7 +344,7 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): result = evaluator.evaluate(**kwargs) # Then - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -360,7 +360,7 @@ class PythonStdoutEvaluationTestCases(unittest.TestCase): # Then self.assertFalse(result.get('success')) - self.assertIn("Incorrect Answer", result.get('error')) + self.assertIn("Incorrect answer", result.get('error')) def test_infinite_loop(self): # Given @@ -404,7 +404,7 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): # Then self.assertTrue(result.get('success')) - self.assertIn("Correct Answer", result.get('error')) + self.assertIn("Correct answer", result.get('error')) def test_correct_answer_list(self): # Given @@ -427,7 +427,7 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): # Then self.assertTrue(result.get('success')) - self.assertIn("Correct Answer", result.get('error')) + self.assertIn("Correct answer", result.get('error')) def test_correct_answer_string(self): # Given @@ -451,7 +451,7 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): # Then self.assertTrue(result.get('success')) - self.assertIn("Correct Answer", result.get('error')) + self.assertIn("Correct answer", result.get('error')) def test_incorrect_answer_integer(self): # Given @@ -474,7 +474,7 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): # Then self.assertFalse(result.get('success')) - self.assertIn("Incorrect Answer", result.get('error')) + self.assertIn("Incorrect answer", result.get('error')) def test_file_based_answer(self): # Given @@ -497,7 +497,7 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): result = evaluator.evaluate(**kwargs) # Then - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': -- cgit From 086620367c481009c9caed68660865ca127a9520 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Tue, 27 Sep 2016 16:03:46 +0530 Subject: documentation for stdio_evaluator --- yaksh/documentation/moderator_docs/creating_question.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/yaksh/documentation/moderator_docs/creating_question.rst b/yaksh/documentation/moderator_docs/creating_question.rst index d80b437..cdb20ef 100644 --- a/yaksh/documentation/moderator_docs/creating_question.rst +++ b/yaksh/documentation/moderator_docs/creating_question.rst @@ -62,11 +62,15 @@ How to write Test cases Finally click on Save & Add Testcase Button to save the test case. - * **Create Standard out Based Test Case** + * **Create Standard Input/Output Based Test Case** - Select Stdout Based TestCase from Test Case Type field and click on Save & Add Testcase button to save the question. + Select StdIO Based TestCase from Test Case Type field and click on Save & Add Testcase button to save the question. - In Expected Output Field type the expected output for a particular question. For e.g type 6 if the output of the user code is 6. + In Expected input field, enter the value(s) that will be passed to the students' code through a standard I/O stream. + + .. note:: If there are multiple input values in a test case, enter the values in new line. + + In Expected Output Field, enter the expected output for that test case. For e.g type 6 if the output of the user code is 6. * **Create MCQ Based Test Case** -- cgit From 155bff9c2a32192405e21ffdb8f533893eb927f4 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Thu, 29 Sep 2016 15:31:50 +0530 Subject: Set OUTPUT_DIR path to a temporary directory. Moved OUTPUT_DIR to settings. Temporary directory path is fetched, and accordingly output path is set. Now, the outpath directory will always have a write permission. --- online_test/settings.py | 3 +++ yaksh/models.py | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/online_test/settings.py b/online_test/settings.py index 81fd6a3..5df0410 100644 --- a/online_test/settings.py +++ b/online_test/settings.py @@ -10,8 +10,11 @@ https://docs.djangoproject.com/en/1.6/ref/settings/ from yaksh.pipeline.settings import AUTH_PIPELINE # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os +import tempfile BASE_DIR = os.path.dirname(os.path.dirname(__file__)) +# The directory where user data can be saved. +OUTPUT_DIR = os.path.join(tempfile.gettempdir(), 'output') # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ diff --git a/yaksh/models.py b/yaksh/models.py index 7c4d5c4..69dff6d 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -21,10 +21,7 @@ import zipfile import tempfile from file_utils import extract_files from yaksh.xmlrpc_clients import code_server - - -# The directory where user data can be saved. -OUTPUT_DIR = abspath(join(dirname(__file__), 'output')) +from django.conf import settings languages = ( @@ -205,9 +202,9 @@ class Profile(models.Model): def get_user_dir(self): """Return the output directory for the user.""" - user_dir = join(OUTPUT_DIR, str(self.user.username)) + user_dir = join(settings.OUTPUT_DIR, str(self.user.username)) if not exists(user_dir): - os.mkdir(user_dir) + os.makedirs(user_dir) # Make it rwx by others. os.chmod(user_dir, stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR -- cgit From ac8d6720bc75676e05462cc38ad144d5aedc14e7 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 21 Sep 2016 15:07:43 +0530 Subject: Migrate python code server and evaluators to python 2/3 compatible --- yaksh/bash_code_evaluator.py | 5 +++-- yaksh/bash_stdio_evaluator.py | 8 +++++--- yaksh/code_evaluator.py | 14 ++++++++++--- yaksh/code_server.py | 14 +++++++++---- yaksh/cpp_code_evaluator.py | 7 ++++--- yaksh/cpp_stdio_evaluator.py | 9 +++++---- yaksh/evaluator_tests/test_bash_evaluation.py | 13 ++++++------ yaksh/evaluator_tests/test_c_cpp_evaluation.py | 27 +++++++++++++------------ yaksh/evaluator_tests/test_code_evaluation.py | 3 ++- yaksh/evaluator_tests/test_java_evaluation.py | 17 ++++++++-------- yaksh/evaluator_tests/test_python_evaluation.py | 1 + yaksh/evaluator_tests/test_scilab_evaluation.py | 5 +++-- yaksh/file_utils.py | 1 + yaksh/java_code_evaluator.py | 7 ++++--- yaksh/java_stdio_evaluator.py | 8 +++++--- yaksh/language_registry.py | 8 ++++++-- yaksh/python_assertion_evaluator.py | 11 +++++----- yaksh/python_stdio_evaluator.py | 19 +++++++++++------ yaksh/scilab_code_evaluator.py | 7 ++++--- yaksh/stdio_evaluator.py | 1 + yaksh/tests/test_code_server.py | 8 +++++--- yaksh/xmlrpc_clients.py | 15 +++++++++++--- 22 files changed, 131 insertions(+), 77 deletions(-) diff --git a/yaksh/bash_code_evaluator.py b/yaksh/bash_code_evaluator.py index bce7f07..0cbce89 100644 --- a/yaksh/bash_code_evaluator.py +++ b/yaksh/bash_code_evaluator.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import absolute_import import traceback import pwd import os @@ -8,8 +9,8 @@ import subprocess import importlib # local imports -from code_evaluator import CodeEvaluator -from file_utils import copy_files, delete_files +from .code_evaluator import CodeEvaluator +from .file_utils import copy_files, delete_files class BashCodeEvaluator(CodeEvaluator): diff --git a/yaksh/bash_stdio_evaluator.py b/yaksh/bash_stdio_evaluator.py index 8ff0743..fbb94ec 100644 --- a/yaksh/bash_stdio_evaluator.py +++ b/yaksh/bash_stdio_evaluator.py @@ -1,11 +1,13 @@ #!/usr/bin/env python +from __future__ import absolute_import import subprocess import os from os.path import isfile -# local imports -from stdio_evaluator import StdIOEvaluator -from file_utils import copy_files, delete_files +#local imports +from .code_evaluator import CodeEvaluator +from .stdio_evaluator import Evaluator +from .file_utils import copy_files, delete_files class BashStdioEvaluator(StdIOEvaluator): diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py index 8a9b7a6..a55aed3 100644 --- a/yaksh/code_evaluator.py +++ b/yaksh/code_evaluator.py @@ -1,5 +1,6 @@ +#!/usr/bin/env python +from __future__ import absolute_import import sys -from SimpleXMLRPCServer import SimpleXMLRPCServer import pwd import os import stat @@ -9,8 +10,15 @@ import traceback from multiprocessing import Process, Queue import subprocess import re -# Local imports. -from settings import SERVER_TIMEOUT + +try: + from SimpleXMLRPCServer import SimpleXMLRPCServer +except ImportError: + # The above import will not work on Python-3.x. + from xmlrpc.server import SimpleXMLRPCServer + +# Local imports +from .settings import SERVER_TIMEOUT MY_DIR = abspath(dirname(__file__)) diff --git a/yaksh/code_server.py b/yaksh/code_server.py index e19e9c8..b753ac7 100755 --- a/yaksh/code_server.py +++ b/yaksh/code_server.py @@ -23,7 +23,7 @@ that returns an available server. """ # Standard library imports -from SimpleXMLRPCServer import SimpleXMLRPCServer +from __future__ import absolute_import import json from multiprocessing import Process, Queue import os @@ -35,6 +35,12 @@ import stat import subprocess import sys +try: + from SimpleXMLRPCServer import SimpleXMLRPCServer +except ImportError: + # The above import will not work on Python-3.x. + from xmlrpc.server import SimpleXMLRPCServer + try: from urllib import unquote except ImportError: @@ -45,9 +51,9 @@ except ImportError: from tornado.ioloop import IOLoop from tornado.web import Application, RequestHandler -# Local imports. -from settings import SERVER_PORTS, SERVER_POOL_PORT -from language_registry import create_evaluator_instance, unpack_json +# Local imports +from .settings import SERVER_PORTS, SERVER_POOL_PORT +from .language_registry import create_evaluator_instance, unpack_json MY_DIR = abspath(dirname(__file__)) diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index c65242d..31b84b2 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import absolute_import import traceback import pwd import os @@ -6,9 +7,9 @@ from os.path import join, isfile import subprocess import importlib -# local imports -from code_evaluator import CodeEvaluator -from file_utils import copy_files, delete_files +# Local imports +from .code_evaluator import CodeEvaluator +from .file_utils import copy_files, delete_files class CppCodeEvaluator(CodeEvaluator): diff --git a/yaksh/cpp_stdio_evaluator.py b/yaksh/cpp_stdio_evaluator.py index 720ed0f..dab3499 100644 --- a/yaksh/cpp_stdio_evaluator.py +++ b/yaksh/cpp_stdio_evaluator.py @@ -1,12 +1,13 @@ #!/usr/bin/env python +from __future__ import absolute_import import subprocess import os from os.path import isfile -#local imports - -from stdio_evaluator import StdIOEvaluator -from file_utils import copy_files, delete_files +#Local imports +from .code_evaluator import CodeEvaluator +from .stdio_evaluator import Evaluator +from .file_utils import copy_files, delete_files class CppStdioEvaluator(StdIOEvaluator): diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py index 084e5e4..01e248f 100755 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import unittest import os from yaksh.bash_code_evaluator import BashCodeEvaluator @@ -28,7 +29,7 @@ class BashAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct answer") def test_error(self): user_answer = ("#!/bin/bash\n[[ $# -eq 2 ]] " @@ -52,7 +53,7 @@ class BashAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) def test_file_based_assert(self): self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] @@ -67,7 +68,7 @@ class BashAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertTrue(result.get("success")) - self.assertEquals(result.get("error"), "Correct answer") + self.assertEqual(result.get("error"), "Correct answer") class BashStdioEvaluationTestCases(unittest.TestCase): def setUp(self): @@ -88,7 +89,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -108,7 +109,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -142,7 +143,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py index c6994f6..d57affa 100755 --- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py +++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import unittest import os from yaksh.cpp_code_evaluator import CppCodeEvaluator @@ -24,7 +25,7 @@ class CAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct answer") def test_incorrect_answer(self): user_answer = "int add(int a, int b)\n{return a-b;}" @@ -58,7 +59,7 @@ class CAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) def test_file_based_assert(self): self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] @@ -82,7 +83,7 @@ class CAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertTrue(result.get('success')) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct answer") class CppStdioEvaluationTestCases(unittest.TestCase): @@ -106,7 +107,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -126,7 +127,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_string_input(self): @@ -144,7 +145,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -191,7 +192,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) def test_only_stdout(self): self.test_case_data = [{'expected_output': '11', @@ -207,7 +208,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_cpp_correct_answer(self): @@ -224,7 +225,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_cpp_array_input(self): @@ -245,7 +246,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_cpp_string_input(self): @@ -264,7 +265,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_cpp_incorrect_answer(self): @@ -314,7 +315,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) def test_cpp_only_stdout(self): self.test_case_data = [{'expected_output': '11', @@ -331,7 +332,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_code_evaluation.py b/yaksh/evaluator_tests/test_code_evaluation.py index cbca32d..4efe119 100644 --- a/yaksh/evaluator_tests/test_code_evaluation.py +++ b/yaksh/evaluator_tests/test_code_evaluation.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import unittest import os from yaksh import python_assertion_evaluator @@ -38,7 +39,7 @@ class RegistryTestCase(unittest.TestCase): "stdiobasedtestcase": stdout_evaluator_path } ) - self.assertEquals(evaluator_class, class_name) + self.assertEqual(evaluator_class, class_name) def tearDown(self): self.registry_object = None diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index 60afb3b..f94c6d1 100755 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import unittest import os from yaksh import code_evaluator as evaluator @@ -30,7 +31,7 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -65,7 +66,7 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) def test_file_based_assert(self): self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] @@ -97,7 +98,7 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertTrue(result.get("success")) - self.assertEquals(result.get("error"), "Correct answer") + self.assertEqual(result.get("error"), "Correct answer") class JavaStdioEvaluationTestCases(unittest.TestCase): @@ -127,7 +128,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -149,7 +150,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -202,7 +203,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) def test_only_stdout(self): self.test_case_data = [{'expected_output': '11', @@ -219,7 +220,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) def test_string_input(self): @@ -239,7 +240,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct Answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index b72d26b..6852136 100755 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import unittest import os from yaksh.python_assertion_evaluator import PythonAssertionEvaluator diff --git a/yaksh/evaluator_tests/test_scilab_evaluation.py b/yaksh/evaluator_tests/test_scilab_evaluation.py index f5e3767..f1b29dd 100644 --- a/yaksh/evaluator_tests/test_scilab_evaluation.py +++ b/yaksh/evaluator_tests/test_scilab_evaluation.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import unittest import os from yaksh import code_evaluator as evaluator @@ -22,7 +23,7 @@ class ScilabEvaluationTestCases(unittest.TestCase): 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) - self.assertEquals(result.get('error'), "Correct answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_error(self): @@ -61,7 +62,7 @@ class ScilabEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertEquals(result.get("error"), self.timeout_msg) + self.assertEqual(result.get("error"), self.timeout_msg) if __name__ == '__main__': unittest.main() diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index afcf9e8..82956bf 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import shutil import os import zipfile diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index ff76317..2e8c0c6 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import absolute_import import traceback import pwd import os @@ -6,9 +7,9 @@ from os.path import join, isfile import subprocess import importlib -# local imports -from code_evaluator import CodeEvaluator -from file_utils import copy_files, delete_files +# Local imports +from .code_evaluator import CodeEvaluator +from .file_utils import copy_files, delete_files class JavaCodeEvaluator(CodeEvaluator): diff --git a/yaksh/java_stdio_evaluator.py b/yaksh/java_stdio_evaluator.py index f4b8773..71768ef 100644 --- a/yaksh/java_stdio_evaluator.py +++ b/yaksh/java_stdio_evaluator.py @@ -1,11 +1,13 @@ #!/usr/bin/env python +from __future__ import absolute_import import subprocess import os from os.path import isfile -#local imports -from stdio_evaluator import StdIOEvaluator -from file_utils import copy_files, delete_files +#Local imports +from .code_evaluator import CodeEvaluator +from .stdio_evaluator import Evaluator +from .file_utils import copy_files, delete_files class JavaStdioEvaluator(StdIOEvaluator): diff --git a/yaksh/language_registry.py b/yaksh/language_registry.py index 398e1aa..5b2b519 100644 --- a/yaksh/language_registry.py +++ b/yaksh/language_registry.py @@ -1,6 +1,10 @@ -from settings import code_evaluators +from __future__ import absolute_import import importlib import json +import six + +# Local imports +from .settings import code_evaluators registry = None @@ -24,7 +28,7 @@ def create_evaluator_instance(language, test_case_type, json_data, in_dir): class _LanguageRegistry(object): def __init__(self): self._register = {} - for language, module in code_evaluators.iteritems(): + for language, module in six.iteritems(code_evaluators): self._register[language] = None # Public Protocol ########## diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 1b66fd2..78154a3 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -1,13 +1,14 @@ #!/usr/bin/env python +from __future__ import absolute_import import sys import traceback import os from os.path import join import importlib -# local imports -from code_evaluator import CodeEvaluator, TimeoutException -from file_utils import copy_files, delete_files +# Local imports +from .code_evaluator import CodeEvaluator, TimeoutException +from .file_utils import copy_files, delete_files class PythonAssertionEvaluator(CodeEvaluator): @@ -32,7 +33,7 @@ class PythonAssertionEvaluator(CodeEvaluator): else: submitted = compile(user_answer, '', mode='exec') self.exec_scope = {} - exec submitted in self.exec_scope + exec(submitted, self.exec_scope) return self.exec_scope def check_code(self, user_answer, file_paths, test_case): @@ -40,7 +41,7 @@ class PythonAssertionEvaluator(CodeEvaluator): try: tb = None _tests = compile(test_case, '', mode='exec') - exec _tests in self.exec_scope + exec(_tests, self.exec_scope) except AssertionError: type, value, tb = sys.exc_info() info = traceback.extract_tb(tb) diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index 2cfd9c8..3011179 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -1,16 +1,23 @@ #!/usr/bin/env python +from __future__ import absolute_import import sys import traceback import os from os.path import join import importlib from contextlib import contextmanager -from ast import literal_eval -# local imports -from code_evaluator import CodeEvaluator -from StringIO import StringIO -from file_utils import copy_files, delete_files from textwrap import dedent + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +# Local imports +from .code_evaluator import CodeEvaluator +from .file_utils import copy_files, delete_files + + @contextmanager def redirect_stdout(): new_target = StringIO() @@ -43,7 +50,7 @@ class PythonStdioEvaluator(CodeEvaluator): sys.stdin = input_buffer with redirect_stdout() as output_buffer: exec_scope = {} - exec submitted in exec_scope + exec(submitted, exec_scope) self.output_value = output_buffer.getvalue().rstrip("\n") return self.output_value diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 53fa343..011cb69 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import absolute_import import traceback import os from os.path import join, isfile @@ -6,9 +7,9 @@ import subprocess import re import importlib -# local imports -from code_evaluator import CodeEvaluator -from file_utils import copy_files, delete_files +# Local imports +from .code_evaluator import CodeEvaluator +from .file_utils import copy_files, delete_files class ScilabCodeEvaluator(CodeEvaluator): diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py index efb2ae5..f1def95 100644 --- a/yaksh/stdio_evaluator.py +++ b/yaksh/stdio_evaluator.py @@ -15,6 +15,7 @@ class StdIOEvaluator(CodeEvaluator): def evaluate_stdio(self, user_answer, proc, expected_input, expected_output): success = False ip = expected_input.replace(",", " ") + print (type(expected_input), type(ip)) user_output, output_err = proc.communicate(input='{0}\n'.format(ip)) expected_output = expected_output.replace("\r", "") if not expected_input: diff --git a/yaksh/tests/test_code_server.py b/yaksh/tests/test_code_server.py index a73f073..1d6584b 100644 --- a/yaksh/tests/test_code_server.py +++ b/yaksh/tests/test_code_server.py @@ -1,3 +1,4 @@ +from __future__ import absolute_import import json try: from Queue import Queue @@ -5,7 +6,7 @@ except ImportError: from queue import Queue from threading import Thread import unittest -import urllib +from six.moves import urllib from yaksh.code_server import ServerPool, SERVER_POOL_PORT from yaksh import settings @@ -18,7 +19,7 @@ class TestCodeServer(unittest.TestCase): def setUpClass(cls): settings.code_evaluators['python']['standardtestcase'] = \ "yaksh.python_assertion_evaluator.PythonAssertionEvaluator" - ports = range(8001, 8006) + ports = range(8001, 8006) server_pool = ServerPool(ports=ports, pool_port=SERVER_POOL_PORT) cls.server_pool = server_pool cls.server_thread = t = Thread(target=server_pool.run) @@ -117,7 +118,8 @@ class TestCodeServer(unittest.TestCase): url = "http://localhost:%s/status"%SERVER_POOL_PORT # When - data = urllib.urlopen(url).read() + response = urllib.request.urlopen(url) + data = response.read().decode('utf-8') # Then expect = 'out of 5 are free' diff --git a/yaksh/xmlrpc_clients.py b/yaksh/xmlrpc_clients.py index 6bfe0d6..83ba277 100644 --- a/yaksh/xmlrpc_clients.py +++ b/yaksh/xmlrpc_clients.py @@ -1,11 +1,19 @@ -from xmlrpclib import ServerProxy +from __future__ import absolute_import import time import random import socket import json import urllib +from six.moves import urllib -from settings import SERVER_PORTS, SERVER_POOL_PORT +try: + from xmlrpclib import ServerProxy +except ImportError: + # The above import will not work on Python-3.x. + from xmlrpc.client import ServerProxy + +# Local imports +from .settings import SERVER_PORTS, SERVER_POOL_PORT class ConnectionError(Exception): @@ -58,7 +66,8 @@ class CodeServerProxy(object): return result def _get_server(self): - port = json.loads(urllib.urlopen(self.pool_url).read()) + response = urllib.request.urlopen(self.pool_url) + port = json.loads(response.read().decode('utf-8')) proxy = ServerProxy('http://localhost:%d' % port) return proxy -- cgit From 6b6e58b06bd49e36edd87a027c08d223571a0c0b Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 27 Sep 2016 15:08:18 +0530 Subject: Fix test cases and corresponding changes in evaluators for Python 2/3 compatibility --- yaksh/code_evaluator.py | 3 +- yaksh/cpp_code_evaluator.py | 22 ++-- yaksh/cpp_stdio_evaluator.py | 4 +- yaksh/evaluator_tests/test_bash_evaluation.py | 14 ++- yaksh/evaluator_tests/test_c_cpp_evaluation.py | 22 +++- yaksh/evaluator_tests/test_java_evaluation.py | 21 ++-- yaksh/evaluator_tests/test_python_evaluation.py | 137 ++++++++++-------------- yaksh/evaluator_tests/test_scilab_evaluation.py | 14 ++- yaksh/java_code_evaluator.py | 5 +- yaksh/java_stdio_evaluator.py | 2 +- yaksh/python_assertion_evaluator.py | 3 +- yaksh/python_stdio_evaluator.py | 2 +- yaksh/scilab_code_evaluator.py | 2 +- yaksh/stdio_evaluator.py | 8 +- yaksh/test.txt | 1 - 15 files changed, 143 insertions(+), 117 deletions(-) delete mode 100644 yaksh/test.txt diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py index a55aed3..b4740c0 100644 --- a/yaksh/code_evaluator.py +++ b/yaksh/code_evaluator.py @@ -135,6 +135,7 @@ class CodeEvaluator(object): def teardown(self): # Cancel the signal delete_signal_handler() + self._change_dir(MY_DIR) def check_code(self): raise NotImplementedError("check_code method not implemented") @@ -184,7 +185,7 @@ class CodeEvaluator(object): proc.kill() # Re-raise exception. raise - return proc, stdout, stderr + return proc, stdout.decode('utf-8'), stderr.decode('utf-8') def _change_dir(self, in_dir): if in_dir is not None and isdir(in_dir): diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index 31b84b2..adef658 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -19,9 +19,10 @@ class CppCodeEvaluator(CodeEvaluator): self.submit_code_path = self.create_submit_code_file('submit.c') self.compiled_user_answer = None self.compiled_test_code = None + self.user_output_path = "" + self.ref_output_path = "" def teardown(self): - super(CppCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) if os.path.exists(self.ref_output_path): @@ -30,9 +31,11 @@ class CppCodeEvaluator(CodeEvaluator): os.remove(self.user_output_path) if self.files: delete_files(self.files) + super(CppCodeEvaluator, self).teardown() + def set_file_paths(self): - user_output_path = os.getcwd() + '/output' + user_output_path = os.getcwd() + '/output_file' ref_output_path = os.getcwd() + '/executable' return user_output_path, ref_output_path @@ -105,7 +108,6 @@ class CppCodeEvaluator(CodeEvaluator): Returns (False, error_msg): If mandatory arguments are not files or if the required permissions are not given to the file(s). """ - success = False proc, stdnt_out, stdnt_stderr = self.compiled_user_answer stdnt_stderr = self._remove_null_substitute_char(stdnt_stderr) @@ -126,28 +128,28 @@ class CppCodeEvaluator(CodeEvaluator): if proc.returncode == 0: success, err = True, "Correct answer" else: - err = stdout + "\n" + stderr + err = "{0} \n {1}".format(stdout, stderr) else: err = "Error:" try: error_lines = main_err.splitlines() for e in error_lines: if ':' in e: - err = err + "\n" + e.split(":", 1)[1] + err = "{0} \n {1}".format(err, e.split(":", 1)[1]) else: - err = err + "\n" + e + err = "{0} \n {1}".format(err, e) except: - err = err + "\n" + main_err + err = "{0} \n {1}".format(err, main_err) else: err = "Compilation Error:" try: error_lines = stdnt_stderr.splitlines() for e in error_lines: if ':' in e: - err = err + "\n" + e.split(":", 1)[1] + err = "{0} \n {1}".format(err, e.split(":", 1)[1]) else: - err = err + "\n" + e + err = "{0} \n {1}".format(err, e) except: - err = err + "\n" + stdnt_stderr + err = "{0} \n {1}".format(err, stdnt_stderr) return success, err diff --git a/yaksh/cpp_stdio_evaluator.py b/yaksh/cpp_stdio_evaluator.py index dab3499..64de54d 100644 --- a/yaksh/cpp_stdio_evaluator.py +++ b/yaksh/cpp_stdio_evaluator.py @@ -18,13 +18,13 @@ class CppStdioEvaluator(StdIOEvaluator): self.submit_code_path = self.create_submit_code_file('main.c') def teardown(self): - super(CppStdioEvaluator, self).teardown() os.remove(self.submit_code_path) if self.files: delete_files(self.files) + super(CppStdioEvaluator, self).teardown() def set_file_paths(self): - user_output_path = os.getcwd() + '/output' + user_output_path = os.getcwd() + '/output_file' ref_output_path = os.getcwd() + '/executable' return user_output_path, ref_output_path diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py index 01e248f..7fdff48 100755 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -1,6 +1,8 @@ from __future__ import absolute_import import unittest import os +import shutil +import tempfile from yaksh.bash_code_evaluator import BashCodeEvaluator from yaksh.bash_stdio_evaluator import BashStdioEvaluator from yaksh.settings import SERVER_TIMEOUT @@ -9,15 +11,23 @@ from textwrap import dedent class BashAssertionEvaluationTestCases(unittest.TestCase): def setUp(self): + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + tmp_in_dir_path = tempfile.mkdtemp() self.test_case_data = [ {"test_case": "bash_files/sample.sh,bash_files/sample.args"} ] - self.in_dir = os.getcwd() + tmp_in_dir_path = tempfile.mkdtemp() + self.in_dir = tmp_in_dir_path self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your" " code.").format(SERVER_TIMEOUT) self.file_paths = None + def tearDown(self): + os.remove('/tmp/test.txt') + shutil.rmtree(self.in_dir) + def test_correct_answer(self): user_answer = ("#!/bin/bash\n[[ $# -eq 2 ]]" " && echo $(( $1 + $2 )) && exit $(( $1 + $2 ))" @@ -56,7 +66,7 @@ class BashAssertionEvaluationTestCases(unittest.TestCase): self.assertEqual(result.get("error"), self.timeout_msg) def test_file_based_assert(self): - self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.file_paths = [('/tmp/test.txt', False)] self.test_case_data = [ {"test_case": "bash_files/sample1.sh,bash_files/sample1.args"} ] diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py index d57affa..b3551bf 100755 --- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py +++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py @@ -1,6 +1,8 @@ from __future__ import absolute_import import unittest import os +import shutil +import tempfile from yaksh.cpp_code_evaluator import CppCodeEvaluator from yaksh.cpp_stdio_evaluator import CppStdioEvaluator from yaksh.settings import SERVER_TIMEOUT @@ -9,13 +11,20 @@ from textwrap import dedent class CAssertionEvaluationTestCases(unittest.TestCase): def setUp(self): + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + tmp_in_dir_path = tempfile.mkdtemp() self.test_case_data = [{"test_case": "c_cpp_files/main.cpp"}] - self.in_dir = os.getcwd() + self.in_dir = tmp_in_dir_path self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in your" " code.").format(SERVER_TIMEOUT) self.file_paths = None + def tearDown(self): + os.remove('/tmp/test.txt') + shutil.rmtree(self.in_dir) + def test_correct_answer(self): user_answer = "int add(int a, int b)\n{return a+b;}" get_class = CppCodeEvaluator(self.in_dir) @@ -35,9 +44,10 @@ class CAssertionEvaluationTestCases(unittest.TestCase): 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) + lines_of_error = len(result.get('error').splitlines()) self.assertFalse(result.get('success')) self.assertIn("Incorrect:", result.get('error')) - self.assertTrue(result.get('error').splitlines > 1) + self.assertTrue(lines_of_error > 1) def test_compilation_error(self): user_answer = "int add(int a, int b)\n{return a+b}" @@ -62,7 +72,7 @@ class CAssertionEvaluationTestCases(unittest.TestCase): self.assertEqual(result.get("error"), self.timeout_msg) def test_file_based_assert(self): - self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.file_paths = [('/tmp/test.txt', False)] self.test_case_data = [{"test_case": "c_cpp_files/file_data.c"}] user_answer = dedent(""" #include @@ -160,9 +170,10 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) + lines_of_error = len(result.get('error').splitlines()) self.assertFalse(result.get('success')) self.assertIn("Incorrect", result.get('error')) - self.assertTrue(result.get('error').splitlines > 1) + self.assertTrue(lines_of_error > 1) def test_error(self): user_answer = dedent(""" @@ -281,9 +292,10 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) + lines_of_error = len(result.get('error').splitlines()) self.assertFalse(result.get('success')) self.assertIn("Incorrect", result.get('error')) - self.assertTrue(result.get('error').splitlines > 1) + self.assertTrue(lines_of_error > 1) def test_cpp_error(self): user_answer = dedent(""" diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index f94c6d1..a9708e8 100755 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -1,6 +1,8 @@ from __future__ import absolute_import import unittest import os +import shutil +import tempfile from yaksh import code_evaluator as evaluator from yaksh.java_code_evaluator import JavaCodeEvaluator from yaksh.java_stdio_evaluator import JavaStdioEvaluator @@ -10,10 +12,13 @@ from textwrap import dedent class JavaAssertionEvaluationTestCases(unittest.TestCase): def setUp(self): + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + tmp_in_dir_path = tempfile.mkdtemp() self.test_case_data = [ {"test_case": "java_files/main_square.java"} ] - self.in_dir = os.getcwd() + self.in_dir = tmp_in_dir_path evaluator.SERVER_TIMEOUT = 9 self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop in" @@ -21,7 +26,8 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): self.file_paths = None def tearDown(self): - evaluator.SERVER_TIMEOUT = 2 + os.remove('/tmp/test.txt') + shutil.rmtree(self.in_dir) def test_correct_answer(self): user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a*a;\n\t}\n}" @@ -43,8 +49,10 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get('success')) - self.assertIn("Incorrect:", result.get('error')) - self.assertTrue(result.get('error').splitlines > 1) + lines_of_error = len(result.get('error').splitlines()) + self.assertFalse(result.get('success')) + self.assertIn("Incorrect", result.get('error')) + self.assertTrue(lines_of_error > 1) def test_error(self): user_answer = "class Test {\n\tint square_num(int a) {\n\treturn a*a" @@ -69,7 +77,7 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): self.assertEqual(result.get("error"), self.timeout_msg) def test_file_based_assert(self): - self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.file_paths = [("/tmp/test.txt", False)] self.test_case_data = [ {"test_case": "java_files/read_file.java"} ] @@ -169,9 +177,10 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) + lines_of_error = len(result.get('error').splitlines()) self.assertFalse(result.get('success')) self.assertIn("Incorrect", result.get('error')) - self.assertTrue(result.get('error').splitlines > 1) + self.assertTrue(lines_of_error > 1) def test_error(self): diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index 6852136..b93aa13 100755 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -1,14 +1,23 @@ from __future__ import absolute_import +from six.moves import input import unittest import os +import tempfile +import shutil +from textwrap import dedent + +# Local import from yaksh.python_assertion_evaluator import PythonAssertionEvaluator from yaksh.python_stdio_evaluator import PythonStdioEvaluator from yaksh.settings import SERVER_TIMEOUT -from textwrap import dedent class PythonAssertionEvaluationTestCases(unittest.TestCase): def setUp(self): + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + tmp_in_dir_path = tempfile.mkdtemp() + self.in_dir = tmp_in_dir_path self.test_case_data = [{"test_case": 'assert(add(1,2)==3)'}, {"test_case": 'assert(add(-1,2)==1)'}, {"test_case": 'assert(add(-1,-2)==-3)'}, @@ -18,6 +27,10 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): " your code.").format(SERVER_TIMEOUT) self.file_paths = None + def tearDown(self): + os.remove('/tmp/test.txt') + shutil.rmtree(self.in_dir) + def test_correct_answer(self): # Given user_answer = "def add(a,b):\n\treturn a + b" @@ -160,7 +173,6 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): """) recursion_error_msg = ["Traceback", "call", - "RuntimeError", "maximum recursion depth exceeded" ] kwargs = {'user_answer': user_answer, @@ -175,7 +187,6 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): # Then self.assertFalse(result.get("success")) - self.assertEqual(969, len(err)) for msg in recursion_error_msg: self.assertIn(msg, result.get("error")) @@ -188,7 +199,6 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): type_error_msg = ["Traceback", "call", "TypeError", - "exactly", "argument" ] kwargs = {'user_answer': user_answer, @@ -239,7 +249,7 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): def test_file_based_assert(self): # Given self.test_case_data = [{"test_case": "assert(ans()=='2')"}] - self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.file_paths = [('/tmp/test.txt', False)] user_answer = dedent(""" def ans(): with open("test.txt") as f: @@ -323,65 +333,10 @@ class PythonAssertionEvaluationTestCases(unittest.TestCase): self.assertIn(msg, result.get("error")) -class PythonStdoutEvaluationTestCases(unittest.TestCase): - def setUp(self): - self.test_case_data = [{"expected_input": None, - "expected_output": "0 1 1 2 3" - }] - - self.timeout_msg = ("Code took more than {0} seconds to run. " - "You probably have an infinite loop" - " in your code.").format(SERVER_TIMEOUT) - - def test_correct_answer(self): - # Given - user_answer = "a,b=0,1\nfor i in range(5):\n\tprint a,\n\ta,b=b,a+b" - kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data, - } - - # When - evaluator = PythonStdioEvaluator() - result = evaluator.evaluate(**kwargs) - - # Then - self.assertEqual(result.get('error'), "Correct answer") - self.assertTrue(result.get('success')) - - def test_incorrect_answer(self): - # Given - user_answer = "a,b=0,1\nfor i in range(5):\n\tprint b,\n\ta,b=b,a+b" - kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data, - } - - # When - evaluator = PythonStdioEvaluator() - result = evaluator.evaluate(**kwargs) - - # Then - self.assertFalse(result.get('success')) - self.assertIn("Incorrect answer", result.get('error')) - - def test_infinite_loop(self): - # Given - user_answer = "def add(a, b):\n\twhile True:\n\t\tpass\nadd(1,2)" - kwargs = {'user_answer': user_answer, - 'test_case_data': self.test_case_data - } - - # When - evaluator = PythonStdioEvaluator() - result = evaluator.evaluate(**kwargs) - - # Then - self.assertEqual(result.get('error'), self.timeout_msg) - self.assertFalse(result.get('success')) - - class PythonStdIOEvaluationTestCases(unittest.TestCase): - def setUp(self): + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) self.file_paths = None def test_correct_answer_integer(self): @@ -390,9 +345,9 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): "expected_output": "3" }] user_answer = dedent(""" - a = input() - b = input() - print a+b + a = int(input()) + b = int(input()) + print(a+b) """ ) kwargs = {'user_answer': user_answer, @@ -409,13 +364,16 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): def test_correct_answer_list(self): # Given - self.test_case_data = [{"expected_input": "[1,2,3]\n[5,6,7]", + self.test_case_data = [{"expected_input": "1,2,3\n5,6,7", "expected_output": "[1, 2, 3, 5, 6, 7]" }] user_answer = dedent(""" - a = input() - b = input() - print a+b + from six.moves import input + input_a = input() + input_b = input() + a = [int(i) for i in input_a.split(',')] + b = [int(i) for i in input_b.split(',')] + print(a+b) """ ) kwargs = {'user_answer': user_answer, @@ -432,14 +390,14 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): def test_correct_answer_string(self): # Given - self.test_case_data = [{"expected_input": """the quick brown fox jumps\ - over the lazy dog\nthe""", + self.test_case_data = [{"expected_input": ("the quick brown fox jumps over the lazy dog\nthe"), "expected_output": "2" }] user_answer = dedent(""" - a = raw_input() - b = raw_input() - print (a.count(b)) + from six.moves import input + a = str(input()) + b = str(input()) + print(a.count(b)) """ ) kwargs = {'user_answer': user_answer, @@ -460,9 +418,9 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): "expected_output": "3" }] user_answer = dedent(""" - a = input() - b = input() - print a-b + a = int(input()) + b = int(input()) + print(a-b) """ ) kwargs = {'user_answer': user_answer, @@ -480,12 +438,12 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): def test_file_based_answer(self): # Given self.test_case_data = [{"expected_input": "", "expected_output": "2"}] - self.file_paths = [(os.getcwd()+"/yaksh/test.txt", False)] + self.file_paths = [('/tmp/test.txt', False)] user_answer = dedent(""" with open("test.txt") as f: a = f.read() - print a[0] + print(a[0]) """ ) kwargs = {'user_answer': user_answer, @@ -501,5 +459,26 @@ class PythonStdIOEvaluationTestCases(unittest.TestCase): self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) + def test_infinite_loop(self): + # Given + test_case_data = [{"expected_input": "1\n2", + "expected_output": "3" + }] + timeout_msg = ("Code took more than {0} seconds to run. " + "You probably have an infinite loop in" + " your code.").format(SERVER_TIMEOUT) + user_answer = "while True:\n\tpass" + kwargs = {'user_answer': user_answer, + 'test_case_data': test_case_data + } + + # When + evaluator = PythonStdioEvaluator() + result = evaluator.evaluate(**kwargs) + + # Then + self.assertEqual(result.get('error'), timeout_msg) + self.assertFalse(result.get('success')) + if __name__ == '__main__': unittest.main() diff --git a/yaksh/evaluator_tests/test_scilab_evaluation.py b/yaksh/evaluator_tests/test_scilab_evaluation.py index f1b29dd..0bb7bfd 100644 --- a/yaksh/evaluator_tests/test_scilab_evaluation.py +++ b/yaksh/evaluator_tests/test_scilab_evaluation.py @@ -1,19 +1,26 @@ from __future__ import absolute_import import unittest import os +import shutil +import tempfile + from yaksh import code_evaluator as evaluator from yaksh.scilab_code_evaluator import ScilabCodeEvaluator from yaksh.settings import SERVER_TIMEOUT class ScilabEvaluationTestCases(unittest.TestCase): def setUp(self): + tmp_in_dir_path = tempfile.mkdtemp() self.test_case_data = [{"test_case": "scilab_files/test_add.sce"}] - self.in_dir = os.getcwd() + self.in_dir = tmp_in_dir_path self.timeout_msg = ("Code took more than {0} seconds to run. " "You probably have an infinite loop" " in your code.").format(SERVER_TIMEOUT) self.file_paths = None + def tearDown(self): + shutil.rmtree(self.in_dir) + def test_correct_answer(self): user_answer = ("funcprot(0)\nfunction[c]=add(a,b)" "\n\tc=a+b;\nendfunction") @@ -36,7 +43,7 @@ class ScilabEvaluationTestCases(unittest.TestCase): } result = get_class.evaluate(**kwargs) self.assertFalse(result.get("success")) - self.assertTrue("error" in result.get("error")) + self.assertTrue('error' in result.get("error")) def test_incorrect_answer(self): @@ -48,9 +55,10 @@ class ScilabEvaluationTestCases(unittest.TestCase): 'file_paths': self.file_paths } result = get_class.evaluate(**kwargs) + lines_of_error = len(result.get('error').splitlines()) self.assertFalse(result.get('success')) self.assertIn("Message", result.get('error')) - self.assertTrue(result.get('error').splitlines > 1) + self.assertTrue(lines_of_error > 1) def test_infinite_loop(self): user_answer = ("funcprot(0)\nfunction[c]=add(a,b)" diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index 2e8c0c6..a294b8e 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -19,9 +19,10 @@ class JavaCodeEvaluator(CodeEvaluator): self.submit_code_path = self.create_submit_code_file('Test.java') self.compiled_user_answer = None self.compiled_test_code = None + self.user_output_path = "" + self.ref_output_path = "" def teardown(self): - super(JavaCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) if os.path.exists(self.user_output_path): @@ -30,6 +31,8 @@ class JavaCodeEvaluator(CodeEvaluator): os.remove(self.ref_output_path) if self.files: delete_files(self.files) + super(JavaCodeEvaluator, self).teardown() + def get_commands(self, clean_ref_code_path, user_code_directory): compile_command = 'javac {0}'.format(self.submit_code_path), diff --git a/yaksh/java_stdio_evaluator.py b/yaksh/java_stdio_evaluator.py index 71768ef..b5a52f3 100644 --- a/yaksh/java_stdio_evaluator.py +++ b/yaksh/java_stdio_evaluator.py @@ -18,10 +18,10 @@ class JavaStdioEvaluator(StdIOEvaluator): self.submit_code_path = self.create_submit_code_file('Test.java') def teardown(self): - super(JavaStdioEvaluator, self).teardown() os.remove(self.submit_code_path) if self.files: delete_files(self.files) + super(JavaStdioEvaluator, self).teardown() def set_file_paths(self, directory, file_name): output_path = "{0}{1}.class".format(directory, file_name) diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 78154a3..4f159bf 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -1,5 +1,6 @@ #!/usr/bin/env python from __future__ import absolute_import +from six.moves import input import sys import traceback import os @@ -19,10 +20,10 @@ class PythonAssertionEvaluator(CodeEvaluator): self.exec_scope = None def teardown(self): - super(PythonAssertionEvaluator, self).teardown() # Delete the created file. if self.files: delete_files(self.files) + super(PythonAssertionEvaluator, self).teardown() def compile_code(self, user_answer, file_paths, test_case): self.files = [] diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index 3011179..5f672e2 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -32,10 +32,10 @@ class PythonStdioEvaluator(CodeEvaluator): """Tests the Python code obtained from Code Server""" def teardown(self): - super(PythonStdioEvaluator, self).teardown() # Delete the created file. if self.files: delete_files(self.files) + super(PythonStdioEvaluator, self).teardown() def compile_code(self, user_answer, file_paths, expected_input, expected_output): diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 011cb69..075bf6b 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -20,11 +20,11 @@ class ScilabCodeEvaluator(CodeEvaluator): self.create_submit_code_file('function.sci') def teardown(self): - super(ScilabCodeEvaluator, self).teardown() # Delete the created file. os.remove(self.submit_code_path) if self.files: delete_files(self.files) + super(ScilabCodeEvaluator, self).teardown() def check_code(self, user_answer, file_paths, test_case): self.files = [] diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py index f1def95..b5924ff 100644 --- a/yaksh/stdio_evaluator.py +++ b/yaksh/stdio_evaluator.py @@ -15,8 +15,10 @@ class StdIOEvaluator(CodeEvaluator): def evaluate_stdio(self, user_answer, proc, expected_input, expected_output): success = False ip = expected_input.replace(",", " ") - print (type(expected_input), type(ip)) - user_output, output_err = proc.communicate(input='{0}\n'.format(ip)) + encoded_input = '{0}\n'.format(ip).encode('utf-8') + user_output_bytes, output_err_bytes = proc.communicate(encoded_input) + user_output = user_output_bytes.decode('utf-8') + output_err = output_err_bytes.decode('utf-8') expected_output = expected_output.replace("\r", "") if not expected_input: error_msg = "Expected Output is {0} ".\ @@ -31,5 +33,5 @@ class StdIOEvaluator(CodeEvaluator): err = " Incorrect answer\n" + error_msg +\ "\n Your output is {0}".format(repr(user_output)) else: - err = "Error:"+"\n"+output_err + err = "Error:\n {0}".format(output_err) return success, err diff --git a/yaksh/test.txt b/yaksh/test.txt deleted file mode 100644 index 0cfbf08..0000000 --- a/yaksh/test.txt +++ /dev/null @@ -1 +0,0 @@ -2 -- cgit From be024a511b63bcdf1433078aa7512266bd3ce9ba Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 27 Sep 2016 15:44:16 +0530 Subject: Update travis.yml file --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 3484d2d..4759ca4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: python python: - "2.7" + - "3.5" env: - DJANGO=1.8.13 -- cgit From cb0569a32422a195d4165ef405ac5560024dabda Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 29 Sep 2016 18:10:09 +0530 Subject: Add unicode_literal import for Python2 --- yaksh/bash_code_evaluator.py | 2 +- yaksh/bash_stdio_evaluator.py | 2 +- yaksh/code_evaluator.py | 2 +- yaksh/code_server.py | 2 +- yaksh/cpp_code_evaluator.py | 2 +- yaksh/cpp_stdio_evaluator.py | 2 +- yaksh/evaluator_tests/test_bash_evaluation.py | 2 +- yaksh/evaluator_tests/test_code_evaluation.py | 2 +- yaksh/evaluator_tests/test_java_evaluation.py | 2 +- yaksh/evaluator_tests/test_python_evaluation.py | 3 +-- yaksh/evaluator_tests/test_scilab_evaluation.py | 2 +- yaksh/file_utils.py | 1 - yaksh/java_code_evaluator.py | 2 +- yaksh/java_stdio_evaluator.py | 2 +- yaksh/language_registry.py | 2 +- yaksh/python_assertion_evaluator.py | 3 +-- yaksh/python_stdio_evaluator.py | 2 +- yaksh/scilab_code_evaluator.py | 2 +- yaksh/stdio_evaluator.py | 3 ++- yaksh/tests/test_code_server.py | 2 +- yaksh/xmlrpc_clients.py | 2 +- 21 files changed, 21 insertions(+), 23 deletions(-) diff --git a/yaksh/bash_code_evaluator.py b/yaksh/bash_code_evaluator.py index 0cbce89..e148fa8 100644 --- a/yaksh/bash_code_evaluator.py +++ b/yaksh/bash_code_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import traceback import pwd import os diff --git a/yaksh/bash_stdio_evaluator.py b/yaksh/bash_stdio_evaluator.py index fbb94ec..8545ccb 100644 --- a/yaksh/bash_stdio_evaluator.py +++ b/yaksh/bash_stdio_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import subprocess import os from os.path import isfile diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py index b4740c0..90ca6e0 100644 --- a/yaksh/code_evaluator.py +++ b/yaksh/code_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import sys import pwd import os diff --git a/yaksh/code_server.py b/yaksh/code_server.py index b753ac7..b3c9c30 100755 --- a/yaksh/code_server.py +++ b/yaksh/code_server.py @@ -23,7 +23,7 @@ that returns an available server. """ # Standard library imports -from __future__ import absolute_import +from __future__ import unicode_literals import json from multiprocessing import Process, Queue import os diff --git a/yaksh/cpp_code_evaluator.py b/yaksh/cpp_code_evaluator.py index adef658..5380dea 100644 --- a/yaksh/cpp_code_evaluator.py +++ b/yaksh/cpp_code_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import traceback import pwd import os diff --git a/yaksh/cpp_stdio_evaluator.py b/yaksh/cpp_stdio_evaluator.py index 64de54d..d2fa281 100644 --- a/yaksh/cpp_stdio_evaluator.py +++ b/yaksh/cpp_stdio_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import subprocess import os from os.path import isfile diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py index 7fdff48..25fe18d 100755 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import unittest import os import shutil diff --git a/yaksh/evaluator_tests/test_code_evaluation.py b/yaksh/evaluator_tests/test_code_evaluation.py index 4efe119..88e0253 100644 --- a/yaksh/evaluator_tests/test_code_evaluation.py +++ b/yaksh/evaluator_tests/test_code_evaluation.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import unittest import os from yaksh import python_assertion_evaluator diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index a9708e8..ebe05b8 100755 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import unittest import os import shutil diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py index b93aa13..45cc40d 100755 --- a/yaksh/evaluator_tests/test_python_evaluation.py +++ b/yaksh/evaluator_tests/test_python_evaluation.py @@ -1,5 +1,4 @@ -from __future__ import absolute_import -from six.moves import input +from __future__ import unicode_literals import unittest import os import tempfile diff --git a/yaksh/evaluator_tests/test_scilab_evaluation.py b/yaksh/evaluator_tests/test_scilab_evaluation.py index 0bb7bfd..b366480 100644 --- a/yaksh/evaluator_tests/test_scilab_evaluation.py +++ b/yaksh/evaluator_tests/test_scilab_evaluation.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import unittest import os import shutil diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 82956bf..afcf9e8 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -1,4 +1,3 @@ -from __future__ import absolute_import import shutil import os import zipfile diff --git a/yaksh/java_code_evaluator.py b/yaksh/java_code_evaluator.py index a294b8e..1ce1c0e 100644 --- a/yaksh/java_code_evaluator.py +++ b/yaksh/java_code_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import traceback import pwd import os diff --git a/yaksh/java_stdio_evaluator.py b/yaksh/java_stdio_evaluator.py index b5a52f3..3199885 100644 --- a/yaksh/java_stdio_evaluator.py +++ b/yaksh/java_stdio_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import subprocess import os from os.path import isfile diff --git a/yaksh/language_registry.py b/yaksh/language_registry.py index 5b2b519..0e0140b 100644 --- a/yaksh/language_registry.py +++ b/yaksh/language_registry.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import importlib import json import six diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index 4f159bf..dd1c041 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import -from six.moves import input +from __future__ import unicode_literals import sys import traceback import os diff --git a/yaksh/python_stdio_evaluator.py b/yaksh/python_stdio_evaluator.py index 5f672e2..cbbbfd6 100644 --- a/yaksh/python_stdio_evaluator.py +++ b/yaksh/python_stdio_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import sys import traceback import os diff --git a/yaksh/scilab_code_evaluator.py b/yaksh/scilab_code_evaluator.py index 075bf6b..915491c 100644 --- a/yaksh/scilab_code_evaluator.py +++ b/yaksh/scilab_code_evaluator.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from __future__ import absolute_import +from __future__ import unicode_literals import traceback import os from os.path import join, isfile diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py index b5924ff..326d2e7 100644 --- a/yaksh/stdio_evaluator.py +++ b/yaksh/stdio_evaluator.py @@ -1,9 +1,10 @@ +from __future__ import unicode_literals + # Local imports from code_evaluator import CodeEvaluator class StdIOEvaluator(CodeEvaluator): - def setup(self): super(StdIOEvaluator, self).setup() pass diff --git a/yaksh/tests/test_code_server.py b/yaksh/tests/test_code_server.py index 1d6584b..8835110 100644 --- a/yaksh/tests/test_code_server.py +++ b/yaksh/tests/test_code_server.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import json try: from Queue import Queue diff --git a/yaksh/xmlrpc_clients.py b/yaksh/xmlrpc_clients.py index 83ba277..4da70dd 100644 --- a/yaksh/xmlrpc_clients.py +++ b/yaksh/xmlrpc_clients.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import +from __future__ import unicode_literals import time import random import socket -- cgit From b77c5083c2f954e9bf36b4515c337556a0ba47b3 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 29 Sep 2016 18:36:48 +0530 Subject: Reset the current working directory to top most level after evaluation --- yaksh/code_evaluator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yaksh/code_evaluator.py b/yaksh/code_evaluator.py index 90ca6e0..870a67f 100644 --- a/yaksh/code_evaluator.py +++ b/yaksh/code_evaluator.py @@ -135,7 +135,7 @@ class CodeEvaluator(object): def teardown(self): # Cancel the signal delete_signal_handler() - self._change_dir(MY_DIR) + self._change_dir(dirname(MY_DIR)) def check_code(self): raise NotImplementedError("check_code method not implemented") -- cgit From b515ee32315cc406a28549e3ba10d47d5e0e523b Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Fri, 30 Sep 2016 11:18:11 +0530 Subject: - Remove unused imports - Fix minor test case issues --- yaksh/bash_stdio_evaluator.py | 3 +-- yaksh/cpp_stdio_evaluator.py | 3 +-- yaksh/evaluator_tests/test_bash_evaluation.py | 6 +++--- yaksh/evaluator_tests/test_c_cpp_evaluation.py | 16 ++++++++-------- yaksh/evaluator_tests/test_java_evaluation.py | 8 ++++---- yaksh/evaluator_tests/test_python_evaluation.py | 0 yaksh/java_stdio_evaluator.py | 3 +-- yaksh/stdio_evaluator.py | 2 +- 8 files changed, 19 insertions(+), 22 deletions(-) mode change 100755 => 100644 yaksh/evaluator_tests/test_bash_evaluation.py mode change 100755 => 100644 yaksh/evaluator_tests/test_c_cpp_evaluation.py mode change 100755 => 100644 yaksh/evaluator_tests/test_java_evaluation.py mode change 100755 => 100644 yaksh/evaluator_tests/test_python_evaluation.py diff --git a/yaksh/bash_stdio_evaluator.py b/yaksh/bash_stdio_evaluator.py index 8545ccb..e5e0da6 100644 --- a/yaksh/bash_stdio_evaluator.py +++ b/yaksh/bash_stdio_evaluator.py @@ -5,8 +5,7 @@ import os from os.path import isfile #local imports -from .code_evaluator import CodeEvaluator -from .stdio_evaluator import Evaluator +from .stdio_evaluator import StdIOEvaluator from .file_utils import copy_files, delete_files diff --git a/yaksh/cpp_stdio_evaluator.py b/yaksh/cpp_stdio_evaluator.py index d2fa281..9d2b969 100644 --- a/yaksh/cpp_stdio_evaluator.py +++ b/yaksh/cpp_stdio_evaluator.py @@ -5,8 +5,7 @@ import os from os.path import isfile #Local imports -from .code_evaluator import CodeEvaluator -from .stdio_evaluator import Evaluator +from .stdio_evaluator import StdIOEvaluator from .file_utils import copy_files, delete_files diff --git a/yaksh/evaluator_tests/test_bash_evaluation.py b/yaksh/evaluator_tests/test_bash_evaluation.py old mode 100755 new mode 100644 index 25fe18d..66ade19 --- a/yaksh/evaluator_tests/test_bash_evaluation.py +++ b/yaksh/evaluator_tests/test_bash_evaluation.py @@ -99,7 +99,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -119,7 +119,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -153,7 +153,7 @@ class BashStdioEvaluationTestCases(unittest.TestCase): "test_case_data": test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_c_cpp_evaluation.py b/yaksh/evaluator_tests/test_c_cpp_evaluation.py old mode 100755 new mode 100644 index b3551bf..c990436 --- a/yaksh/evaluator_tests/test_c_cpp_evaluation.py +++ b/yaksh/evaluator_tests/test_c_cpp_evaluation.py @@ -117,7 +117,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -137,7 +137,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_string_input(self): @@ -155,7 +155,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -219,7 +219,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_correct_answer(self): @@ -236,7 +236,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_array_input(self): @@ -257,7 +257,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_string_input(self): @@ -276,7 +276,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_cpp_incorrect_answer(self): @@ -344,7 +344,7 @@ class CppStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py old mode 100755 new mode 100644 index ebe05b8..5da66e0 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -136,7 +136,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_array_input(self): @@ -158,7 +158,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_incorrect_answer(self): @@ -229,7 +229,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) def test_string_input(self): @@ -249,7 +249,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): 'test_case_data': self.test_case_data } result = get_class.evaluate(**kwargs) - self.assertEqual(result.get('error'), "Correct Answer") + self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) if __name__ == '__main__': diff --git a/yaksh/evaluator_tests/test_python_evaluation.py b/yaksh/evaluator_tests/test_python_evaluation.py old mode 100755 new mode 100644 diff --git a/yaksh/java_stdio_evaluator.py b/yaksh/java_stdio_evaluator.py index 3199885..bc9cf80 100644 --- a/yaksh/java_stdio_evaluator.py +++ b/yaksh/java_stdio_evaluator.py @@ -5,8 +5,7 @@ import os from os.path import isfile #Local imports -from .code_evaluator import CodeEvaluator -from .stdio_evaluator import Evaluator +from .stdio_evaluator import StdIOEvaluator from .file_utils import copy_files, delete_files diff --git a/yaksh/stdio_evaluator.py b/yaksh/stdio_evaluator.py index 326d2e7..7530b96 100644 --- a/yaksh/stdio_evaluator.py +++ b/yaksh/stdio_evaluator.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals # Local imports -from code_evaluator import CodeEvaluator +from .code_evaluator import CodeEvaluator class StdIOEvaluator(CodeEvaluator): -- cgit From 513f5a52fec1945250d2fe7d48f1f4e3103cf3a1 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 30 Sep 2016 17:54:36 +0530 Subject: added utf-8 decoding in custom filters --- yaksh/templatetags/custom_filters.py | 1 + 1 file changed, 1 insertion(+) diff --git a/yaksh/templatetags/custom_filters.py b/yaksh/templatetags/custom_filters.py index 5baa977..9d7b939 100644 --- a/yaksh/templatetags/custom_filters.py +++ b/yaksh/templatetags/custom_filters.py @@ -6,6 +6,7 @@ register = template.Library() @stringfilter @register.filter(name='escape_quotes') def escape_quotes(value): + value = value.decode("utf-8") escape_single_quotes = value.replace("'", "\\'") escape_single_and_double_quotes = escape_single_quotes.replace('"', '\\"') -- cgit From 4a7762831ad7ae298f047e72e784630285219787 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 30 Sep 2016 17:55:40 +0530 Subject: modified views, models, forms for django as per python 3 --- yaksh/forms.py | 6 +++++- yaksh/models.py | 29 ++++++++++++++++------------- yaksh/views.py | 7 ++++--- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/yaksh/forms.py b/yaksh/forms.py index 23131b7..a12ce9a 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -9,7 +9,11 @@ from taggit.managers import TaggableManager from taggit.forms import TagField from django.forms.models import inlineformset_factory from django.db.models import Q -from string import letters, punctuation, digits +try: + from string import letters +except ImportError: + from string import ascii_letters as letters +from string import punctuation, digits import datetime import pytz diff --git a/yaksh/models.py b/yaksh/models.py index 69dff6d..444df4a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -11,7 +11,10 @@ from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone from django.core.files import File -from StringIO import StringIO +try: + from StringIO import StringIO as string_io +except ImportError: + from io import BytesIO as string_io import pytz import os import stat @@ -19,7 +22,7 @@ from os.path import join, abspath, dirname, exists import shutil import zipfile import tempfile -from file_utils import extract_files +from .file_utils import extract_files from yaksh.xmlrpc_clients import code_server from django.conf import settings @@ -177,7 +180,7 @@ class Course(models.Model): success = False return success - def __unicode__(self): + def __str__(self): return self.name ############################################################################### @@ -270,7 +273,7 @@ class Question(models.Model): def dump_questions(self, question_ids, user): questions = Question.objects.filter(id__in=question_ids, user_id=user.id) questions_dict = [] - zip_file_name = StringIO() + zip_file_name = string_io() zip_file = zipfile.ZipFile(zip_file_name, "a") for question in questions: test_case = question.get_test_cases() @@ -364,7 +367,7 @@ class Question(models.Model): self.read_json("questions_dump.json", user) - def __unicode__(self): + def __str__(self): return self.summary @@ -418,7 +421,7 @@ class Answer(models.Model): else: self.marks = marks - def __unicode__(self): + def __str__(self): return self.answer @@ -540,7 +543,7 @@ class Quiz(models.Model): course=course) return demo_quiz - def __unicode__(self): + def __str__(self): desc = self.description or 'Quiz' return '%s: on %s for %d minutes' % (desc, self.start_date_time, self.duration) @@ -682,7 +685,7 @@ class QuestionPaper(models.Model): for question in questions: question_paper.fixed_questions.add(question) - def __unicode__(self): + def __str__(self): return "Question Paper for " + self.quiz.description ############################################################################### @@ -702,7 +705,7 @@ class QuestionSet(models.Model): def get_random_questions(self): """ Returns random questions from set of questions""" - return sample(self.questions.all(), self.num_questions) + return sample(list(self.questions.all()), self.num_questions) ############################################################################### @@ -1075,7 +1078,7 @@ class AnswerPaper(models.Model): self.update_marks('complete') return True, msg - def __unicode__(self): + def __str__(self): u = self.user q = self.question_paper.quiz return u'AnswerPaper paper of {0} {1} for quiz {2}'\ @@ -1099,7 +1102,7 @@ class StandardTestCase(TestCase): def get_field_value(self): return {"test_case": self.test_case} - def __unicode__(self): + def __str__(self): return u'Question: {0} | Test Case: {1}'.format(self.question, self.test_case ) @@ -1113,7 +1116,7 @@ class StdioBasedTestCase(TestCase): return {"expected_output": self.expected_output, "expected_input": self.expected_input} - def __unicode__(self): + def __str__(self): return u'Question: {0} | Exp. Output: {1} | Exp. Input: {2}'.format(self.question, self.expected_output, self.expected_input ) @@ -1126,7 +1129,7 @@ class McqTestCase(TestCase): def get_field_value(self): return {"options": self.options, "correct": self.correct} - def __unicode__(self): + def __str__(self): return u'Question: {0} | Correct: {1}'.format(self.question, self.correct ) diff --git a/yaksh/views.py b/yaksh/views.py index 4c5b9b8..0ed5f5a 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -21,6 +21,7 @@ from taggit.models import Tag from itertools import chain import json import zipfile +import six # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ @@ -29,9 +30,9 @@ from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ get_object_form, FileForm -from settings import URL_ROOT +from .settings import URL_ROOT from yaksh.models import AssignmentUpload -from file_utils import extract_files +from .file_utils import extract_files @@ -982,7 +983,7 @@ def grade_user(request, quiz_id=None, user_id=None, attempt_number=None): if request.method == "POST": papers = data['papers'] for paper in papers: - for question, answers in paper.get_question_answers().iteritems(): + for question, answers in six.iteritems(paper.get_question_answers()): marks = float(request.POST.get('q%d_marks' % question.id, 0)) answers = answers[-1] answers.set_marks(marks) -- cgit From 151470b8299cd53d79859d6759f706f8fb962003 Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 30 Sep 2016 17:56:20 +0530 Subject: changed code_evaluators dict in settings --- yaksh/settings.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/yaksh/settings.py b/yaksh/settings.py index b1336f4..6383999 100644 --- a/yaksh/settings.py +++ b/yaksh/settings.py @@ -20,21 +20,21 @@ SERVER_TIMEOUT = 4 URL_ROOT = '' code_evaluators = { - "python": {"standardtestcase": "python_assertion_evaluator.PythonAssertionEvaluator", - "stdiobasedtestcase": "python_stdio_evaluator.PythonStdioEvaluator" + "python": {"standardtestcase": "yaksh.python_assertion_evaluator.PythonAssertionEvaluator", + "stdiobasedtestcase": "yaksh.python_stdio_evaluator.PythonStdioEvaluator" }, - "c": {"standardtestcase": "cpp_code_evaluator.CppCodeEvaluator", - "stdiobasedtestcase": "cpp_stdio_evaluator.CppStdioEvaluator" + "c": {"standardtestcase": "yaksh.cpp_code_evaluator.CppCodeEvaluator", + "stdiobasedtestcase": "yaksh.cpp_stdio_evaluator.CppStdioEvaluator" }, - "cpp": {"standardtestcase": "cpp_code_evaluator.CppCodeEvaluator", - "stdiobasedtestcase": "cpp_stdio_evaluator.CppStdioEvaluator" + "cpp": {"standardtestcase": "yaksh.cpp_code_evaluator.CppCodeEvaluator", + "stdiobasedtestcase": "yaksh.cpp_stdio_evaluator.CppStdioEvaluator" }, - "java": {"standardtestcase": "java_code_evaluator.JavaCodeEvaluator", - "stdiobasedtestcase": "java_stdio_evaluator.JavaStdioEvaluator"}, + "java": {"standardtestcase": "yaksh.java_code_evaluator.JavaCodeEvaluator", + "stdiobasedtestcase": "yaksh.java_stdio_evaluator.JavaStdioEvaluator"}, - "bash": {"standardtestcase": "bash_code_evaluator.BashCodeEvaluator", - "stdiobasedtestcase": "bash_stdio_evaluator.BashStdioEvaluator" + "bash": {"standardtestcase": "yaksh.bash_code_evaluator.BashCodeEvaluator", + "stdiobasedtestcase": "yaksh.bash_stdio_evaluator.BashStdioEvaluator" }, - "scilab": {"standardtestcase": "scilab_code_evaluator.ScilabCodeEvaluator"}, + "scilab": {"standardtestcase": "yaksh.scilab_code_evaluator.ScilabCodeEvaluator"}, } -- cgit From ca83832cdb43908c965ce552f714df8256117b3d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 30 Sep 2016 17:57:10 +0530 Subject: changed test cases for models as per python 3 --- yaksh/test_models.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 50ead1d..019a339 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -58,6 +58,9 @@ def setUpModule(): language='Python', prerequisite=quiz, course=course) + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + def tearDownModule(): User.objects.all().delete() Question.objects.all().delete() @@ -66,7 +69,8 @@ def tearDownModule(): que_id_list = ["25", "22", "24", "27"] for que_id in que_id_list: dir_path = os.path.join(os.getcwd(), "yaksh", "data","question_{0}".format(que_id)) - shutil.rmtree(dir_path) + if os.path.exists(dir_path): + shutil.rmtree(dir_path) ############################################################################### class ProfileTestCases(unittest.TestCase): @@ -115,7 +119,7 @@ class QuestionTestCases(unittest.TestCase): self.question2.save() # create a temp directory and add files for loading questions test - file_path = os.path.join(os.getcwd(), "yaksh", "test.txt") + file_path = "/tmp/test.txt" self.load_tmp_path = tempfile.mkdtemp() shutil.copy(file_path, self.load_tmp_path) file1 = os.path.join(self.load_tmp_path, "test.txt") @@ -164,7 +168,8 @@ class QuestionTestCases(unittest.TestCase): tag_list = [] for tag in self.question1.tags.all(): tag_list.append(tag.name) - self.assertEqual(tag_list, ['python', 'function']) + for tag in tag_list: + self.assertIn(tag, ['python', 'function']) def test_dump_questions(self): """ Test dump questions into json """ @@ -714,7 +719,7 @@ class AnswerPaperTestCases(unittest.TestCase): def test_get_question_answer(self): """ Test get_question_answer() method of Answer Paper""" answered = self.answerpaper.get_question_answers() - first_answer = answered.values()[0][0] + first_answer = list(answered.values())[0][0] self.assertEqual(first_answer.answer, 'Demo answer') self.assertTrue(first_answer.correct) self.assertEqual(len(answered), 2) @@ -895,4 +900,7 @@ class TestCaseTestCases(unittest.TestCase): result = self.question1.consolidate_answer_data( user_answer="demo_answer" ) - self.assertEqual(result, self.answer_data_json) + actual_data = json.loads(result) + exp_data = json.loads(self.answer_data_json) + self.assertEqual(actual_data['user_answer'], exp_data['user_answer']) + self.assertEqual(actual_data['test_case_data'], exp_data['test_case_data']) -- cgit From 55e3f5d5d6888735b79b067303707bc27458d04d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 30 Sep 2016 18:29:59 +0530 Subject: added test for file based stdout in java --- yaksh/evaluator_tests/test_java_evaluation.py | 54 +++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/yaksh/evaluator_tests/test_java_evaluation.py b/yaksh/evaluator_tests/test_java_evaluation.py index 5da66e0..e375bdb 100644 --- a/yaksh/evaluator_tests/test_java_evaluation.py +++ b/yaksh/evaluator_tests/test_java_evaluation.py @@ -111,6 +111,10 @@ class JavaAssertionEvaluationTestCases(unittest.TestCase): class JavaStdioEvaluationTestCases(unittest.TestCase): def setUp(self): + with open('/tmp/test.txt', 'wb') as f: + f.write('2'.encode('ascii')) + tmp_in_dir_path = tempfile.mkdtemp() + self.in_dir = tmp_in_dir_path self.test_case_data = [{'expected_output': '11', 'expected_input': '5\n6'}] evaluator.SERVER_TIMEOUT = 4 @@ -118,8 +122,10 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): "You probably have an infinite loop in" " your code.").format(evaluator.SERVER_TIMEOUT) - def teardown(self): + def tearDown(self): evaluator.SERVER_TIMEOUT = 4 + os.remove('/tmp/test.txt') + shutil.rmtree(self.in_dir) def test_correct_answer(self): user_answer = dedent(""" @@ -131,7 +137,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): int b = s.nextInt(); System.out.print(a+b); }}""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -153,7 +159,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): a[i] = s.nextInt(); System.out.print(a[i]);} }}""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -172,7 +178,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): int b = s.nextInt(); System.out.print(a); }}""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -189,7 +195,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): { System.out.print("a"); }""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -206,7 +212,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): { System.out.print("a");} }}""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -224,7 +230,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): int b = 6; System.out.print(a+b); }}""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -244,7 +250,7 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): String b = s.nextLine(); System.out.print(a+b); }}""") - get_class = JavaStdioEvaluator() + get_class = JavaStdioEvaluator(self.in_dir) kwargs = {'user_answer': user_answer, 'test_case_data': self.test_case_data } @@ -252,5 +258,37 @@ class JavaStdioEvaluationTestCases(unittest.TestCase): self.assertEqual(result.get('error'), "Correct answer") self.assertTrue(result.get('success')) + def test_file_based_stdout(self): + self.file_paths = [("/tmp/test.txt", False)] + self.test_case_data = [{'expected_output': '2', + 'expected_input': ''}] + user_answer = dedent(""" + import java.io.BufferedReader; + import java.io.FileReader; + import java.io.IOException; + class Test{ + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new FileReader("test.txt")); + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + while (line != null) { + sb.append(line); + line = br.readLine();} + System.out.print(sb.toString()); + } finally { + br.close(); + }}} + """) + get_class = JavaStdioEvaluator(self.in_dir) + kwargs = {'user_answer': user_answer, + 'test_case_data': self.test_case_data, + 'file_paths': self.file_paths + } + result = get_class.evaluate(**kwargs) + self.assertTrue(result.get("success")) + self.assertEqual(result.get("error"), "Correct answer") + + if __name__ == '__main__': unittest.main() -- cgit From 7c0b1ea01dabaabfb13340432b6acad06778f72c Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 3 Oct 2016 12:01:51 +0530 Subject: changes in travis for python 3 --- .travis.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4759ca4..4c2f8ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,12 +10,9 @@ env: # command to install dependencies install: - - pip install tornado - - pip install git+https://github.com/FOSSEE/online_test.git#egg=yaksh-0.1 + - if [[$TRAVIS_PYTHON_VERSION == 2.7 ]]; then pip install -r requirements/requirements-py2.txt; fi + - if [[$TRAVIS_PYTHON_VERSION == 3.5 ]]; then pip install -r requirements/requirements-py3.txt; fi - pip install -q Django==$DJANGO - - pip install -q pytz==2016.4 - - pip install -q python-social-auth==0.2.19 - - pip install selenium before_install: - sudo apt-get update -qq -- cgit From 5ab7ced762258805dde89661bdf187c46ab33049 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 3 Oct 2016 12:03:39 +0530 Subject: added separate requirements for py2 and py3 --- requirements.txt | 6 ------ requirements/requirements-common.txt | 6 ++++++ requirements/requirements-py2.txt | 2 ++ requirements/requirements-py3.txt | 2 ++ 4 files changed, 10 insertions(+), 6 deletions(-) delete mode 100644 requirements.txt create mode 100644 requirements/requirements-common.txt create mode 100644 requirements/requirements-py2.txt create mode 100644 requirements/requirements-py3.txt diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index bea0017..0000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -django==1.9.5 -mysql-python==1.2.5 -django-taggit==0.18.1 -pytz==2016.4 -python-social-auth==0.2.19 -tornado diff --git a/requirements/requirements-common.txt b/requirements/requirements-common.txt new file mode 100644 index 0000000..8138a4f --- /dev/null +++ b/requirements/requirements-common.txt @@ -0,0 +1,6 @@ +django==1.9.5 +django-taggit==0.18.1 +pytz==2016.4 +python-social-auth==0.2.19 +tornado +selenium diff --git a/requirements/requirements-py2.txt b/requirements/requirements-py2.txt new file mode 100644 index 0000000..38777a1 --- /dev/null +++ b/requirements/requirements-py2.txt @@ -0,0 +1,2 @@ +-r requirements-common.txt +mysql-python==1.2.5 diff --git a/requirements/requirements-py3.txt b/requirements/requirements-py3.txt new file mode 100644 index 0000000..3d13335 --- /dev/null +++ b/requirements/requirements-py3.txt @@ -0,0 +1,2 @@ +-r requirements-common.txt +mysqlclient==1.3.9 -- cgit From 59fa975a9fd0f6728cf62b1069abecac95a77b68 Mon Sep 17 00:00:00 2001 From: adityacp Date: Mon, 3 Oct 2016 12:44:22 +0530 Subject: change setup.py with py 3 --- .travis.yml | 3 +-- setup.py | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4c2f8ab..c47785e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,7 @@ env: # command to install dependencies install: - - if [[$TRAVIS_PYTHON_VERSION == 2.7 ]]; then pip install -r requirements/requirements-py2.txt; fi - - if [[$TRAVIS_PYTHON_VERSION == 3.5 ]]; then pip install -r requirements/requirements-py3.txt; fi + - pip install git+https://github.com/FOSSEE/online_test.git#egg=yaksh-0.1 - pip install -q Django==$DJANGO before_install: diff --git a/setup.py b/setup.py index 0c0b7f9..3ab362a 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,28 @@ import os from setuptools import setup, find_packages +import sys README = open(os.path.join(os.path.dirname(__file__), 'README.md')).read() # allow setup.py to be run from any path os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) +install_requires=[ + 'django==1.9.5', + 'django-taggit==0.18.1', + 'pytz==2016.4', + 'python-social-auth==0.2.19', + 'tornado', +] +if sys.version_info[:2] == (2, 7): + install_requires+=[ + 'mysql-python==1.2.5' +] +if sys.version_info[0] >= 3: + install_requires+=[ + 'mysqlclient==1.3.9' +] + setup( name='yaksh', author='Python Team at FOSSEE, IIT Bombay', @@ -22,13 +39,7 @@ setup( }, description='A django app to conduct online tests.', long_description=README, - install_requires=[ - 'django==1.9.5', - 'mysql-python==1.2.5', - 'django-taggit==0.18.1', - 'pytz==2016.4', - 'python-social-auth==0.2.19' - ], + install_requires=install_requires, classifiers=[ 'Development Status :: 4 - Beta', 'Environment :: Web Environment', -- cgit