• 使用python批量给设备安装apk


    今天写了一个利用python自动批量给设备安装apk的脚本,具体实现过程如下:

    1.首先需要从xml文件中读取相关apk的信息:

    xml文件结构定义如下:

    <?xml version="1.0"?>
    <data>
        <apks>
            <apk id='0001'>test01.apk
            </apk>
            <apk id='0002'>test02.apk
            </apk>
        </apks>
    </data>

    使用ElementTree解析xml文件的节点,其中apk.text的描述是我们需要安装的apk的name,先存到一个list中;

    import xml.etree.ElementTree as ET 
     
    tree = ET.parse('ApkList.xml')
    root = tree.getroot()
    appList= []
    for apk in root.iter('apk'):
        print apk.tag, apk.attrib, apk.text
        appList.append(apk.text)
    print "====app list====" 
    print appList 
    =====================================================================================
    Result:
    apk {'id': '0001'} test01.apk
    apk {'id': '0002'} test02.apk

    2.在获取到apk List的基础上,我们需要知道连接到这个电脑的Android设备信息,这里使用的genymotoin的虚拟机,如果真机或者Google的emulator,显示的参数会略有不同:

    使用adb devices的command命令去获取参数,并在获取的结果基础上,稍加修正,得到最后的Devices List

    cmd = r'adb devices'
    devList = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    devices = []
    for line in devList.stdout.readlines()[1:]:
        if 'List' not in line:
            devices.append((line.split('	'))[0])
    devices.pop()
    print "===devices==="
    print devices
    =====================================================================================
    Result:

    ===devices===
    ['192.168.56.101:5555']

    3.已经获取到了相关的apk list和device list,下一步需要进行安装的步骤,使用adb的install相比较push要简单方便一点:

    print "===install apk==="
    for dev in devices:
        for apk in appList:
            installCmd = r'adb -s %s install -r .\%s ' % (dev, apk)
            print installCmd
            result = subprocess.Popen(installCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            print(result.stdout.readlines())
    ====================================================================================
    Result:

    ===install apk===
    adb -s 192.168.56.101:5555 install -r . est01.apk
            
    [' pkg: /data/local/tmp/test01.apk ', 'Success ']
    adb -s 192.168.56.101:5555 install -r . est02.apk
            
    [' pkg: /data/local/tmp/test02.apk ', 'Success ']

    使用了两个循环来完成设备和apk params的输入,并在结果中发现”Success”的关键字~安装Okay~

    4.最后为了查看安装的结果,使用logging模块去获取其中的安装步骤,将print的代码部分改成logging

    import logging
    logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='installer.log',
                    filemode='w')
    logging.info("start to install apk")
     
    ===================================================================================
    Result:
    Thu, 26 Nov 2015 23:23:48 InstallApk.py[line:10] INFO start to install apk

    总结:python中logging模块确实强大+易用~,明天详解研究一下logging的原理和api~

  • 相关阅读:
    java分页查询--oracle
    Tomcat Excel中的数据导出到页面中显示
    接口调用类3
    接口工具类2
    接口工具类
    redis 对外访问
    Spring 项目启动时执行
    scp 本地上传/下载服务器文件
    CentOS 安装 rabbitMQ
    卸载rabbitMQ
  • 原文地址:https://www.cnblogs.com/zhaoqi3215/p/4999374.html
Copyright © 2020-2023  润新知