# -*- coding:utf-8 -*-
# 1.获取连接设备的机器ip
# 2.获取需要安装的apk
# 3.对每一台机器安装apk
# 4.检查apk安装是否成功
# 5.安装完成
import os,subprocess
import _thread
#获取设备ip地址,并连接
result=0
def connectDevice(dress):
'''检查设别是否连接成功,如果成功返回True,否则返回False'''
ip_dress=deviceName(dress)
for ipdressline in ip_dress:
cmd="adb connect"+" "+str(ipdressline)
print(cmd)
device_con=subprocess.getoutput(cmd)
print(device_con)
Devicecmd='adb devices'
device_str=subprocess.getoutput(Devicecmd)
device_list=[]
m=(device_str.strip('\n').strip(' ')).split('\n')
for line in m:
if 'List' not in line:
device_list.append(line.split('\t')[0])
print(device_list)
else:
print("无设备连接")
return device_list
#读取apk
def readapk(apk_path):
apkpackage=os.listdir(apk_path)
apk_list=[]
for list in apkpackage:
if '.apk' in list:
apk_list.append(list)
print("需要安装的apk是%s" % (apk_list))
return apk_list
#读取apk,安装apk
def readFile():
global result
#current=os.getcwd()
device_list = connectDevice("E:\cewen\deviceIPdress.txt")
apklist = readapk(r'E:\cewen\package')
for dev in device_list:
for apk in apklist:
apk='E:\cewen\package\\'+apk
installCmd = r'adb -s %s install -r %s ' % (dev, apk)
result =result+1
_thread.start_new_thread(manythread,(installCmd,apk))
#result = subprocess.Popen(installCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)
while result!=0:
pass
def manythread(installCmd,apk):
global result
print(installCmd)
print('正在安装apk包:%s' % apk)
result = subprocess.Popen(installCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
universal_newlines=True)
print(result.stdout.readlines())
result=result-1
#导入设备编号
def deviceName(ipdress):
filename=open(ipdress,'r')
devicelist=[]
for readline in filename:
devicelist.append(readline.strip('\n'))
return devicelist
#connectDevice()
#print(readapk(r'G:\测温软件\apk'))
readFile()
运行时很容易有问题。。。。