forked from Dylan-Zheng/Rise-of-Kingdoms-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adb.py
61 lines (43 loc) · 2 KB
/
adb.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
from ppadb.client import Client as PPADBClient
from utils import resource_path
from utils import build_command
from filepath.file_relative_paths import FilePaths
import subprocess
import traceback
bridge = None
class Adb:
def __init__(self, host='127.0.0.1', port=5037):
self.client = PPADBClient(host, port)
def connect_to_device(self, host='127.0.0.1', port=5555):
adb_path = resource_path(FilePaths.ADB_EXE_PATH.value)
cmd = build_command(adb_path, 'connect', "{}:{}".format(host, port))
ret = subprocess.check_output(cmd, shell=True, stderr=subprocess.PIPE, encoding="utf-8", timeout=2)
return self.get_device(host, port)
def get_client_devices(self):
return self.client.devices()
def get_device(self, host='127.0.0.1', port=5555):
device = self.client.device('{}:{}'.format(host, port))
try:
if device is None:
self.connect_to_device(host, port)
device = self.client.device('{}:{}'.format(host, port))
except Exception as e:
traceback.print_exc()
return None
return device
def enable_adb(host='127.0.0.1', port=5037):
adb = None
try:
adb = Adb(host, port)
version = adb.client.version()
if version != 41:
raise RuntimeError('Error: require adb version 41, but version is {}'.format(version))
except RuntimeError as err:
adb_path = resource_path(FilePaths.ADB_EXE_PATH.value)
ret = subprocess.run(build_command(adb_path, '-P', str(port), 'kill-server', host), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
ret = subprocess.run(build_command(adb_path, '-P', str(port), 'connect', host), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8")
if ret.returncode != 0:
raise RuntimeError('Error: fail to start adb server. \n({})'.format(ret))
return adb