#!/usr/bin/env python# -*- coding: utf-8 -*-'''Created on 2015年1月23日@author: xuxu'''import osimport platformimport reimport subprocessimport timeimport Tkinter as tkimport ttkimport exceptionserialno_num = ""# 判断系统类型,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"])def get_screen_size(window):return window.winfo_screenwidth(),window.winfo_screenheight()def get_window_size(window):return window.winfo_reqwidth(),window.winfo_reqheight()def center_window(root, width, height):screenwidth = root.winfo_screenwidth()screenheight = root.winfo_screenheight()size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)root.geometry(size)class Window(object):device_id = ""device_id_list = []device_name_list = []device_name_dict = {}root = Nonebox = Nonedef __init__(self, device_id_list, root):self.device_id_list = device_id_listself.device_name_dict = get_device_name_dict(self.device_id_list)self.get_device_name_list()self.device_id = device_id_list[0]self.root = rootself.box = Nonedef show_window(self):self.root.title(u'Serialno Number')center_window(self.root, 300, 240)self.root.maxsize(600, 400)self.root.minsize(300, 240)# options = self.device_id_listoptions = self.device_name_listself.box = ttk.Combobox(values=options)self.box.current(0)self.box.pack(expand = tk.YES)self.box.bind("<<ComboboxSelected>>", self.select)ttk.Button(text=u"确定", command=self.ok).pack(expand = tk.YES)self.root.mainloop()def select(self, event=None):for key, value in self.device_name_dict.iteritems():if value == self.box.selection_get():self.device_id = key# self.device_id = self.box.selection_get()def ok(self):global serialno_numserialno_num = self.device_idself.root.destroy()def get_device_name_list(self):for id in self.device_id_list:self.device_name_list.append(self.device_name_dict.get(id))# adb命令def adb(args):global serialno_numif serialno_num == "":devices = get_device_list()if len(devices) == 1:# global serialno_numserialno_num = devices[0]else:root = tk.Tk()window = Window(devices, root)window.show_window()cmd = "%s -s %s %s" %(command, serialno_num, str(args))return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)#adb shell命令def shell(args):global serialno_numif serialno_num == "":devices = get_device_list()if len(devices) == 1:serialno_num = devices[0]else:root = tk.Tk()window = Window(devices, root)window.show_window()cmd = "%s -s %s shell %s" %(command, serialno_num, str(args))return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)#获取设备状态def get_state():return os.popen("adb -s %s get-state" %serialno_num).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\.]+")tmp = shell("dumpsys activity | %s mFocusedActivity" %find_util).stdout.read()name = ""try:name = pattern.findall(tmp)[0]except:tmp = shell("dumpsys window w | %s \/ | %s name=" %(find_util, find_util)).stdout.read()name = pattern.findall(tmp)[0]return name# 获取当前应用的包名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()))def get_device_list():devices = []result = subprocess.Popen("adb devices", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readlines()result.reverse()for line in result[1:]:if "attached" not in line.strip():devices.append(line.split()[0])else:breakreturn devicesdef get_device_name_dict(devices):device_dict = {}if not devices:returnfor device in devices:cmd = "adb -s %s shell getprop ro.product.model" %devicedevice_name = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.readline().strip()device_dict[device] = device_namereturn device_dict# 连接设备# adb("kill-server").wait()# adb("start-server").wait()adb("wait-for-device").wait()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__":print get_focused_package_and_activity()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. Open source ecosystem
2. Collaboration, People, Software
3. Evaluation model