Ejemplo n.º 1
0
    def execute_code(self, inputContents, verbose=False):
        """
        Executes the code by first compiling it (if necessary), then running it,
        then returning the output or an ExecutionError if one occurred
        """
        if not self._compileCommand is None:
            self._path = fileops.get_path_with_changed_extension(self._path,
                          self._runExtension)

        runCommand = [self._runCommand]
        runCommand.extend(self._runArguments)
        try:
            encodedInput = inputContents.encode('utf-8')
            output = subprocess.check_output(runCommand, input=encodedInput, 
                stderr = (open(os.devnull, 'w') if not verbose else sys.stderr),
                timeout=20).decode('utf-8')
            if not output is None:
                output = output.replace('\r','')
        except subprocess.CalledProcessError as e:
            raise ExecutionError('Runtime Error')
        except subprocess.TimeoutExpired as e:
            raise ExecutionError('Timeout Expired')
        except Exception:
            raise ExecutionError('Could not run command {}'.format(
                runCommand[0])) from None

        return output[:-1]
Ejemplo n.º 2
0
    def _compile_code(self, verbose=False):
        """
        Attempts to compile the code found at the given path

        Returns: The path of the compiled code object
        """
        if self._compileCommand is None:
            return

        compileCommand = [self._compileCommand]
        compileCommand.extend(self._compileArguments)
        try:
            if not subprocess.call(compileCommand, stderr = (open(os.devnull, 'w')
                if not verbose else sys.stderr)) == 0:
                raise ExecutionError('Failed to compile')
        except Exception:
            raise ExecutionError('Could not run command {}'.format(
                compileCommand[0])) from None

        return fileops.get_path_with_changed_extension(self._path, 
                self._runExtension)