コード例 #1
0
 def __init__(self, appnamelist, Type, serialNumber=None):
     self.appnamelist = appnamelist
     self.device = AdbDevice(Type, serialNumber)
     if not self.device.adbRoot():
         loger.logError(u"无法获取root权限,不能获得进程的内存情况!")
     self.device.runRootShellCmd("chmod 777 /data/")
     self.PID = self.getPid()
     print 'pid =', self.PID
     self.uid = self.getUid()
     print 'uid =', self.uid
     #self.casename=""
     '''self.NameToPid={}
コード例 #2
0
ファイル: clearlog.py プロジェクト: wxpokay/autogui
 def __init__(self,appnamelist,Type,serialNumber=None):
     self.appnamelist = appnamelist
     self.device=AdbDevice(Type,serialNumber)
     if not self.device.adbRoot():
         loger.logError(u"无法获取root权限,不能获得进程的内存情况!")
     self.device.runRootShellCmd("chmod 777 /data/")
     self.PID=self.getPid()
     print 'pid =',self.PID
     self.uid = self.getUid()
     print 'uid =',self.uid
     #self.casename=""
     '''self.NameToPid={}
コード例 #3
0
class AndroidResMonitor(BaseMonitor):
    def __init__(self, appnamelist, Type, serialNumber=None):
        self.appnamelist = appnamelist
        self.device = AdbDevice(Type, serialNumber)
        if not self.device.adbRoot():
            loger.logError(u"无法获取root权限,不能获得进程的内存情况!")
        self.device.runRootShellCmd("chmod 777 /data/")
        self.PID = self.getPid()
        print 'pid =', self.PID
        self.uid = self.getUid()
        print 'uid =', self.uid
        #self.casename=""
        '''self.NameToPid={}
        for appname in appnamelist:
            self.NameToPid[appname]=[]'''

    def getPid(self):  #通过adb shell ps更新(获取)进程的pid值
        res = self.device.runCmd("shell ps")
        flag = False
        if not res:
            print 'ps faild'
            return False
        #print 'res =',res
        for res_item in res[1:-1]:
            print res_item.split()[-1]
            print self.appnamelist[0]
            print '---------------------'
            if self.appnamelist[0] == res_item.split()[-1]:
                print 'finded'
                flag = True
                return res_item.split()[1]
        print 'flag =', flag
        if flag == False:
            print 'can not find pid'

    def getUid(self):  #进程的uid值
        CMD = "shell cat "  #将所有需要cat的文件,一次都cat出
        CMD += "data/system/packages.list"
        res = self.device.runCmd(CMD)
        flag = False
        if not res:
            print 'ps faild'
            return False
        #print 'res =',res
        for res_item in res[1:-1]:
            print res_item.split()[-1]
            print '---------------------'
            if self.appnamelist[0] == res_item.split()[0]:
                print 'finded'
                flag = True
                print res_item.split()[1]
                return res_item.split()[1]
        print 'flag =', flag
        if flag == False:
            print 'can not find uid'

    def get_networkTraffic(self):
        CMD = "shell cat "  #将所有需要cat的文件,一次都cat出
        CMD += "/proc/net/xt_qtaguid/stats"

        print 'CMD=', CMD
        res = self.device.runCmd(CMD)
        #print res
        list_rx = []  # 接收网络数据流量列表
        list_tx = []  # 发送网络数据流量列表
        try:
            for res_item in res[1:-1]:
                if res_item.split()[3] == self.uid:
                    rx_bytes = res_item.split()[5]  # 接收网络数据流量
                    tx_bytes = res_item.split()[7]  # 发送网络数据流量
                    list_rx.append(int(rx_bytes))
                    list_tx.append(int(tx_bytes))
            # print list_rx, sum(list_rx)
            floatTotalNetTraffic = (sum(list_rx) + sum(list_tx)) / 1024.0
            floatTotalNetTraffic = round(floatTotalNetTraffic, 4)
            print 'net=', floatTotalNetTraffic, 'kb'
            return floatTotalNetTraffic
        except:
            print "[ERROR]: cannot get the /proc/net/xt_qtaguid/stats, return 0.0"
            return 0.0

    def checkProcessPid(self, num, res):  #检查进程id是否变换
        i = 0
        #print res
        #print "num: ",num
        #print "len appname:",len(self._appnames)
        for appname in self._appnames:
            pidList = self.NameToPid[appname]
            for pid in pidList:
                #print res[i].split()[0],pid
                if res[i].split(
                )[0] != pid:  #出现这个说明访问到了不存在的文件,说明进程已经停止运行,需要重新更新进程pid
                    #print "return False"
                    return False
                m = re.match(r'\((.*)\)', res[i].split()[1])  #获取文件内容的第二个字段内容
                if m.group(
                        1
                ) not in appname:  #截取/proc/pid/stat文件的第二个字段,获得进程名称,看是否是以前的那个进程,不是就需要更新进程pid
                    return False
                i += 1
        return True
