def run_process_with_timeout(<EOL>args,<EOL>filename_in=None,<EOL>filename_out=None,<EOL>filename_err=None,<EOL>cwd=None,<EOL>timeout=None,<EOL>sudo=None):
if timeout is None:<EOL><INDENT>timeout = current_app.config['<STR_LIT>']<EOL><DEDENT>stdout = stderr = None<EOL>if filename_in is not None:<EOL><INDENT>stdin = open(filename_in)<EOL><DEDENT>else:<EOL><INDENT>stdin = open('<STR_LIT>', '<STR_LIT:r>')<EOL><DEDENT>if filename_out:<EOL><INDENT>stdout = open(filename_out, '<STR_LIT:w>')<EOL><DEDENT>if filename_err:<EOL><INDENT>stderr = open(filename_err, '<STR_LIT:w>')<EOL><DEDENT>tmp_stdout = StringIO()<EOL>tmp_stderr = StringIO()<EOL>if sudo is not None:<EOL><INDENT>args = ['<STR_LIT>', '<STR_LIT>', sudo, '<STR_LIT>'] + list(args)<EOL><DEDENT>process = subprocess.Popen(<EOL>args,<EOL>stdin=stdin,<EOL>stdout=subprocess.PIPE,<EOL>stderr=subprocess.PIPE,<EOL>close_fds=True,<EOL>cwd=cwd,<EOL>preexec_fn=os.setpgrp)<EOL>fd = process.stdout.fileno()<EOL>fl = fcntl.fcntl(fd, fcntl.F_GETFL)<EOL>fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)<EOL>fd = process.stderr.fileno()<EOL>fl = fcntl.fcntl(fd, fcntl.F_GETFL)<EOL>fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)<EOL>fd_to_poll = [process.stdout, process.stderr]<EOL>select_timeout = <NUM_LIT:0.5><EOL>t1 = time.time()<EOL>try:<EOL><INDENT>while process.poll() is None:<EOL><INDENT>if time.time() - t1 >= timeout:<EOL><INDENT>if process.stdin is not None:<EOL><INDENT>process.stdin.close()<EOL><DEDENT>time.sleep(<NUM_LIT:1>)<EOL>if process.poll() is None:<EOL><INDENT>os.killpg(process.pid, signal.SIGTERM)<EOL>time.sleep(<NUM_LIT:1>)<EOL><DEDENT>if process.poll() is None:<EOL><INDENT>os.killpg(process.pid, signal.SIGKILL)<EOL><DEDENT>try:<EOL><INDENT>os.waitpid(process.pid, <NUM_LIT:0>)<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT>raise Timeout()<EOL><DEDENT>for fd in select.select(fd_to_poll, [], [], select_timeout)[<NUM_LIT:0>]:<EOL><INDENT>if fd == process.stdout:<EOL><INDENT>buf = process.stdout.read(<NUM_LIT>)<EOL>if stdout is None:<EOL><INDENT>tmp_stdout.write(buf)<EOL><DEDENT>else:<EOL><INDENT>stdout.write(buf)<EOL><DEDENT><DEDENT>elif fd == process.stderr:<EOL><INDENT>buf = process.stderr.read(<NUM_LIT>)<EOL>if stderr is None:<EOL><INDENT>tmp_stderr.write(buf)<EOL><DEDENT>else:<EOL><INDENT>stderr.write(buf)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise OSError("<STR_LIT>" % fd)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>while True:<EOL><INDENT>for fd in select.select(fd_to_poll, [], [], select_timeout)[<NUM_LIT:0>]:<EOL><INDENT>if fd == process.stdout:<EOL><INDENT>buf = process.stdout.read(<NUM_LIT>)<EOL>tmp_stdout.write(buf)<EOL>if stdout is not None:<EOL><INDENT>stdout.write(buf)<EOL><DEDENT><DEDENT>elif fd == process.stderr:<EOL><INDENT>buf = process.stderr.read(<NUM_LIT>)<EOL>tmp_stderr.write(buf)<EOL>if stderr is not None:<EOL><INDENT>stderr.write(buf)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise OSError("<STR_LIT>" % fd)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>return process.poll(), tmp_stdout.getvalue(), tmp_stderr.getvalue()<EOL>
Execute the specified process but within a certain timeout.

    @param args: the actuall process. This should be a list of string as in:
         ['/usr/bin/foo', '--bar', 'baz']
    @type args: list of string

    @param filename_in: the path to a file that should be provided as standard
        input to the process. If None this will default to /dev/null
    @type filename_in: string

    @param filename_out: Desired filename for stdout output
        (optional; see below).
    @type filename_out: string

    @param filename_err: Desired filename for stderr output
        (optional; see below).
    @type filename_err: string

    @param cwd: the path from where to execute the process
    @type cwd: string

    @param timeout: the timeout in seconds after which to consider the
        process execution as failed. a Timeout exception will be raised
    @type timeout: int

    @param sudo: the optional name of the user under which to execute the
        process (by using sudo, without prompting for a password)
    @type sudo: string

    @return: Tuple (exit code, string containing stdout output buffer,
        string containing stderr output buffer).

        However, if either filename_out or filename_err are defined,
        then the output buffers are not passed back but rather written
        into filename_out/filename_err pathnames.  This is useful for
        commands that produce big files, for which it is not practical
        to pass results back to the callers in a Python text buffer.
        Note that it is the client's responsibility to name these
        files in the proper fashion (e.g. to be unique) and to close
        these files after use.
    @rtype: (number, string, string)

    @raise Timeout: if the process does not terminate within the timeout