#!/usr/bin/env python# -*- coding: utf-8 -*-'''Created on 2015年1月23日@author: xuxu'''import osimport platformimport reimport subprocessimport timeimport exception#判断系统类型,windows使用findstr,linux使用grepsystem = platform.system()if system is "Windows":find_util = "findstr"else:find_util = "grep"#判断是否设置环境变量ANDROID_HOMEif "ANDROID_HOME" in os.environ:if system == "Windows":command = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb.exe")else:command = os.path.join(os.environ["ANDROID_HOME"], "platform-tools", "adb")else:raise EnvironmentError("Adb not found in $ANDROID_HOME path: %s." %os.environ["ANDROID_HOME"])#adb命令def adb(args):cmd = "%s %s" %(command, str(args))return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)#adb shell命令def shell(args):cmd = "%s shell %s" %(command, str(args))return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)#获取设备状态def get_state():return os.popen("adb get-state").read().strip()#获取对应包名的piddef get_app_pid(pkg_name):if system is "Windows":string = shell("ps | findstr %s$" %pkg_name).stdout.read()string = shell("ps | grep -w %s" %pkg_name).stdout.read()if string == '':return "the process doesn't exist."pattern = re.compile(r"\d+")result = string.split(" ")result.remove(result[0])return pattern.findall(" ".join(result))[0]#杀掉对应包名的进程def kill_process(pkg_name):pid = get_app_pid(pkg_name)result = shell("kill %s" %str(pid)).stdout.read().split(": ")[-1]if result != "":raise exception.SriptException("Operation not permitted or No such process")#获取设备上当前应用的包名与activitydef get_focused_package_and_activity():pattern = re.compile(r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+")out = shell("dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).stdout.read()return pattern.findall(out)[0]#获取当前应用的包名def get_current_package_name():return get_focused_package_and_activity().split("/")[0]#获取当前设备的activitydef get_current_activity():return get_focused_package_and_activity().split("/")[-1]#时间戳def timestamp():return time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))#连接设备# adb("kill-server").wait()# adb("start-server").wait()adb("wait-for-device")if get_state() != "device":adb("kill-server").wait()adb("start-server").wait()if get_state() != "device":raise exception.SriptException("Device not run")if __name__ == "__main__":pass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。