1111import re
1212import subprocess
1313import time
14+ import Tkinter as tk
15+ import ttk
1416
1517import exception
1618
19+ serialno_num = ""
20+
1721#判断系统类型,windows使用findstr,linux使用grep
1822system = platform .system ()
1923if system is "Windows" :
2024 find_util = "findstr"
2125else :
2226 find_util = "grep"
23-
27+
2428#判断是否设置环境变量ANDROID_HOME
2529if "ANDROID_HOME" in os .environ :
2630 if system == "Windows" :
3034else :
3135 raise EnvironmentError (
3236 "Adb not found in $ANDROID_HOME path: %s." % os .environ ["ANDROID_HOME" ])
33-
37+
38+
39+ def get_screen_size (window ):
40+ return window .winfo_screenwidth (),window .winfo_screenheight ()
41+
42+ def get_window_size (window ):
43+ return window .winfo_reqwidth (),window .winfo_reqheight ()
44+
45+ def center_window (root , width , height ):
46+ screenwidth = root .winfo_screenwidth ()
47+ screenheight = root .winfo_screenheight ()
48+ size = '%dx%d+%d+%d' % (width , height , (screenwidth - width )/ 2 , (screenheight - height )/ 2 )
49+ root .geometry (size )
50+
51+ class Window (object ):
52+ device_id = ""
53+ device_id_list = []
54+ root = None
55+ box = None
56+ def __init__ (self , device_id_list , root ):
57+ self .device_id_list = device_id_list
58+ self .device_id = device_id_list [0 ]
59+ self .root = root
60+ self .box = None
61+
62+ def show_window (self ):
63+ self .root .title (u'Serialno Number' )
64+ center_window (self .root , 300 , 240 )
65+ self .root .maxsize (600 , 400 )
66+ self .root .minsize (300 , 240 )
67+
68+ options = self .device_id_list
69+ self .box = ttk .Combobox (values = options )
70+ self .box .current (0 )
71+ self .box .pack (expand = tk .YES )
72+ self .box .bind ("<<ComboboxSelected>>" , self .select )
73+ ttk .Button (text = u"确定" , command = self .ok ).pack (expand = tk .YES )
74+
75+ self .root .mainloop ()
76+
77+
78+ def select (self , event = None ):
79+ self .device_id = self .box .selection_get ()
80+
81+ def ok (self ):
82+ global serialno_num
83+ serialno_num = self .device_id
84+ self .root .destroy ()
85+
3486#adb命令
3587def adb (args ):
36- cmd = "%s %s" % (command , str (args ))
88+ global serialno_num
89+ if serialno_num == "" :
90+ devices = get_device_list ()
91+ if len (devices ) == 1 :
92+ #global serialno_num
93+ serialno_num = devices [0 ]
94+ else :
95+ root = tk .Tk ()
96+ window = Window (devices , root )
97+ window .show_window ()
98+ cmd = "%s -s %s %s" % (command , serialno_num , str (args ))
3799 return subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
38100
39101#adb shell命令
40102def shell (args ):
41- cmd = "%s shell %s" % (command , str (args ))
103+ global serialno_num
104+ if serialno_num == "" :
105+ devices = get_device_list ()
106+ if len (devices ) == 1 :
107+ serialno_num = devices [0 ]
108+ else :
109+ root = tk .Tk ()
110+ window = Window (devices , root )
111+ window .show_window ()
112+ cmd = "%s -s %s shell %s" % (command , serialno_num , str (args ))
42113 return subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
43114
44115#获取设备状态
45116def get_state ():
46- return os .popen ("adb get-state" ).read ().strip ()
117+ return os .popen ("adb -s %s get-state" % serialno_num ).read ().strip ()
47118
48119#获取对应包名的pid
49- def get_app_pid (pkg_name ):
120+ def get_app_pid (pkg_name ):
50121 if system is "Windows" :
51122 string = shell ("ps | findstr %s$" % pkg_name ).stdout .read ()
52- else :
53- string = shell ("ps | grep -w %s" % pkg_name ).stdout .read ()
123+
124+ string = shell ("ps | grep -w %s" % pkg_name ).stdout .read ()
54125
55126 if string == '' :
56127 return "the process doesn't exist."
@@ -64,13 +135,13 @@ def get_app_pid(pkg_name):
64135#杀掉对应包名的进程
65136def kill_process (pkg_name ):
66137 pid = get_app_pid (pkg_name )
67-
138+
68139 result = shell ("kill %s" % str (pid )).stdout .read ().split (": " )[- 1 ]
69-
140+
70141 if result != "" :
71142 raise exception .SriptException ("Operation not permitted or No such process" )
72143
73- #获取设备上当前应用的包名与activity
144+ #获取设备上当前应用的包名与activity
74145def get_focused_package_and_activity ():
75146 pattern = re .compile (r"[a-zA-Z0-9\.]+/.[a-zA-Z0-9\.]+" )
76147 out = shell ("dumpsys window w | %s \/ | %s name=" % (find_util , find_util )).stdout .read ()
@@ -89,6 +160,18 @@ def get_current_activity():
89160def timestamp ():
90161 return time .strftime ('%Y-%m-%d-%H-%M-%S' ,time .localtime (time .time ()))
91162
163+ def get_device_list ():
164+ devices = []
165+ result = subprocess .Popen ("adb devices" , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE ).stdout .readlines ()
166+ result .reverse ()
167+ for line in result [1 :]:
168+ if "attached" not in line .strip ():
169+ devices .append (line .split ()[0 ])
170+ else :
171+ break
172+ return devices
173+
174+
92175#连接设备
93176# adb("kill-server").wait()
94177# adb("start-server").wait()
@@ -97,10 +180,10 @@ def timestamp():
97180if get_state () != "device" :
98181 adb ("kill-server" ).wait ()
99182 adb ("start-server" ).wait ()
100-
183+
101184if get_state () != "device" :
102185 raise exception .SriptException ("Device not run" )
103-
186+
104187
105188if __name__ == "__main__" :
106189 pass
0 commit comments