コード例 #4
0
ファイル: clearlog.py プロジェクト: wxpokay/autogui
class AndroidResMonitor(BaseMonitor):

    def __init__(self,appnamelist,Type,serialNumber=None):
        self.appnamelist = appnamelist
        self.device=AdbDevice(Type,serialNumber)
        if not self.device.adbRoot():
            loger.logError(u"无法获取root权限,不能获得进程的内存情况!")
        self.device.runRootShellCmd("chmod 777 /data/")
        self.PID=self.getPid()
        print 'pid =',self.PID
        self.uid = self.getUid()
        print 'uid =',self.uid
        #self.casename=""
        '''self.NameToPid={}
        for appname in appnamelist:
            self.NameToPid[appname]=[]'''

    def getPid(self):#通过adb shell ps更新(获取)进程的pid值
        res=self.device.runCmd("shell ps")
        flag = False
        if not res:
            print 'ps faild'
            return False
        #print 'res =',res
        for res_item in res[1:-1]:
            print res_item.split()[-1]
            print self.appnamelist[0]
            print '---------------------'
            if  self.appnamelist[0]==res_item.split()[-1]:
                print 'finded'
                flag = True
                return res_item.split()[1]
        print 'flag =',flag
        if flag == False:
            print 'can not find pid'
                
    def getUid(self):#进程的uid值
        CMD="shell cat "#将所有需要cat的文件,一次都cat出
        CMD+="data/system/packages.list"
        res=self.device.runCmd(CMD)
        flag = False
        if not res:
            print 'ps faild'
            return False
        #print 'res =',res
        for res_item in res[1:-1]:
            print res_item.split()[-1]
            print '---------------------'
            if  self.appnamelist[0]==res_item.split()[0]:
                print 'finded'
                flag = True
                print res_item.split()[1]
                return res_item.split()[1]
        print 'flag =',flag
        if flag == False:
            print 'can not find uid'

    def get_networkTraffic(self):
        CMD="shell cat "#将所有需要cat的文件,一次都cat出
        CMD+="/proc/net/xt_qtaguid/stats"
            
        print 'CMD=',CMD
        res=self.device.runCmd(CMD)
        #print res
        list_rx = [] # 接收网络数据流量列表
        list_tx = [] # 发送网络数据流量列表
        try:
            for res_item in res[1:-1]:
                if res_item.split()[3] == self.uid:
                    rx_bytes = res_item.split()[5] # 接收网络数据流量
                    tx_bytes = res_item.split()[7] # 发送网络数据流量
                    list_rx.append(int(rx_bytes))
                    list_tx.append(int(tx_bytes))
            # print list_rx, sum(list_rx)
            floatTotalNetTraffic = (sum(list_rx) + sum(list_tx))/1024.0
            floatTotalNetTraffic = round(floatTotalNetTraffic,4)
            print 'net=',floatTotalNetTraffic,'kb'
            return floatTotalNetTraffic
        except:
                print "[ERROR]: cannot get the /proc/net/xt_qtaguid/stats, return 0.0"
                return 0.0

    def checkProcessPid(self,num,res):#检查进程id是否变换
        i=0 
        #print res
        #print "num: ",num
        #print "len appname:",len(self._appnames) 
        for appname in self._appnames:
            pidList=self.NameToPid[appname]
            for pid in pidList:
                #print res[i].split()[0],pid
                if res[i].split()[0]!=pid :#出现这个说明访问到了不存在的文件,说明进程已经停止运行,需要重新更新进程pid
                    #print "return False"
                    return False
                m=re.match(r'\((.*)\)',res[i].split()[1])#获取文件内容的第二个字段内容
                if m.group(1) not in  appname:#截取/proc/pid/stat文件的第二个字段,获得进程名称,看是否是以前的那个进程,不是就需要更新进程pid
                    return False
                i+=1
        return True