adb 常用命令封装

from androguard.core.bytecodes.apk import APK
import os

class ADB:

    def __init__(self, log):
        self.log = log

    def get_devices(self) -> list:
        """获取链接的设备"""
        all_devices = []
        cmd = "adb devices"
        reslut = os.popen(cmd).readlines()[1:]
        for item in reslut:
            if item != "
":
                all_devices.append(str(item).split("	")[0])
        self.log.info("获取的设备:%s" % str(all_devices))
        return all_devices

    def get_platform_version(self):
        """获取系统版本"""
        return os.popen('adb shell getprop ro.build.version.release').read()

    def get_apkname(self, apk):
        """
        获取apk 的名字
        :param apk: Android apk包路径
        :return:
        """
        a = APK(apk, False, "r")
        self.log.info("获取到的包名: %s" % a.get_package())
        return a.get_package()

    def get_apk_lautc(self, apk):
        """
        获取apk 的主activity
        :param apk: Android apk包路径
        :return:
        """
        a = APK(apk, False, "r")
        self.log.info("获取到的主activity: %s" % a.get_main_activity())
        return a.get_main_activity()

    def installapk(self, paknamepath: str, device: str) -> bool:
        """
        安装apk
        :param paknamepath: apk 包路径
        :param devname:  设备名
        :return:
        """
        cmd = 'adb -s %s install %s' % (device, paknamepath)
        self.log.info("安装信息:%s" % str(os.popen(cmd).read()))
        return True

    def isinstallapk(self, packname: str, device: str) -> bool:
        """
        是否安装apk
        :param packname:
        :param device:
        :return:
        """
        cmd = "adb -s {} shell pm list packages -3".format(device)
        reslut = os.popen(cmd).readlines()
        all_apkname = []
        for i in reslut:
            apkname = str(i).split('
')[0].split(":")[1]
            all_apkname.append(apkname)
        if packname in all_apkname:
            return True
        return False

    def uninstallapk(self, packname: str, device: str) -> bool:
        """
        卸载app, 如果村长就卸载
        :param packname:  apk 名
        :param devname: 设备名
        :return:
        """
        cmd = "adb -s {} shell pm list packages -3".format(device)
        reslut = os.popen(cmd).readlines()
        all_apkname = []
        for i in reslut:
            apkname = str(i).split('
')[0].split(":")[1]
            all_apkname.append(apkname)
        if packname in all_apkname:
            cmd = 'adb -s %s uninstall %s ' % (device, packname)
            os.system(cmd)
            return True
        return False

    def push_file(self, file):
        """往手机push文件"""
        cmd = "adb push {} /sdcard".format(file)
        self.log.info("执行push 命令:%s" % cmd)
        try:
            os.popen(cmd).readlines()
            return True
        except Exception as e:
            self.log.error(e)
            return False

    def adb_shell_get(self, file1, file2):
        """
        手机往电脑传送文件
        :param file1: 手机上的文件
        :param file2: 放在电脑上什么地方
        :return:
        """
        cmd = "adb pull {} {}".format(file1, file2)
        self.log.info("执行pull 命令:%s" % cmd)
        try:
            os.popen(cmd).readlines()
            return True
        except Exception as e:
            self.log.error(e)
            return False

    def get_device_frim(self):
        """
        获取手机厂商
        ro.product.manufacturer
        ro.product.vendor.brand
        ro.product.system.brand
        :return:
        """
        info = os.popen("adb shell getprop  ro.product.manufacturer").read()
        self.log.info("get_device_frim %s" % info)
        # 手机厂商不一样,可能获取不到,兼容
        if len(info) == 1:
            info = os.popen("adb shell getprop ro.product.vendor.brand").read()
        return str(info).replace(" ", "").lower()

    def get_device_model(self):
        """
        获取手机型号
        ro.product.odm.marketname
        ro.product.product.marketname
        ro.product.system.marketname
        ro.product.vendor.marketname

        ro.product.vendor.model
        ro.product.model
        :return:
        """

        info = os.popen("adb shell getprop ro.product.model").read()
        if len(info) == 1:
            info = os.popen("adb shell getprop ro.product.system.model").read()
        return str(info).replace(" ", "").lower()

  

学习最大的乐趣在于分享,我是绝世老中医,欢迎来诊 个人qq:1978529954
原文地址:https://www.cnblogs.com/jueshilaozhongyi/p/15146876.html