forked from smart-test-ti/SoloX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogcateThread.py
143 lines (119 loc) · 5.42 KB
/
logcateThread.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
140
141
142
143
# -*- coding: utf-8 -*-
import re
import subprocess
import threading
import time
import consts
import monitor
from adbTool import Adb
from espressoMonitor import clearLogcat
from espressoMonitor import getLogPath
from espressoMonitor import readStdout
class logcateThread(threading.Thread):
def __init__(self, device, taskId, taskName, testResults, loghd, serial=None):
threading.Thread.__init__(self);
# self.running = True
self.adb = Adb(serial=serial)
self.logcatSubProcess = None
self.device = device
self.taskId = taskId
self.taskName = taskName
self.testResults = testResults
self.appPid = 0
self.loghd = loghd
self.is_alive = False
def stop(self):
# self.running = False
try:
while self.is_alive and self.getAppPid() == self.appPid and self.getAppPid() != 0:
self.loghd.info("logcate Thread still alive, wait ...")
time.sleep(1)
if self.logcatSubProcess.poll() == None:
self.logcatSubProcess.terminate()
except:
pass
def getAppPid(self):
# self.appPid = self.adb.cmd("shell", "set", "`ps|grep com.duowan.mobile$`;", "echo", "$2").communicate()[0].decode("utf-8").strip()
outputs = self.adb.cmd("shell", "top", "-m", "10", "-n", "1").communicate()[0].decode("utf-8").strip()
r = "(\\d+).*\\s+%s[^:]" % "com.duowan.mobile" # hardcode 手y
m = re.search(r, outputs)
# print outputs
if m:
return m.group(1)
else:
self.loghd.info("app still not get up")
return 0
# 执行用例中途会退出导致logcat抓取不全
# def run(self):
# self.loghd.info("logcateThread run \n")
# self.is_alive = True
# clearLogcat(self.device)
# logcatLogfile=open(getLogPath(self.taskId,consts.logcatLogName %(self.taskName),self.device),'w+')
# self.logcatSubProcess=subprocess.Popen((consts.logcatCommand %self.device).split(),stdout=logcatLogfile,stderr=logcatLogfile)
# while ((not monitor.isTimeout(self.taskId,self.device)) and self.running):
# monitor.heart(self.taskId,self.device)
# time.sleep(1)
# logcatLogfile.close()
# self.is_alive = False
def run(self):
self.loghd.info("logcateThread run \n")
self.is_alive = True
# 清楚logcat log
clearLogcat(self.device)
logcatLogfile = open(getLogPath(self.taskId, consts.logcatLogName % (self.taskName), self.device), 'w+')
self.appPid = self.getAppPid()
# == start logcat ==subprocess.PIPE
self.logcatSubProcess = subprocess.Popen((consts.logcatCommand % self.device).split(), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
total = 0
finishedIndex = 0
timeInterval = 0.1
# timeoutFlag=False
# while ((not monitor.isTimeout(self.taskId,self.device)) and self.running):
while (not monitor.isTimeout(self.taskId, self.device)):
content = monitor.timeoutTask(self.taskId, self.device, readStdout, self.logcatSubProcess)
if not content:
time.sleep(timeInterval)
continue
logcatLogfile.write(content)
logcatLogfile.flush()
monitor.heart(self.taskId, self.device)
if "1 failed" in content:
self.testResults[self.taskName] = False
# 结束内容出现,则结束当前logcate抓取线程
# if "): finished:" in content:
# self.running = False
# break
# avoid uiauto log
filte = re.findall(r"run finished:\s+(\d+)\s+tests.+?(\d+)\s+ignored", content)
if len(filte) and filte[0][0] != 3 and filte[0][1] != 0:
# self.running = False
self.loghd.info("logcat finish.")
break
# progress report
# filte=re.findall(r"run started:\s+(\d+)\s+tests",content)
# if filte:
# total=int(filte[0])
# # if step==3:
# # total+=self.config.getTotal(self.taskId,self.device)
# config.setTotal(self.taskId,self.device,total)
# if "): finished:" in content:
# finishedIndex+=1
# # if step==1:
# totalFinishedIndex=finishedIndex
# # if step==3:
# # totalFinishedIndex=finishedIndex+config.getFinished(self.taskId,self.device)
# config.setFinished(self.taskId,self.device,totalFinishedIndex)
# progress=int(float(config.getFinished(self.taskId,self.device))/config.getTotal(self.taskId,self.device)*100)
# config.setProgress(self.taskId,self.device,progress)
# filte=re.findall(r"run finished:\s+(\d+)\s+tests.+?(\d+)\s+ignored",content)
self.is_alive = False
# avoid uiauto log
# if len(filte) and filte[0][0]!=3 and filte[0][1]!=0:
# log(self.logfile,"logcat finish.")
# monitor.clear(self.taskId,self.device)
# break
# else:
# # log(self.logfile,"============== %d timeout =============="%step)
# monitor.clear(self.taskId,self.device)
# timeoutFlag=True