-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathautomation.py
135 lines (100 loc) · 3.6 KB
/
automation.py
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
#!/usr/local/bin/python -u
# Script (written in Python 2.7.15) to automate the configurations to show the screenshot on your
# default web browser.
# To get started, simply run : 'python ./automation.py'
import subprocess
import webbrowser
import argparse
from threading import Timer
adb = ['adb']
parser = argparse.ArgumentParser(
description='Automation script to activate capturing screenshot of Android device')
parser.add_argument('-s', '--serial', dest='device_serial',
help='Device serial number (adb -s option)')
parser.add_argument(
'-p',
'--port',
dest='port',
nargs='?',
const=53516,
type=int,
default=53516,
help='Port number to be connected, by default it\'s 53516')
args_in = parser.parse_args()
def run_adb(args, pipeOutput=True):
if(args_in.device_serial):
args = adb + ['-s', args_in.device_serial] + args
else:
args = adb + args
# print('exec cmd : %s' % args)
out = None
if (pipeOutput):
out = subprocess.PIPE
p = subprocess.Popen([str(arg)
for arg in args], stdout=out)
stdout, stderr = p.communicate()
return (p.returncode, stdout, stderr)
def locate_apk_path():
(rc, out, _) = run_adb(["shell", "pm",
"path",
"com.rayworks.droidcast"])
if rc or out == "":
raise RuntimeError(
"Locating apk failure, have you installed the app successfully?")
prefix = "package:"
postfix = ".apk"
beg = out.index(prefix, 0)
end = out.rfind(postfix)
return "CLASSPATH=" + out[beg + len(prefix):(end + len(postfix))].strip()
def open_browser():
url = 'http://localhost:%d/screenshot' % args_in.port
webbrowser.open_new(url)
def identify_device():
(rc, out, _) = run_adb(["devices"])
if(rc):
raise RuntimeError("Fail to find devices")
else:
# Output as following:
# List of devices attached
# 6466eb0c device
print out
device_serial_no = args_in.device_serial
devicesInfo = str(out)
deviceCnt = devicesInfo.count('device') - 1
if deviceCnt < 1:
raise RuntimeError("Fail to find devices")
if(deviceCnt > 1 and (not device_serial_no)):
raise RuntimeError(
"Please specify the serial number of target device you want to use ('-s serial_number').")
def print_url():
# ip route:
# e.g. 192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.125
(rc, out, _) = run_adb(
["shell", "ip route | awk '/wlan*/{ print $9 }'| tr -d '\n'"])
print "\n>>> Share the url 'http://{0}:{1}/screenshot' to see the live screen! <<<\n".format(str(out), args_in.port)
def automate():
try:
identify_device()
class_path = locate_apk_path()
(code, _, err) = run_adb(
["forward", "tcp:%d" % args_in.port, "tcp:%d" % args_in.port])
print(">>> adb forward tcp:%d " % args_in.port, code)
print_url()
args = ["shell",
class_path,
"app_process",
"/", # unused
"com.rayworks.droidcast.Main",
"--port=%d" % args_in.port]
# delay opening the web page
t = Timer(2, open_browser)
t.start()
# event loop starts
run_adb(args, pipeOutput=False)
(code, out, err) = run_adb(
["forward", "--remove", "tcp:%d" % args_in.port])
print(">>> adb unforward tcp:%d " % args_in.port, code)
except (Exception) as e:
print e
if __name__ == "__main__":
automate()