测试ADB Pipe的封装
封装ADB Pipe 模块
import os import subprocess import sys from subprocess import * import threading import setting class commandPipe(): def __init__(self,command,func,exitFunc,readyFunc=None,shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE,code="GBK"): ''' :param command:命令 :param func:正常的输出函数 :param exitFunc:异常反馈函数 :param readyFunc:当管道创建完毕后调用 ''' self._thread = threading.Thread(target=self.__run,args=(command,shell,stdin,stdout,stderr,readyFunc)) self._code=code self._func=func self._exitFunc=exitFunc self._readyFunc=readyFunc def __run(self,command,shell,stdin,stdout,stderr,readyFunc): '''私有函数''' global tmp try: self._process=subprocess.Popen( command, shell=shell, stdin=stdin, stdout=stdout, stderr=stderr,) except OSError as e: self._exitFunc(e) fun=self._process.stdout.readline if readyFunc!=None: threading.Thread(target=readyFunc).start() while True: line=fun() if not line: break try: tmp=line.decode(self._code) except Exception as e: print(e) self._func(tmp) self._process.stdout.close() def start(self): self._thread.start() def close(self): #self._process.stdout.close() self._thread.join() del self
调用
import setting from commandPipe import commandPipe class runCmd(): instance=None def __new__(cls, *args, **kwargs): if cls.instance is None: cls.instance = super().__new__(cls) return cls.instance def __init__(self,name): self.name=name def exitFunc(self,exception): print("Exception is {}".format(exception)) def readyFunc(self): print("Start running the command: {}".format(self.name)) def func(self,line): self.line=line print("The standard output for {} is {}".format(self.name,line)) def run(self): #tmp=self.name s="setting.{}.cmd".format(self.name) #cmd=(setting.{}.cmd) E= commandPipe(eval(s),self.func,self.exitFunc,self.readyFunc) E.start() E.close() return self.line if __name__=='__main__': test=runCmd("deviceId").run()
setting.py 中设置adb的运行命令
from collections import namedtuple ''' 利用namedtuple 设置变量 ''' default=[None,None] ConfigItem=namedtuple("ConfigItem",["cmd_name","cmd"]) #获取电脑上链接的设备id deviceId=ConfigItem(cmd_name="device_Id",cmd="adb devices | sed -n '2p' | awk '{print $1}'") #获取app的内存 meminfo=ConfigItem(cmd_name="meminfo",cmd="adb shell dumpsys meminfo packageName | sed -n '2p' |awk -F " " '{print $2}'") #获取app的CPU cpuinfo=ConfigItem(cmd_name="cpuinfo",cmd="adb shell dumpsys cpuinfo | grep packageName |awk -F " " '{print $1}'| awk '{SUM+=$1}'END'{print SUM}'") #获取app(设备)的FPS数据,这个计算的是加载时长,计算是 1000/(fps/1000/100) fpsinfo=ConfigItem(cmd_name="fpsinfo",cmd="adb shell dumpsys SurfaceFlinger --latency SurfaceView[packapgeName]#0 | sed -n '1p' |awk -F 'print {$1}'") #获取页面加载时长,计算页面渲染时间