zoukankan      html  css  js  c++  java
  • 使用bat脚本调用py文件直接获取应用的包名和targetversion

    背景:

    在上一篇已经介绍过如何利用python调用aapt获取包名

    https://www.cnblogs.com/reseelei-despair/p/11078750.html

    但是因为每次都要修改py文件内的安装包所在路径,感觉不是很方便,所以想办法用bat脚本实现直接将文件拖入bat文件直接运行获取到相关信息

    思路:

    批处理脚本直接运行py文件,修改py文件,导入sys模块,直接在cmd中将安装包路径传入py后运行

    代码:

    bat脚本代码如下

    @echo off
    COLOR 2
    set "getname=%1"
    echo %getname%
    python D:Pyexericeaapt_badging.py %getname%
    pause

    使用方法:创建一个txt文件,代码复制进去,保存后退出,将文件后缀名修改成bat即可

    python代码修改部分

    # -*- coding: utf-8 -*-
    import re
    import subprocess
    import os
    import sys#导入sys模块
    class ApkInfo:
        def __init__(self, apk_path):
            self.apkPath = apk_path
            self.aapt_path = self.get_aapt()
    
        @staticmethod
        def get_aapt():
            if "ANDROID_HOME" in os.environ:
                root_dir = os.path.join(os.environ["ANDROID_HOME"], "build-tools")
                for path, subdir, files in os.walk(root_dir):
                    if "aapt.exe" in files:
                        return os.path.join(path, "aapt.exe")
            else:
                return "ANDROID_HOME not exist"
    
        def get_apk_base_info(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("package: name='(S+)'").match(output.decode())
            if not match:
                raise Exception("can't get packageinfo")
            package_name = match.group(1)
            return package_name
    
        def get_apk_activity(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("launchable-activity: name='(S+)'").search(output.decode())
            if match is not None:
                return match.group(1)
    
        def get_apk_sdkVersion(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("sdkVersion:'(S+)'").search(output.decode())
            return match.group(1)
    
        def get_apk_targetSdkVersion(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("targetSdkVersion:'(S+)'").search(output.decode())
            return match.group(1)
    
    if __name__ == '__main__':
        apk_info = ApkInfo(sys.argv[1])#直接将cmd中给的参数引用至此
        print("Activity:%s"%apk_info.get_apk_activity())
        print("apkName:%s"%apk_info.get_apk_base_info())
        print("sdkVersion:%s"%apk_info.get_apk_sdkVersion())
        print("targetSdkVersion:%s"%apk_info.get_apk_targetSdkVersion())

    效果:

    将文件拖入

    运行结果如下

  • 相关阅读:
    服务器
    python的并发编程学习
    python的网络编程学习
    python的基础学习 模块
    pymysql基础操作
    查询简单练习
    数据准备
    数据库的三大设计范式
    数据库基础-建表约束
    mysql基础-基本语法
  • 原文地址:https://www.cnblogs.com/reseelei-despair/p/11082060.html
Copyright © 2011-2022 走看看