예제 #1
0
def run_command(cmd, env=None, **kwargs):
    """Run the given command in killable process."""
    killable_kwargs = {'stdout':stdout ,'stderr':stderr, 'stdin':stdin}
    killable_kwargs.update(kwargs)

    if sys.platform != "win32":
        return killableprocess.Popen(cmd, preexec_fn=lambda : os.setpgid(0, 0),
                                     env=env, **killable_kwargs)
    else:
        return killableprocess.Popen(cmd, env=env, **killable_kwargs)
    def _execute_with_timeout(self, cmd, max_attempts=3, max_timeout=120):
        logging.debug("$> %s # with timeout %ds", ' '.join(cmd), max_timeout)

        attempt = 0

        while attempt < max_attempts:
            attempt = attempt + 1
            out = tempfile.NamedTemporaryFile(delete=False)
            err = tempfile.NamedTemporaryFile(delete=False)
            p = killableprocess.Popen(cmd, stdout=out, stderr=err)
            p.wait(max_timeout)
            out.flush()
            out.close()
            err.flush()
            err.close()

            # -9 and 127 are returned by killableprocess when a timeout happens
            if  p.returncode == -9 or p.returncode == 127:
                logging.warn("Executing %s failed executing in less then %d seconds and was killed, attempt number %d of %d" % (
                    ' '.join(cmd), max_timeout, attempt, max_attempts))
                continue

        try:
            outfile = open(out.name, 'r')
            errfile = open(err.name, 'r')
            return p.returncode, outfile.read(), errfile.read()
        finally:
            outfile.close()
            os.unlink(out.name)
            errfile.close()
            os.unlink(errfile.name)
예제 #3
0
    def __init__(self,
                 encoding,
                 external_id=None,
                 cmd_postfix="\n",
                 suppress_echo=False,
                 cmd=None,
                 env=None,
                 cwd=None,
                 extend_env=None,
                 soft_quit=""):
        super(SubprocessRepl, self).__init__(encoding, external_id,
                                             cmd_postfix, suppress_echo)
        settings = load_settings('SublimeREPL.sublime-settings')

        env = self.env(env, extend_env, settings)
        self._cmd = self.cmd(cmd, env)
        self._soft_quit = soft_quit
        self.popen = killableprocess.Popen(
            self._cmd,
            startupinfo=self.startupinfo(settings),
            creationflags=self.creationflags(settings),
            bufsize=1,
            cwd=self.cwd(cwd, settings),
            env=env,
            stderr=subprocess.STDOUT,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE)
예제 #4
0
파일: update.py 프로젝트: nox/presto-testo
def git(command, *args):
    command_line = ["git", command] + list(args)
    sys.stderr.write("Running command %s\n" % " ".join(command_line))
    proc = killableprocess.Popen(command_line,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
    return proc
예제 #5
0
def runVrPrepare(project, logfile):
    # run
    log = open(logfile, "w")
    process = killableprocess.Popen([VR_PREPARE, project],
                                    stdout=log,
                                    stderr=log)
    ret = process.wait(TIMEOUT * 60, True)
    log.close()
    return (ret != -9)  # -9 means timeout
예제 #6
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]  # don't include the script name

    ADB_TIMEOUT = os.getenv('ADB_TIMEOUT', 300)
    ADB_MAX_ATTEMPTS = os.getenv('ADB_MAX_ATTEMPTS', 2)
    attempt = 0

    args = ""
    for arg in argv:
        args += " %s" % arg

    cmd = "adb %s" % (args)
    #print "cmd: %s" % cmd
    while attempt < ADB_MAX_ATTEMPTS:

        attempt = attempt + 1
        # Need to call the subprocess using a file object for stdout. There have been
        # problems with the OS blocking when large amounts of data is being returned
        # in stdout when making the call to adb. Poth processes must be buffering and
        # causing the process to become blocked.
        f = tempfile.NamedTemporaryFile(delete=False)
        p = killableprocess.Popen(cmd,
                                  shell=True,
                                  stdout=f,
                                  stderr=subprocess.PIPE)
        try:
            p.wait(ADB_TIMEOUT)
        except OSError:
            '''
            We have seen the following error happen in the build system, so guard against
            this error and just treat like a timeout and retry the call
                "killableprocess.py", line 177, in kill
                    os.killpg(self.pid, signal.SIGKILL)
                    OSError: [Errno 3] No such process
            '''
            p.returncode = 127
        f.flush()
        f.close()

        # -9 and 127 are returned by killableprocess when a timeout happens
        if p.returncode == -9 or p.returncode == 127:
            failure = file('/tmp/adb_failures', 'a')
            failure.write('%s, %s, %s\n' %
                          (datetime.datetime.now(), attempt, cmd))
            failure.flush()
            failure.close()
            continue

        stdout = ""
        outfile = open(f.name, 'r')
        for line in outfile:
            stdout += line
        outfile.close()
        os.unlink(f.name)
        return stdout
