Esempio n. 1
0
 def kill_by_name(self, name):
     if magic.is_windows():
         cmd = "TASKKILL /F /IM %s " % name
     else:
         cmd = "killall %s" % name
     logger.info(cmd)
     self._run(cmd)
Esempio n. 2
0
def _run_cmd(cmd, timeout_sec, sync=True):
    """
    不支持重定向命令, 如: echo aa >> output
    """
    def _cb(p):
        logger.error("timeout %s", cmd)
        p.kill()

    if not magic.is_windows():
        if isinstance(cmd, basestring):
            args = re.split(r"\s+", cmd)
        elif isinstance(cmd, list):
            args = cmd
        else:
            raise TypeError(u"cmd should be string or list")
        proc = subprocess.Popen(args,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    else:
        proc = subprocess.Popen(cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                shell=True)
    if not sync:
        return proc
    timer = threading.Timer(timeout_sec, _cb, [proc])
    try:
        timer.start()
        return proc.communicate()
    finally:
        timer.cancel()
Esempio n. 3
0
    def _run_app_server(self, cmd):
        logger.info(cmd)

        if magic.is_windows():
            process = subprocess.Popen(cmd,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=True)
        else:
            args = re.split(r"\s+", cmd)
            process = subprocess.Popen(args,
                                       close_fds=True,
                                       stdin=subprocess.PIPE,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=False)
        q = Queue()
        t = threading.Thread(target=enqueue_output, args=(process.stdout, q))
        t.daemon = True  # thread dies with the program
        t.start()
        t = threading.Thread(target=enqueue_output, args=(process.stderr, q))
        t.daemon = True  # thread dies with the program
        t.start()
        while not process.poll():
            line = None
            try:
                line = q.get_nowait()
                if magic.is_windows():
                    line = line.decode("gbk")
                else:
                    line = line.decode("utf-8")
            except Empty:
                time.sleep(0.5)
            if line is not None and len(
                    line.strip()) > 0 and "CDS" not in line:
                if "test=%s" % config.STUB_CASE_NAME in line:
                    # 检查到用例真正运行才算启动成功
                    self._cmd_process = process
                    self.set_app_server_run(True)
                self.app_outputs.append(line)
                if "close [socket]" not in line:
                    logger.debug("java print:" + line.strip())
        self.set_app_server_run(False)
        logger.info("subprocess stopped, ret code:" + str(process.returncode))
        self._server_thread = None
Esempio n. 4
0
 def kill_pid(self, pid):
     if not self.is_pid_running(pid):
         return
     if magic.is_windows():
         cmd = "taskkill /F /PID %d" % pid
     else:
         cmd = "kill -9 %d" % pid
     self._run(cmd)
Esempio n. 5
0
 def is_pid_running(self, pid):
     if magic.is_windows():
         cmd = "tasklist /FI \"PID eq %d\"" % pid
         output = self._run(cmd)
         if str(pid) in output:
             return True
         else:
             return False
     else:
         try:
             os.kill(pid, 0)
         except OSError:
             return False
         else:
             return True
Esempio n. 6
0
    def _run(self, cmd, sync=True, timeout_sec=60):
        logger.debug(cmd)
        if not sync:
            return _run_cmd(cmd, timeout_sec, sync)
        output, error = _run_cmd(cmd, timeout_sec, sync)
        try:
            if magic.is_windows():
                output = output.decode("gbk")
                error = error.decode("gbk")
            else:
                output = output.decode("utf-8")
                error = error.decode("utf-8")
        except UnicodeDecodeError:
            pass

        self._last_output = output
        self._error_msg = error
        for error_tag in self.error_tags:
            if (error and error_tag in error) or error_tag in output:
                self.set_connected_state(False)
        if error:
            logger.warning(error)

        return output