pytho指令碼安裝.apk檔案
阿新 • • 發佈:2020-08-07
# -*- coding:utf-8 -*-
# 1.獲取連線裝置的機器ip
# 2.獲取需要安裝的apk
# 3.對每一臺機器安裝apk
# 4.檢查apk安裝是否成功
# 5.安裝完成
import os,subprocess
#獲取裝置ip地址,並連線
def connectDevice(dress):
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()).split('\n')
for line in m[1:]:
if 'List' not in line:
device_list.append(line.split('\t')[0])
print(device_list)
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():
#current=os.getcwd()
device_list = connectDevice("E:\cewen\deviceIPdress.txt")apklist = readapk(r'E:\cewen\package')
for apk in apklist:
apk = 'E:\cewen\package\\'+apk
for dev in device_list:
installCmd = r'adb -s %s install -r %s ' % (dev,apk)
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())
#匯入裝置編號
def deviceName(ipdress):
filename=open(ipdress,'r')
devicelist=[]
for readline in filename:
devicelist.append(readline.strip('\n'))
return devicelist
readFile()