# -*- coding: utf-8 -*- import subprocess import os #import time class transfer: def push(self, filePath, target='/mnt/sdcard/jp.apk'): transferred = 0.0 fileSize = float(globalFunction.getFileSize(filePath)) pushSp = subprocess.Popen( [globalConfig.adb, 'push', filePath, target], env=globalFunction.setAdbTraceEnv('1'), stderr=subprocess.PIPE, stdout=subprocess.PIPE ) '''< some output >''' #for line in mysp.stderr 似乎不能逐行输出 诡异 while True: line = pushSp.stderr.readline() if line.find('DATA', 95) >= 0: #输出65544,每次多传4个不知道干嘛的字节 transferred += 65540 process = transferred / fileSize #这里最后一次不记录准确字节数,以65540计,超过size就结束 if transferred <= fileSize: print process * 100 else: print 'finish' break '''''' class globalFunction: @staticmethod def getFileSize(filePath): return os.path.getsize(filePath) #设置全局变量 @staticmethod def setAdbTraceEnv(state): new_env = os.environ.copy() new_env['ADB_TRACE'] = state return new_env class globalConfig: adb = 'adb' # Test file # file = 'C:\\Users\\tastypear\\Downloads\\baoweiluobo.apk' #好像时间和不处理输出的时间差不多 #currentTime = time.time() transfer().push(filePath=file) #print time.time() - currentTime