Repository navigation
Expand file tree
/
Copy pathMultiDevicesADB.py
More file actions
140 lines (127 loc) · 5.58 KB
/
Copy pathMultiDevicesADB.py
File metadata and controls
140 lines (127 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import subprocess
import sys
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
# from termcolor import colored, cprint
# import shlex
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
adb = os.environ['ANDROID_HOME'] + '/platform-tools/adb'
FAILURE_STATUS = ['offline', 'unauthorized']
NO_MULTI_COMMAND = ['help', 'version', 'connect', 'disconnect', 'pair', 'start-server', 'kill-server', 'mdns']
def format(fg=None, bg=None, bright=False, bold=False, dim=False, reset=False):
# Manually derived from http://en.wikipedia.org/wiki/ANSI_escape_code#Codes
codes = []
if reset:
codes.append("0")
else:
if fg is not None: codes.append("3%d" % (fg))
# if not fg is None: codes.append("38;5;100")
if bg is not None:
if not bright:
codes.append("4%d" % (bg))
else:
codes.append("10%d" % (bg))
if bold:
codes.append("1")
elif dim:
codes.append("2")
else:
codes.append("22")
return "\033[%sm" % (";".join(codes))
def multi_cmd(run_cmd):
# adb get serial no
devices = os.popen(adb + " devices | sed '1,1d' | sed '$d' | cut -f 1").read().splitlines()
# print(devices)
for device in devices[:]:
if device.startswith('*') and device.endswith('*'):
print(device)
devices.remove(device)
# adb get-state
devices_status = os.popen(adb + " devices | sed '1,1d' | sed '$d' | cut -f 2").read().splitlines()
# print(devices)
# print(devices_status)
devices_number = len(devices);
if devices_number == 0:
print('No emulators or devices detected - nothing to do.')
elif devices_number == 1 and run_cmd:
subprocess.call(cmd)
else:
devices_model_name = []
devices_versions = []
fornum = 0
for device in devices:
if FAILURE_STATUS.count(devices_status[fornum]) == 0:
# model_name_cmd = adb + ' devices | grep ' + device + r' | cut -f 1 | xargs -I $ ' + adb + ' -s $ shell cat /system/build.prop | grep "ro.product.model" | cut -d "=" -f 2 | tr -d ' + r'" \r\t\n"'
model_name_cmd = adb + ' devices | grep ' + device + r' | cut -f 1 | xargs -I $ ' + adb + ' -s $ shell getprop ro.product.model'
# platform_versions_cmd = adb + ' devices | grep ' + device + r' | cut -f 1 | xargs -I $ ' + adb + ' -s $ shell cat /system/build.prop | grep "ro.build.version.release" | cut -d "=" -f 2 | tr -d ' + r'"\r\t\n"'
platform_versions_cmd = adb + ' devices | grep ' + device + r' | cut -f 1 | xargs -I $ ' + adb + ' -s $ shell getprop ro.build.version.release'
devices_model_name.append(os.popen(model_name_cmd).read())
devices_versions.append(os.popen(platform_versions_cmd).read())
else:
devices_model_name.append(devices_status[fornum])
devices_versions.append(devices_status[fornum])
fornum += 1
# print(devices_model_name)
# print(devices_versions)
if run_cmd:
print('Multiple devices detected, please select one')
else:
print('List of devices attached')
num = 1
for device in devices:
linebuf = StringIO()
linebuf.write(
"%s%s%s " % (format(fg=BLACK, bg=BLACK, bright=True), str(num).strip().center(3), format(reset=True)))
linebuf.write(
"%s%s%s " % (format(fg=YELLOW, bright=True), device.strip()[:20].ljust(20), format(reset=True)))
linebuf.write("%s%s%s " % (
format(fg=CYAN, bright=True), devices_versions[num - 1].strip().center(16), format(reset=True)))
linebuf.write("%s%s%s " % (
format(fg=MAGENTA, bright=True), devices_model_name[num - 1].strip().ljust(20), format(reset=True)))
line = linebuf.getvalue()
print(line)
# print(str(num)+": " + device + "\t\t" + devices_versions[num-1] + "\t\t"+ devices_model_name[num-1])
num += 1
sys.stdout.flush()
if run_cmd:
try:
user_input = raw_input("> ")
except NameError:
user_input = input("> ")
# print(user_input)
if user_input.isdigit() and 0 < int(user_input) <= len(devices):
print("Executing following command:")
linebuf = StringIO()
cmd = adb + " -s " + devices[int(user_input) - 1] + ' ' + ' '.join(sys.argv[1:])
# print(cmd + "\n")
# linebuf.write(adb)
linebuf.write("%s%s%s " % (format(fg=BLACK, bg=BLACK, bright=True), adb, format(reset=True)))
linebuf.write("%s%s%s " % (format(fg=YELLOW), "-s " + devices[int(user_input) - 1], format(reset=True)))
linebuf.write("%s%s%s " % (format(fg=GREEN), ' '.join(sys.argv[1:]), format(reset=True)))
line = linebuf.getvalue()
print(line)
print("")
subprocess.call(cmd.split())
else:
print('You must enter a valid number')
cmd = [adb] + sys.argv[1:]
try:
first_args = sys.argv[1]
if first_args == 'devices':
multi_cmd(False)
else:
if NO_MULTI_COMMAND.count(first_args) == 1 or first_args == '-s':
subprocess.call(cmd)
else:
multi_cmd(True)
except KeyboardInterrupt:
pass
except:
# multi_cmd(False)
try:
subprocess.call(cmd)
except KeyboardInterrupt:
pass