-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadbhelper.py
139 lines (118 loc) · 4.02 KB
/
adbhelper.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
136
137
138
139
# !/usr/bin/env python
# encoding=utf-8
# Date: 2018-10-23
# Author: pangjian
from const import PLATFORMNAME, APKPATH, SYSTEM
from androidhelper import AndroidHelper
import os,time,re
from log import Log
logger = Log.get_logger(__name__)
PROJECTPATH = os.getcwd()
class AdbHelper(object):
def __init__(self):
pass
@classmethod
def getPid(cls, packageName):
if SYSTEM == 'Windows':
cmd = 'adb shell ps | findstr ' + packageName
elif SYSTEM == 'Linux':
cmd = 'adb shell ps | grep ' + packageName
else:
return False
text = os.popen(cmd).read()
if len(text) == 0:
return False
return text.split()[1]
@classmethod
def getUid(cls, pid):
if pid is False:
return False
cmd = 'adb shell cat /proc/%s/status'%(pid,)
text = os.popen(cmd).readlines()
if len(text) == 0:
return False
for i in text:
if i.find('Uid') >=0:
return i.split()[1]
return False
@classmethod
def getDataUsage(cls, packageName):
rx_bytes = 0
tx_bytes = 0
uid = cls.getUid(cls.getPid(packageName))
if uid is False:
return False
if SYSTEM == 'Windows':
cmd = 'adb shell cat /proc/net/xt_qtaguid/stats | findstr ' + uid
elif SYSTEM == 'Linux':
cmd = 'adb shell cat /proc/net/xt_qtaguid/stats | grep ' + uid
else:
return False
text = os.popen(cmd).readlines()
if len(text) == 0:
return False
for i in text:
if i == '\n':
continue
logger.debug(i.split())
rx_bytes = rx_bytes + int(i.split()[5])
tx_bytes = tx_bytes + int(i.split()[7])
return rx_bytes,tx_bytes
def getPlatformName(self):
return PLATFORMNAME
def getPlatformVersion(self):
cmd = 'adb shell getprop ro.build.version.release'
return os.popen(cmd).read().strip('\n')
def getDeviceName(self):
cmd = 'adb get-serialno'
# cmd2 = 'adb shell getprop ro.serialno'
return os.popen(cmd).read().strip('\n')
def getMobilePhoneModel(self):
cmd = 'adb -d shell getprop ro.product.model'
return os.popen(cmd).read().strip('\n')
def getPhoneResolution(self):
cmd = 'adb shell "dumpsys window | grep mUnrestrictedScreen"'
return os.popen(cmd).read().split()[1]
@classmethod
def screenShot(cls, savepath):
timestamp = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
os.popen("adb wait-for-device")
os.popen("adb shell screencap -p /data/local/tmp/tmp.png")
os.popen("adb pull /data/local/tmp/tmp.png " + os.path.join(savepath, timestamp + ".png"))
os.popen("adb shell rm /data/local/tmp/tmp.png")
@classmethod
def getPssFromText(cls, text):
pattern = '( +)(TOTAL)( +)(\d+)'
m = re.match(pattern, text)
return m.group(4)
@classmethod
def getMemory(cls, packageName):
try:
cmd = 'adb shell dumpsys meminfo ' + packageName
text = os.popen(cmd).readlines()
if len(text) == 0:
return False
for i in text:
if i.find('TOTAL') >= 0:
return cls.getPssFromText(i)
except Exception as e:
logger.debug(str(e))
return False
@classmethod
def getCpu(cls, packageName):
try:
if SYSTEM == 'Windows':
cmd = 'adb shell top -n 1 | findstr /E ' + packageName
elif SYSTEM == 'Linux':
cmd = 'adb shell top -n 1 | grep /E ' + packageName
else:
return False
text = os.popen(cmd).readlines()
if len(text) == 0:
return False
return text[0].split()[2]
except Exception as e:
logger.debug(str(e))
return False
if __name__ == '__main__':
pass