Ejemplo n.º 1
0
    def test_write_a_file_in_sandbox(self):
        sandbox = Sandbox(54321)
        file_contents = b"I want to write a file in sandbox"
        file_name = "write_file_test.txt"
        files = [(file_name, file_contents)]

        # copy
        result = sandbox.write_files_in_sandbox(files, '/')
        self.assertTrue(result)

        # check existence
        result = sandbox.exec('[ -e "/write_file_test.txt" ]')
        self.assertEqual(0, result.get('ExitCode'))

        # check contents
        result = sandbox.exec('cat /write_file_test.txt')
        self.assertEqual(file_contents, result.get('Output'))
Ejemplo n.º 2
0
    def test_send_and_get_files_from_sandbox(self):
        sandbox = Sandbox(54321)

        # get .py files under tests/resources/
        files = []
        resources_path = self.PROJECT_ROOT.joinpath('./tests/resources')
        for path in resources_path.glob('*.py'):
            file_obj = open(path, 'rb')
            files.append((path.name, file_obj.read()))
            file_obj.close()

        # make directory for them
        res = sandbox.exec('mkdir /workdir/test_copy')
        self.assertEqual(0, res.get('ExitCode'))

        # send them all
        res = sandbox.write_files_in_sandbox(files, '/workdir/test_copy')
        self.assertTrue(res)

        # get and check
        res = sandbox.get_files_from_sandbox('/workdir/test_copy/.')
        self.assertListEqual(files, res)
Ejemplo n.º 3
0
    def prepare(self, runtime_context: RuntimeContext) -> Sandbox:
        sandbox = Sandbox(54321)
        input_files = [("solution.cc", str.encode(runtime_context.source_code))
                       ]
        for input_name, input_content in runtime_context.problem_metadata.inputs.items(
        ):
            input_files.append((input_name, str.encode(input_content)))

        res = sandbox.write_files_in_sandbox(input_files, "/workdir")
        if res is False:
            raise Exception

        res = sandbox.exec("mkdir /workdir/outputs")
        if res.get('ExitCode') is not 0:
            raise Exception

        res = sandbox.exec(
            "g++ -std=c++11 -O2 -Wall /workdir/solution.cc -o /workdir/solution"
        )
        if res.get('ExitCode') is not 0:
            print('compile error')
            raise Exception

        return sandbox