Advertisement
tastypear

get progress from adb push

Feb 11th, 2013
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import subprocess
  3. import os
  4. #import time
  5.  
  6.  
  7. class transfer:
  8.     def push(self, filePath, target='/mnt/sdcard/jp.apk'):
  9.         transferred = 0.0
  10.         fileSize = float(globalFunction.getFileSize(filePath))
  11.         pushSp = subprocess.Popen(
  12.             [globalConfig.adb, 'push', filePath, target],
  13.             env=globalFunction.setAdbTraceEnv('1'),
  14.             stderr=subprocess.PIPE,
  15.             stdout=subprocess.PIPE
  16.         )
  17.  
  18.         '''< some output >'''
  19.         #for line in mysp.stderr 似乎不能逐行输出 诡异
  20.         while True:
  21.             line = pushSp.stderr.readline()
  22.             if line.find('DATA', 95) >= 0:
  23.                 #输出65544,每次多传4个不知道干嘛的字节
  24.                 transferred += 65540
  25.                 process = transferred / fileSize
  26.                 #这里最后一次不记录准确字节数,以65540计,超过size就结束
  27.                 if transferred <= fileSize:
  28.                     print process * 100
  29.                 else:
  30.                     print 'finish'
  31.                     break
  32.         '''</ some output >'''
  33.  
  34.  
  35. class globalFunction:
  36.     @staticmethod
  37.     def getFileSize(filePath):
  38.         return os.path.getsize(filePath)
  39.  
  40.     #设置全局变量
  41.     @staticmethod
  42.     def setAdbTraceEnv(state):
  43.         new_env = os.environ.copy()
  44.         new_env['ADB_TRACE'] = state
  45.         return new_env
  46.  
  47.  
  48. class globalConfig:
  49.     adb = 'adb'
  50.  
  51. # Test file
  52. # file = 'C:\\Users\\tastypear\\Downloads\\baoweiluobo.apk'
  53. #好像时间和不处理输出的时间差不多
  54. #currentTime = time.time()
  55. transfer().push(filePath=file)
  56. #print time.time() - currentTime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement