Пример #1
0
 def communicate(self):
     '''
     '''
     while True:
         if self._event.wait(0.001) == True or self.poll() == 0:
             if self._running: raise TimeoutError('Execute timeout')
             return self.stdout.read(), self.stderr.read()
Пример #2
0
    def call(self, cmd, *args, **kwds):
        '''调用命令字
        '''
        cmd = cmd.replace('-', '_')
        if cmd == 'forward' and args[1] == '--remove':
            method = getattr(self, 'remove_forward')
            args = list(args)
            args.pop(1)  # remove --remove args
        else:
            method = getattr(self, cmd)
        # print (args)
        sync = True
        if 'sync' in kwds: sync = kwds.pop('sync')
        if 'timeout' in kwds and not cmd in ('shell', 'install', 'uninstall',
                                             'wait_for_device', 'reboot'):
            kwds.pop('timeout')
        if sync:
            ret = None
            retry_count = kwds.pop('retry_count')
            i = 0
            socket_error_count = 0
            while i < retry_count:
                try:
                    self._lock.acquire()
                    ret = method(*args, **kwds)
                    break
                except socket.error as e:
                    logger.exception(u'执行%s %s error' % (cmd, ' '.join(args)))
                    socket_error_count += 1
                    if socket_error_count <= 10: i -= 1
                    time.sleep(1)
                except AdbError as e:
                    err_msg = str(e)
                    if 'device not found' in err_msg:
                        return '', 'error: device not found'
                    elif 'cannot bind to socket' in err_msg:
                        return '', err_msg
                    elif 'cannot remove listener' in err_msg:
                        return '', err_msg
                    elif 'device offline' in err_msg:
                        return '', 'error: device offline'
                    elif 'Bad response' in err_msg or 'Device or resource busy' in err_msg or 'closed' in err_msg:  # wetest设备有时候会返回closed错误
                        # 需要重试
                        logger.exception('Run %s%s %r' %
                                         (cmd, ' '.join(args), e))
                    else:
                        raise RuntimeError(u'执行%s %s 命令失败:%s' %
                                           (cmd, ' '.join(args), e))
                    time.sleep(1)
                    if i >= retry_count - 1: raise e
                except RuntimeError as e:
                    logger.exception(u'执行%s%s %r' % (cmd, ' '.join(args), e))
                    if 'device not found' in str(e):
                        self.wait_for_device(args[0],
                                             retry_count=1,
                                             timeout=300)
                        self._sock = None
                        return self.call(cmd, *args, **kwds)
                finally:
                    i += 1
                    if self._sock != None:
                        self._sock.close()
                        self._sock = None
                    self._lock.release()

            if ret == None:
                raise TimeoutError(u'Run cmd %s %s failed' %
                                   (cmd, ' '.join(args)))

            if isinstance(ret, (six.string_types, six.binary_type)):
                return ret, ''
            else:
                return ret
        else:
            self._transport(args[0])  # 异步操作的必然需要发送序列号
            if cmd == 'shell':
                self._lock.acquire()
                self._send_command('shell:' + ' '.join(args[1:]))
                pipe = ADBPopen(self._sock)
                self._sock = None
                self._lock.release()
                return pipe
Пример #3
0
    def call(self, cmd, *args, **kwds):
        """调用命令字
        """
        cmd = cmd.replace("-", "_")
        if cmd == "forward" and args[1] == "--remove":
            method = getattr(self, "remove_forward")
            args = list(args)
            args.pop(1)  # remove --remove args
        else:
            method = getattr(self, cmd)
        # print (args)
        sync = True
        if "sync" in kwds:
            sync = kwds.pop("sync")
        if "timeout" in kwds and not cmd in (
                "shell",
                "install",
                "uninstall",
                "wait_for_device",
                "reboot",
        ):
            kwds.pop("timeout")
        if sync:
            ret = None
            retry_count = kwds.pop("retry_count")
            i = 0
            socket_error_count = 0
            while i < retry_count:
                try:
                    self._lock.acquire()
                    ret = method(*args, **kwds)
                    break
                except socket.error as e:
                    logger.exception("执行%s %s error" % (cmd, " ".join(args)))
                    socket_error_count += 1
                    if socket_error_count <= 10:
                        i -= 1
                    time.sleep(1)
                except AdbError as e:
                    err_msg = str(e)
                    if "device not found" in err_msg:
                        return "", "error: device not found"
                    elif "cannot bind to socket" in err_msg:
                        return "", err_msg
                    elif "cannot remove listener" in err_msg:
                        return "", err_msg
                    elif "device offline" in err_msg:
                        return "", "error: device offline"
                    elif "Permission denied" in err_msg:
                        return "", "error: %s" % err_msg
                    elif ("Bad response" in err_msg
                          or "Device or resource busy" in err_msg
                          or "closed" in err_msg):  # wetest设备有时候会返回closed错误
                        # 需要重试
                        logger.exception("Run %s%s %r" %
                                         (cmd, " ".join(args), e))
                    else:
                        raise RuntimeError("执行%s %s 命令失败:%s" %
                                           (cmd, " ".join(args), e))
                    time.sleep(1)
                    if i >= retry_count - 1:
                        raise e
                except RuntimeError as e:
                    logger.exception("执行%s%s %r" % (cmd, " ".join(args), e))
                    if "device not found" in str(e):
                        self.wait_for_device(args[0],
                                             retry_count=1,
                                             timeout=300)
                        self._sock = None
                        return self.call(cmd, *args, **kwds)
                finally:
                    i += 1
                    if self._sock != None:
                        self._sock.close()
                        self._sock = None
                    self._lock.release()

            if ret == None:
                raise TimeoutError("Run cmd %s %s failed" %
                                   (cmd, " ".join(args)))

            if isinstance(ret, (six.string_types, six.binary_type)):
                return ret, ""
            else:
                return ret
        else:
            self._transport(args[0])  # 异步操作的必然需要发送序列号
            if cmd == "shell":
                self._lock.acquire()
                self._send_command("shell:" + " ".join(args[1:]))
                pipe = ADBPopen(self._sock)
                self._sock = None
                self._lock.release()
                return pipe