-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb_helper.py
67 lines (62 loc) · 1.95 KB
/
adb_helper.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
from adb_shell.adb_device import AdbDeviceTcp, AdbDeviceUsb
from adb_shell.auth.sign_pythonrsa import PythonRSASigner
class ADB():
def __init__(self, root: str):
"""
Args:
root:
Root path of adbkey, usually is "/home/xxx/./android"
"""
with open(root + "adbkey.pub", "r") as f:
pub = f.read()
with open(root + "/adbkey", "r") as f:
priv = f.read()
self.device = AdbDeviceUsb()
self.signer = PythonRSASigner(pub, priv)
def push(self, src: str, dst: str, **kwargs):
"""
Args:
src:
path of file in host device
dst:
path of file in mobile device
"""
try:
self.device.connect(rsa_keys=[self.signer])
ret = self.device.push(src, dst, **kwargs)
return ret
self.device.close()
except Exception as e:
print(f"Error in push cmd from {src} to {dst}: {e}")
finally:
self.device.close()
def pull(self, src: str, dst: str, **kwargs):
"""
Args:
src:
path of file in mobile device
dst:
path of file in host device
"""
try:
self.device.connect(rsa_keys=[self.signer])
ret = self.device.pull(src, dst, **kwargs)
return ret
self.device.close()
except Exception as e:
print(f"Error in pull cmd from {src} to {dst}: {e}")
finally:
self.device.close()
def shell(self, cmd: str, **kwargs):
"""
Args:
cmd: adb command
"""
try:
self.device.connect(rsa_keys=[self.signer])
ret = self.device.shell(cmd, **kwargs)
return ret
except Exception as e:
print(f"Error in shell cmd {cmd}: {e}")
finally:
self.device.close()