def getAVDs(): """ 获取当前的虚拟设备 :returns: 返回当前拥有的虚拟设备列表 :rtype: list """ re_avd = re.compile('^[ ]+Name: ([a-zA-Z0-9_:.-]+)', re.MULTILINE) avds = re_avd.findall(runcommand.runCommand("android list avd")) return avds
def isSuSupport(self): """判断手机是否已经root了并且支持 ``su`` :returns: 如果支持,返回 ``True`` , 否则返回 ``False`` :rtype: bool """ adb_cmd = "adb %s shell su -c ps" % self._target_arg result = runcommand.runCommand(adb_cmd) failure_string_list = ["su: not found", "connect ui: Timer expired"] for failure_string in failure_string_list: if failure_string in result: return False return True
def getApkInfo(apk_path): r""" 获取APK的信息(apk_size, versioncode, versionname, packagename, launchActivity), 其中APK的大小以MB为单位 :param apk_path: apk包所在的路径 :type apk_path: string :returns: 返回APK信息,格式如下: {'versioncode': 'xxx', 'launchActivity': 'xxx', 'packagename': 'xxx', 'versionname': 'xxx', 'app_size': 'xxx'} :rtype: dict """ system = platform.system() tools_location = fileutils.getUCToolsLocation() aapt_path = fileutils.createPath([tools_location, "uc_tools","resource","aapt"]) if system.upper() == "Windows".upper(): aapt_path = fileutils.createPath([aapt_path, "aapt_windows.exe"]) else: aapt_path = fileutils.createPath([aapt_path, "aapt_linux"]) apk_info_list = {} cmd_aapt = aapt_path + " d badging "+ apk_path # cmd_aapt = r"F:\git\qmsinterceptor\resource\aapt\aapt_windows d badging "+ apk_path out = runcommand.runCommand(cmd_aapt) rs = out pkg = re.match('package: ' ,rs) apk_info = pkg.string.split('\n') reg = re.compile(r"'.+'") package = reg.findall(apk_info[0].split()[1])[0].strip('\'') versioncode = reg.findall(apk_info[0].split()[2])[0].strip('\'') versionname = reg.findall(apk_info[0].split()[3])[0].strip('\'') for i in range(len(apk_info)): if ("launchable-activity" in apk_info[i]): launchActivity = reg.findall(apk_info[i].split()[1])[0].strip('\'') apk_path = apk_path.replace("\\(","(").replace("\\)",")") app_size = os.path.getsize(apk_path) app_size = str(app_size/(1024.0*1000)) apk_info_list["apk_size"] = app_size apk_info_list["versioncode"] = versioncode apk_info_list["versionname"] = versionname apk_info_list["packagename"] = package apk_info_list["launchActivity"] = launchActivity return apk_info_list
def getAttachedDevices(hardware=True, emulator=False, offline=False): """ 获取当前PC上连接着的设备。 :param hardware: 返回的设备中是否包含真实的设备,默认为 ``True`` :type hardware: bool :param emulator: 返回的设备中是否包含模拟器,默认为 ``False`` :type emulator: bool :param offline: 返回的设备中是否包含掉线的设备,默认为 ``False`` :type offline: bool :returns: 根据设置返回设备列表 :rtype: list """ adb_devices_output = runcommand.runCommand("adb devices") if platform.system().upper() == "Windows".upper(): adb_devices_output = adb_devices_output.replace("\r\n", "\n") re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE) online_devices = re_device.findall(adb_devices_output) re_device = re.compile('^(emulator-[0-9]+)\tdevice', re.MULTILINE) emulator_devices = re_device.findall(adb_devices_output) re_device = re.compile('^([a-zA-Z0-9_:.-]+)\toffline$', re.MULTILINE) offline_devices = re_device.findall(adb_devices_output) devices = [] # First determine list of online devices (e.g. hardware and/or emulator). if hardware and emulator: devices = online_devices elif hardware: devices = [device for device in online_devices if device not in emulator_devices] elif emulator: devices = emulator_devices # Now add offline devices if offline is true if offline: devices = devices + offline_devices preferred_device = os.environ.get('ANDROID_SERIAL') if preferred_device in devices: devices.remove(preferred_device) devices.insert(0, preferred_device) return devices
def isSingleQuoteSupport(self): """判断手机执行adb命令的时候是否需要给命令加上单引号 ``'`` :returns: 如果支持,返回 ``True`` , 否则返回 ``False`` :rtype: bool """ su_arg = "" is_su_support = self.isSuSupport() if is_su_support: su_arg = "su -c" #setSuString adb_cmd = "adb %s shell \"%s 'ls /system'\"" % (self._target_arg, su_arg) result = runcommand.runCommand(adb_cmd) error_string_array = ["sdcard", "No such file or directory", "not found"] for error_string in error_string_array: if error_string in result: return False return True
def sendCommand(self, command_string, timeout_time=20, retry_count=3, block=True): """ 通过adb发送一条命令给安卓设备执行 :param command_string: 需要手机执行的adb命令 :type serial: string :param timeout_time: 超时时间,当命令执行超过该限定时间时将会被kill掉。默认为 ``20`` ,表示超时时间为20s :type timeout_time: int :param retry_count: 重试次数,当任务执行超时或者失败,将会按照重试次数重新执行,默认为3 :type retry_count: int :param block: 如果值为 ``True`` ,则该命令执行为阻塞式 :type block: bool :returns: 以 ``string`` 类型返回命令执行结果 :raises errors.WaitForResponseTimedOutError: 当命令多次重试仍然超时没有响应的时候跑出该异常 """ adb_cmd = "adb %s %s" % (self._target_arg, command_string) return runcommand.runCommand(adb_cmd, timeout_time=timeout_time, retry_count=retry_count,block=block)
def startAdbServer(): """ 启动 adb server """ runcommand.runCommand("adb start-server")
def killAdbServer(): """ 关闭adb server """ runcommand.runCommand("adb kill-server")