双击main.py即可运行。如果有什么异常,窗口一闪而逝,可在cmd窗口下,输入main.py后回车,可以查看输出打印来debug
template_lcd.py 是写重启和查看节点的逻辑
main.py是对template_lcd.py的封装(也能封装bat脚本),它能通过adb devices命令的输出来自动检测手机数量,将template_lcd.py中的xxxt替换为每个手机的真实序列号,并以template_lcd.py为模板,为每个手机产生一个以 序列号.py 或 序列号.bat 命名的专属脚本,所以,你可以在template_lcd.py中只考虑一个手机的情况,写对操作一个手机的操作逻辑。至于同时连接多个手机的情况,由main.py来完成。main.py会通过 os.system('start cmd /k xxx.suffix')弹一个cmd窗口执行
import time
import subprocess
import os
if 1:
import os
if os.name == "nt":
os.system("")
print("os.name=",os.name)
print("\033[1;32m Welcome! \033[0m")
def getdevlist1():
devlist = []
#connectfile = os.popen('type serialno_list.txt')
connectfile = os.popen('adb devices')
list1 = connectfile.readlines()
#print(list1)
#print(len(list1))
#print(1,len(list1)-1)
list2=list(list1[1:len(list1)-1])
#print(list2)
for i in range(len(list2)):
#print(list1[i].find('\tdevice') )
#print(list1[i].find('\tdevice') != -1)
#if list1[i].find('\tdevice') != -1:
#temp = list1[i].split('\t')
temp = list2[i].split()
#print(22,temp,temp[0],temp[1])
devlist.append(temp[0])
#print(11,devlist)
return devlist
def create_file(file1,serialno):
(name, suffix) = os.path.splitext(file1)
#print("name=",name,"suffix=",suffix)
with open(file1,"r") as f1:
cmd1=f1.read()
#print("before: \n",cmd1)
cmd2=cmd1.replace("xxx",serialno)
#print("after: \n",cmd2)
with open(str(serialno)+suffix,"w") as f2:
print(cmd2,file=f2)
def main(file2):
(name, suffix) = os.path.splitext(file2)
#detect_only_once=True
detect_only_once=False
ret=[]
s=set(ret)
t=0
total=0
while True:
#ret=getdevlist1()
#print("-"*8)
#lst1=["431f92af"] #new
#lst1=["431f92af","aa"] #new
#with open("a.txt","r") as f1:
# lst1=f1.readlines()
#for i in range(len(lst1)):
# lst1[i]=lst1[i][0:len(lst1[i])-1]
#print("111")
#time.sleep(3)
lst1=getdevlist1()
#print("222")
s1=set(lst1)
#print(ret,lst1)
#print(s,s1,s1-s,s-s1)
s2=s1-s
#print(s-s1,(s-s1)!=set())
#print(s2,s2!=set())
if s2!=set():
c=0
print("")
for i in s2:
total+=1
print(f"{c}. detect new devices: \033[33m {i} \033[0m,\033[32m {total} \033[0mdevices in total")
create_file(file2,i)
cmd='start cmd /k xxx'+suffix
cmd1=cmd.replace("xxx",i)
#print(cmd)
#print(cmd1)
#os.popen(cmd1)
os.system(cmd1)
#print("cmd1=",cmd1)
c+=1
s=s|s1
#print(s,s1,s1-s,s-s1)
t+=1
if detect_only_once:
break
#exit()
print(f"\r continuously detecting! \033[32m {t} \033[0m Seconds ",end="")
time.sleep(1)
if __name__=="__main__":
main("template_lcd.py")
#main("demo.py")
#main("a.bat")
'''
import os
import time
import subprocess
#enable print with color
if 1:
import os
if os.name == "nt":
os.system("")
print("os.name=",os.name)
print("\033[1;32m Welcome! \033[0m")
def acquire_serialno_list():
with os.popen("adb devices","r") as p:
r=p.read()
f1=open("serialno_list.txt","w")
print(r,file=f1)
f1.close()
f1=open("serialno_list.txt","r")
#print(f1.read())
#print(f1.readlines())
ret=f1.readlines()
#print(ret)
#print(type(ret))
#print(len(ret))
#print(ret[1:])
len1=len(ret)-2
#print(len1)
#print(ret[1:len1])
ret2=ret[1:len1]
f1.close()
return ret2
def all_devices_do_in_cmd_window2(file1):
(name, suffix) = os.path.splitext(file1)
print("name=",name,"suffix=",suffix)
ret=acquire_serialno_list()
counter=0
#while(len(ret)>=0):
while(len(ret)<=0):
counter+=1
#print("please keep \033[32m ss \033[0m device !")
#print("\rcurrent %d devices,please at least 1 device ! waiting...%d Seconds" % (len(ret),counter),end=" ")
print("\rcurrent \033[31m %d \033[0m devices,please at least 1 device ! waiting...%d Seconds" % (len(ret),counter),end=" ")
#os.system("ping -n 1 127.0.0.1 >nul")
time.sleep(1)
ret=acquire_serialno_list()
print("")
no_list=[]
for i in ret:
#print("mei hang shu ju: ",i)
sp=i.split()
#print("mei hang chai fen hou: ",sp)
#print("no= ",sp[0])
no_list.append(sp[0])
#print("no_list = ",no_list)
#print("there are %d devices !" % len(ret))
#print("there are %d devices ! the serialno are:" % len(no_list))
print("there are \033[32m %d \033[0m devices! the serialno are:" % len(no_list))
c=0
for i in no_list:
print(c,". ",i)
c+=1
c=0
f3=open(name+"_do.bat","w")
for i in no_list:
print("---the %d device---" % c)
# zhe li gai xi tong ming ling
#os.system("ls %s" % (i))
with open(file1,"r") as f1:
cmd1=f1.read()
print("before: \n",cmd1)
cmd2=cmd1.replace("xxx",i)
print("after: \n",cmd2)
with open(str(i)+suffix,"w") as f2:
print(cmd2,file=f2)
#os.popen(cmd2)
#subprocess.run(' {}'.format( cmd2), shell=True, capture_output=True,universal_newlines=True)
s1 = 'start cmd /k "{}"'.format(str(i)+suffix)
#print('start cmd /k "{}"'.format(cmd2))
print(s1)
print(s1,file=f3)
#ret=subprocess.run(' {}'.format( cmd2), shell=True, capture_output=True,universal_newlines=True)
#print(cmd2)
#os.popen("adb -s %s reboot autodloader" % i)
#print("adb -s %s reboot autodloader" % i)
print("---the %d device---" % c)
c+=1
f3.close()
os.startfile(name+"_do.bat")
all_devices_do_in_cmd_window2("template_lcd.py")
print('ab')
#all_devices_do_in_cmd_window2("tp_self-test.bat")
exit()
'''
import os
import re
import time
import subprocess
#enable print with color
if 1:
import os
if os.name == "nt":
os.system("")
print("os.name=",os.name)
print("\033[1;32m Welcome! \033[0m")
counter=0
setting={
#"lcd":{"open":True,"version":"userdebug"},
"lcd":{"open":True,"version":"pv"},
#"tp":{"open":False,},
"tp":{"open":True,},
"finger":{"open":True,},
"sensor":{"open":True,},
}
d={"test_item":"error_count","lcd":0,"tp":0,"finger":0,"sensor":0}
version="pv"
#version="userdebug"
def clean_old_files():
#checking if file exist or not
list1=["xxx_lcd.txt",
"xxx_tp.txt",
"xxx_finger.txt",
"xxx_sensor.txt"]
for i in list1:
#print(i)
if(os.path.isfile(i)):
os.remove(i)
#Printing the confirmation message of deletion
#print(i,"Deleted successfully")
print("\033[32m {} Deleted successfully \033[0m".format(i))
else:
#print(i,"does not exist")
print("\033[32m {} does not exist \033[0m".format(i))
def clean_old_files2():
#originfiles = {"template_lcd.py","main.py","ReadMe.txt","b.py","a.py"}
originfiles = {"template_lcd.py","main.py","ReadMe.txt"}
needDelete = set(os.listdir(os.path.curdir))- originfiles
#print(needDelete)
for filename in needDelete:
os.remove(filename)
print("\033[32m {} Deleted successfully \033[0m".format(filename))
def get_phone_time():
cmdtask= subprocess.run(' {}'.format("adb -s xxx shell date"), shell=True, capture_output=True,universal_newlines=True)
return cmdtask.stdout
def show_result():
print("-"*32)
for i in d:
if d[i]=="error_count":
print("| \033[33m {0:^10} \t {1:^10} \033[0m |".format(i,d[i]))
elif d[i]==0:
print("| {0:^10} \t \033[32m {1:^10} \033[0m |".format(i,d[i]))
else:
print("| {0:^10} \t \033[31m {1:^10} \033[0m |".format(i,d[i]))
print("-"*32)
def perform_by_subprocess(item,cmd_list):
filename="xxx_"+item+".txt"
#print("filename=",filename)
with open(filename,"a") as f1:
print("counter=",counter," \t phone time=",get_phone_time(),file=f1)
for i in cmd_list:
cmdtask= subprocess.run(' {}'.format(i), shell=True, capture_output=True,universal_newlines=True)
print(cmdtask.stdout,file=f1)
print("\033[33m counter=",counter," ",item,": \033[0m ",i,"=",cmdtask.stdout,"\033[36m phone time=",get_phone_time(),"\033[0m")
return cmdtask.stdout
def detect_connect():
cmdtask= subprocess.run(' {}'.format("adb devices"), shell=True, capture_output=True,universal_newlines=True)
#print(cmdtask.stdout)
t=0
while("xxx" not in cmdtask.stdout):
t+=1
print("\r \033[31m xxx not connect! \033[0m waiting...\033[32m %d \033[0m Seconds" % t,end=" ")
time.sleep(1)
cmdtask= subprocess.run(' {}'.format("adb devices"), shell=True, capture_output=True,universal_newlines=True)
print("")
def wait(wait_time):
for i in range(wait_time):
#print("\rwaiting ...{} Seconds left ".format(wait_time-i),end='')
print("\rwaiting ...\033[33m {}\033[0m Seconds left ".format(wait_time-i),end='')
time.sleep(1)
print("\n")
def reboot():
global counter
while True:
counter+=1
####################### lcd
#open lcd test
if setting["lcd"]["open"] :
if setting["lcd"]["version"]=="pv":
cmds=[
"adb -s xxx shell charge_disable.sh",
"adb -s xxx shell enter_lpm.sh",
"adb -s xxx shell cat /proc/driver/lcd_id",
"adb -s xxx shell cat /proc/cmdline",
]
elif setting["lcd"]["version"]=="userdebug":
cmds=[
"adb -s xxx shell cat /proc/driver/lcd_id",
"adb -s xxx shell cat /proc/cmdline",
]
ret=perform_by_subprocess("lcd",cmds)
#print("ret=",ret)
#ret=""
lcd_is_error="lcd_dummy" in ret or ret ==""
#print("lcd_is_error=",lcd_is_error)
if (lcd_is_error):
d["lcd"]+=1
ret2=re.search(r"lcd_name\S+",ret)
#print("ret2=",ret2)
if ret2!=None:
print("\033[33m counter=",counter," lcd : \033[0m ",ret2.group(),"\033[36m phone time=",get_phone_time(),"\033[0m")
with open("xxx_lcd.txt","a") as f1:
print(ret2.group(),"\n",file=f1)
####################### lcd
####################### tp
if setting["tp"]["open"]:
cmds=[
"adb -s xxx shell tpdtest",
]
ret=perform_by_subprocess("tp",cmds)
#time.sleep(8)
####################### tp
####################### finger
if setting["finger"]["open"]:
cmds=[
'adb -s xxx shell "getprop | grep vendor.fingerprint.id"',
]
ret=perform_by_subprocess("finger",cmds)
finger_is_error="Unknown" in ret or ret==""
#print("finger_is_error=",finger_is_error)
if (finger_is_error):
d["finger"]+=1
####################### finger
####################### sensor
if setting["sensor"]["open"]:
cmds=[
'adb -s xxx shell "dumpsys sensorservice | grep accelerometer"',
'adb -s xxx shell "dumpsys sensorservice | grep Proximity"',
]
ret=perform_by_subprocess("sensor",cmds)
####################### sensor
show_result()
cmdtask = subprocess.run(' {}'.format("adb -s xxx shell reboot"), shell=True, capture_output=True,universal_newlines=True)
wait(40)
#cmdtask= subprocess.run(' {}'.format("adb -s xxx shell cat /proc/cmdline"), shell=True, capture_output=True,universal_newlines=True)
#print(cmdtask.stdout)
#wait(3)
detect_connect()
if setting["lcd"]["open"]:
if setting["lcd"]["version"]=="pv":
wait(20)
#wait(3)
cmdtask= subprocess.run(' {}'.format("adb -s xxx shell input keyevent 26"), shell=True, capture_output=True,universal_newlines=True)
print("adb -s xxx shell input keyevent 26")
wait(1110)
#wait(3)
if __name__=="__main__":
clean_old_files()
#clean_old_files2()
reboot()
'''
panel_list=['tongxingda',
'easyquick',
'skyworth']
counter=0
#print(type(cmdtask1.stdout))
with open("xxx.txt","w") as f1:
print("",file=f1)
while True:
counter+=1
cmdtask = subprocess.run(' {}'.format("adb -s xxx shell reboot"), shell=True, capture_output=True,universal_newlines=True)
print(cmdtask.stdout)
time.sleep(40)
print("\033[31m lcd_is_error={} \033[0m".format(lcd_is_error))
else:
#print("\033[32m lcd_is_error={} \033[0m".format(lcd_is_error))
#cmdtask= subprocess.run(' {}'.format("adb -s xxx shell cat /proc/driver/lcd_id"), shell=True, capture_output=True,universal_newlines=True)
#print(cmdtask.stdout)
#time.sleep(1)
print("\033[31m finger_is_error={} \033[0m".format(finger_is_error))
else:
print("\033[32m finger_is_error={} \033[0m".format(finger_is_error))
cmdtask1= subprocess.run(' {}'.format("adb -s xxx shell cat /proc/driver/lcd_id"), shell=True, capture_output=True,universal_newlines=True)
print("the",counter,"times: adb shell cat /proc/driver/lcd_id=",cmdtask1.stdout)
with open("xxx.txt","a") as f1:
print(cmdtask.stdout,file=f1)
if "lcd_name" in cmdtask.stdout:
ret=re.search(r"lcd_name\S+",cmdtask.stdout)
print("\033[33m the ",counter,"times, lcd: \033[0m ",ret.group())
#print(ret)
#print(ret.span())
#print(type(cmdtask1.stdout))
for i in panel_list:
if (i in cmdtask1.stdout):
print("the ",counter,"times ok")
break
else:
print("the ",counter,"times bad !")
break
with open("xxx_finger.txt","a") as f1:
print("counter=",counter," \t phone time=",get_phone_time(),file=f1)
cmdtask= subprocess.run(' {}'.format('adb -s xxx shell "getprop | grep vendor.fingerprint.id" '), shell=True, capture_output=True,universal_newlines=True)
print(cmdtask.stdout,file=f1)
print("\033[33m counter=",counter," finger: \033[0m ",'adb -s xxx shell "getprop | grep vendor.fingerprint.id"=',cmdtask.stdout,"\033[36m phone_time=",get_phone_time(),"\033[0m")
#finger_is_error="fpc1553" in cmdtask.stdout
#cmdtask = subprocess.run(' {}'.format("adb -s xxx shell reboot"), shell=True, capture_output=True,universal_newlines=True)
#print(cmdtask.stdout)
#print("adb -s xxx shell cat /proc/cmdline is done! sleeping 40 s")
#time.sleep(40)
for i in d:
#print(i,"\t",d[i])
#print("%10s \t %10s" % (i,d[i]))
print("{0:^10} \t {1:^10} ".format(i,d[i]))
# if (lcd_is_error or finger_is_error):
# #print("the ",counter,"times error!")
# print("\033[31m the {} times error!\033[0m".format(counter))
# #break
# else:
# #print("the ",counter,"times ok")
# print("\033[32m the {} times ok\033[0m".format(counter))
with open("xxx_sensor.txt","a") as f1:
print("counter=",counter," \t phone time=",get_phone_time(),file=f1)
cmdtask= subprocess.run(' {}'.format('adb -s xxx shell "dumpsys sensorservice | grep accelerometer" '), shell=True, capture_output=True,universal_newlines=True)
print(cmdtask.stdout,file=f1)
print("\033[33m counter=",counter," sensor: \033[0m ",'adb -s xxx shell "dumpsys sensorservice | grep accelerometer"= ',cmdtask.stdout,"\033[36m phone_time=",get_phone_time(),"\033[0m")
cmdtask= subprocess.run(' {}'.format('adb -s xxx shell "dumpsys sensorservice | grep Proximity" '), shell=True, capture_output=True,universal_newlines=True)
print(cmdtask.stdout,file=f1)
print("\033[33m counter=",counter," sensor: \033[0m ",'adb -s xxx shell "dumpsys sensorservice | grep Proximity"= ',cmdtask.stdout)
cmdtask= subprocess.run(' {}'.format("adb -s xxx shell cat /proc/cmdline"), shell=True, capture_output=True,universal_newlines=True)
print(cmdtask.stdout)
print("adb -s xxx shell cat /proc/cmdline is done! sleeping 1 s")
time.sleep(1)
#with open("xxx_lcd.txt","w") as f1:
# print("",file=f1)
#with open("xxx_lcd.txt","a") as f1:
# print("counter=",counter," \t phone time=",get_phone_time(),file=f1)
# cmdtask= subprocess.run(' {}'.format("adb -s xxx shell cat /proc/driver/lcd_id"), shell=True, capture_output=True,universal_newlines=True)
# print(cmdtask.stdout,file=f1)
# print("\033[33m counter=",counter," lcd: \033[0m ","adb -s xxx shell cat /proc/driver/lcd_id=",cmdtask.stdout,"\033[36m phone_time=",get_phone_time(),"\033[0m")
# cmdtask= subprocess.run(' {}'.format("adb -s xxx shell cat /proc/cmdline"), shell=True, capture_output=True,universal_newlines=True)
# print(cmdtask.stdout,file=f1)
# print("\033[33m counter=",counter," lcd: \033[0m ","adb -s xxx shell cat /proc/cmdline=",cmdtask.stdout)
# lcd_is_error="lcd_dummy" in cmdtask.stdout or cmdtask.stdout ==""
# #print("lcd_is_error=",lcd_is_error)
# if (lcd_is_error):
# d["lcd"]+=1
# ret=re.search(r"lcd_name\S+",cmdtask.stdout)
# #print("ret=",ret)
# if ret!=None:
# print("\033[33m counter=",counter," lcd: \033[0m ",ret.group())
# print(ret.group(),"\n",file=f1)文章来源:https://www.toymoban.com/news/detail-840580.html
# cmdtask= subprocess.run(' {}'.format("adb -s xxx shell charge_disable.sh"), shell=True, capture_output=True,universal_newlines=True)
# #print(cmdtask.stdout,file=f1)
# print("\033[33m counter=",counter," lcd: \033[0m ","adb -s xxx shell charge_disable.sh=",cmdtask.stdout)
# with open("xxx_tp.txt","a") as f1:
# print("counter=",counter," \t phone time=",get_phone_time(),file=f1)
# cmdtask= subprocess.run(' {}'.format('adb -s xxx shell tpdtest'), shell=True, capture_output=True,universal_newlines=True)
# print(cmdtask.stdout,file=f1)
# print("\033[33m counter=",counter," tp: \033[0m ","adb -s xxx shell tpdtest=",cmdtask.stdout,"\033[36m phone_time=",get_phone_time(),"\033[0m")
# cmdtask= subprocess.run(' {}'.format("adb -s xxx shell enter_lpm.sh"), shell=True, capture_output=True,universal_newlines=True)
# #print(cmdtask.stdout,file=f1)
# print("\033[33m counter=",counter," lcd: \033[0m ","adb -s xxx shell enter_lpm.sh=",cmdtask.stdout)
print("\033[33m the ",counter,"times, tp: \033[0m "," adb -s xxx shell tpdtest=",cmdtask.stdout)
'''文章来源地址https://www.toymoban.com/news/detail-840580.html
到了这里,关于手机多模块反复重启压力测试脚本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!