예제 #7
0
def _execute(process_arguments, workaround=False):
    """underlying execution method to call filebot as subprocess

    Handles the actual execution and output capture

    Args:
        process_arguments: list of the arguments to be passed to filebotCLI
        workaround: implements a work around for capturing unicode
            characters on windows systems. should always be true except in
            special circumstances.

    Returns:
        tuple in format '(exit_code, stdout, stderr)'
    """
    # open and close a temp file so filebot can use it as a log file.
    # this is a workaround for malfunctioning UTF-8 chars in Windows.
    file_temp = tempfile.NamedTemporaryFile(delete=False)
    file_temp.close()
    if workaround:
        process_arguments = (
            [FILEBOT_EXE, "--log-file", file_temp.name] + process_arguments)
    else:
        process_arguments = ([FILEBOT_EXE] + process_arguments)

    if os.name == "nt":  # used to hide cmd window popup
        startupinfo = killableprocess.winprocess.STARTUPINFO()
        startupinfo.dwFlags |= killableprocess.winprocess.STARTF_USESHOWWINDOW
    else:
        startupinfo = None

    try:
        process = killableprocess.Popen(
            process_arguments,
            stdout=killableprocess.subprocess.PIPE,
            stderr=killableprocess.subprocess.PIPE,
            stdin=killableprocess.subprocess.PIPE,
            startupinfo=startupinfo)
    except OSError as e:
        raise FilebotFatalError("Error running Filebot! {0}".format(str(e)))

    stdout, error = process.communicate()
    exit_code = process.returncode

    if workaround:
        with open(file_temp.name, 'rU') as log:
            data = log.read()  # read and cleanup temp/logfile
            log.close()
    else:
        data = stdout

    os.remove(file_temp.name)

    return exit_code, data, error
예제 #8
0
def run_command(cmd, env=None):
    """Run the given command in killable process."""
    if hasattr(sys.stdout, "fileno"):
        kwargs = {
            'stdout': sys.stdout,
            'stderr': sys.stderr,
            'stdin': sys.stdin
        }
    else:
        kwargs = {
            'stdout': sys.__stdout__,
            'stderr': sys.__stderr__,
            'stdin': sys.stdin
        }

    if sys.platform != "win32":
        return killableprocess.Popen(cmd,
                                     preexec_fn=lambda: os.setpgid(0, 0),
                                     env=env,
                                     **kwargs)
    else:
        return killableprocess.Popen(cmd, **kwargs)
예제 #9
0
    def __init__(self,
                 encoding,
                 external_id=None,
                 cmd_postfix="\n",
                 suppress_echo=False,
                 cmd=None,
                 env=None,
                 cwd=None,
                 extend_env=None,
                 soft_quit="",
                 autocomplete_server=False):
        super(SubprocessRepl, self).__init__(encoding, external_id,
                                             cmd_postfix, suppress_echo)
        settings = load_settings('SublimeREPL.sublime-settings')

        if cmd[0] == "[unsupported]":
            raise Unsupported(cmd[1:])

        self._autocomplete_server = None
        if autocomplete_server:
            self._autocomplete_server = AutocompleteServer(
                self, settings.get("autocomplete_server_ip"))
            self._autocomplete_server.start()

        env = self.env(env, extend_env, settings)
        env["SUBLIMEREPL_AC_PORT"] = str(self.autocomplete_server_port())
        env["SUBLIMEREPL_AC_IP"] = settings.get("autocomplete_server_ip")

        self._cmd = self.cmd(cmd, env)
        self._soft_quit = soft_quit
        self._killed = False
        self.popen = killableprocess.Popen(
            self._cmd,
            startupinfo=self.startupinfo(settings),
            creationflags=self.creationflags(settings),
            bufsize=1,
            cwd=self.cwd(cwd, settings),
            env=env,
            stderr=subprocess.STDOUT,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE)
