测试:python调用cmd命令,让push包自动化

一、python与monkey script脚本相结合,达到修改.script脚本中的内容

 1 #coding:utf-8
 2 '''
 3 push脚本进室内机,并运行
 4 
 5 '''
 6 import os
 7 import re
 8 import subprocess
 9 
10 #执行脚本
11 def run_monkey(path):
12     filelist=os.listdir(path)
13     for filename in filelist:
14         filepath=os.path.join(path,filename)
15         cmd="adb remount"
16         p=subprocess.Popen(cmd,shell=True)
17         p.wait()
18         if p.returncode!=0:
19             return -1
20         cmd="adb push "+filepath+" sdcard/"
21         p=subprocess.Popen(cmd,shell=True)
22         p.wait()
23         if p.returncode!=0:
24             return -1
25         tarpath=os.path.join(r"sdcard/",filename)
26         cmd="adb shell monkey -f "+tarpath+" -v 1"
27         p=subprocess.Popen(cmd,shell=True)
28         p.wait()
29         if p.returncode!=0:
30             return -1
31 
32 #修改脚本的内容
33 def modify_monkey(path,repstr):
34     filelist=os.listdir(path)
35     for filename in filelist:
36         filepath=os.path.join(path,filename)
37         contect=""
38         with open(filepath,encoding='utf-8') as file:
39             text=file.read()
40             pattern=re.compile(r"captureDispatchString(.*)")
41             repstr="captureDispatchString({})".format(repstr)
42             contect=re.sub(pattern,repstr,text)
43         with open(filepath,mode='w',encoding='utf-8') as file:
44             file.write(contect)     
45 if __name__=="__main__":
46     path=r"C:UsersAdministratorDesktoppythoncmd
unscript"
47     times=500
48     for i in range(times):
49         modify_monkey(path,str(i))
50         run_monkey(path) 
51     print("执行完毕")   
52         

二、push .apk和.so文件自动化

 1 #coding:utf-8
 2 '''
 3 push脚本进室内机,并运行
 4 
 5 '''
 6 import os
 7 import re
 8 import subprocess
 9 
10 #执行脚本
11 def push_apk(path):
12     filelist=os.listdir(path)
13     cmd="adb remount"
14     p=subprocess.Popen(cmd,shell=True)
15     p.wait()
16     if p.returncode!=0:
17         return -1
18     for filename in filelist:
19         filepath=os.path.join(path,filename)
20         filename,extension=os.path.splitext(filename)
21         if extension==".apk":
22             #print(".apk")
23             cmd="adb push "+filepath+" /system/app"
24             p=subprocess.Popen(cmd,shell=True)
25             p.wait()
26             if p.returncode!=0:
27                 return -1
28         if extension==".so":
29             #print(".so")
30             cmd="adb push "+filepath+" /system/lib"
31             p=subprocess.Popen(cmd,shell=True)
32             p.wait()
33             if p.returncode!=0:
34                 return -1        
35     cmd="adb reboot"
36     p=subprocess.Popen(cmd,shell=True)
37     p.wait()
38     if p.returncode!=0:
39         return -1    
40 
41 if __name__=="__main__":
42     path=r"C:UsersAdministratorDesktoppythoncmdpushapk"
43     push_apk(path)
44     print("执行完毕,正在重启")   
45         
原文地址:https://www.cnblogs.com/yizhenfeng168/p/6953312.html