예제 #10
0
    def start(self):
        """Start IE"""
        self.set_proxy()

        # allow_reg = wreg.OpenKey(wreg.HKEY_CURRENT_USER,
        #                          "Software\\Microsoft\\Internet Explorer\\New Windows\\Allow", 0, wreg.KEY_ALL_ACCESS)
        #
        # wreg.SetValueEx(allow_reg, urlparse(windmill.settings['TEST_URL']).hostname,
        #                 0, wreg.REG_BINARY, None)
        #Workaround for but in nose
        if hasattr(sys.stdout, 'fileno'):
            kwargs = {
                'stdout': sys.stdout,
                'stderr': sys.stderr,
                'stdin': sys.stdin
            }
        else:
            kwargs = {
                'stdout': sys.__stdout__,
                'stderr': sys.__stderr__,
                'stdin': sys.stdin
            }

        self.p_handle = killableprocess.Popen(self.cmd, **kwargs)
예제 #11
0
def runCommand(cmd, env=None, timeout=-1, logger=log, ignorepreexec=False):
    """
    Execute the given command and log all output

        Success and failure codes:

        >>> runCommand(['true'])
        0
        >>> runCommand(['false'])
        1

        Interleaved stdout and stderr messages:

        >>> runCommand(['python', '-c', r'print 1;import sys;sys.stdout.flush();print >>sys.stderr, 2;print 3'])
        1
        2
        3
        0

        Now with timeout:

        >>> runCommand(['python', '-c', r'print 1;import sys;sys.stdout.flush();print >>sys.stderr, 2;print 3'], timeout=5)
        1
        2
        3
        0

        Setting environment variable:

        >>> runCommand(['python', '-c', 'import os;print os.getenv("ENVTEST")'], env={'ENVTEST': '42'})
        42
        0

        Timeout:
        >>> runCommand(['sleep', '60'], timeout=5)
        -9
    """
    redirect = True

    if logger == log and _logFile is None:
        redirect = False
    else:
        if timeout == -1:
            output = subprocess.PIPE
        else:
            output = tempfile.TemporaryFile()

    if ignorepreexec:
        preexec_fn = None
    else:
        preexec_fn = setpgid_preexec_fn

    if redirect:
        p = killableprocess.Popen(cmd,
                                  env=env,
                                  stdin=subprocess.PIPE,
                                  stdout=output,
                                  stderr=subprocess.STDOUT,
                                  preexec_fn=preexec_fn)
    else:
        p = killableprocess.Popen(cmd,
                                  env=env,
                                  stdin=subprocess.PIPE,
                                  preexec_fn=preexec_fn)

    try:
        if timeout == -1 and redirect:
            for line in p.stdout:
                logger(line[:-1])

        p.wait(timeout=timeout, group=True)

    except KeyboardInterrupt:
        try:
            p.kill(group=True)

        except OSError:
            p.wait(30)

    if timeout != -1 and redirect:
        output.seek(0)
        for line in output:
            logger(line[:-1])

    return p.returncode
예제 #12
0
        """
        raise NotImplementedError

    def runCommand(self, filenames, async=False, timeout=10, *args):
        """Run the shell loading the files in filename and passing any extra 
        arguments in args. Returns a tuple of (success, stdout)"""
        commandline = self.getCommand(filenames, *args)
        if self.verbose:
            print " ".join(
                map(
                    lambda s: (' ' in s or '\n' in s) and '"' + re.sub(
                        "\\s+", " ", s) + '"' or s, commandline))
        try:
            proc = killableprocess.Popen(commandline,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT,
                                         env=os.environ,
                                         bufsize=-1)
        except:
            raise
            sys.stderr.write(
                "Command %s failed\n Did you specify the correct path the the jsshell?\n"
                % " ".join(commandline))
            sys.exit(1)
        if async:
            return proc
        else:
            res = proc.wait(timeout)
            return res, proc.stdout.read()

예제 #13
0
                "--error-exitcode=102"
            ] + commandline  # 102 is fairly random

        if use_temp_file:
            stdout = tempfile.TemporaryFile()
        else:
            stdout = subprocess.PIPE

        if self.verbose:
            print " ".join(
                map(
                    lambda s: (' ' in s or '\n' in s) and '"' + re.sub(
                        "\\s+", " ", s) + '"' or s, commandline))
        try:
            proc = killableprocess.Popen(commandline,
                                         stdout=stdout,
                                         stderr=subprocess.STDOUT,
                                         env=os.environ)
            if use_temp_file:
                proc.stdout = stdout
        except:
            raise
            sys.stderr.write(
                "Command %s failed\n Did you specify the correct path the the jsshell?\n"
                % " ".join(commandline))
            sys.exit(1)
        if async:
            return proc
        else:
            res = proc.wait(timeout)
            return res == 0, proc.stdout.read()